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