]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/monitoring/default.nix
baeebc9e5231583945300a7a9ccfbd73d8131ae8
[perso/Immae/Config/Nix.git] / modules / private / monitoring / default.nix
1 { config, myconfig, pkgs, lib, name, hostFQDN, ... }:
2 let
3 myplugins = pkgs.runCommand "buildplugins" {
4 buildInputs = [ pkgs.makeWrapper pkgs.perl ];
5 } ''
6 mkdir $out
7 cp ${./plugins}/* $out/
8 patchShebangs $out
9 wrapProgram $out/check_command --prefix PATH : ${config.security.wrapperDir}
10 wrapProgram $out/send_nrdp.sh --prefix PATH : ${lib.makeBinPath [
11 pkgs.curl pkgs.jq
12 ]}
13 wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [
14 pkgs.gnugrep pkgs.gawk pkgs.procps-ng
15 ]}
16 wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [
17 pkgs.postgresql
18 ]}
19 '';
20 toObjects = pkgs.callPackage ./to_objects.nix {};
21 commonConfig = {
22 eldiron = {
23 processWarn = "250"; processAlert = "400";
24 loadWarn = "8.0"; loadAlert = "10.0";
25 };
26 backup-2 = {
27 processWarn = "50"; processAlert = "60";
28 loadWarn = "1.0"; loadAlert = "2.0";
29 };
30 };
31 commonObjects = pkgs.callPackage ./objects_common.nix ({
32 inherit hostFQDN;
33 sudo = "/run/wrappers/bin/sudo";
34 } // builtins.getAttr name commonConfig);
35 hostObjects =
36 let
37 specific_file = ./. + "/objects_" + name + ".nix";
38 in
39 lib.attrsets.optionalAttrs (builtins.pathExists specific_file) (pkgs.callPackage specific_file {});
40 in
41 {
42 options = {
43 myServices.monitoring = {
44 enable = lib.mkOption {
45 type = lib.types.bool;
46 default = false;
47 description = ''
48 Whether to enable monitoring.
49 '';
50 };
51 };
52 };
53
54 config = lib.mkIf config.myServices.monitoring.enable {
55 services.duplyBackup.profiles.monitoring = {
56 rootDir = config.services.naemon.varDir;
57 };
58 security.sudo.extraRules = [
59 {
60 commands = [
61 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
62 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
63 ];
64 users = [ "naemon" ];
65 runAs = "root";
66 }
67 {
68 commands = [
69 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
70 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
71 ];
72 users = [ "naemon" ];
73 runAs = "postgres";
74 }
75 {
76 commands = [
77 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
78 ];
79 users = [ "naemon" ];
80 runAs = "mysql";
81 }
82 {
83 commands = [
84 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
85 ];
86 users = [ "naemon" ];
87 runAs = "backup";
88 }
89 ];
90 environment.etc."mdadm.conf" = {
91 enable = true;
92 mode = "0644";
93 user = "root";
94 text = "MAILADDR ${myconfig.env.monitoring.email}";
95 };
96
97 # needed since extraResource is not in the closure
98 systemd.services.naemon.path = [ myplugins ];
99 services.naemon = {
100 enable = true;
101 extraConfig = ''
102 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
103 use_syslog=1
104 log_initial_states=1
105 date_format=iso8601
106 admin_email=${myconfig.env.monitoring.email}
107
108 obsess_over_services=1
109 ocsp_command=notify-master
110 '';
111 extraResource = ''
112 $USER2$=${myplugins}
113 $USER200$=${myconfig.env.monitoring.status_url}
114 $USER201$=${myconfig.env.monitoring.status_token}
115 '';
116 objectDefs = toObjects commonObjects + toObjects hostObjects;
117 };
118 };
119 }