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:
@@ -11,6 +11,7 @@
|
||||
./ddns.nix
|
||||
./acme.nix
|
||||
./rollback.nix
|
||||
./metrics.nix
|
||||
./power.nix
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# Metrics for Prometheus. The router's own state is /metrics of the web UI
|
||||
# (pkgs/router-webui/promtext.go); the machine's is node_exporter, which this
|
||||
# module starts when `router.metrics.nodeExporter.enable` is set and opens to
|
||||
# the zones named there, and to no one else.
|
||||
{ config, lib, ... }:
|
||||
let
|
||||
cfg = config.router;
|
||||
m = cfg.metrics.nodeExporter;
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (cfg.enable && m.enable) {
|
||||
services.prometheus.exporters.node = {
|
||||
enable = true;
|
||||
inherit (m) port;
|
||||
enabledCollectors = [ "systemd" ];
|
||||
};
|
||||
|
||||
# An allow rule per zone; the unknown ones are refused by the firewall's own check.
|
||||
router.firewall.rules = map (zone: {
|
||||
from = zone;
|
||||
to = "local";
|
||||
protocol = "tcp";
|
||||
ports = [ m.port ];
|
||||
}) m.zones;
|
||||
|
||||
assertions = [
|
||||
{
|
||||
assertion = !(lib.elem "wan" m.zones);
|
||||
message = "router.metrics.nodeExporter.zones: `wan` is the internet; node_exporter must not be open to it. Scrape it from a LAN or through a WireGuard tunnel.";
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
@@ -452,6 +452,31 @@ in
|
||||
'';
|
||||
};
|
||||
|
||||
metrics.nodeExporter = {
|
||||
enable = mkEnableOption "Prometheus node_exporter: the machine's CPU, memory, disks, network interfaces and systemd units";
|
||||
port = mkOption {
|
||||
type = types.port;
|
||||
default = 9100;
|
||||
description = "The port node_exporter listens on.";
|
||||
};
|
||||
zones = mkOption {
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
example = [ "main" ];
|
||||
description = ''
|
||||
Zones (a LAN's or tunnel's `zone`, which is the LAN's name unless
|
||||
set) that may reach node_exporter, with a firewall rule for each. The
|
||||
port is closed to every other zone and to the internet, so nothing
|
||||
can scrape it until a zone is named; the router itself can always
|
||||
reach it on localhost. `wan` is not allowed.
|
||||
|
||||
The router's own state (uplink, pings, WireGuard peers, pending
|
||||
changes, certificate) is at `/metrics` of the web UI instead, which
|
||||
needs an API token and is reachable wherever the web UI is.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
firewall.zones = mkOption {
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
|
||||
Reference in New Issue
Block a user