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