nixos: monitoring as native services
Every host runs node_exporter, cAdvisor (now against Podman), Alloy and the security-metrics script, on its private address. Each service declares its own expected ports, users and privileged containers next to its definition, so the security baseline follows the config instead of a separate inventory list. On ops: Prometheus, Loki, Grafana, the blackbox prober and the Postgres exporter, with the same scrape jobs, probes, retention, dashboards and 33 alert rules. Two rules matched container-era names and now match the native ones: Forgejo's log is unit forgejo.service, Grafana's route is grafana@file. Grafana keeps its default secret_key on production, because its database was written with it; rotating it is a separate step. Prometheus, Grafana and Alloy come from nixos-unstable: their data is from newer versions than 26.05 ships. LLMNR and mDNS are off: the baseline flagged them as public listeners. Tried on the rehearsal pair: all 28 scrape targets up, logs from both hosts in Loki, Grafana WireGuard-only, no unexpected port, user or container on either host. Assisted-by: Claude:opus-5-5
This commit is contained in:
parent
422eb3bc6e
commit
cbf14b4b99
1 changed files with 30417 additions and 104 deletions
267
nixos/modules/monitoring-agent.nix
Normal file
267
nixos/modules/monitoring-agent.nix
Normal file
|
|
@ -0,0 +1,267 @@
|
|||
# What every host reports: node metrics, container metrics, its logs to
|
||||
# Loki on asxpio-ops, and its security state (peers, devices on the
|
||||
# private network, accounts, listening ports, privileged containers).
|
||||
# Everything listens on the private address only; Prometheus on ops
|
||||
# scrapes it.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
ip = config.asxpio.privateIPv4;
|
||||
sec = config.asxpio.security;
|
||||
textfile = "/var/lib/node_exporter/textfile";
|
||||
|
||||
securityConfig = pkgs.writeText "security-metrics.json" (
|
||||
builtins.toJSON {
|
||||
textfile = "${textfile}/security.prom";
|
||||
vpc_network = "10.10.0.0/20";
|
||||
vpc_hosts = [
|
||||
"10.10.0.1"
|
||||
"10.10.0.10"
|
||||
"10.10.0.20"
|
||||
];
|
||||
wg_peer_keys = lib.listToAttrs (
|
||||
map (p: lib.nameValuePair p.publicKey p.name) config.asxpio.wireguard.peers
|
||||
);
|
||||
login_users = sec.loginUsers;
|
||||
groups = {
|
||||
wheel = sec.adminUsers;
|
||||
podman = sec.adminUsers ++ sec.podmanUsers;
|
||||
};
|
||||
ports = sec.ports;
|
||||
containers = sec.containers;
|
||||
# Files a deploy does not rewrite; the rest of /etc is the Nix store
|
||||
filesets = {
|
||||
accounts = [
|
||||
"/etc/passwd"
|
||||
"/etc/shadow"
|
||||
"/etc/group"
|
||||
];
|
||||
authorized_keys = [ "/etc/ssh/authorized_keys.d/*" ];
|
||||
host_keys = [ "/etc/ssh/ssh_host_*" ];
|
||||
};
|
||||
forgejo_db = sec.forgejoDb;
|
||||
}
|
||||
);
|
||||
|
||||
alloyConfig = pkgs.writeText "config.alloy" ''
|
||||
// Logs of this host to Loki.
|
||||
|
||||
discovery.docker "containers" {
|
||||
host = "unix:///run/podman/podman.sock"
|
||||
}
|
||||
|
||||
discovery.relabel "containers" {
|
||||
targets = discovery.docker.containers.targets
|
||||
rule {
|
||||
source_labels = ["__meta_docker_container_name"]
|
||||
regex = "/(.*)"
|
||||
target_label = "container"
|
||||
}
|
||||
rule {
|
||||
source_labels = ["__meta_docker_container_label_com_docker_compose_project"]
|
||||
target_label = "compose_project"
|
||||
}
|
||||
rule {
|
||||
source_labels = ["__meta_docker_container_label_com_docker_compose_service"]
|
||||
target_label = "compose_service"
|
||||
}
|
||||
}
|
||||
|
||||
loki.source.docker "containers" {
|
||||
host = "unix:///run/podman/podman.sock"
|
||||
targets = discovery.docker.containers.targets
|
||||
relabel_rules = discovery.relabel.containers.rules
|
||||
labels = { job = "docker" }
|
||||
forward_to = [loki.process.containers.receiver]
|
||||
}
|
||||
|
||||
loki.process "containers" {
|
||||
// CI job containers are many, short-lived and noisy: the runner logs suffice
|
||||
stage.match {
|
||||
selector = "{container=~\"FORGEJO-ACTIONS-TASK-.*\"}"
|
||||
action = "drop"
|
||||
}
|
||||
forward_to = [loki.write.default.receiver]
|
||||
}
|
||||
|
||||
loki.relabel "journal" {
|
||||
forward_to = []
|
||||
rule {
|
||||
source_labels = ["__journal__systemd_unit"]
|
||||
target_label = "unit"
|
||||
}
|
||||
// Every SSH login (CI deploys) gets its own session-N.scope: one
|
||||
// stream each blew Loki's active stream limit.
|
||||
rule {
|
||||
source_labels = ["unit"]
|
||||
regex = "session-\\d+\\.scope"
|
||||
target_label = "unit"
|
||||
replacement = "session.scope"
|
||||
}
|
||||
rule {
|
||||
source_labels = ["__journal_priority_keyword"]
|
||||
target_label = "level"
|
||||
}
|
||||
}
|
||||
|
||||
loki.source.journal "journal" {
|
||||
max_age = "12h"
|
||||
relabel_rules = loki.relabel.journal.rules
|
||||
labels = { job = "journal" }
|
||||
forward_to = [loki.write.default.receiver]
|
||||
}
|
||||
${lib.optionalString config.services.traefik.enable ''
|
||||
|
||||
local.file_match "traefik" {
|
||||
path_targets = [{ "__path__" = "/var/log/traefik/access.log", job = "traefik-access" }]
|
||||
}
|
||||
|
||||
loki.source.file "traefik" {
|
||||
targets = local.file_match.traefik.targets
|
||||
forward_to = [loki.write.default.receiver]
|
||||
}
|
||||
''}
|
||||
loki.write "default" {
|
||||
endpoint {
|
||||
url = "http://10.10.0.10:3100/loki/api/v1/push"
|
||||
}
|
||||
external_labels = { host = "${config.asxpio.backup.hostName}" }
|
||||
}
|
||||
'';
|
||||
in
|
||||
{
|
||||
options.asxpio.privateIPv4 = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "This host's address on the private network.";
|
||||
};
|
||||
|
||||
options.asxpio.security = {
|
||||
loginUsers = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
description = "Accounts that may have a login shell.";
|
||||
};
|
||||
adminUsers = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [
|
||||
"asxpi"
|
||||
"deploy"
|
||||
"zerrtika"
|
||||
];
|
||||
};
|
||||
podmanUsers = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
default = [ ];
|
||||
description = "Service users allowed on the container socket.";
|
||||
};
|
||||
ports = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.str;
|
||||
description = "Expected listening sockets outside loopback: proto/port/process.";
|
||||
};
|
||||
containers = lib.mkOption {
|
||||
type = lib.types.attrsOf (lib.types.listOf lib.types.str);
|
||||
default = { };
|
||||
description = "Containers allowed host-level access: name pattern to flags.";
|
||||
};
|
||||
forgejoDb = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "";
|
||||
};
|
||||
};
|
||||
|
||||
config = {
|
||||
asxpio.security.loginUsers = [
|
||||
"root"
|
||||
"asxpi"
|
||||
"deploy"
|
||||
"zerrtika"
|
||||
];
|
||||
asxpio.security.ports = [
|
||||
"tcp/22/sshd"
|
||||
"udp/${toString config.asxpio.wireguard.listenPort}/"
|
||||
"tcp/12345/alloy"
|
||||
"tcp/9100/node_exporter"
|
||||
"tcp/9180/cadvisor"
|
||||
"udp/68/systemd-network" # DHCP
|
||||
];
|
||||
|
||||
systemd.tmpfiles.rules = [ "d ${textfile} 0755 root root -" ];
|
||||
|
||||
services.prometheus.exporters.node = {
|
||||
enable = true;
|
||||
listenAddress = ip;
|
||||
port = 9100;
|
||||
enabledCollectors = [ "textfile" ];
|
||||
extraFlags = [
|
||||
"--collector.textfile.directory=${textfile}"
|
||||
"--collector.filesystem.mount-points-exclude=^/(dev|proc|run|sys|var/lib/containers/.+)($|/)"
|
||||
];
|
||||
};
|
||||
|
||||
services.cadvisor = {
|
||||
enable = true;
|
||||
listenAddress = ip;
|
||||
port = 9180;
|
||||
extraOptions = [
|
||||
"--podman=unix:///run/podman/podman.sock"
|
||||
"--docker_only=true"
|
||||
"--housekeeping_interval=30s"
|
||||
"--store_container_labels=false"
|
||||
"--whitelisted_container_labels=com.docker.compose.project,com.docker.compose.service"
|
||||
"--disable_metrics=percpu,sched,tcp,udp,hugetlb,referenced_memory,cpu_topology,resctrl,advtcp,process"
|
||||
];
|
||||
};
|
||||
|
||||
services.alloy = {
|
||||
enable = true;
|
||||
configPath = alloyConfig;
|
||||
extraFlags = [ "--server.http.listen-addr=${ip}:12345" ];
|
||||
};
|
||||
# They bind the private address: wait for it, and retry
|
||||
systemd.services = lib.mkMerge [
|
||||
(lib.genAttrs [ "prometheus-node-exporter" "cadvisor" "alloy" ] (_: {
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
serviceConfig = {
|
||||
Restart = lib.mkForce "always";
|
||||
RestartSec = lib.mkForce 5;
|
||||
};
|
||||
}))
|
||||
{
|
||||
alloy.serviceConfig.SupplementaryGroups = [
|
||||
"podman"
|
||||
"systemd-journal"
|
||||
]
|
||||
++ lib.optional config.services.traefik.enable "traefik";
|
||||
}
|
||||
{
|
||||
security-metrics = {
|
||||
description = "Security state for Prometheus (node_exporter textfile)";
|
||||
path = [
|
||||
pkgs.wireguard-tools
|
||||
pkgs.iproute2
|
||||
pkgs.coreutils
|
||||
config.virtualisation.podman.package
|
||||
"/run/current-system/sw" # docker, the alias for podman
|
||||
];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = "${pkgs.python3}/bin/python3 ${./security-metrics.py} ${securityConfig}";
|
||||
# Runs every minute: keep "Starting/Finished" out of the journal
|
||||
LogLevelMax = "notice";
|
||||
};
|
||||
};
|
||||
}
|
||||
];
|
||||
systemd.timers.security-metrics = {
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnCalendar = "minutely";
|
||||
AccuracySec = "5s";
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue