1 { config, pkgs, lib, name, nodes, ... }:
3 cfg = config.myServices.monitoring;
4 send_mails = pkgs.runCommand "send_mails" {
5 buildInputs = [ pkgs.makeWrapper ];
8 cp ${./send_mails} $out/bin/send_mails
10 wrapProgram $out/bin/send_mails --prefix PATH : ${lib.makeBinPath [
14 postgresqlBinary = if config.myServices.databasesReplication.postgresql.enable
15 then config.myServices.databasesReplication.postgresql.mainPackage
16 else if config.myServices.databases.enable
17 then config.myServices.databases.postgresql.package
19 zfsPlugin = pkgs.fetchurl {
20 url = "https://www.claudiokuenzler.com/monitoring-plugins/check_zpools.sh";
21 sha256 = "0p9ms9340in80jkds4kfspw62xnzsv5s7ni9m28kxyd0bnzkbzhf";
23 myplugins = pkgs.runCommand "buildplugins" {
24 buildInputs = [ pkgs.makeWrapper pkgs.perl ];
27 cp ${zfsPlugin} $out/check_zpool.sh && chmod +x $out/check_zpool.sh
28 cp ${./plugins}/* $out/
30 wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir}
31 wrapProgram $out/check_zpool.sh --prefix PATH : ${lib.makeBinPath [
32 pkgs.which pkgs.zfs pkgs.gawk
34 wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [
37 wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [
38 pkgs.gnugrep pkgs.gawk pkgs.procps-ng
40 wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [
43 wrapProgram $out/check_redis_replication --prefix PATH : ${lib.makeBinPath [
44 pkgs.gnugrep pkgs.coreutils pkgs.redis
46 wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [
47 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb
49 wrapProgram $out/check_openldap_replication --prefix PATH : ${lib.makeBinPath [
50 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.openldap
52 wrapProgram $out/check_emails --prefix PATH : ${lib.makeBinPath [
53 pkgs.openssh send_mails
54 ]} --prefix PERL5LIB : ${pkgs.perlPackages.makePerlPath [
55 pkgs.perlPackages.TimeDate
57 wrapProgram $out/check_ftp_database --prefix PATH : ${lib.makeBinPath [
60 wrapProgram $out/check_git --prefix PATH : ${lib.makeBinPath [
63 wrapProgram $out/check_imap_connection --prefix PATH : ${lib.makeBinPath [
66 wrapProgram $out/check_eriomem --prefix PATH : ${lib.makeBinPath [
67 pkgs.s3cmd pkgs.python3
69 makeWrapper $out/check_backup_age $out/check_backup_eriomem_age --prefix PATH : ${lib.makeBinPath [
71 ]} --set SECRETS_PATH ${lib.optionalString cfg.master config.secrets.fullPaths."eriomem_access_key"}
72 makeWrapper $out/check_backup_age $out/check_backup_ovh_age --prefix PATH : ${lib.makeBinPath [
74 ]} --set SECRETS_PATH ${lib.optionalString cfg.master config.secrets.fullPaths."ovh_access_key"}
75 wrapProgram $out/notify_by_email --prefix PATH : ${lib.makeBinPath [
78 wrapProgram $out/notify_by_slack --prefix PATH : ${lib.makeBinPath [
81 wrapProgram $out/check_ovh_sms --prefix PATH : ${lib.makeBinPath [
82 (pkgs.python3.withPackages (ps: [ps.ovh]))
84 wrapProgram $out/check_bandwidth --prefix PATH : ${lib.makeBinPath [
88 toObjects = pkgs.callPackage ./to_objects.nix {};
91 processWarn = "250"; processAlert = "400";
92 loadWarn = "8.0"; loadAlert = "10.0";
96 processWarn = "250"; processAlert = "400";
97 loadWarn = "8.0"; loadAlert = "10.0";
101 processWarn = "60"; processAlert = "70";
102 loadWarn = "1.0"; loadAlert = "2.0";
106 processWarn = "50"; processAlert = "60";
107 loadWarn = "4.0"; loadAlert = "6.0";
108 load15Warn = "1.0"; load15Alert = "2.0";
112 processWarn = "250"; processAlert = "400";
113 loadWarn = "8.0"; loadAlert = "10.0";
117 externalObjects = lib.genAttrs [ "tiboqorl-fr" ]
118 (n: pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; });
119 masterPassiveObjects = let
120 passiveNodes = lib.attrsets.filterAttrs (n: _: builtins.elem n ["backup-2" "eldiron" "quatresaisons" "dilion"]) nodes;
121 toPassiveServices = map (s: s.passiveInfo.filter s // s.passiveInfo);
122 passiveServices = lib.flatten (lib.attrsets.mapAttrsToList
123 (_: n: toPassiveServices n.config.myServices.monitoring.services)
125 ) ++ lib.flatten (lib.attrsets.mapAttrsToList
126 (_: n: toPassiveServices n.service)
129 service = passiveServices;
130 host = lib.lists.foldr
133 (lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes
134 ++ lib.attrsets.mapAttrsToList (_: n: n.host) externalObjects);
136 emailCheck = host: hostFQDN: let
137 allCfg = config.myEnv.monitoring.email_check;
138 cfg = allCfg."${host}";
139 reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem host v.targets) allCfg);
140 to_email = cfg': host':
141 let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+";
142 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
143 mails_to_send = builtins.concatStringsSep "," (map (n: to_email allCfg."${n}" host) cfg.targets);
144 mails_to_receive = builtins.concatStringsSep "," (map (n: "${to_email cfg n}:${n}") reverseTargets);
145 command = if cfg.local
147 [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ]
149 [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ];
152 service_description = "${hostFQDN} email service is active";
153 use = "mail-service";
154 host_name = hostFQDN;
155 servicegroups = "webstatus-email";
156 check_command = command;
159 (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; }))
160 [ "ulminfo-fr" "phare" ];
161 masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; };
162 commonObjects = pkgs.callPackage ./objects_common.nix ({
164 hostFQDN = config.hostEnv.fqdn;
166 sudo = "/run/wrappers/bin/sudo";
167 } // builtins.getAttr name commonConfig);
170 specific_file = ./. + "/objects_" + name + ".nix";
172 lib.attrsets.optionalAttrs
173 (builtins.pathExists specific_file)
174 (pkgs.callPackage specific_file {
175 inherit config nodes emailCheck;
176 hostFQDN = config.hostEnv.fqdn;
179 objectsFiles = lib.mapAttrs' (name: value: lib.nameValuePair
180 "=/${name}/objects.conf" { alias = pkgs.writeText "objects.conf" (toObjects value); }
185 myServices.monitoring = {
186 enable = lib.mkOption {
187 type = lib.types.bool;
190 Whether to enable monitoring.
193 master = lib.mkOption {
194 type = lib.types.bool;
197 This instance is the master instance
200 hosts = lib.mkOption {
202 description = "Hosts list for this host";
203 default = (commonObjects.host or {}) // (hostObjects.host or {});
205 services = lib.mkOption {
207 description = "Services list for this host";
208 default = commonObjects.service ++ hostObjects.service;
213 config = lib.mkIf cfg.enable {
214 services.nginx = lib.mkIf config.myServices.status.enable {
215 virtualHosts."status.immae.eu".locations = objectsFiles;
218 services.duplyBackup.profiles.monitoring = {
219 rootDir = config.services.naemon.varDir;
221 security.sudo.extraRules = [
224 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
225 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
227 users = [ "naemon" ];
232 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
234 users = [ "naemon" ];
239 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
241 users = [ "naemon" ];
246 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
248 users = [ "naemon" ];
253 { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; }
255 users = [ "naemon" ];
260 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
262 users = [ "naemon" ];
266 environment.etc."mdadm.conf" = {
270 text = "MAILADDR ${config.myEnv.monitoring.email}";
275 dest = "naemon/id_rsa";
278 permissions = "0400";
279 text = config.myEnv.monitoring.ssh_secret_key;
281 ] ++ lib.optionals cfg.master (
282 lib.mapAttrsToList (k: v:
284 dest = "${k}_access_key";
287 permissions = "0400";
289 export AWS_ACCESS_KEY_ID="${v.accessKeyId}"
290 export AWS_SECRET_ACCESS_KEY="${v.secretAccessKey}"
291 export BASE_URL="${v.remote "immae-eldiron"}"
293 }) config.myEnv.backup.remotes
295 # needed since extraResource is not in the closure
296 systemd.services.naemon.path = [ myplugins ];
303 admin_email=${config.myEnv.monitoring.email}
304 '' + lib.optionalString (!cfg.master) ''
305 obsess_over_services=1
306 ocsp_command=notify-master
307 '' + lib.optionalString (cfg.master) ''
308 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
309 broker_module=${pkgs.status_engine.module}/lib/status-engine/naemon/statusengine-${pkgs.naemon.status_engine_version}.o use_service_perfdata=1 use_process_data=0 use_system_command_data=0 use_external_command_data=0 use_flapping_data=0 use_program_status_data=0 use_notification_data=0 use_contact_status_data=0 use_contact_notification_data=0 use_event_handler_data=0 use_object_data=0
313 $USER200$=${config.myEnv.monitoring.status_url}
314 $USER201$=${config.myEnv.monitoring.status_token}
315 $USER202$=${config.myEnv.monitoring.http_user_password}
316 $USER203$=${config.secrets.fullPaths."naemon/id_rsa"}
317 $USER204$=${config.myEnv.monitoring.imap_login}
318 $USER205$=${config.myEnv.monitoring.imap_password}
319 $USER206$=${config.myEnv.monitoring.slack_channel}
320 $USER207$=${config.myEnv.monitoring.slack_url}
321 $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)}
322 $USER209$=${builtins.concatStringsSep "," [
323 config.myEnv.monitoring.ovh_sms.endpoint
324 config.myEnv.monitoring.ovh_sms.application_key
325 config.myEnv.monitoring.ovh_sms.application_secret
326 config.myEnv.monitoring.ovh_sms.consumer_key
327 config.myEnv.monitoring.ovh_sms.account
330 objectDefs = toObjects commonObjects
331 + toObjects hostObjects
332 + lib.optionalString cfg.master (toObjects masterObjects)
333 + lib.optionalString cfg.master (toObjects masterPassiveObjects)
334 + lib.optionalString cfg.master (builtins.concatStringsSep "\n" (map toObjects otherObjects));