@cryptotaxi247 / infra / commits / fb11b4ec

prometheus-nixos-exporter: Expose whether current-system kernel is booted

This allows alerting on when the current-system has a different kernel, than the booted one, which usually indicates we should reboot the machine.

Martin Weinelt committed Feb 16, 2024 at 23:54 UTC fb11b4ec2a00b41984fe3d2e164c4b364ea45d4d
1 file changed +17
modules/prometheus/nixos-exporter/prometheus_nixos_exporter/__main__.py
+17
@@ -4,6 +4,7 @@
4
5 import subprocess
6 import json
7 +import os
8 import sys
9 import time
10 from packaging.version import Version
@@ -52,6 +53,19 @@ class NixosSystemCollector:
53 )
54 yield booted_system
55
56 + current_system_kernel_booted = GaugeMetricFamily(
57 + "nixos_current_system_kernel_booted",
58 + "Whether the currently booted kernel matches the one in the current generation.",
59 + labels=["booted", "current"],
60 + )
61 + booted_kernel = self.get_kernel_out("/run/booted-system")
62 + current_kernel = self.get_kernel_out("/run/current-system")
63 +
64 + current_system_kernel_booted.add_metric(
65 + [booted_kernel, current_kernel], booted_kernel == current_kernel
66 + )
67 + yield current_system_kernel_booted
68 +
69 def get_version_id(self, path):
70 result = subprocess.run(
71 ["bash", "-c", f"source {path}/etc/os-release; echo $VERSION_ID"],
@@ -62,6 +76,9 @@ class NixosSystemCollector:
76
77 return None
78
79 + def get_kernel_out(self, path):
80 + return os.path.dirname(os.readlink(os.path.join(path, "kernel")))
81 +
82 def get_time(self, path):
83 result = subprocess.run(
84 ["nix", "path-info", "--json", path], stdout=subprocess.PIPE