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