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 megacli = pkgs.megacli.overrideAttrs(old: { meta = old.meta // { license = null; }; });
24 megaCliPlugin = pkgs.runCommand "megaCliPlugin" {
25 plugin = pkgs.fetchurl {
26 name = "check_megaraid_sas";
27 url = "https://exchange.nagios.org/components/com_mtree/attachment.php?link_id=6381&cf_id=24";
28 sha256 = "0yf60p4c0hb4q3fng9fc14qc89bqm0f1sijayzygadaqcl44jx4p";
32 cp $plugin $out/check_megaraid_sas
33 chmod +x $out/check_megaraid_sas
35 substituteInPlace $out/check_megaraid_sas --replace /usr/sbin/MegaCli ${megacli}/bin/MegaCli64
36 substituteInPlace $out/check_megaraid_sas --replace 'sudo $megacli' '/run/wrappers/bin/sudo $megacli'
37 sed -i -e "s/use utils qw(%ERRORS);/my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3);/" $out/check_megaraid_sas
39 myplugins = pkgs.runCommand "buildplugins" {
40 buildInputs = [ pkgs.makeWrapper pkgs.perl ];
43 cp ${zfsPlugin} $out/check_zpool.sh && chmod +x $out/check_zpool.sh
44 cp ${megaCliPlugin}/check_megaraid_sas $out/
45 cp ${./plugins}/* $out/
47 wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir}
48 wrapProgram $out/check_zpool.sh --prefix PATH : ${lib.makeBinPath [
49 pkgs.which pkgs.zfs pkgs.gawk
51 wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [
54 wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [
55 pkgs.gnugrep pkgs.gawk pkgs.procps-ng
57 wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [
60 wrapProgram $out/check_redis_replication --prefix PATH : ${lib.makeBinPath [
61 pkgs.gnugrep pkgs.coreutils pkgs.redis
63 wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [
64 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb
66 wrapProgram $out/check_openldap_replication --prefix PATH : ${lib.makeBinPath [
67 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.openldap
69 wrapProgram $out/check_emails --prefix PATH : ${lib.makeBinPath [
70 pkgs.openssh send_mails
71 ]} --prefix PERL5LIB : ${pkgs.perlPackages.makePerlPath [
72 pkgs.perlPackages.TimeDate
74 wrapProgram $out/check_ftp_database --prefix PATH : ${lib.makeBinPath [
77 wrapProgram $out/check_git --prefix PATH : ${lib.makeBinPath [
80 wrapProgram $out/check_imap_connection --prefix PATH : ${lib.makeBinPath [
83 wrapProgram $out/check_eriomem --prefix PATH : ${lib.makeBinPath [
84 pkgs.s3cmd pkgs.python3
86 makeWrapper $out/check_backup_age $out/check_backup_eriomem_age --prefix PATH : ${lib.makeBinPath [
88 ]} --set SECRETS_PATH ${lib.optionalString cfg.master config.secrets.fullPaths."eriomem_access_key"}
89 makeWrapper $out/check_backup_age $out/check_backup_ovh_age --prefix PATH : ${lib.makeBinPath [
91 ]} --set SECRETS_PATH ${lib.optionalString cfg.master config.secrets.fullPaths."ovh_access_key"}
92 wrapProgram $out/notify_by_email --prefix PATH : ${lib.makeBinPath [
95 wrapProgram $out/notify_by_slack --prefix PATH : ${lib.makeBinPath [
98 wrapProgram $out/notify_eban_url --prefix PATH : ${lib.makeBinPath [
101 wrapProgram $out/check_ovh_sms --prefix PATH : ${lib.makeBinPath [
102 (pkgs.python3.withPackages (ps: [ps.ovh]))
104 wrapProgram $out/check_bandwidth --prefix PATH : ${lib.makeBinPath [
108 toObjects = pkgs.callPackage ./to_objects.nix {};
111 processWarn = "250"; processAlert = "400";
112 loadWarn = "8.0"; loadAlert = "10.0";
116 processWarn = "250"; processAlert = "400";
117 loadWarn = "8.0"; loadAlert = "10.0";
121 processWarn = "60"; processAlert = "70";
122 loadWarn = "1.0"; loadAlert = "2.0";
126 processWarn = "50"; processAlert = "60";
127 loadWarn = "4.0"; loadAlert = "6.0";
128 load15Warn = "1.0"; load15Alert = "2.0";
132 processWarn = "250"; processAlert = "400";
133 loadWarn = "8.0"; loadAlert = "10.0";
137 externalObjects = lib.genAttrs [ "tiboqorl-fr" ]
138 (n: pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; });
139 masterPassiveObjects = let
140 passiveNodes = lib.attrsets.filterAttrs (n: _: builtins.elem n ["backup-2" "eldiron" "quatresaisons" "dilion"]) nodes;
141 toPassiveServices = map (s: s.passiveInfo.filter s // s.passiveInfo);
142 passiveServices = lib.flatten (lib.attrsets.mapAttrsToList
143 (_: n: toPassiveServices n.config.myServices.monitoring.services)
145 ) ++ lib.flatten (lib.attrsets.mapAttrsToList
146 (_: n: toPassiveServices n.service)
149 service = passiveServices;
150 host = lib.lists.foldr
153 (lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes
154 ++ lib.attrsets.mapAttrsToList (_: n: n.host) externalObjects);
156 emailCheck = host: hostFQDN: let
157 allCfg = config.myEnv.monitoring.email_check;
158 cfg = allCfg."${host}";
159 reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem host v.targets) allCfg);
160 to_email = cfg': host':
161 let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+";
162 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
163 mails_to_send = builtins.concatStringsSep "," (map (n: to_email allCfg."${n}" host) cfg.targets);
164 mails_to_receive = builtins.concatStringsSep "," (map (n: "${to_email cfg n}:${n}") reverseTargets);
165 command = if cfg.local
167 [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ]
169 [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ];
172 service_description = "${hostFQDN} email service is active";
173 use = "mail-service";
174 host_name = hostFQDN;
175 servicegroups = "webstatus-email";
176 check_command = command;
179 (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; }))
180 [ "ulminfo-fr" "phare" "eban" ];
181 masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; };
182 commonObjects = pkgs.callPackage ./objects_common.nix ({
184 hostFQDN = config.hostEnv.fqdn;
186 sudo = "/run/wrappers/bin/sudo";
187 } // builtins.getAttr name commonConfig);
190 specific_file = ./. + "/objects_" + name + ".nix";
192 lib.attrsets.optionalAttrs
193 (builtins.pathExists specific_file)
194 (pkgs.callPackage specific_file {
195 inherit config nodes emailCheck;
196 hostFQDN = config.hostEnv.fqdn;
199 objectsFiles = lib.mapAttrs' (name: value: lib.nameValuePair
200 "=/${name}/objects.conf" { alias = pkgs.writeText "objects.conf" (toObjects value); }
205 myServices.monitoring = {
206 enable = lib.mkOption {
207 type = lib.types.bool;
210 Whether to enable monitoring.
213 master = lib.mkOption {
214 type = lib.types.bool;
217 This instance is the master instance
220 hosts = lib.mkOption {
222 description = "Hosts list for this host";
223 default = (commonObjects.host or {}) // (hostObjects.host or {});
225 services = lib.mkOption {
227 description = "Services list for this host";
228 default = commonObjects.service ++ hostObjects.service;
233 config = lib.mkIf cfg.enable {
234 services.nginx = lib.mkIf config.myServices.status.enable {
235 virtualHosts."status.immae.eu".locations = objectsFiles;
238 services.duplyBackup.profiles.monitoring = {
239 rootDir = config.services.naemon.varDir;
241 security.sudo.extraRules = [
244 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
245 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
246 { command = "${megacli}/bin/MegaCli64"; options = [ "NOPASSWD" ]; }
248 users = [ "naemon" ];
253 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
255 users = [ "naemon" ];
260 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
262 users = [ "naemon" ];
267 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
269 users = [ "naemon" ];
274 { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; }
276 users = [ "naemon" ];
281 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
283 users = [ "naemon" ];
287 environment.etc."mdadm.conf" = {
291 text = "MAILADDR ${config.myEnv.monitoring.email}";
296 dest = "naemon/id_rsa";
299 permissions = "0400";
300 text = config.myEnv.monitoring.ssh_secret_key;
302 ] ++ lib.optionals cfg.master (
303 lib.mapAttrsToList (k: v:
305 dest = "${k}_access_key";
308 permissions = "0400";
310 export AWS_ACCESS_KEY_ID="${v.accessKeyId}"
311 export AWS_SECRET_ACCESS_KEY="${v.secretAccessKey}"
312 export BASE_URL="${v.remote "immae-eldiron"}"
314 }) config.myEnv.backup.remotes
316 # needed since extraResource is not in the closure
317 systemd.services.naemon.path = [ myplugins ];
324 admin_email=${config.myEnv.monitoring.email}
325 '' + lib.optionalString (!cfg.master) ''
326 obsess_over_services=1
327 ocsp_command=notify-master
328 '' + lib.optionalString (cfg.master) ''
329 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
330 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
334 $USER200$=${config.myEnv.monitoring.status_url}
335 $USER201$=${config.myEnv.monitoring.status_token}
336 $USER202$=${config.myEnv.monitoring.http_user_password}
337 $USER203$=${config.secrets.fullPaths."naemon/id_rsa"}
338 $USER204$=${config.myEnv.monitoring.imap_login}
339 $USER205$=${config.myEnv.monitoring.imap_password}
340 $USER206$=${config.myEnv.monitoring.slack_channel}
341 $USER207$=${config.myEnv.monitoring.slack_url}
342 $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)}
343 $USER209$=${builtins.concatStringsSep "," [
344 config.myEnv.monitoring.ovh_sms.endpoint
345 config.myEnv.monitoring.ovh_sms.application_key
346 config.myEnv.monitoring.ovh_sms.application_secret
347 config.myEnv.monitoring.ovh_sms.consumer_key
348 config.myEnv.monitoring.ovh_sms.account
350 $USER210$=${config.myEnv.monitoring.eban.password}
352 objectDefs = toObjects commonObjects
353 + toObjects hostObjects
354 + lib.optionalString cfg.master (toObjects masterObjects)
355 + lib.optionalString cfg.master (toObjects masterPassiveObjects)
356 + lib.optionalString cfg.master (builtins.concatStringsSep "\n" (map toObjects otherObjects));