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