]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/monitoring/default.nix
Repackage slang correctly
[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";
94 loadWarn = "1.0"; loadAlert = "2.0";
2d7caffb 95 interface = "ens3";
e820134d 96 };
eb071dd4 97 };
46a61a1b
IB
98 externalObjects = lib.genAttrs [ "tiboqorl-fr" ]
99 (n: pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; });
e820134d 100 masterPassiveObjects = let
e820134d
IB
101 passiveNodes = lib.attrsets.filterAttrs (n: _: builtins.elem n ["backup-2" "eldiron"]) nodes;
102 toPassiveServices = map (s: s.passiveInfo.filter s // s.passiveInfo);
103 passiveServices = lib.flatten (lib.attrsets.mapAttrsToList
104 (_: n: toPassiveServices n.config.myServices.monitoring.services)
105 passiveNodes
46a61a1b
IB
106 ) ++ lib.flatten (lib.attrsets.mapAttrsToList
107 (_: n: toPassiveServices n.service)
108 externalObjects);
e820134d 109 in {
71a2425e 110 service = passiveServices;
e820134d
IB
111 host = lib.lists.foldr
112 (a: b: a//b)
113 {}
46a61a1b
IB
114 (lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes
115 ++ lib.attrsets.mapAttrsToList (_: n: n.host) externalObjects);
e820134d 116 };
71a2425e
IB
117 emailCheck = host: hostFQDN: let
118 allCfg = config.myEnv.monitoring.email_check;
119 cfg = allCfg."${host}";
120 reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem host v.targets) allCfg);
121 to_email = cfg': host':
122 let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+";
123 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
124 mails_to_send = builtins.concatStringsSep "," (map (n: to_email allCfg."${n}" host) cfg.targets);
125 mails_to_receive = builtins.concatStringsSep "," (map (n: "${to_email cfg n}:${n}") reverseTargets);
126 command = if cfg.local
127 then
128 [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ]
129 else
130 [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ];
131 in
132 {
133 service_description = "${hostFQDN} email service is active";
134 use = "mail-service";
135 host_name = hostFQDN;
136 servicegroups = "webstatus-email";
137 check_command = command;
138 };
139 otherObjects = map
140 (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; }))
6c95e93c 141 [ "ulminfo-fr" "phare" ];
e820134d 142 masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; };
eb071dd4 143 commonObjects = pkgs.callPackage ./objects_common.nix ({
e820134d 144 master = cfg.master;
619e4f46 145 hostFQDN = config.hostEnv.fqdn;
e820134d 146 hostName = name;
eb071dd4
IB
147 sudo = "/run/wrappers/bin/sudo";
148 } // builtins.getAttr name commonConfig);
149 hostObjects =
150 let
151 specific_file = ./. + "/objects_" + name + ".nix";
9f202523 152 in
e820134d
IB
153 lib.attrsets.optionalAttrs
154 (builtins.pathExists specific_file)
155 (pkgs.callPackage specific_file {
171d8e1a 156 inherit config nodes emailCheck;
619e4f46 157 hostFQDN = config.hostEnv.fqdn;
e820134d
IB
158 hostName = name;
159 });
46a61a1b
IB
160 objectsFiles = lib.mapAttrs' (name: value: lib.nameValuePair
161 "=/${name}/objects.conf" { alias = pkgs.writeText "objects.conf" (toObjects value); }
162 ) externalObjects;
3bc32d9e
IB
163in
164{
165 options = {
9f202523
IB
166 myServices.monitoring = {
167 enable = lib.mkOption {
168 type = lib.types.bool;
169 default = false;
170 description = ''
171 Whether to enable monitoring.
172 '';
173 };
e820134d
IB
174 master = lib.mkOption {
175 type = lib.types.bool;
176 default = false;
177 description = ''
178 This instance is the master instance
179 '';
180 };
181 hosts = lib.mkOption {
182 readOnly = true;
183 description = "Hosts list for this host";
184 default = (commonObjects.host or {}) // (hostObjects.host or {});
185 };
186 services = lib.mkOption {
187 readOnly = true;
188 description = "Services list for this host";
189 default = commonObjects.service ++ hostObjects.service;
190 };
3bc32d9e
IB
191 };
192 };
193
e820134d 194 config = lib.mkIf cfg.enable {
46a61a1b
IB
195 services.nginx = lib.mkIf config.myServices.status.enable {
196 virtualHosts."status.immae.eu".locations = objectsFiles;
197 };
198
d2e703c5 199 services.duplyBackup.profiles.monitoring = {
6a8252b1
IB
200 rootDir = config.services.naemon.varDir;
201 };
3bc32d9e
IB
202 security.sudo.extraRules = [
203 {
204 commands = [
205 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
206 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
207 ];
208 users = [ "naemon" ];
209 runAs = "root";
210 }
9f202523
IB
211 {
212 commands = [
9f202523
IB
213 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
214 ];
215 users = [ "naemon" ];
16b80abd
IB
216 runAs = "ALL";
217 }
218 {
219 commands = [
220 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
221 ];
222 users = [ "naemon" ];
9f202523
IB
223 runAs = "postgres";
224 }
225 {
226 commands = [
6015a3b5 227 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
9f6a7862
IB
228 ];
229 users = [ "naemon" ];
230 runAs = "mysql";
231 }
6015a3b5
IB
232 {
233 commands = [
16b80abd 234 { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; }
6015a3b5
IB
235 ];
236 users = [ "naemon" ];
16b80abd 237 runAs = "openldap";
6015a3b5 238 }
9f6a7862
IB
239 {
240 commands = [
16b80abd 241 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
9f202523
IB
242 ];
243 users = [ "naemon" ];
16b80abd 244 runAs = "redis";
9f202523 245 }
3bc32d9e
IB
246 ];
247 environment.etc."mdadm.conf" = {
248 enable = true;
249 mode = "0644";
250 user = "root";
ab8f306d 251 text = "MAILADDR ${config.myEnv.monitoring.email}";
3bc32d9e
IB
252 };
253
e820134d
IB
254 secrets.keys = [
255 {
256 dest = "naemon/id_rsa";
257 user = "naemon";
258 group = "naemon";
171d8e1a 259 permissions = "0400";
e820134d
IB
260 text = config.myEnv.monitoring.ssh_secret_key;
261 }
5a61f6ad
IB
262 ] ++ lib.optionals cfg.master (
263 lib.mapAttrsToList (k: v:
171d8e1a 264 {
5a61f6ad 265 dest = "${k}_access_key";
171d8e1a
IB
266 user = "naemon";
267 group = "naemon";
268 permissions = "0400";
269 text = ''
5a61f6ad
IB
270 export AWS_ACCESS_KEY_ID="${v.accessKeyId}"
271 export AWS_SECRET_ACCESS_KEY="${v.secretAccessKey}"
272 export BASE_URL="${v.remote "immae-eldiron"}"
171d8e1a 273 '';
5a61f6ad 274 }) config.myEnv.backup.remotes
171d8e1a 275 );
3bc32d9e
IB
276 # needed since extraResource is not in the closure
277 systemd.services.naemon.path = [ myplugins ];
278 services.naemon = {
279 enable = true;
280 extraConfig = ''
3bc32d9e
IB
281 use_syslog=1
282 log_initial_states=1
283 date_format=iso8601
ab8f306d 284 admin_email=${config.myEnv.monitoring.email}
e820134d 285 '' + lib.optionalString (!cfg.master) ''
3bc32d9e
IB
286 obsess_over_services=1
287 ocsp_command=notify-master
a97118c4
IB
288 '' + lib.optionalString (cfg.master) ''
289 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
290 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
291 '';
292 extraResource = ''
293 $USER2$=${myplugins}
ab8f306d
IB
294 $USER200$=${config.myEnv.monitoring.status_url}
295 $USER201$=${config.myEnv.monitoring.status_token}
e820134d
IB
296 $USER202$=${config.myEnv.monitoring.http_user_password}
297 $USER203$=${config.secrets.fullPaths."naemon/id_rsa"}
298 $USER204$=${config.myEnv.monitoring.imap_login}
299 $USER205$=${config.myEnv.monitoring.imap_password}
300 $USER206$=${config.myEnv.monitoring.slack_channel}
301 $USER207$=${config.myEnv.monitoring.slack_url}
25844101 302 $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)}
6191bdeb
IB
303 $USER209$=${builtins.concatStringsSep "," [
304 config.myEnv.monitoring.ovh_sms.endpoint
305 config.myEnv.monitoring.ovh_sms.application_key
306 config.myEnv.monitoring.ovh_sms.application_secret
307 config.myEnv.monitoring.ovh_sms.consumer_key
308 config.myEnv.monitoring.ovh_sms.account
309 ]}
3bc32d9e 310 '';
e820134d
IB
311 objectDefs = toObjects commonObjects
312 + toObjects hostObjects
313 + lib.optionalString cfg.master (toObjects masterObjects)
71a2425e
IB
314 + lib.optionalString cfg.master (toObjects masterPassiveObjects)
315 + lib.optionalString cfg.master (builtins.concatStringsSep "\n" (map toObjects otherObjects));
3bc32d9e
IB
316 };
317 };
318}