{ 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. (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" ]; }; }; }