]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/monitoring/default.nix
Adjust load warnings for monitoring
[perso/Immae/Config/Nix.git] / modules / private / monitoring / default.nix
CommitLineData
e820134d 1{ config, pkgs, lib, name, nodes, ... }:
3bc32d9e 2let
e820134d 3 cfg = config.myServices.monitoring;
71a2425e
IB
4 send_mails = pkgs.runCommand "send_mails" {
5 buildInputs = [ pkgs.makeWrapper ];
6 } ''
7 mkdir -p $out/bin
8 cp ${./send_mails} $out/bin/send_mails
9 patchShebangs $out
10 wrapProgram $out/bin/send_mails --prefix PATH : ${lib.makeBinPath [
11 pkgs.mailutils
12 ]}
13 '';
9f16e659
IB
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
18 else pkgs.postgresql;
3bc32d9e
IB
19 myplugins = pkgs.runCommand "buildplugins" {
20 buildInputs = [ pkgs.makeWrapper pkgs.perl ];
21 } ''
22 mkdir $out
23 cp ${./plugins}/* $out/
24 patchShebangs $out
25 wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir}
26 wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [
b11f0e17 27 pkgs.curl pkgs.jq
3bc32d9e
IB
28 ]}
29 wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [
30 pkgs.gnugrep pkgs.gawk pkgs.procps-ng
31 ]}
9f202523 32 wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [
9f16e659 33 postgresqlBinary
9f202523 34 ]}
6015a3b5
IB
35 wrapProgram $out/check_redis_replication --prefix PATH : ${lib.makeBinPath [
36 pkgs.gnugrep pkgs.coreutils pkgs.redis
37 ]}
38 wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [
39 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb
40 ]}
16b80abd
IB
41 wrapProgram $out/check_openldap_replication --prefix PATH : ${lib.makeBinPath [
42 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.openldap
43 ]}
71a2425e
IB
44 wrapProgram $out/check_emails --prefix PATH : ${lib.makeBinPath [
45 pkgs.openssh send_mails
46 ]} --prefix PERL5LIB : ${pkgs.perlPackages.makePerlPath [
47 pkgs.perlPackages.TimeDate
48 ]}
e820134d
IB
49 wrapProgram $out/check_ftp_database --prefix PATH : ${lib.makeBinPath [
50 pkgs.lftp
51 ]}
52 wrapProgram $out/check_git --prefix PATH : ${lib.makeBinPath [
53 pkgs.git pkgs.openssh
54 ]}
55 wrapProgram $out/check_imap_connection --prefix PATH : ${lib.makeBinPath [
56 pkgs.openssl
57 ]}
25844101
IB
58 wrapProgram $out/check_eriomem --prefix PATH : ${lib.makeBinPath [
59 pkgs.s3cmd pkgs.python3
60 ]}
5a61f6ad 61 makeWrapper $out/check_backup_age $out/check_backup_eriomem_age --prefix PATH : ${lib.makeBinPath [
171d8e1a
IB
62 pkgs.duplicity
63 ]} --set SECRETS_PATH ${lib.optionalString cfg.master config.secrets.fullPaths."eriomem_access_key"}
5a61f6ad
IB
64 makeWrapper $out/check_backup_age $out/check_backup_ovh_age --prefix PATH : ${lib.makeBinPath [
65 pkgs.duplicity
66 ]} --set SECRETS_PATH ${lib.optionalString cfg.master config.secrets.fullPaths."ovh_access_key"}
e820134d
IB
67 wrapProgram $out/notify_by_email --prefix PATH : ${lib.makeBinPath [
68 pkgs.mailutils
69 ]}
70 wrapProgram $out/notify_by_slack --prefix PATH : ${lib.makeBinPath [
71 pkgs.curl pkgs.jq
72 ]}
6191bdeb
IB
73 wrapProgram $out/check_ovh_sms --prefix PATH : ${lib.makeBinPath [
74 (pkgs.python3.withPackages (ps: [ps.ovh]))
75 ]}
2d7caffb
IB
76 wrapProgram $out/check_bandwidth --prefix PATH : ${lib.makeBinPath [
77 pkgs.iproute pkgs.bc
78 ]}
3bc32d9e 79 '';
eb071dd4
IB
80 toObjects = pkgs.callPackage ./to_objects.nix {};
81 commonConfig = {
82 eldiron = {
83 processWarn = "250"; processAlert = "400";
84 loadWarn = "8.0"; loadAlert = "10.0";
2d7caffb 85 interface = "eth0";
eb071dd4
IB
86 };
87 backup-2 = {
25844101 88 processWarn = "60"; processAlert = "70";
eb071dd4 89 loadWarn = "1.0"; loadAlert = "2.0";
2d7caffb 90 interface = "ens3";
eb071dd4 91 };
e820134d
IB
92 monitoring-1 = {
93 processWarn = "50"; processAlert = "60";
7ad4966f
IB
94 loadWarn = "4.0"; loadAlert = "6.0";
95 load15Warn = "1.0"; load15Alert = "2.0";
2d7caffb 96 interface = "ens3";
e820134d 97 };
eb071dd4 98 };
46a61a1b
IB
99 externalObjects = lib.genAttrs [ "tiboqorl-fr" ]
100 (n: pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; });
e820134d 101 masterPassiveObjects = let
e820134d
IB
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)
106 passiveNodes
46a61a1b
IB
107 ) ++ lib.flatten (lib.attrsets.mapAttrsToList
108 (_: n: toPassiveServices n.service)
109 externalObjects);
e820134d 110 in {
71a2425e 111 service = passiveServices;
e820134d
IB
112 host = lib.lists.foldr
113 (a: b: a//b)
114 {}
46a61a1b
IB
115 (lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes
116 ++ lib.attrsets.mapAttrsToList (_: n: n.host) externalObjects);
e820134d 117 };
71a2425e
IB
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
128 then
129 [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ]
130 else
131 [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ];
132 in
133 {
134 service_description = "${hostFQDN} email service is active";
135 use = "mail-service";
136 host_name = hostFQDN;
137 servicegroups = "webstatus-email";
138 check_command = command;
139 };
140 otherObjects = map
141 (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; }))
6c95e93c 142 [ "ulminfo-fr" "phare" ];
e820134d 143 masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; };
eb071dd4 144 commonObjects = pkgs.callPackage ./objects_common.nix ({
e820134d 145 master = cfg.master;
619e4f46 146 hostFQDN = config.hostEnv.fqdn;
e820134d 147 hostName = name;
eb071dd4
IB
148 sudo = "/run/wrappers/bin/sudo";
149 } // builtins.getAttr name commonConfig);
150 hostObjects =
151 let
152 specific_file = ./. + "/objects_" + name + ".nix";
9f202523 153 in
e820134d
IB
154 lib.attrsets.optionalAttrs
155 (builtins.pathExists specific_file)
156 (pkgs.callPackage specific_file {
171d8e1a 157 inherit config nodes emailCheck;
619e4f46 158 hostFQDN = config.hostEnv.fqdn;
e820134d
IB
159 hostName = name;
160 });
46a61a1b
IB
161 objectsFiles = lib.mapAttrs' (name: value: lib.nameValuePair
162 "=/${name}/objects.conf" { alias = pkgs.writeText "objects.conf" (toObjects value); }
163 ) externalObjects;
3bc32d9e
IB
164in
165{
166 options = {
9f202523
IB
167 myServices.monitoring = {
168 enable = lib.mkOption {
169 type = lib.types.bool;
170 default = false;
171 description = ''
172 Whether to enable monitoring.
173 '';
174 };
e820134d
IB
175 master = lib.mkOption {
176 type = lib.types.bool;
177 default = false;
178 description = ''
179 This instance is the master instance
180 '';
181 };
182 hosts = lib.mkOption {
183 readOnly = true;
184 description = "Hosts list for this host";
185 default = (commonObjects.host or {}) // (hostObjects.host or {});
186 };
187 services = lib.mkOption {
188 readOnly = true;
189 description = "Services list for this host";
190 default = commonObjects.service ++ hostObjects.service;
191 };
3bc32d9e
IB
192 };
193 };
194
e820134d 195 config = lib.mkIf cfg.enable {
46a61a1b
IB
196 services.nginx = lib.mkIf config.myServices.status.enable {
197 virtualHosts."status.immae.eu".locations = objectsFiles;
198 };
199
d2e703c5 200 services.duplyBackup.profiles.monitoring = {
6a8252b1
IB
201 rootDir = config.services.naemon.varDir;
202 };
3bc32d9e
IB
203 security.sudo.extraRules = [
204 {
205 commands = [
206 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
207 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
208 ];
209 users = [ "naemon" ];
210 runAs = "root";
211 }
9f202523
IB
212 {
213 commands = [
9f202523
IB
214 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
215 ];
216 users = [ "naemon" ];
16b80abd
IB
217 runAs = "ALL";
218 }
219 {
220 commands = [
221 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
222 ];
223 users = [ "naemon" ];
9f202523
IB
224 runAs = "postgres";
225 }
226 {
227 commands = [
6015a3b5 228 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
9f6a7862
IB
229 ];
230 users = [ "naemon" ];
231 runAs = "mysql";
232 }
6015a3b5
IB
233 {
234 commands = [
16b80abd 235 { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; }
6015a3b5
IB
236 ];
237 users = [ "naemon" ];
16b80abd 238 runAs = "openldap";
6015a3b5 239 }
9f6a7862
IB
240 {
241 commands = [
16b80abd 242 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
9f202523
IB
243 ];
244 users = [ "naemon" ];
16b80abd 245 runAs = "redis";
9f202523 246 }
3bc32d9e
IB
247 ];
248 environment.etc."mdadm.conf" = {
249 enable = true;
250 mode = "0644";
251 user = "root";
ab8f306d 252 text = "MAILADDR ${config.myEnv.monitoring.email}";
3bc32d9e
IB
253 };
254
e820134d
IB
255 secrets.keys = [
256 {
257 dest = "naemon/id_rsa";
258 user = "naemon";
259 group = "naemon";
171d8e1a 260 permissions = "0400";
e820134d
IB
261 text = config.myEnv.monitoring.ssh_secret_key;
262 }
5a61f6ad
IB
263 ] ++ lib.optionals cfg.master (
264 lib.mapAttrsToList (k: v:
171d8e1a 265 {
5a61f6ad 266 dest = "${k}_access_key";
171d8e1a
IB
267 user = "naemon";
268 group = "naemon";
269 permissions = "0400";
270 text = ''
5a61f6ad
IB
271 export AWS_ACCESS_KEY_ID="${v.accessKeyId}"
272 export AWS_SECRET_ACCESS_KEY="${v.secretAccessKey}"
273 export BASE_URL="${v.remote "immae-eldiron"}"
171d8e1a 274 '';
5a61f6ad 275 }) config.myEnv.backup.remotes
171d8e1a 276 );
3bc32d9e
IB
277 # needed since extraResource is not in the closure
278 systemd.services.naemon.path = [ myplugins ];
279 services.naemon = {
280 enable = true;
281 extraConfig = ''
3bc32d9e
IB
282 use_syslog=1
283 log_initial_states=1
284 date_format=iso8601
ab8f306d 285 admin_email=${config.myEnv.monitoring.email}
e820134d 286 '' + lib.optionalString (!cfg.master) ''
3bc32d9e
IB
287 obsess_over_services=1
288 ocsp_command=notify-master
a97118c4
IB
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
3bc32d9e
IB
292 '';
293 extraResource = ''
294 $USER2$=${myplugins}
ab8f306d
IB
295 $USER200$=${config.myEnv.monitoring.status_url}
296 $USER201$=${config.myEnv.monitoring.status_token}
e820134d
IB
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}
25844101 303 $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)}
6191bdeb
IB
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
310 ]}
3bc32d9e 311 '';
e820134d
IB
312 objectDefs = toObjects commonObjects
313 + toObjects hostObjects
314 + lib.optionalString cfg.master (toObjects masterObjects)
71a2425e
IB
315 + lib.optionalString cfg.master (toObjects masterPassiveObjects)
316 + lib.optionalString cfg.master (builtins.concatStringsSep "\n" (map toObjects otherObjects));
3bc32d9e
IB
317 };
318 };
319}