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