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_maison_bbc --prefix PATH : ${lib.makeBinPath [
32 wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [
33 pkgs.gnugrep pkgs.gawk pkgs.procps-ng
35 wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [
38 wrapProgram $out/check_redis_replication --prefix PATH : ${lib.makeBinPath [
39 pkgs.gnugrep pkgs.coreutils pkgs.redis
41 wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [
42 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb
44 wrapProgram $out/check_openldap_replication --prefix PATH : ${lib.makeBinPath [
45 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.openldap
47 wrapProgram $out/check_emails --prefix PATH : ${lib.makeBinPath [
48 pkgs.openssh send_mails
49 ]} --prefix PERL5LIB : ${pkgs.perlPackages.makePerlPath [
50 pkgs.perlPackages.TimeDate
52 wrapProgram $out/check_ftp_database --prefix PATH : ${lib.makeBinPath [
55 wrapProgram $out/check_git --prefix PATH : ${lib.makeBinPath [
58 wrapProgram $out/check_imap_connection --prefix PATH : ${lib.makeBinPath [
61 wrapProgram $out/check_eriomem --prefix PATH : ${lib.makeBinPath [
62 pkgs.s3cmd pkgs.python3
64 wrapProgram $out/notify_maison_bbc_by_email --prefix PATH : ${lib.makeBinPath [
65 pkgs.mailutils pkgs.gawk
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]))
77 toObjects = pkgs.callPackage ./to_objects.nix {};
80 processWarn = "250"; processAlert = "400";
81 loadWarn = "8.0"; loadAlert = "10.0";
84 processWarn = "60"; processAlert = "70";
85 loadWarn = "1.0"; loadAlert = "2.0";
88 processWarn = "50"; processAlert = "60";
89 loadWarn = "1.0"; loadAlert = "2.0";
92 masterPassiveObjects = let
93 passiveNodes = lib.attrsets.filterAttrs (n: _: builtins.elem n ["backup-2" "eldiron"]) nodes;
94 toPassiveServices = map (s: s.passiveInfo.filter s // s.passiveInfo);
95 passiveServices = lib.flatten (lib.attrsets.mapAttrsToList
96 (_: n: toPassiveServices n.config.myServices.monitoring.services)
100 service = passiveServices;
101 host = lib.lists.foldr
104 (lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes);
106 emailCheck = host: hostFQDN: let
107 allCfg = config.myEnv.monitoring.email_check;
108 cfg = allCfg."${host}";
109 reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem host v.targets) allCfg);
110 to_email = cfg': host':
111 let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+";
112 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
113 mails_to_send = builtins.concatStringsSep "," (map (n: to_email allCfg."${n}" host) cfg.targets);
114 mails_to_receive = builtins.concatStringsSep "," (map (n: "${to_email cfg n}:${n}") reverseTargets);
115 command = if cfg.local
117 [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ]
119 [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ];
122 service_description = "${hostFQDN} email service is active";
123 use = "mail-service";
124 host_name = hostFQDN;
125 servicegroups = "webstatus-email";
126 check_command = command;
129 (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; }))
130 [ "caldance-1" "ulminfo-fr" "phare" "tiboqorl-fr" ];
131 masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; };
132 commonObjects = pkgs.callPackage ./objects_common.nix ({
134 hostFQDN = config.hostEnv.fqdn;
136 sudo = "/run/wrappers/bin/sudo";
137 } // builtins.getAttr name commonConfig);
140 specific_file = ./. + "/objects_" + name + ".nix";
142 lib.attrsets.optionalAttrs
143 (builtins.pathExists specific_file)
144 (pkgs.callPackage specific_file {
145 inherit config emailCheck;
146 hostFQDN = config.hostEnv.fqdn;
152 myServices.monitoring = {
153 enable = lib.mkOption {
154 type = lib.types.bool;
157 Whether to enable monitoring.
160 master = lib.mkOption {
161 type = lib.types.bool;
164 This instance is the master instance
167 hosts = lib.mkOption {
169 description = "Hosts list for this host";
170 default = (commonObjects.host or {}) // (hostObjects.host or {});
172 services = lib.mkOption {
174 description = "Services list for this host";
175 default = commonObjects.service ++ hostObjects.service;
180 config = lib.mkIf cfg.enable {
181 services.duplyBackup.profiles.monitoring = {
182 rootDir = config.services.naemon.varDir;
184 security.sudo.extraRules = [
187 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
188 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
190 users = [ "naemon" ];
195 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
197 users = [ "naemon" ];
202 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
204 users = [ "naemon" ];
209 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
211 users = [ "naemon" ];
216 { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; }
218 users = [ "naemon" ];
223 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
225 users = [ "naemon" ];
229 environment.etc."mdadm.conf" = {
233 text = "MAILADDR ${config.myEnv.monitoring.email}";
238 dest = "naemon/id_rsa";
241 premissions = "0400";
242 text = config.myEnv.monitoring.ssh_secret_key;
245 # needed since extraResource is not in the closure
246 systemd.services.naemon.path = [ myplugins ];
250 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
254 admin_email=${config.myEnv.monitoring.email}
255 '' + lib.optionalString (!cfg.master) ''
256 obsess_over_services=1
257 ocsp_command=notify-master
261 $USER200$=${config.myEnv.monitoring.status_url}
262 $USER201$=${config.myEnv.monitoring.status_token}
263 $USER202$=${config.myEnv.monitoring.http_user_password}
264 $USER203$=${config.secrets.fullPaths."naemon/id_rsa"}
265 $USER204$=${config.myEnv.monitoring.imap_login}
266 $USER205$=${config.myEnv.monitoring.imap_password}
267 $USER206$=${config.myEnv.monitoring.slack_channel}
268 $USER207$=${config.myEnv.monitoring.slack_url}
269 $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)}
270 $USER209$=${builtins.concatStringsSep "," [
271 config.myEnv.monitoring.ovh_sms.endpoint
272 config.myEnv.monitoring.ovh_sms.application_key
273 config.myEnv.monitoring.ovh_sms.application_secret
274 config.myEnv.monitoring.ovh_sms.consumer_key
275 config.myEnv.monitoring.ovh_sms.account
278 objectDefs = toObjects commonObjects
279 + toObjects hostObjects
280 + lib.optionalString cfg.master (toObjects masterObjects)
281 + lib.optionalString cfg.master (toObjects masterPassiveObjects)
282 + lib.optionalString cfg.master (builtins.concatStringsSep "\n" (map toObjects otherObjects));