Add custom nix-daemon exporter #99

Manually merged
rarias merged 1 commits from monitor-nix-daemon into master 2025-05-29 12:58:47 +02:00
3 changed files with 51 additions and 0 deletions
Showing only changes of commit 1c15e77c83 - Show all commits

View File

@@ -4,6 +4,7 @@
imports = [
../module/slurm-exporter.nix
./gpfs-probe.nix
./nix-daemon-exporter.nix
];
age.secrets.grafanaJungleRobotPassword = {
@@ -108,6 +109,7 @@
"127.0.0.1:${toString config.services.prometheus.exporters.smartctl.port}"
"127.0.0.1:9341" # Slurm exporter
"127.0.0.1:9966" # GPFS custom exporter
"127.0.0.1:9999" # Nix-daemon custom exporter
"127.0.0.1:${toString config.services.prometheus.exporters.blackbox.port}"
];
}];

26
m/hut/nix-daemon-builds.sh Executable file
View File

@@ -0,0 +1,26 @@
#!/bin/sh
# Locate nix daemon pid
nd=$(pgrep -o nix-daemon)
# Locate children of nix-daemon
pids1=$(tr ' ' '\n' < "/proc/$nd/task/$nd/children")
# For each children, locate 2nd level children
pids2=$(echo "$pids1" | xargs -I @ /bin/sh -c 'cat /proc/@/task/*/children' | tr ' ' '\n')
cat <<EOF
HTTP/1.1 200 OK
Content-Type: text/plain; version=0.0.4; charset=utf-8; escaping=values
# HELP nix_daemon_build Nix daemon derivation build state.
# TYPE nix_daemon_build gauge
EOF
for pid in $pids2; do
name=$(cat /proc/$pid/environ 2>/dev/null | tr '\0' '\n' | rg "^name=(.+)" - --replace '$1' | tr -dc ' [:alnum:]_\-\.')

I think we could use --null-data in rg and avoid tr. There is also --max-count 1

I think we could use `--null-data` in `rg` and avoid `tr`. There is also `--max-count 1`

I'm not able to make it work with --null-data:

m/hut/nix-daemon-builds.sh: line 21: warning: command substitution: ignored null byte in input
nix_daemon_build{user="nixbld1",name="name=wxparaver-4.11.2"} 1

Maybe you can open another MR to improve it.

Edit: Reproducer:

$ printf 'bar=baz\0name=foo\0version=123\0' | rg --null-data "^name=(.+)" - --replace '$1'
name=foo
I'm not able to make it work with --null-data: ``` m/hut/nix-daemon-builds.sh: line 21: warning: command substitution: ignored null byte in input nix_daemon_build{user="nixbld1",name="name=wxparaver-4.11.2"} 1 ``` Maybe you can open another MR to improve it. Edit: Reproducer: ``` $ printf 'bar=baz\0name=foo\0version=123\0' | rg --null-data "^name=(.+)" - --replace '$1' name=foo ```
user=$(ps -o uname= -p "$pid")
if [ -n "$name" -a -n "$user" ]; then
printf 'nix_daemon_build{user="%s",name="%s"} 1\n' "$user" "$name"
rarias marked this conversation as resolved Outdated

I would pass name through tr -dc ' [:alnum:]_\-\.' to avoid getting garbage in grafana.

I would pass name through `tr -dc ' [:alnum:]_\-\.'` to avoid getting garbage in grafana.

Done

Done
fi
done

View File

@@ -0,0 +1,23 @@
{ pkgs, config, lib, ... }:
let
script = pkgs.runCommand "nix-daemon-exporter.sh" { }
''
cp ${./nix-daemon-builds.sh} $out;
chmod +x $out
''
;
in
{
systemd.services.nix-daemon-exporter = {
description = "Daemon to export nix-daemon metrics";
path = [ pkgs.procps pkgs.ripgrep ];
wantedBy = [ "default.target" ];
serviceConfig = {
Type = "simple";
ExecStart = "${pkgs.socat}/bin/socat TCP4-LISTEN:9999,fork EXEC:${script}";
# Needed root to read the environment, potentially unsafe
User = "root";
Group = "root";
};
};
}