Files
nix/modules/system/monitoring.nix
T
2026-10-25 10:08:15 -05:00

82 lines
3.1 KiB
Nix

{ inputs, ... }: {
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);
noProbe = [ "music" "monitor" "metrics" ];
external = [ host.domain "tv.${host.domain}" "tv5.${host.domain}" "cad.${host.domain}" "llama.${host.domain}" ];
targets = map (h: "https://${h}") (lib.attrNames (lib.filterAttrs
(n: v: lib.hasPrefix "http" v && !lib.elem (lib.head (lib.splitString "." n)) noProbe) ingress) ++ external);
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";
exporters = {
node = {
enable = true;
listenAddress = "127.0.0.1";
enabledCollectors = [ "systemd" "drm" ];
};
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-node-exporter.serviceConfig.ProtectHome = lib.mkForce "read-only";
systemd.services.prometheus.serviceConfig.LoadCredential =
lib.mkIf config.services.prometheus.enableAgentMode [ "pw:${home}/.config/monitoring/password" ];
};
};
}