monitoring: prometheus + grafana on box, push agents on hosts

This commit is contained in:
4DBug committed 2026-10-18 11:21:10 -05:00
1 parent 0ff70fc78c
commit aebceda490
5 files changed
+177 -1

No files matched your search

+1
View File
@@ -13,6 +13,7 @@
system.network.syncthing
system.network.tailscale
system.network.openvpn
system.monitoring
software.shell
+1
View File
@@ -50,6 +50,7 @@
# services.omnisearch
services.slskd
services.mpd
services.monitoring
];
};
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
den.aspects.services.glances = let port = 61208; in {
tunnel = { subdomain = "monitor"; inherit port; };
tunnel = { subdomain = "glances"; inherit port; };
nixos = { pkgs, ... }: {
+94
View File
@@ -0,0 +1,94 @@
{
# Central monitoring on the server: Prometheus (30d, receives remote_write from the other hosts) + Grafana.
# monitor.<domain> = Grafana (anonymous read-only, admin = owner), metrics.<domain> = Prometheus (basic auth).
# Secrets in ~/.config/monitoring/ on the server: password (plain), web.yml (bcrypt of it for user "push"),
# grafana-secret (random). Dashboards from grafana.com, pinned by revision + hash.
den.aspects.services.monitoring = let grafanaPort = 3005; in {
tunnel = [
{ subdomain = "monitor"; port = grafanaPort; }
{ subdomain = "metrics"; port = 9090; }
];
nixos = { config, lib, pkgs, host, ... }: let
home = config.users.users.${host.owner}.home;
secrets = "${home}/.config/monitoring";
cred = name: "$__file{/run/credentials/grafana.service/${name}}";
dashboard = id: rev: hash: pkgs.fetchurl {
url = "https://grafana.com/api/dashboards/${toString id}/revisions/${toString rev}/download";
inherit hash;
};
dashboards = pkgs.runCommand "grafana-dashboards" { } ''
mkdir $out
for f in ${dashboard 1860 45 "sha256-GExrdAnzBtp1Ul13cvcZRbEM6iOtFrXXjEaY6g6lGYY="}:node \
${dashboard 7587 3 "sha256-zkA7uHjiLdJQd1t6qFIZE+YgF0EXfKlU+npPsd2Jj6w="}:uptime \
${dashboard 249 2 "sha256-fX+krJmDDSwC87Xoecy70vUnLdOQ8sKkXPppgQy/FVE="}:processes; do
sed -E 's/\$\{DS_[A-Za-z0-9_-]+\}/prometheus/g' "''${f%:*}" > "$out/''${f##*:}.json"
done
'';
exp = config.services.prometheus.exporters;
in {
services.prometheus = {
enableAgentMode = false;
port = 9090;
retentionTime = "30d";
extraFlags = [ "--web.enable-remote-write-receiver" ];
webConfigFile = "/run/credentials/prometheus.service/web.yml";
exporters.process = {
enable = true;
listenAddress = "127.0.0.1";
settings.process_names = [{ name = "{{.Comm}}"; cmdline = [ ".+" ]; }];
};
scrapeConfigs = [
{ job_name = "process"; static_configs = [{ targets = [ "127.0.0.1:${toString exp.process.port}" ]; labels.instance = config.networking.hostName; }]; }
{ job_name = "cloudflared"; static_configs = [{ targets = [ "127.0.0.1:9300" ]; labels.instance = config.networking.hostName; }]; }
];
};
systemd.services.prometheus.serviceConfig.LoadCredential = [ "web.yml:${secrets}/web.yml" ];
# cloudflared's own metrics (requests / errors per tunnel)
systemd.services."cloudflared-tunnel-${host.cloudflared.uuid}".environment.TUNNEL_METRICS = "127.0.0.1:9300";
services.grafana = {
enable = true;
settings = {
server = {
http_addr = "127.0.0.1";
http_port = grafanaPort;
domain = "monitor.${host.domain}";
root_url = "https://monitor.${host.domain}";
};
security = {
admin_user = host.owner;
admin_password = cred "pw";
secret_key = cred "secret";
};
"auth.anonymous" = { enabled = true; org_role = "Viewer"; };
analytics.reporting_enabled = false;
};
provision = {
datasources.settings.datasources = [{
name = "Prometheus";
type = "prometheus";
uid = "prometheus";
url = "http://127.0.0.1:9090";
isDefault = true;
basicAuth = true;
basicAuthUser = "push";
secureJsonData.basicAuthPassword = cred "pw";
}];
dashboards.settings.providers = [{ name = "default"; options.path = dashboards; }];
};
};
systemd.services.grafana.serviceConfig.LoadCredential = [
"pw:${secrets}/password"
"secret:${secrets}/grafana-secret"
];
};
};
}
+80
View File
@@ -0,0 +1,80 @@
{ inputs, ... }: {
# Every host: node + blackbox exporters scraped by a local Prometheus.
# Hosts without services.monitoring run it in agent mode and push to https://metrics.<domain> (basic auth "push",
# password in ~/.config/monitoring/password), buffering in the WAL while the server is unreachable.
# Uptime probes go to every http(s) tunnel hostname of the server, labelled with the probing host (`from`).
den.aspects.system.monitoring = {
nixos = { config, lib, host, ... }: let
name = config.networking.hostName;
home = config.users.users.${host.owner}.home;
ingress = lib.foldl' (a: t: a // t.ingress) { }
(lib.attrValues inputs.self.nixosConfigurations.box.config.services.cloudflared.tunnels);
targets = map (h: "https://${h}") (lib.attrNames (lib.filterAttrs (_: lib.hasPrefix "http") ingress));
nvidia = lib.elem "nvidia" config.services.xserver.videoDrivers;
exp = config.services.prometheus.exporters;
local = job: port: {
job_name = job;
static_configs = [{ targets = [ "127.0.0.1:${toString port}" ]; labels.instance = name; }];
};
in {
services.prometheus = {
enable = true;
enableAgentMode = lib.mkDefault true;
listenAddress = "127.0.0.1";
checkConfig = "syntax-only"; # credential files only exist at runtime
exporters = {
node = {
enable = true;
listenAddress = "127.0.0.1";
enabledCollectors = [ "systemd" "drm" ]; # drm: amdgpu busy / vram
};
blackbox = {
enable = true;
listenAddress = "127.0.0.1";
configFile = builtins.toFile "blackbox.yml" (builtins.toJSON {
modules.http = {
prober = "http";
timeout = "10s";
http = {
preferred_ip_protocol = "ip4";
valid_status_codes = [ 200 201 204 301 302 401 403 ];
};
};
});
};
nvidia-gpu = lib.mkIf nvidia {
enable = true;
listenAddress = "127.0.0.1";
};
};
scrapeConfigs = [
(local "node" exp.node.port)
{
job_name = "blackbox";
metrics_path = "/probe";
params.module = [ "http" ];
scrape_interval = "30s";
static_configs = [{ inherit targets; labels.from = name; }];
relabel_configs = [
{ source_labels = [ "__address__" ]; target_label = "__param_target"; }
{ source_labels = [ "__param_target" ]; target_label = "instance"; }
{ target_label = "__address__"; replacement = "127.0.0.1:${toString exp.blackbox.port}"; }
];
}
] ++ lib.optional nvidia (local "nvidia-gpu" exp.nvidia-gpu.port);
remoteWrite = lib.mkIf config.services.prometheus.enableAgentMode [{
url = "https://metrics.${host.domain}/api/v1/write";
basic_auth = { username = "push"; password_file = "/run/credentials/prometheus.service/pw"; };
}];
};
systemd.services.prometheus.serviceConfig.LoadCredential =
lib.mkIf config.services.prometheus.enableAgentMode [ "pw:${home}/.config/monitoring/password" ];
};
};
}