mirror of
https://github.com/ryan4yin/nix-config.git
synced 2026-11-18 22:08:16 +01:00
Exporters raced networkd for the LAN address at boot; drop the network-online ordering and unlimited-restart hack and let a FreeBind socket unit hold the port, passed in via --web.systemd-socket. Assisted-by: Claude Code (claude-opus-5-5)
104 lines
4.3 KiB
Nix
104 lines
4.3 KiB
Nix
{
|
|
config,
|
|
lib,
|
|
myvars,
|
|
...
|
|
}:
|
|
let
|
|
# Exporters bind to this host's static LAN IPv4 (the same address
|
|
# VictoriaMetrics scrapes), so the socket exists only on the LAN interface —
|
|
# defence in depth behind the firewall allowlist. Hosts without an always-on
|
|
# static LAN address (the `homeOnly` laptop, DHCP VMs like akane) run no
|
|
# exporters.
|
|
hostAddr = myvars.networking.hostsAddr.${config.networking.hostName} or { };
|
|
lanAddr = if hostAddr.homeOnly or false then null else hostAddr.ipv4 or null;
|
|
|
|
# Where node_exporter's textfile collector reads `*.prom` files. Keep in sync
|
|
# with modules/nixos/base/restic-backup.nix, which writes the last successful
|
|
# backup timestamp here.
|
|
textfileDir = "/var/lib/node_exporter/textfile_collector";
|
|
|
|
lanExporters = lib.filterAttrs (_: cfg: cfg.enable) {
|
|
inherit (config.services.prometheus.exporters) node smartctl nvidia-gpu;
|
|
};
|
|
in
|
|
{
|
|
# node_exporter on every host that has a static LAN IPv4 (see `lanAddr` above)
|
|
# https://github.com/NixOS/nixpkgs/blob/nixos-26.05/nixos/modules/services/monitoring/prometheus/exporters/node.nix
|
|
services.prometheus.exporters.node = lib.mkIf (lanAddr != null) {
|
|
enable = true;
|
|
listenAddress = lanAddr;
|
|
port = 9100;
|
|
# There're already a lot of collectors enabled by default
|
|
# https://github.com/prometheus/node_exporter?tab=readme-ov-file#enabled-by-default
|
|
enabledCollectors = [
|
|
"systemd"
|
|
"logind"
|
|
# Metrics that no exporter can produce itself, e.g. the timestamp of the
|
|
# last successful restic backup (written by restic-backup.nix).
|
|
"textfile"
|
|
];
|
|
|
|
# use either enabledCollectors or disabledCollectors
|
|
# disabledCollectors = [];
|
|
|
|
extraFlags = [
|
|
# Exclude pseudo/ephemeral FS:
|
|
# - /proc, /sys: kernel pseudo-FS, always size 0
|
|
# - /dev: tmpfs/devices, not meaningful for disk usage
|
|
# Exclude system/runtime tmp dirs:
|
|
# - /run/credentials/... → systemd service secrets (strict perms)
|
|
# - /run/user/... → per-user tmpfs (0700, IPC sockets, not storage)
|
|
# Exclude container/runtime mounts:
|
|
# - /var/lib/docker/, /var/lib/containers/ and /var/lib/kubelet/ → too much overlay/tmpfs mounts,
|
|
# often EACCES (strict perms, namespaces) → false alerts
|
|
# Exclude user bind mounts:
|
|
# - /home/ryan/.+ → bind-mounted from /persistent (NixOS tmpfs-root setup),
|
|
# monitoring /persistent is sufficient
|
|
# Note: ^(/|/persistent/) prefix ensures both root-level and
|
|
# /persistent-prefixed paths (used in NixOS's tmpfs-as-root setup) are excluded.
|
|
"--collector.filesystem.mount-points-exclude=^(/|/persistent/)(dev|proc|sys|run/credentials/.+|run/user/.+|var/lib/docker/.+|var/lib/containers/.+|var/lib/kubelet/.+|home/ryan/.+)($|/)"
|
|
# The collector does not create its directory, and a missing one logs an
|
|
# error on every scrape.
|
|
"--collector.textfile.directory=${textfileDir}"
|
|
"--web.systemd-socket"
|
|
];
|
|
};
|
|
|
|
# Created here so the textfile collector has somewhere to read from before
|
|
# the first successful backup writes its metric.
|
|
systemd.tmpfiles.rules = lib.mkIf (lanAddr != null) [
|
|
"d ${textfileDir} 0755 root root - -"
|
|
];
|
|
|
|
# Drive SMART health (wear, spare, media errors, temperature, bytes written)
|
|
# on physical hosts. node_exporter's nvme collector only exposes
|
|
# node_nvme_info and namespace capacity, NOT SMART health, so this is the only
|
|
# source of SSD lifespan data. `hardwareTools` is off on MicroVM/QEMU guests,
|
|
# which have no physical disks to inspect.
|
|
services.prometheus.exporters.smartctl =
|
|
lib.mkIf (lanAddr != null && config.modules.hardwareTools.enable)
|
|
{
|
|
enable = true;
|
|
listenAddress = lanAddr;
|
|
port = 9633;
|
|
maxInterval = "60s";
|
|
extraFlags = [ "--web.systemd-socket" ];
|
|
};
|
|
|
|
# Socket activation with FreeBind: the LAN address may not exist yet at boot.
|
|
systemd.sockets = lib.mkIf (lanAddr != null) (
|
|
lib.mapAttrs' (
|
|
name: cfg:
|
|
lib.nameValuePair "prometheus-${name}-exporter" {
|
|
wantedBy = [ "sockets.target" ];
|
|
listenStreams = [ "${cfg.listenAddress}:${toString cfg.port}" ];
|
|
socketConfig.FreeBind = true;
|
|
}
|
|
) lanExporters
|
|
);
|
|
services.prometheus.exporters.nvidia-gpu.extraFlags = lib.mkIf (lanAddr != null) [
|
|
"--web.systemd-socket"
|
|
];
|
|
}
|