Add optional query statistics
Off by default (stats in jarl.yaml). When enabled, Jarl adds a loopback statistics channel and a response log to named's configuration (merged into existing logging/statistics-channels statements) and switches response logging with rndc responselog, because the responselog option only counts when named starts. jarld polls named's counters every minute and tails the response log (resuming after restarts and across rotation). It stores aggregates, never individual queries, in a separate SQLite database: per-minute, hourly and daily counters (queries, result codes, record types, views, zones, cache hits, upstream latency, DNSSEC) and hourly/daily top lists (names, clients, NXDOMAIN and SERVFAIL names, refused clients), each with its own retention. Clients can be anonymized or excluded. - Web UI: Statistics page with KPI tiles, queries and failed answers over time, a weekday x hour heatmap, top lists and breakdowns; SVG charts with hover/keyboard readouts, a table view and light/dark palettes validated for colour-vision deficiency. - CLI: jarl stats status|summary|top|timeline|heatmap|breakdown. - API: /api/v1/stats/*, behind a new "stats" permission resource. - jarld brings named's configuration up to date with jarl.yaml at startup (engine.SyncConfig), never overwriting hand-edited files. - resync now refuses to overwrite drifted files without force, as documented. - tmpfiles.d creates /var/log/named (allowed by named's AppArmor profile on Debian 13 and Ubuntu). - The e2e harness runs named with -f like the Debian unit. - docs/stats.md, plus API, CLI, configuration, permissions and README updates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0795a1001e
commit
3b7bee67f8
44 files changed
+4163
-42
No files matched your search
+1
-1
@@ -302,7 +302,7 @@ func renderCmd() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
paths := render.Paths{ConfigDir: filepath.Join(out, "config"), ZoneDir: filepath.Join(out, "zones"), CacheDir: filepath.Join(out, "cache")}
|
||||
o := render.Render(st, paths)
|
||||
o := render.Render(st, paths, renderExtras(a.cfg))
|
||||
for _, f := range o.Files {
|
||||
if err := os.MkdirAll(filepath.Dir(f.Path), 0o750); err != nil {
|
||||
return err
|
||||
|
||||
+109
-2
@@ -11,6 +11,7 @@ import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
@@ -28,6 +29,7 @@ import (
|
||||
"jarl/internal/model"
|
||||
"jarl/internal/render"
|
||||
"jarl/internal/server"
|
||||
"jarl/internal/stats"
|
||||
"jarl/internal/store"
|
||||
"jarl/internal/version"
|
||||
"jarl/internal/web"
|
||||
@@ -118,10 +120,72 @@ func openApp(allowMissingConfig bool) (*app, error) {
|
||||
FileGroup: cfg.BIND.FileGroup,
|
||||
ReloadTimeout: time.Duration(cfg.BIND.ReloadTimeout),
|
||||
NoReload: cfg.BIND.NoReload,
|
||||
Extras: renderExtras(cfg),
|
||||
}, log)
|
||||
return &app{cfg: cfg, log: log, store: st, eng: eng, owner: owner}, nil
|
||||
}
|
||||
|
||||
// openStats opens the statistics database (next to, not inside, Jarl's
|
||||
// configuration database).
|
||||
func openStats(a *app) (*stats.Store, error) {
|
||||
path := a.cfg.Stats.Database
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ss, err := stats.OpenStore(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
func statsOptions(cfg *config.Config) stats.Options {
|
||||
sc := cfg.Stats
|
||||
o := stats.Options{QueryLog: sc.QueryLog, LogFile: sc.LogFile, StatisticsChannel: sc.StatisticsChannel, TopN: sc.TopN,
|
||||
RetainMinute: time.Duration(sc.Retention.Minute), RetainHour: time.Duration(sc.Retention.Hour), RetainDay: time.Duration(sc.Retention.Day),
|
||||
Anonymize: sc.AnonymizeClients}
|
||||
for _, p := range sc.ExcludeClients {
|
||||
if pf, err := config.ParsePrefix(p); err == nil {
|
||||
o.Exclude = append(o.Exclude, pf.Masked())
|
||||
}
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
// zoneIndex lists the zones named answers authoritatively, per view, so
|
||||
// statistics can be broken down by zone.
|
||||
func zoneIndex(a *app) stats.ZoneIndex {
|
||||
return func() map[string][]string {
|
||||
st, err := a.store.LoadState(context.Background())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
out := map[string][]string{}
|
||||
for _, z := range st.Zones {
|
||||
if z.Type != model.ZonePrimary && z.Type != model.ZoneSecondary && z.Type != model.ZoneMirror {
|
||||
continue
|
||||
}
|
||||
v := st.ViewName(z)
|
||||
out[v] = append(out[v], strings.TrimSuffix(z.Name, "."))
|
||||
}
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
// renderExtras turns jarl.yaml settings that change named's configuration
|
||||
// into render extras.
|
||||
func renderExtras(cfg *config.Config) render.Extras {
|
||||
var x render.Extras
|
||||
if cfg.Stats.Enabled {
|
||||
h := &render.StatsHooks{Channel: netip.MustParseAddrPort(cfg.Stats.StatisticsChannel)}
|
||||
if cfg.Stats.QueryLog {
|
||||
h.LogFile, h.LogVersions, h.LogSize = cfg.Stats.LogFile, cfg.Stats.LogVersions, cfg.Stats.LogSize
|
||||
}
|
||||
x.Stats = h
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func (a *app) close() {
|
||||
a.store.Close()
|
||||
// Commands run as root must not leave root-owned files where the
|
||||
@@ -172,14 +236,57 @@ func serveCmd() *cobra.Command {
|
||||
if users, err := a.store.ListUsers(ctx); err == nil && len(users) == 0 {
|
||||
a.log.Warn("there are no users yet; create an administrator with 'jarld init' or 'jarld user add --admin NAME'")
|
||||
}
|
||||
if ok, err := a.eng.Adopted(); err == nil && !ok {
|
||||
adopted, aerr := a.eng.Adopted()
|
||||
if aerr == nil && !adopted {
|
||||
a.log.Warn("named does not load Jarl's configuration yet; changes are written but named is not reloaded until 'jarld adopt' runs", "main_config", a.cfg.BIND.MainConfig)
|
||||
}
|
||||
if adopted {
|
||||
// Settings in jarl.yaml (such as stats) can change named's
|
||||
// configuration; bring it up to date.
|
||||
if wrote, res, err := a.eng.SyncConfig(ctx); err != nil {
|
||||
a.log.Warn("could not update named's configuration for the settings in jarl.yaml; fix the problem and restart jarl, or run 'jarl resync'", "err", err)
|
||||
} else if wrote {
|
||||
a.log.Info("updated named's configuration for the settings in jarl.yaml", "files", res.Files)
|
||||
}
|
||||
// Response logging is switched at runtime (the option only
|
||||
// counts when named starts). Turn it off again when
|
||||
// statistics no longer use it, unless the configuration
|
||||
// asks for it.
|
||||
if !a.cfg.Stats.Enabled || !a.cfg.Stats.QueryLog {
|
||||
if st, err := a.store.LoadState(ctx); err == nil && st.FindOption(model.ScopeOptions, 0, "responselog") == nil {
|
||||
if changed, err := a.eng.SetResponseLogging(ctx, false); err == nil && changed {
|
||||
a.log.Info("turned off named's response logging, which query statistics no longer use")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
var collector *stats.Collector
|
||||
collectorDone := make(chan struct{})
|
||||
if a.cfg.Stats.Enabled {
|
||||
ss, err := openStats(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer ss.Close()
|
||||
opts := statsOptions(a.cfg)
|
||||
if adopted {
|
||||
opts.EnsureLogging = func(ctx context.Context) (bool, error) { return a.eng.SetResponseLogging(ctx, true) }
|
||||
}
|
||||
collector = stats.NewCollector(ss, opts, zoneIndex(a), a.log)
|
||||
cctx, cancel := context.WithCancel(ctx)
|
||||
go func() {
|
||||
collector.Run(cctx)
|
||||
close(collectorDone)
|
||||
}()
|
||||
// Stop the collector (it writes what it has) before the
|
||||
// database closes, however serving ends.
|
||||
defer func() { cancel(); <-collectorDone }()
|
||||
}
|
||||
srv := server.New(server.Config{
|
||||
Listen: a.cfg.Listen, TLSCertFile: a.cfg.TLS.CertFile, TLSKeyFile: a.cfg.TLS.KeyFile,
|
||||
TrustedProxies: a.cfg.Proxies(), SessionLifetime: time.Duration(a.cfg.Session.Lifetime),
|
||||
SessionIdle: time.Duration(a.cfg.Session.IdleTimeout), CookieSecure: a.cfg.Session.CookieSecure,
|
||||
Version: version.Version,
|
||||
Version: version.Version, Stats: collector,
|
||||
}, a.eng, a.log, web.FS())
|
||||
hup := make(chan os.Signal, 1)
|
||||
signal.Notify(hup, syscall.SIGHUP)
|
||||
|
||||
Reference in new issue
Block a user