]>
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 | 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 [ | |
27 | pkgs.curl pkgs.jq | |
28 | ]} | |
29 | wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [ | |
30 | pkgs.gnugrep pkgs.gawk pkgs.procps-ng | |
31 | ]} | |
32 | wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [ | |
33 | postgresqlBinary | |
34 | ]} | |
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 | ]} | |
41 | wrapProgram $out/check_openldap_replication --prefix PATH : ${lib.makeBinPath [ | |
42 | pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.openldap | |
43 | ]} | |
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 | ]} | |
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 | ]} | |
58 | wrapProgram $out/check_eriomem --prefix PATH : ${lib.makeBinPath [ | |
59 | pkgs.s3cmd pkgs.python3 | |
60 | ]} | |
61 | wrapProgram $out/check_eriomem_age --prefix PATH : ${lib.makeBinPath [ | |
62 | pkgs.duplicity | |
63 | ]} --set SECRETS_PATH ${lib.optionalString cfg.master config.secrets.fullPaths."eriomem_access_key"} | |
64 | wrapProgram $out/notify_by_email --prefix PATH : ${lib.makeBinPath [ | |
65 | pkgs.mailutils | |
66 | ]} | |
67 | wrapProgram $out/notify_by_slack --prefix PATH : ${lib.makeBinPath [ | |
68 | pkgs.curl pkgs.jq | |
69 | ]} | |
70 | wrapProgram $out/check_ovh_sms --prefix PATH : ${lib.makeBinPath [ | |
71 | (pkgs.python3.withPackages (ps: [ps.ovh])) | |
72 | ]} | |
73 | ''; | |
74 | toObjects = pkgs.callPackage ./to_objects.nix {}; | |
75 | commonConfig = { | |
76 | eldiron = { | |
77 | processWarn = "250"; processAlert = "400"; | |
78 | loadWarn = "8.0"; loadAlert = "10.0"; | |
79 | }; | |
80 | backup-2 = { | |
81 | processWarn = "60"; processAlert = "70"; | |
82 | loadWarn = "1.0"; loadAlert = "2.0"; | |
83 | }; | |
84 | monitoring-1 = { | |
85 | processWarn = "50"; processAlert = "60"; | |
86 | loadWarn = "1.0"; loadAlert = "2.0"; | |
87 | }; | |
88 | }; | |
89 | masterPassiveObjects = let | |
90 | passiveNodes = lib.attrsets.filterAttrs (n: _: builtins.elem n ["backup-2" "eldiron"]) nodes; | |
91 | toPassiveServices = map (s: s.passiveInfo.filter s // s.passiveInfo); | |
92 | passiveServices = lib.flatten (lib.attrsets.mapAttrsToList | |
93 | (_: n: toPassiveServices n.config.myServices.monitoring.services) | |
94 | passiveNodes | |
95 | ); | |
96 | in { | |
97 | service = passiveServices; | |
98 | host = lib.lists.foldr | |
99 | (a: b: a//b) | |
100 | {} | |
101 | (lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes); | |
102 | }; | |
103 | emailCheck = host: hostFQDN: let | |
104 | allCfg = config.myEnv.monitoring.email_check; | |
105 | cfg = allCfg."${host}"; | |
106 | reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem host v.targets) allCfg); | |
107 | to_email = cfg': host': | |
108 | let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+"; | |
109 | in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}"; | |
110 | mails_to_send = builtins.concatStringsSep "," (map (n: to_email allCfg."${n}" host) cfg.targets); | |
111 | mails_to_receive = builtins.concatStringsSep "," (map (n: "${to_email cfg n}:${n}") reverseTargets); | |
112 | command = if cfg.local | |
113 | then | |
114 | [ "check_emails_local" "/var/lib/naemon/checks/email" mails_to_send mails_to_receive ] | |
115 | else | |
116 | [ "check_emails" cfg.login cfg.port mails_to_send mails_to_receive ]; | |
117 | in | |
118 | { | |
119 | service_description = "${hostFQDN} email service is active"; | |
120 | use = "mail-service"; | |
121 | host_name = hostFQDN; | |
122 | servicegroups = "webstatus-email"; | |
123 | check_command = command; | |
124 | }; | |
125 | otherObjects = map | |
126 | (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") { inherit emailCheck; })) | |
127 | [ "caldance-1" "ulminfo-fr" "phare" "tiboqorl-fr" ]; | |
128 | masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; }; | |
129 | commonObjects = pkgs.callPackage ./objects_common.nix ({ | |
130 | master = cfg.master; | |
131 | hostFQDN = config.hostEnv.fqdn; | |
132 | hostName = name; | |
133 | sudo = "/run/wrappers/bin/sudo"; | |
134 | } // builtins.getAttr name commonConfig); | |
135 | hostObjects = | |
136 | let | |
137 | specific_file = ./. + "/objects_" + name + ".nix"; | |
138 | in | |
139 | lib.attrsets.optionalAttrs | |
140 | (builtins.pathExists specific_file) | |
141 | (pkgs.callPackage specific_file { | |
142 | inherit config nodes emailCheck; | |
143 | hostFQDN = config.hostEnv.fqdn; | |
144 | hostName = name; | |
145 | }); | |
146 | in | |
147 | { | |
148 | options = { | |
149 | myServices.monitoring = { | |
150 | enable = lib.mkOption { | |
151 | type = lib.types.bool; | |
152 | default = false; | |
153 | description = '' | |
154 | Whether to enable monitoring. | |
155 | ''; | |
156 | }; | |
157 | master = lib.mkOption { | |
158 | type = lib.types.bool; | |
159 | default = false; | |
160 | description = '' | |
161 | This instance is the master instance | |
162 | ''; | |
163 | }; | |
164 | hosts = lib.mkOption { | |
165 | readOnly = true; | |
166 | description = "Hosts list for this host"; | |
167 | default = (commonObjects.host or {}) // (hostObjects.host or {}); | |
168 | }; | |
169 | services = lib.mkOption { | |
170 | readOnly = true; | |
171 | description = "Services list for this host"; | |
172 | default = commonObjects.service ++ hostObjects.service; | |
173 | }; | |
174 | }; | |
175 | }; | |
176 | ||
177 | config = lib.mkIf cfg.enable { | |
178 | services.duplyBackup.profiles.monitoring = { | |
179 | rootDir = config.services.naemon.varDir; | |
180 | }; | |
181 | security.sudo.extraRules = [ | |
182 | { | |
183 | commands = [ | |
184 | { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; } | |
185 | { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; } | |
186 | ]; | |
187 | users = [ "naemon" ]; | |
188 | runAs = "root"; | |
189 | } | |
190 | { | |
191 | commands = [ | |
192 | { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; } | |
193 | ]; | |
194 | users = [ "naemon" ]; | |
195 | runAs = "ALL"; | |
196 | } | |
197 | { | |
198 | commands = [ | |
199 | { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; } | |
200 | ]; | |
201 | users = [ "naemon" ]; | |
202 | runAs = "postgres"; | |
203 | } | |
204 | { | |
205 | commands = [ | |
206 | { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; } | |
207 | ]; | |
208 | users = [ "naemon" ]; | |
209 | runAs = "mysql"; | |
210 | } | |
211 | { | |
212 | commands = [ | |
213 | { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; } | |
214 | ]; | |
215 | users = [ "naemon" ]; | |
216 | runAs = "openldap"; | |
217 | } | |
218 | { | |
219 | commands = [ | |
220 | { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; } | |
221 | ]; | |
222 | users = [ "naemon" ]; | |
223 | runAs = "redis"; | |
224 | } | |
225 | ]; | |
226 | environment.etc."mdadm.conf" = { | |
227 | enable = true; | |
228 | mode = "0644"; | |
229 | user = "root"; | |
230 | text = "MAILADDR ${config.myEnv.monitoring.email}"; | |
231 | }; | |
232 | ||
233 | secrets.keys = [ | |
234 | { | |
235 | dest = "naemon/id_rsa"; | |
236 | user = "naemon"; | |
237 | group = "naemon"; | |
238 | permissions = "0400"; | |
239 | text = config.myEnv.monitoring.ssh_secret_key; | |
240 | } | |
241 | ] ++ lib.optional cfg.master ( | |
242 | { | |
243 | dest = "eriomem_access_key"; | |
244 | user = "naemon"; | |
245 | group = "naemon"; | |
246 | permissions = "0400"; | |
247 | text = '' | |
248 | export AWS_ACCESS_KEY_ID="${config.myEnv.backup.accessKeyId}" | |
249 | export AWS_SECRET_ACCESS_KEY="${config.myEnv.backup.secretAccessKey}" | |
250 | export BASE_URL="${config.myEnv.backup.remote}" | |
251 | ''; | |
252 | } | |
253 | ); | |
254 | # needed since extraResource is not in the closure | |
255 | systemd.services.naemon.path = [ myplugins ]; | |
256 | services.naemon = { | |
257 | enable = true; | |
258 | extraConfig = '' | |
259 | use_syslog=1 | |
260 | log_initial_states=1 | |
261 | date_format=iso8601 | |
262 | admin_email=${config.myEnv.monitoring.email} | |
263 | '' + lib.optionalString (!cfg.master) '' | |
264 | obsess_over_services=1 | |
265 | ocsp_command=notify-master | |
266 | '' + lib.optionalString (cfg.master) '' | |
267 | broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live | |
268 | 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 | |
269 | ''; | |
270 | extraResource = '' | |
271 | $USER2$=${myplugins} | |
272 | $USER200$=${config.myEnv.monitoring.status_url} | |
273 | $USER201$=${config.myEnv.monitoring.status_token} | |
274 | $USER202$=${config.myEnv.monitoring.http_user_password} | |
275 | $USER203$=${config.secrets.fullPaths."naemon/id_rsa"} | |
276 | $USER204$=${config.myEnv.monitoring.imap_login} | |
277 | $USER205$=${config.myEnv.monitoring.imap_password} | |
278 | $USER206$=${config.myEnv.monitoring.slack_channel} | |
279 | $USER207$=${config.myEnv.monitoring.slack_url} | |
280 | $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)} | |
281 | $USER209$=${builtins.concatStringsSep "," [ | |
282 | config.myEnv.monitoring.ovh_sms.endpoint | |
283 | config.myEnv.monitoring.ovh_sms.application_key | |
284 | config.myEnv.monitoring.ovh_sms.application_secret | |
285 | config.myEnv.monitoring.ovh_sms.consumer_key | |
286 | config.myEnv.monitoring.ovh_sms.account | |
287 | ]} | |
288 | ''; | |
289 | objectDefs = toObjects commonObjects | |
290 | + toObjects hostObjects | |
291 | + lib.optionalString cfg.master (toObjects masterObjects) | |
292 | + lib.optionalString cfg.master (toObjects masterPassiveObjects) | |
293 | + lib.optionalString cfg.master (builtins.concatStringsSep "\n" (map toObjects otherObjects)); | |
294 | }; | |
295 | }; | |
296 | } |