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 myplugins = pkgs.runCommand "buildplugins" {
15 buildInputs = [ pkgs.makeWrapper pkgs.perl ];
18 cp ${./plugins}/* $out/
20 wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir}
21 wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [
24 wrapProgram $out/check_maison_bbc --prefix PATH : ${lib.makeBinPath [
27 wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [
28 pkgs.gnugrep pkgs.gawk pkgs.procps-ng
30 wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [
33 wrapProgram $out/check_redis_replication --prefix PATH : ${lib.makeBinPath [
34 pkgs.gnugrep pkgs.coreutils pkgs.redis
36 wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [
37 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb
39 wrapProgram $out/check_openldap_replication --prefix PATH : ${lib.makeBinPath [
40 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.openldap
42 wrapProgram $out/check_emails --prefix PATH : ${lib.makeBinPath [
43 pkgs.openssh send_mails
44 ]} --prefix PERL5LIB : ${pkgs.perlPackages.makePerlPath [
45 pkgs.perlPackages.TimeDate
47 wrapProgram $out/check_ftp_database --prefix PATH : ${lib.makeBinPath [
50 wrapProgram $out/check_git --prefix PATH : ${lib.makeBinPath [
53 wrapProgram $out/check_imap_connection --prefix PATH : ${lib.makeBinPath [
56 wrapProgram $out/check_eriomem --prefix PATH : ${lib.makeBinPath [
57 pkgs.s3cmd pkgs.python3
59 wrapProgram $out/notify_maison_bbc_by_email --prefix PATH : ${lib.makeBinPath [
60 pkgs.mailutils pkgs.gawk
62 wrapProgram $out/notify_by_email --prefix PATH : ${lib.makeBinPath [
65 wrapProgram $out/notify_by_slack --prefix PATH : ${lib.makeBinPath [
68 wrapProgram $out/check_ovh_sms --prefix PATH : ${lib.makeBinPath [
69 (pkgs.python3.withPackages (ps: [ps.ovh]))
72 toObjects = pkgs.callPackage ./to_objects.nix {};
75 processWarn = "250"; processAlert = "400";
76 loadWarn = "8.0"; loadAlert = "10.0";
79 processWarn = "60"; processAlert = "70";
80 loadWarn = "1.0"; loadAlert = "2.0";
83 processWarn = "50"; processAlert = "60";
84 loadWarn = "1.0"; loadAlert = "2.0";
87 masterPassiveObjects = let
88 passiveNodes = lib.attrsets.filterAttrs (n: _: builtins.elem n ["backup-2" "eldiron"]) nodes;
89 toPassiveServices = map (s: s.passiveInfo.filter s // s.passiveInfo);
90 passiveServices = lib.flatten (lib.attrsets.mapAttrsToList
91 (_: n: toPassiveServices n.config.myServices.monitoring.services)
95 service = passiveServices;
96 host = lib.lists.foldr
99 (lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes);
101 emailCheck = host: hostFQDN: let
102 allCfg = config.myEnv.monitoring.email_check;
103 cfg = allCfg."${host}";
104 reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem host v.targets) allCfg);
105 to_email = cfg': host':
106 let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+";
107 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
108 mails_to_send = builtins.concatStringsSep "," (map (n: to_email allCfg."${n}" host) cfg.targets);
109 mails_to_receive = builtins.concatStringsSep "," (map (n: "${to_email cfg n}:${n}") reverseTargets);
110 command = if cfg.local
112 [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ]
114 [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ];
117 service_description = "${hostFQDN} email service is active";
118 use = "mail-service";
119 host_name = hostFQDN;
120 servicegroups = "webstatus-email";
121 check_command = command;
124 (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; }))
125 [ "caldance-1" "ulminfo-fr" "phare" "tiboqorl-fr" ];
126 masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; };
127 commonObjects = pkgs.callPackage ./objects_common.nix ({
129 hostFQDN = config.hostEnv.fqdn;
131 sudo = "/run/wrappers/bin/sudo";
132 } // builtins.getAttr name commonConfig);
135 specific_file = ./. + "/objects_" + name + ".nix";
137 lib.attrsets.optionalAttrs
138 (builtins.pathExists specific_file)
139 (pkgs.callPackage specific_file {
140 inherit config emailCheck;
141 hostFQDN = config.hostEnv.fqdn;
147 myServices.monitoring = {
148 enable = lib.mkOption {
149 type = lib.types.bool;
152 Whether to enable monitoring.
155 master = lib.mkOption {
156 type = lib.types.bool;
159 This instance is the master instance
162 hosts = lib.mkOption {
164 description = "Hosts list for this host";
165 default = (commonObjects.host or {}) // (hostObjects.host or {});
167 services = lib.mkOption {
169 description = "Services list for this host";
170 default = commonObjects.service ++ hostObjects.service;
175 config = lib.mkIf cfg.enable {
176 services.duplyBackup.profiles.monitoring = {
177 rootDir = config.services.naemon.varDir;
179 security.sudo.extraRules = [
182 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
183 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
185 users = [ "naemon" ];
190 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
192 users = [ "naemon" ];
197 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
199 users = [ "naemon" ];
204 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
206 users = [ "naemon" ];
211 { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; }
213 users = [ "naemon" ];
218 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
220 users = [ "naemon" ];
224 environment.etc."mdadm.conf" = {
228 text = "MAILADDR ${config.myEnv.monitoring.email}";
233 dest = "naemon/id_rsa";
236 premissions = "0400";
237 text = config.myEnv.monitoring.ssh_secret_key;
240 # needed since extraResource is not in the closure
241 systemd.services.naemon.path = [ myplugins ];
245 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
249 admin_email=${config.myEnv.monitoring.email}
250 '' + lib.optionalString (!cfg.master) ''
251 obsess_over_services=1
252 ocsp_command=notify-master
256 $USER200$=${config.myEnv.monitoring.status_url}
257 $USER201$=${config.myEnv.monitoring.status_token}
258 $USER202$=${config.myEnv.monitoring.http_user_password}
259 $USER203$=${config.secrets.fullPaths."naemon/id_rsa"}
260 $USER204$=${config.myEnv.monitoring.imap_login}
261 $USER205$=${config.myEnv.monitoring.imap_password}
262 $USER206$=${config.myEnv.monitoring.slack_channel}
263 $USER207$=${config.myEnv.monitoring.slack_url}
264 $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)}
265 $USER209$=${builtins.concatStringsSep "," [
266 config.myEnv.monitoring.ovh_sms.endpoint
267 config.myEnv.monitoring.ovh_sms.application_key
268 config.myEnv.monitoring.ovh_sms.application_secret
269 config.myEnv.monitoring.ovh_sms.consumer_key
270 config.myEnv.monitoring.ovh_sms.account
273 objectDefs = toObjects commonObjects
274 + toObjects hostObjects
275 + lib.optionalString cfg.master (toObjects masterObjects)
276 + lib.optionalString cfg.master (toObjects masterPassiveObjects)
277 + lib.optionalString cfg.master (builtins.concatStringsSep "\n" (map toObjects otherObjects));