]>
Commit | Line | Data |
---|---|---|
1 | { config, pkgs, lib, name, nodes, ... }: | |
2 | let | |
3 | cfg = config.myServices.monitoring; | |
4 | myplugins = pkgs.runCommand "buildplugins" { | |
5 | buildInputs = [ pkgs.makeWrapper pkgs.perl ]; | |
6 | } '' | |
7 | mkdir $out | |
8 | cp ${./plugins}/* $out/ | |
9 | patchShebangs $out | |
10 | wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir} | |
11 | wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [ | |
12 | pkgs.curl pkgs.jq | |
13 | ]} | |
14 | wrapProgram $out/check_maison_bbc --prefix PATH : ${lib.makeBinPath [ | |
15 | pkgs.curl pkgs.jq | |
16 | ]} | |
17 | wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [ | |
18 | pkgs.gnugrep pkgs.gawk pkgs.procps-ng | |
19 | ]} | |
20 | wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [ | |
21 | pkgs.postgresql | |
22 | ]} | |
23 | wrapProgram $out/check_redis_replication --prefix PATH : ${lib.makeBinPath [ | |
24 | pkgs.gnugrep pkgs.coreutils pkgs.redis | |
25 | ]} | |
26 | wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [ | |
27 | pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb | |
28 | ]} | |
29 | wrapProgram $out/check_openldap_replication --prefix PATH : ${lib.makeBinPath [ | |
30 | pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.openldap | |
31 | ]} | |
32 | wrapProgram $out/check_ftp_database --prefix PATH : ${lib.makeBinPath [ | |
33 | pkgs.lftp | |
34 | ]} | |
35 | wrapProgram $out/check_git --prefix PATH : ${lib.makeBinPath [ | |
36 | pkgs.git pkgs.openssh | |
37 | ]} | |
38 | wrapProgram $out/check_imap_connection --prefix PATH : ${lib.makeBinPath [ | |
39 | pkgs.openssl | |
40 | ]} | |
41 | wrapProgram $out/check_eriomem --prefix PATH : ${lib.makeBinPath [ | |
42 | pkgs.s3cmd pkgs.python3 | |
43 | ]} | |
44 | wrapProgram $out/notify_maison_bbc_by_email --prefix PATH : ${lib.makeBinPath [ | |
45 | pkgs.mailutils pkgs.gawk | |
46 | ]} | |
47 | wrapProgram $out/notify_by_email --prefix PATH : ${lib.makeBinPath [ | |
48 | pkgs.mailutils | |
49 | ]} | |
50 | wrapProgram $out/notify_by_slack --prefix PATH : ${lib.makeBinPath [ | |
51 | pkgs.curl pkgs.jq | |
52 | ]} | |
53 | ''; | |
54 | toObjects = pkgs.callPackage ./to_objects.nix {}; | |
55 | commonConfig = { | |
56 | eldiron = { | |
57 | processWarn = "250"; processAlert = "400"; | |
58 | loadWarn = "8.0"; loadAlert = "10.0"; | |
59 | }; | |
60 | backup-2 = { | |
61 | processWarn = "60"; processAlert = "70"; | |
62 | loadWarn = "1.0"; loadAlert = "2.0"; | |
63 | }; | |
64 | monitoring-1 = { | |
65 | processWarn = "50"; processAlert = "60"; | |
66 | loadWarn = "1.0"; loadAlert = "2.0"; | |
67 | }; | |
68 | }; | |
69 | masterPassiveObjects = let | |
70 | otherPassiveObjects = map | |
71 | (n: (pkgs.callPackage (./. + "/objects_" + n + ".nix") {})) | |
72 | [ "caldance-1" "ulminfo-fr" "immae-eu" "phare" "tiboqorl-fr" ]; | |
73 | otherPassiveServices = lib.flatten (map (h: h.service or []) otherPassiveObjects); | |
74 | otherPassiveHosts = (map (h: h.host)) otherPassiveObjects; | |
75 | passiveNodes = lib.attrsets.filterAttrs (n: _: builtins.elem n ["backup-2" "eldiron"]) nodes; | |
76 | toPassiveServices = map (s: s.passiveInfo.filter s // s.passiveInfo); | |
77 | passiveServices = lib.flatten (lib.attrsets.mapAttrsToList | |
78 | (_: n: toPassiveServices n.config.myServices.monitoring.services) | |
79 | passiveNodes | |
80 | ); | |
81 | in { | |
82 | service = passiveServices ++ otherPassiveServices; | |
83 | host = lib.lists.foldr | |
84 | (a: b: a//b) | |
85 | {} | |
86 | (otherPassiveHosts ++ lib.attrsets.mapAttrsToList (_: h: h.config.myServices.monitoring.hosts) passiveNodes); | |
87 | }; | |
88 | masterObjects = pkgs.callPackage ./objects_master.nix { inherit config; }; | |
89 | commonObjects = pkgs.callPackage ./objects_common.nix ({ | |
90 | master = cfg.master; | |
91 | hostFQDN = config.hostEnv.fqdn; | |
92 | hostName = name; | |
93 | sudo = "/run/wrappers/bin/sudo"; | |
94 | } // builtins.getAttr name commonConfig); | |
95 | hostObjects = | |
96 | let | |
97 | specific_file = ./. + "/objects_" + name + ".nix"; | |
98 | in | |
99 | lib.attrsets.optionalAttrs | |
100 | (builtins.pathExists specific_file) | |
101 | (pkgs.callPackage specific_file { | |
102 | inherit config; | |
103 | hostFQDN = config.hostEnv.fqdn; | |
104 | hostName = name; | |
105 | }); | |
106 | in | |
107 | { | |
108 | options = { | |
109 | myServices.monitoring = { | |
110 | enable = lib.mkOption { | |
111 | type = lib.types.bool; | |
112 | default = false; | |
113 | description = '' | |
114 | Whether to enable monitoring. | |
115 | ''; | |
116 | }; | |
117 | master = lib.mkOption { | |
118 | type = lib.types.bool; | |
119 | default = false; | |
120 | description = '' | |
121 | This instance is the master instance | |
122 | ''; | |
123 | }; | |
124 | hosts = lib.mkOption { | |
125 | readOnly = true; | |
126 | description = "Hosts list for this host"; | |
127 | default = (commonObjects.host or {}) // (hostObjects.host or {}); | |
128 | }; | |
129 | services = lib.mkOption { | |
130 | readOnly = true; | |
131 | description = "Services list for this host"; | |
132 | default = commonObjects.service ++ hostObjects.service; | |
133 | }; | |
134 | }; | |
135 | }; | |
136 | ||
137 | config = lib.mkIf cfg.enable { | |
138 | services.duplyBackup.profiles.monitoring = { | |
139 | rootDir = config.services.naemon.varDir; | |
140 | }; | |
141 | security.sudo.extraRules = [ | |
142 | { | |
143 | commands = [ | |
144 | { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; } | |
145 | { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; } | |
146 | ]; | |
147 | users = [ "naemon" ]; | |
148 | runAs = "root"; | |
149 | } | |
150 | { | |
151 | commands = [ | |
152 | { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; } | |
153 | ]; | |
154 | users = [ "naemon" ]; | |
155 | runAs = "ALL"; | |
156 | } | |
157 | { | |
158 | commands = [ | |
159 | { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; } | |
160 | ]; | |
161 | users = [ "naemon" ]; | |
162 | runAs = "postgres"; | |
163 | } | |
164 | { | |
165 | commands = [ | |
166 | { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; } | |
167 | ]; | |
168 | users = [ "naemon" ]; | |
169 | runAs = "mysql"; | |
170 | } | |
171 | { | |
172 | commands = [ | |
173 | { command = "${myplugins}/check_openldap_replication *"; options = [ "NOPASSWD" ]; } | |
174 | ]; | |
175 | users = [ "naemon" ]; | |
176 | runAs = "openldap"; | |
177 | } | |
178 | { | |
179 | commands = [ | |
180 | { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; } | |
181 | ]; | |
182 | users = [ "naemon" ]; | |
183 | runAs = "redis"; | |
184 | } | |
185 | ]; | |
186 | environment.etc."mdadm.conf" = { | |
187 | enable = true; | |
188 | mode = "0644"; | |
189 | user = "root"; | |
190 | text = "MAILADDR ${config.myEnv.monitoring.email}"; | |
191 | }; | |
192 | ||
193 | secrets.keys = [ | |
194 | { | |
195 | dest = "naemon/id_rsa"; | |
196 | user = "naemon"; | |
197 | group = "naemon"; | |
198 | premissions = "0400"; | |
199 | text = config.myEnv.monitoring.ssh_secret_key; | |
200 | } | |
201 | ]; | |
202 | # needed since extraResource is not in the closure | |
203 | systemd.services.naemon.path = [ myplugins ]; | |
204 | services.naemon = { | |
205 | enable = true; | |
206 | extraConfig = '' | |
207 | broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live | |
208 | use_syslog=1 | |
209 | log_initial_states=1 | |
210 | date_format=iso8601 | |
211 | admin_email=${config.myEnv.monitoring.email} | |
212 | '' + lib.optionalString (!cfg.master) '' | |
213 | obsess_over_services=1 | |
214 | ocsp_command=notify-master | |
215 | ''; | |
216 | extraResource = '' | |
217 | $USER2$=${myplugins} | |
218 | $USER200$=${config.myEnv.monitoring.status_url} | |
219 | $USER201$=${config.myEnv.monitoring.status_token} | |
220 | $USER202$=${config.myEnv.monitoring.http_user_password} | |
221 | $USER203$=${config.secrets.fullPaths."naemon/id_rsa"} | |
222 | $USER204$=${config.myEnv.monitoring.imap_login} | |
223 | $USER205$=${config.myEnv.monitoring.imap_password} | |
224 | $USER206$=${config.myEnv.monitoring.slack_channel} | |
225 | $USER207$=${config.myEnv.monitoring.slack_url} | |
226 | $USER208$=${builtins.concatStringsSep "," (map (builtins.concatStringsSep ":") config.myEnv.monitoring.eriomem_keys)} | |
227 | ''; | |
228 | objectDefs = toObjects commonObjects | |
229 | + toObjects hostObjects | |
230 | + lib.optionalString cfg.master (toObjects masterObjects) | |
231 | + lib.optionalString cfg.master (toObjects masterPassiveObjects); | |
232 | }; | |
233 | }; | |
234 | } |