Add Prometheus metrics: /metrics, node_exporter and a page for it

The web UI serves the router's own state at /metrics (token needed);
router.metrics.nodeExporter runs node_exporter for the machine's and opens
its port only to the zones named. The Prometheus page edits those settings
and shows the scrape configuration.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 11:30:02 +01:00
co-authored by Claude Sonnet 5.5
parent 32bf0d5fde
commit 6e23d183fc
13 changed files with 672 additions and 1 deletions
+20
View File
@@ -859,6 +859,26 @@ let
};
expect = [ "natOnly does not apply to the router itself" ];
};
exporterOnWan = {
router = {
lans.main = lan { };
metrics.nodeExporter = {
enable = true;
zones = [ "wan" ];
};
};
expect = [ "node_exporter must not be open to it" ];
};
exporterUnknownZone = {
router = {
lans.main = lan { };
metrics.nodeExporter = {
enable = true;
zones = [ "nowhere" ];
};
};
expect = [ "unknown source zone `nowhere`" ];
};
localNoPorts = {
router = {
lans.main = lan { };
+24
View File
@@ -13,6 +13,10 @@ let
stateVersion = "26.11";
};
router = {
metrics.nodeExporter = {
enable = true;
zones = [ "main" ];
};
wan.interface = "eth1";
lans.main = {
interface = "eth2";
@@ -105,6 +109,26 @@ pkgs.testers.runNixOSTest {
assert "<html" not in curl(f"'{ui}/dashboard?range=7d'")
client.succeed("curl -sfk -o /dev/null https://192.168.10.1/static/dashboard.js")
with subtest("Prometheus can scrape the router's state with a token, and the machine's from node_exporter"):
# /metrics of the web UI wants a token of the API access page.
assert client.succeed(f"curl -sk -o /dev/null -w '%{{http_code}}' {ui}/metrics") == "401"
secret = curl(f"-d csrf={csrf('/tokens')} -d name=prometheus -d scope=read {ui}/tokens | grep -o 'rtr_[A-Za-z0-9_-]*' | sed -n 1p").strip()
assert secret.startswith("rtr_"), secret
metrics = client.succeed(f"curl -sk -H 'Authorization: Bearer {secret}' -D /tmp/head {ui}/metrics")
assert "text/plain; version=0.0.4" in client.succeed("cat /tmp/head")
for line in ["# TYPE router_info gauge", "router_settings_unapplied 0", "router_ping_up{target=\"192.168.10.50\"", "router_dhcp_leases{lan=\"main\"} 0", "router_certificate_expiry_timestamp_seconds{source=\"self-signed\"}"]:
assert line in metrics, f"{line!r} missing:\n{metrics}"
# node_exporter: reachable from the LAN zone named in the settings, and on the router itself.
# (Into a file: grep -q would end curl with SIGPIPE, failing the pipe.)
client.wait_until_succeeds("curl -sf -o /tmp/node http://192.168.10.1:9100/metrics && grep -q '^node_systemd_unit_state' /tmp/node", timeout=60)
router.succeed("curl -sf -o /tmp/node http://127.0.0.1:9100/metrics && grep -q '^node_uname_info' /tmp/node")
# Not from the uplink: the rule names the LAN's device only.
rules = router.succeed("nft list chain inet router input")
assert 'iifname "eth2" tcp dport 9100 accept' in rules and 'iifname "eth1" tcp dport 9100' not in rules, rules
# The page for it shows what the settings say.
page = curl(f"{ui}/prometheus")
assert 'name="enabled" checked' in page and 'value="main" checked' in page and "job_name: router-node" in page, page
with subtest("a change saved in the web UI is shown as unapplied and can be discarded"):
router.succeed(f"cp {cfg} /var/lib/router/settings.confirmed.json")
assert "match the confirmed system" in curl(f"{ui}/")