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