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 [
69 toObjects = pkgs.callPackage ./to_objects.nix {};
72 processWarn = "250"; processAlert = "400";
73 loadWarn = "8.0"; loadAlert = "10.0";
76 processWarn = "60"; processAlert = "70";
77 loadWarn = "1.0"; loadAlert = "2.0";
80 processWarn = "50"; processAlert = "60";
81 loadWarn = "1.0"; loadAlert = "2.0";
84 masterPassiveObjects = let
85 passiveNodes = lib.attrsets.filterAttrs (n: _: builtins.elem n ["backup-2" "eldiron"]) nodes;
86 toPassiveServices = map (s: s.passiveInfo.filter s // s.passiveInfo);
87 passiveServices = lib.flatten (lib.attrsets.mapAttrsToList
88 (_: n: toPassiveServices n.config.myServices.monitoring.services)
92 service = passiveServices;
93 host = lib.lists.foldr
96 (lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes);
98 emailCheck = host: hostFQDN: let
99 allCfg = config.myEnv.monitoring.email_check;
100 cfg = allCfg."${host}";
101 reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem host v.targets) allCfg);
102 to_email = cfg': host':
103 let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+";
104 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
105 mails_to_send = builtins.concatStringsSep "," (map (n: to_email allCfg."${n}" host) cfg.targets);
106 mails_to_receive = builtins.concatStringsSep "," (map (n: "${to_email cfg n}:${n}") reverseTargets);
107 command = if cfg.local
109 [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ]
111 [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ];
114 service_description = "${hostFQDN} email service is active";
115 use = "mail-service";
116 host_name = hostFQDN;
117 servicegroups = "webstatus-email";
118 check_command = command;
121 (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; }))
122 [ "caldance-1" "ulminfo-fr" "immae-eu" "phare" "tiboqorl-fr" ];
123 masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; };
124 commonObjects = pkgs.callPackage ./objects_common.nix ({
126 hostFQDN = config.hostEnv.fqdn;
128 sudo = "/run/wrappers/bin/sudo";
129 } // builtins.getAttr name commonConfig);
132 specific_file = ./. + "/objects_" + name + ".nix";
134 lib.attrsets.optionalAttrs
135 (builtins.pathExists specific_file)
136 (pkgs.callPackage specific_file {
137 inherit config emailCheck;
138 hostFQDN = config.hostEnv.fqdn;
144 myServices.monitoring = {
145 enable = lib.mkOption {
146 type = lib.types.bool;
149 Whether to enable monitoring.
152 master = lib.mkOption {
153 type = lib.types.bool;
156 This instance is the master instance
159 hosts = lib.mkOption {
161 description = "Hosts list for this host";
162 default = (commonObjects.host or {}) // (hostObjects.host or {});
164 services = lib.mkOption {
166 description = "Services list for this host";
167 default = commonObjects.service ++ hostObjects.service;
172 config = lib.mkIf cfg.enable {
173 services.duplyBackup.profiles.monitoring = {
174 rootDir = config.services.naemon.varDir;
176 security.sudo.extraRules = [
179 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
180 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
182 users = [ "naemon" ];
187 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
189 users = [ "naemon" ];
194 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
196 users = [ "naemon" ];
201 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
203 users = [ "naemon" ];
208 { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; }
210 users = [ "naemon" ];
215 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
217 users = [ "naemon" ];
221 environment.etc."mdadm.conf" = {
225 text = "MAILADDR ${config.myEnv.monitoring.email}";
230 dest = "naemon/id_rsa";
233 premissions = "0400";
234 text = config.myEnv.monitoring.ssh_secret_key;
237 # needed since extraResource is not in the closure
238 systemd.services.naemon.path = [ myplugins ];
242 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
246 admin_email=${config.myEnv.monitoring.email}
247 '' + lib.optionalString (!cfg.master) ''
248 obsess_over_services=1
249 ocsp_command=notify-master
253 $USER200$=${config.myEnv.monitoring.status_url}
254 $USER201$=${config.myEnv.monitoring.status_token}
255 $USER202$=${config.myEnv.monitoring.http_user_password}
256 $USER203$=${config.secrets.fullPaths."naemon/id_rsa"}
257 $USER204$=${config.myEnv.monitoring.imap_login}
258 $USER205$=${config.myEnv.monitoring.imap_password}
259 $USER206$=${config.myEnv.monitoring.slack_channel}
260 $USER207$=${config.myEnv.monitoring.slack_url}
261 $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)}
263 objectDefs = toObjects commonObjects
264 + toObjects hostObjects
265 + lib.optionalString cfg.master (toObjects masterObjects)
266 + lib.optionalString cfg.master (toObjects masterPassiveObjects)
267 + lib.optionalString cfg.master (builtins.concatStringsSep "\n" (map toObjects otherObjects));