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 myplugins = pkgs.runCommand "buildplugins" {
20 buildInputs = [ pkgs.makeWrapper pkgs.perl ];
23 cp ${./plugins}/* $out/
25 wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir}
26 wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [
29 wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [
30 pkgs.gnugrep pkgs.gawk pkgs.procps-ng
32 wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [
35 wrapProgram $out/check_redis_replication --prefix PATH : ${lib.makeBinPath [
36 pkgs.gnugrep pkgs.coreutils pkgs.redis
38 wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [
39 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb
41 wrapProgram $out/check_openldap_replication --prefix PATH : ${lib.makeBinPath [
42 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.openldap
44 wrapProgram $out/check_emails --prefix PATH : ${lib.makeBinPath [
45 pkgs.openssh send_mails
46 ]} --prefix PERL5LIB : ${pkgs.perlPackages.makePerlPath [
47 pkgs.perlPackages.TimeDate
49 wrapProgram $out/check_ftp_database --prefix PATH : ${lib.makeBinPath [
52 wrapProgram $out/check_git --prefix PATH : ${lib.makeBinPath [
55 wrapProgram $out/check_imap_connection --prefix PATH : ${lib.makeBinPath [
58 wrapProgram $out/check_eriomem --prefix PATH : ${lib.makeBinPath [
59 pkgs.s3cmd pkgs.python3
61 makeWrapper $out/check_backup_age $out/check_backup_eriomem_age --prefix PATH : ${lib.makeBinPath [
63 ]} --set SECRETS_PATH ${lib.optionalString cfg.master config.secrets.fullPaths."eriomem_access_key"}
64 makeWrapper $out/check_backup_age $out/check_backup_ovh_age --prefix PATH : ${lib.makeBinPath [
66 ]} --set SECRETS_PATH ${lib.optionalString cfg.master config.secrets.fullPaths."ovh_access_key"}
67 wrapProgram $out/notify_by_email --prefix PATH : ${lib.makeBinPath [
70 wrapProgram $out/notify_by_slack --prefix PATH : ${lib.makeBinPath [
73 wrapProgram $out/check_ovh_sms --prefix PATH : ${lib.makeBinPath [
74 (pkgs.python3.withPackages (ps: [ps.ovh]))
76 wrapProgram $out/check_bandwidth --prefix PATH : ${lib.makeBinPath [
80 toObjects = pkgs.callPackage ./to_objects.nix {};
83 processWarn = "250"; processAlert = "400";
84 loadWarn = "8.0"; loadAlert = "10.0";
88 processWarn = "60"; processAlert = "70";
89 loadWarn = "1.0"; loadAlert = "2.0";
93 processWarn = "50"; processAlert = "60";
94 loadWarn = "4.0"; loadAlert = "6.0";
95 load15Warn = "1.0"; load15Alert = "2.0";
99 externalObjects = lib.genAttrs [ "tiboqorl-fr" ]
100 (n: pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; });
101 masterPassiveObjects = let
102 passiveNodes = lib.attrsets.filterAttrs (n: _: builtins.elem n ["backup-2" "eldiron"]) nodes;
103 toPassiveServices = map (s: s.passiveInfo.filter s // s.passiveInfo);
104 passiveServices = lib.flatten (lib.attrsets.mapAttrsToList
105 (_: n: toPassiveServices n.config.myServices.monitoring.services)
107 ) ++ lib.flatten (lib.attrsets.mapAttrsToList
108 (_: n: toPassiveServices n.service)
111 service = passiveServices;
112 host = lib.lists.foldr
115 (lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes
116 ++ lib.attrsets.mapAttrsToList (_: n: n.host) externalObjects);
118 emailCheck = host: hostFQDN: let
119 allCfg = config.myEnv.monitoring.email_check;
120 cfg = allCfg."${host}";
121 reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem host v.targets) allCfg);
122 to_email = cfg': host':
123 let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+";
124 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
125 mails_to_send = builtins.concatStringsSep "," (map (n: to_email allCfg."${n}" host) cfg.targets);
126 mails_to_receive = builtins.concatStringsSep "," (map (n: "${to_email cfg n}:${n}") reverseTargets);
127 command = if cfg.local
129 [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ]
131 [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ];
134 service_description = "${hostFQDN} email service is active";
135 use = "mail-service";
136 host_name = hostFQDN;
137 servicegroups = "webstatus-email";
138 check_command = command;
141 (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; }))
142 [ "ulminfo-fr" "phare" ];
143 masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; };
144 commonObjects = pkgs.callPackage ./objects_common.nix ({
146 hostFQDN = config.hostEnv.fqdn;
148 sudo = "/run/wrappers/bin/sudo";
149 } // builtins.getAttr name commonConfig);
152 specific_file = ./. + "/objects_" + name + ".nix";
154 lib.attrsets.optionalAttrs
155 (builtins.pathExists specific_file)
156 (pkgs.callPackage specific_file {
157 inherit config nodes emailCheck;
158 hostFQDN = config.hostEnv.fqdn;
161 objectsFiles = lib.mapAttrs' (name: value: lib.nameValuePair
162 "=/${name}/objects.conf" { alias = pkgs.writeText "objects.conf" (toObjects value); }
167 myServices.monitoring = {
168 enable = lib.mkOption {
169 type = lib.types.bool;
172 Whether to enable monitoring.
175 master = lib.mkOption {
176 type = lib.types.bool;
179 This instance is the master instance
182 hosts = lib.mkOption {
184 description = "Hosts list for this host";
185 default = (commonObjects.host or {}) // (hostObjects.host or {});
187 services = lib.mkOption {
189 description = "Services list for this host";
190 default = commonObjects.service ++ hostObjects.service;
195 config = lib.mkIf cfg.enable {
196 services.nginx = lib.mkIf config.myServices.status.enable {
197 virtualHosts."status.immae.eu".locations = objectsFiles;
200 services.duplyBackup.profiles.monitoring = {
201 rootDir = config.services.naemon.varDir;
203 security.sudo.extraRules = [
206 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
207 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
209 users = [ "naemon" ];
214 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
216 users = [ "naemon" ];
221 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
223 users = [ "naemon" ];
228 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
230 users = [ "naemon" ];
235 { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; }
237 users = [ "naemon" ];
242 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
244 users = [ "naemon" ];
248 environment.etc."mdadm.conf" = {
252 text = "MAILADDR ${config.myEnv.monitoring.email}";
257 dest = "naemon/id_rsa";
260 permissions = "0400";
261 text = config.myEnv.monitoring.ssh_secret_key;
263 ] ++ lib.optionals cfg.master (
264 lib.mapAttrsToList (k: v:
266 dest = "${k}_access_key";
269 permissions = "0400";
271 export AWS_ACCESS_KEY_ID="${v.accessKeyId}"
272 export AWS_SECRET_ACCESS_KEY="${v.secretAccessKey}"
273 export BASE_URL="${v.remote "immae-eldiron"}"
275 }) config.myEnv.backup.remotes
277 # needed since extraResource is not in the closure
278 systemd.services.naemon.path = [ myplugins ];
285 admin_email=${config.myEnv.monitoring.email}
286 '' + lib.optionalString (!cfg.master) ''
287 obsess_over_services=1
288 ocsp_command=notify-master
289 '' + lib.optionalString (cfg.master) ''
290 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
291 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
295 $USER200$=${config.myEnv.monitoring.status_url}
296 $USER201$=${config.myEnv.monitoring.status_token}
297 $USER202$=${config.myEnv.monitoring.http_user_password}
298 $USER203$=${config.secrets.fullPaths."naemon/id_rsa"}
299 $USER204$=${config.myEnv.monitoring.imap_login}
300 $USER205$=${config.myEnv.monitoring.imap_password}
301 $USER206$=${config.myEnv.monitoring.slack_channel}
302 $USER207$=${config.myEnv.monitoring.slack_url}
303 $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)}
304 $USER209$=${builtins.concatStringsSep "," [
305 config.myEnv.monitoring.ovh_sms.endpoint
306 config.myEnv.monitoring.ovh_sms.application_key
307 config.myEnv.monitoring.ovh_sms.application_secret
308 config.myEnv.monitoring.ovh_sms.consumer_key
309 config.myEnv.monitoring.ovh_sms.account
312 objectDefs = toObjects commonObjects
313 + toObjects hostObjects
314 + lib.optionalString cfg.master (toObjects masterObjects)
315 + lib.optionalString cfg.master (toObjects masterPassiveObjects)
316 + lib.optionalString cfg.master (builtins.concatStringsSep "\n" (map toObjects otherObjects));