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
@@ -0,0 +1,99 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"jarl/internal/stats"
|
||||
)
|
||||
|
||||
func TestStatsAPIDisabled(t *testing.T) {
|
||||
e := newAPIEnv(t)
|
||||
st := e.do("GET", "/stats/status", e.token, nil, 200)
|
||||
if st["enabled"] != false {
|
||||
t.Errorf("status = %v", st)
|
||||
}
|
||||
out, status := e.req("GET", "/stats/summary", e.token, nil)
|
||||
if status != 404 || !json.Valid(out) || !strings.Contains(string(out), "stats_disabled") {
|
||||
t.Errorf("summary while disabled = %d %s", status, out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsAPI(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ss, err := stats.OpenStore(":memory:")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer ss.Close()
|
||||
now := time.Now()
|
||||
minute := now.Unix() - now.Unix()%60 - 120
|
||||
hour := minute - minute%3600
|
||||
b := &stats.Batch{}
|
||||
vals := map[string]int64{"queries": 50, "rcode:NOERROR": 45, "rcode:NXDOMAIN": 5, "qtype:A": 30, "qtype:AAAA": 20, "zone:example.com": 40, "zone:-": 10}
|
||||
b.AddCounters(stats.ResMinute, minute, vals)
|
||||
b.AddCounters(stats.ResHour, hour, vals)
|
||||
b.SetTops(stats.ResHour, hour, stats.KindName, []stats.Entry{{Key: "www.example.com", Count: 30}, {Key: "mail.example.com", Count: 10}})
|
||||
b.SetTops(stats.ResHour, hour, stats.KindClient, []stats.Entry{{Key: "10.0.0.7", Count: 50}})
|
||||
if err := ss.Commit(ctx, b); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
opts := stats.Options{StatisticsChannel: "127.0.0.1:1", TopN: 10, RetainMinute: 7 * 24 * time.Hour, RetainHour: 90 * 24 * time.Hour, RetainDay: 730 * 24 * time.Hour}
|
||||
c := stats.NewCollector(ss, opts, nil, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
e := newAPIEnv(t, c)
|
||||
|
||||
if st := e.do("GET", "/stats/status", e.token, nil, 200); st["enabled"] != true {
|
||||
t.Errorf("status = %v", st)
|
||||
}
|
||||
sum := e.do("GET", "/stats/summary?since=1h", e.token, nil, 200)
|
||||
if sum["queries"].(float64) != 50 || sum["resolution"] != "m" {
|
||||
t.Errorf("summary = %v", sum)
|
||||
}
|
||||
tops := e.list("/stats/top?kind=name&since=2h", e.token)
|
||||
if len(tops) != 2 || tops[0]["key"] != "www.example.com" {
|
||||
t.Errorf("top names = %v", tops)
|
||||
}
|
||||
if b := e.list("/stats/breakdown?group=zone&since=1h", e.token); len(b) != 2 || b[0]["key"] != "example.com" {
|
||||
t.Errorf("zones = %v", b)
|
||||
}
|
||||
tl := e.do("GET", "/stats/timeline?since=1h&key=queries,rcode:*", e.token, nil, 200)
|
||||
if tl["step"] != "1m" || len(tl["series"].([]any)) != 3 {
|
||||
t.Errorf("timeline = %v", tl)
|
||||
}
|
||||
if hm := e.do("GET", "/stats/heatmap", e.token, nil, 200); hm["key"] != "queries" {
|
||||
t.Errorf("heatmap = %v", hm)
|
||||
}
|
||||
for _, bad := range []string{"/stats/summary?since=forever", "/stats/top?kind=nope", "/stats/breakdown?group=nope", "/stats/timeline?step=3m"} {
|
||||
if _, status := e.req("GET", bad, e.token, nil); status != 400 {
|
||||
t.Errorf("%s = %d, want 400", bad, status)
|
||||
}
|
||||
}
|
||||
// Statistics show client addresses: they need the stats grant.
|
||||
e.do("POST", "/users", e.token, map[string]any{"username": "editor", "password": "editor-password-1", "role": "dns-editor"}, 201)
|
||||
var editorID int64
|
||||
for _, u := range e.list("/users", e.token) {
|
||||
if u["username"] == "editor" {
|
||||
editorID = int64(u["id"].(float64))
|
||||
}
|
||||
}
|
||||
tok := e.do("POST", "/users/"+itoa(editorID)+"/tokens", e.token, map[string]any{"name": "t"}, 201)["token"].(string)
|
||||
for _, path := range []string{"/stats/status", "/stats/summary", "/stats/top"} {
|
||||
if _, status := e.req("GET", path, tok, nil); status != 403 {
|
||||
t.Errorf("dns-editor %s = %d, want 403", path, status)
|
||||
}
|
||||
}
|
||||
e.do("POST", "/users", e.token, map[string]any{"username": "viewer", "password": "viewer-password-1", "role": "readonly"}, 201)
|
||||
var viewerID int64
|
||||
for _, u := range e.list("/users", e.token) {
|
||||
if u["username"] == "viewer" {
|
||||
viewerID = int64(u["id"].(float64))
|
||||
}
|
||||
}
|
||||
vtok := e.do("POST", "/users/"+itoa(viewerID)+"/tokens", e.token, map[string]any{"name": "t"}, 201)["token"].(string)
|
||||
e.do("GET", "/stats/summary", vtok, nil, 200)
|
||||
}
|
||||
Reference in new issue
Block a user