3 environment.url = "path:../environment";
4 secrets.url = "path:../../secrets";
5 naemon.url = "path:../../naemon";
6 nixpkgs-lib.url = "github:NixOS/nixpkgs?dir=lib";
8 outputs = { self, environment, nixpkgs-lib, secrets, naemon }: {
9 nagios-cli-config = ./nagios-cli.cfg;
11 expandedObject = kind: object: objects:
13 then expandedObject kind objects.templates.${kind}.${object.use} objects // object
16 objectsCommon = import ./objects_common.nix;
17 toObjects = import ./to_objects.nix { inherit (nixpkgs-lib) lib; };
19 toMasterPassiveObject = svcTemplate: freshnessThresholdMultiplier: objects:
21 service = with nixpkgs-lib.lib; map (s:
23 host_name = (expandedObject "service" s objects).host_name;
26 freshness_threshold = let
27 fs = expandedObject "service" s objects;
28 in if builtins.isInt fs.check_interval
29 then builtins.ceil (freshnessThresholdMultiplier * 60 * fs.check_interval)
30 else fs.check_interval;
32 // filterAttrs (k: v: builtins.elem k ["service_description"] || builtins.substring 0 1 k == "_") s
34 (n: nameValuePair (removePrefix "__passive_" n))
35 (filterAttrs (k: _: hasPrefix "__passive_" k) s)
40 emailCheck = allCfg: host: hostFQDN: let
41 cfg = allCfg."${host}";
42 reverseTargets = builtins.attrNames (nixpkgs-lib.lib.filterAttrs (k: v: builtins.elem host v.targets) allCfg);
43 to_email = cfg': host':
44 let sep = if nixpkgs-lib.lib.hasInfix "+" cfg'.mail_address then "_" else "+";
45 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
46 mails_to_send = builtins.concatStringsSep "," (map (n: to_email allCfg."${n}" host) cfg.targets);
47 mails_to_receive = builtins.concatStringsSep "," (map (n: "${to_email cfg n}:${n}") reverseTargets);
48 command = if cfg.local
50 [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ]
52 [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ];
55 service_description = "${hostFQDN} email service is active";
58 servicegroups = "webstatus-email";
59 check_command = command;
62 nixosModule = self.nixosModules.monitoring;
63 nixosModules.monitoring = { config, pkgs, lib, ... }:
65 cfg = config.myServices.monitoring;
66 allPluginsConfig = import ./myplugins.nix {
67 inherit pkgs lib config;
68 sudo = "/run/wrappers/bin/sudo";
70 mypluginsConfig = lib.mapAttrs (n: v:
71 if builtins.isFunction v
72 then v (cfg.pluginsArgs."${n}" or {})
74 ) (lib.getAttrs cfg.activatedPlugins allPluginsConfig);
76 mypluginsChunk = builtins.concatStringsSep "\n" (lib.mapAttrsToList (k: v: v.chunk or "") mypluginsConfig);
77 in pkgs.runCommand "buildplugins" {
78 buildInputs = [ pkgs.makeWrapper pkgs.perl ];
83 objectsModule = with lib.types; submodule {
85 command = lib.mkOption {
88 description = "Command definitions";
92 type = attrsOf (attrsOf str);
94 description = "Host definitions";
96 hostgroup = lib.mkOption {
97 type = attrsOf (attrsOf str);
99 description = "Host group definitions";
101 hostdependency = lib.mkOption {
102 type = listOf (attrsOf str);
104 description = "Host dependency definitions";
107 service = lib.mkOption {
108 type = listOf (attrsOf (oneOf [ str (listOf str) int ]));
110 # listOf str -> list to be concatenated with "!"
113 description = "Service definitions";
115 servicegroup = lib.mkOption {
116 type = attrsOf (attrsOf str);
118 description = "Service group definitions";
120 servicedependency = lib.mkOption {
121 type = listOf (attrsOf str);
123 description = "Service dependency definitions";
126 contact = lib.mkOption {
127 type = attrsOf (attrsOf str);
129 description = "Contact definitions";
131 contactgroup = lib.mkOption {
132 type = attrsOf (attrsOf str);
134 description = "Contact group definitions";
137 timeperiod = lib.mkOption {
138 type = attrsOf (attrsOf str);
140 description = "Time period definitions";
143 templates = lib.mkOption {
144 description = "Template definitions";
148 service = lib.mkOption { type = attrsOf (attrsOf (either str int)); default = {}; };
149 contact = lib.mkOption { type = attrsOf (attrsOf str); default = {}; };
150 host = lib.mkOption { type = attrsOf (attrsOf str); default = {}; };
159 myServices.monitoring = {
160 enable = lib.mkOption {
161 type = lib.types.bool;
164 Whether to enable monitoring.
167 smartdDisks = lib.mkOption {
168 type = lib.types.listOf lib.types.str;
171 List of smartd disks ids (symlinks in /dev/disk/by-id/) to monitor
174 master = lib.mkOption {
175 type = lib.types.bool;
178 This instance is the master instance
181 pluginsArgs = lib.mkOption {
183 description = "Arguments to pass to the naemon plugin configuration";
184 type = lib.types.attrsOf (lib.types.attrsOf lib.types.unspecified);
186 activatedPlugins = lib.mkOption {
188 description = "List of naemon plugins to activate";
189 type = lib.types.listOf (lib.types.enum (builtins.attrNames allPluginsConfig));
191 fromMasterActivatedPlugins = lib.mkOption {
193 description = "List of naemon plugins to activate from master";
194 type = lib.types.listOf (lib.types.str);
196 resources = lib.mkOption {
198 description = "List of additionnal resources elements";
199 type = lib.types.attrsOf (lib.types.str);
201 objects = lib.mkOption {
203 description = "Object definitions";
204 type = objectsModule;
206 fromMasterObjects = lib.mkOption {
208 description = "Object definitions of checks that should be executed from master";
209 type = objectsModule;
215 environment.nixosModule
219 config = lib.mkIf cfg.enable {
220 myServices.monitoring.objects.command =
221 lib.foldr (v: o: o // (v.commands or {})) {} (builtins.attrValues mypluginsConfig);
223 security.sudo.extraRules = let
224 pluginsSudo = lib.lists.remove null (lib.mapAttrsToList (k: v:
226 then ({ users = [ "naemon" ]; } // (v.sudo myplugins))
227 else null) mypluginsConfig);
230 environment.etc.cnagios.source = "${pkgs.cnagios}/share/doc/cnagios";
231 environment.systemPackages = let
232 nagios-cli = pkgs.writeScriptBin "nagios-cli" ''
233 #!${pkgs.stdenv.shell}
234 sudo -u naemon ${pkgs.nagios-cli}/bin/nagios-cli -c ${self.nagios-cli-config}
244 permissions = "0400";
245 text = config.myEnv.monitoring.ssh_secret_key;
247 "naemon/resources.cfg".keyDependencies = [ myplugins ];
255 admin_email=${config.myEnv.monitoring.email}
256 '' + lib.optionalString (!cfg.master) ''
257 obsess_over_services=1
258 ocsp_command=notify-master
261 resources = [cfg.resources or {}] ++ (lib.mapAttrsToList (k: v: v.resources or {}) mypluginsConfig);
262 joined = lib.zipAttrsWith (n: v: if builtins.length (lib.unique v) == 1 then builtins.head v else abort "Non-unique resources names") resources;
263 joinedStr = builtins.concatStringsSep "\n" (lib.mapAttrsToList (k: v: "$" + "${k}$=${v}") joined);
269 self.lib.toObjects cfg.objects;
272 myServices.monitoring.objects.service = builtins.map (d: {
273 service_description = "Disk /dev/disk/by-id/${d} is sane";
274 use = "local-service";
275 check_command = [ "check_smartctl" "/dev/disk/by-id/${d}" ];
276 __passive_servicegroups = "webstatus-resources";
284 OnCalendar = "monthly";
285 RandomizedDelaySec = "3 weeks";
286 FixedRandomDelay = true;
288 wantedBy = [ "timers.target" ];
292 OnCalendar = "monthly";
293 RandomizedDelaySec = "3 weeks";
294 FixedRandomDelay = true;
296 wantedBy = [ "timers.target" ];
299 "check-smartd-long-${id}" = checkLongTimer;
300 "check-smartd-short-${id}" = checkShortTimer;
302 toCheckService = id: type: {
303 description = "Run ${type} smartctl test for /dev/disk/by-id/${id}";
304 after = [ "multi-user.target" ];
307 ExecStart = "${pkgs.smartmontools}/bin/smartctl -t ${type} /dev/disk/by-id/${id}";
311 "check-smartd-long-${id}" = toCheckService id "long";
312 "check-smartd-short-${id}" = toCheckService id "short";
316 services = lib.attrsets.mergeAttrsList (builtins.map toSDServices cfg.smartdDisks);
317 timers = lib.attrsets.mergeAttrsList (builtins.map toSDTimers cfg.smartdDisks);