From e8d50f14185867d490f002aa5c408343ea5ea062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Fri, 19 Apr 2024 23:43:13 +0200 Subject: Factorize smartd monitoring --- flakes/private/monitoring/flake.nix | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'flakes/private') diff --git a/flakes/private/monitoring/flake.nix b/flakes/private/monitoring/flake.nix index b7c3997..5610d67 100644 --- a/flakes/private/monitoring/flake.nix +++ b/flakes/private/monitoring/flake.nix @@ -164,6 +164,13 @@ Whether to enable monitoring. ''; }; + smartdDisks = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + description = '' + List of smartd disks ids (symlinks in /dev/disk/by-id/) to monitor + ''; + }; master = lib.mkOption { type = lib.types.bool; default = false; @@ -261,6 +268,54 @@ objectDefs = self.lib.toObjects cfg.objects; }; + + myServices.monitoring.objects.service = builtins.map (d: { + service_description = "Disk /dev/disk/by-id/${d} is sane"; + use = "local-service"; + check_command = [ "check_smartctl" "/dev/disk/by-id/${d}" ]; + __passive_servicegroups = "webstatus-resources"; + + check_interval = 60; + }) cfg.smartdDisks; + + systemd = let + checkShortTimer = { + timerConfig = { + OnCalendar = "monthly"; + RandomizedDelaySec = "3 weeks"; + FixedRandomDelay = true; + }; + wantedBy = [ "timers.target" ]; + }; + checkLongTimer = { + timerConfig = { + OnCalendar = "monthly"; + RandomizedDelaySec = "3 weeks"; + FixedRandomDelay = true; + }; + wantedBy = [ "timers.target" ]; + }; + toSDTimers = id: { + "check-smartd-long-${id}" = checkLongTimer; + "check-smartd-short-${id}" = checkShortTimer; + }; + toCheckService = id: type: { + description = "Run ${type} smartctl test for /dev/disk/by-id/${id}"; + after = [ "multi-user.target" ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.smartmontools}/bin/smartctl -t ${type} /dev/disk/by-id/${id}"; + }; + }; + toSDServices = id: { + "check-smartd-long-${id}" = toCheckService id "long"; + "check-smartd-short-${id}" = toCheckService id "short"; + }; + + in { + services = lib.attrsets.mergeAttrsList (builtins.map toSDServices cfg.smartdDisks); + timers = lib.attrsets.mergeAttrsList (builtins.map toSDTimers cfg.smartdDisks); + }; }; }; }; -- cgit v1.2.3