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