{ # Central monitoring on the server: Prometheus (30d, receives remote_write from the other hosts) + Grafana. # monitor. = Grafana (anonymous read-only, admin = owner), metrics. = 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" ]; }; }; }