]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/monitoring/default.nix
Add mysql and redis monitoring
[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 wrapProgram $out/check_redis_replication --prefix PATH : ${lib.makeBinPath [
20 pkgs.gnugrep pkgs.coreutils pkgs.redis
21 ]}
22 wrapProgram $out/check_mysql_replication --prefix PATH : ${lib.makeBinPath [
23 pkgs.gnugrep pkgs.gnused pkgs.coreutils pkgs.mariadb
24 ]}
25 '';
26 toObjects = pkgs.callPackage ./to_objects.nix {};
27 commonConfig = {
28 eldiron = {
29 processWarn = "250"; processAlert = "400";
30 loadWarn = "8.0"; loadAlert = "10.0";
31 };
32 backup-2 = {
33 processWarn = "50"; processAlert = "60";
34 loadWarn = "1.0"; loadAlert = "2.0";
35 };
36 };
37 commonObjects = pkgs.callPackage ./objects_common.nix ({
38 inherit hostFQDN;
39 sudo = "/run/wrappers/bin/sudo";
40 } // builtins.getAttr name commonConfig);
41 hostObjects =
42 let
43 specific_file = ./. + "/objects_" + name + ".nix";
44 in
45 lib.attrsets.optionalAttrs (builtins.pathExists specific_file) (pkgs.callPackage specific_file {});
46 in
47 {
48 options = {
49 myServices.monitoring = {
50 enable = lib.mkOption {
51 type = lib.types.bool;
52 default = false;
53 description = ''
54 Whether to enable monitoring.
55 '';
56 };
57 };
58 };
59
60 config = lib.mkIf config.myServices.monitoring.enable {
61 services.duplyBackup.profiles.monitoring = {
62 rootDir = config.services.naemon.varDir;
63 };
64 security.sudo.extraRules = [
65 {
66 commands = [
67 { command = "${pkgs.mdadm}/bin/mdadm --monitor --scan -1"; options = [ "NOPASSWD" ]; }
68 { command = "${pkgs.postfix}/bin/mailq"; options = [ "NOPASSWD" ]; }
69 ];
70 users = [ "naemon" ];
71 runAs = "root";
72 }
73 {
74 commands = [
75 { command = "${myplugins}/check_postgres_replication *"; options = [ "NOPASSWD" ]; }
76 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
77 ];
78 users = [ "naemon" ];
79 runAs = "postgres";
80 }
81 {
82 commands = [
83 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
84 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
85 ];
86 users = [ "naemon" ];
87 runAs = "mysql";
88 }
89 {
90 commands = [
91 { command = "${myplugins}/check_redis_replication *"; options = [ "NOPASSWD" ]; }
92 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
93 ];
94 users = [ "naemon" ];
95 runAs = "redis";
96 }
97 {
98 commands = [
99 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
100 ];
101 users = [ "naemon" ];
102 runAs = "backup";
103 }
104 ];
105 environment.etc."mdadm.conf" = {
106 enable = true;
107 mode = "0644";
108 user = "root";
109 text = "MAILADDR ${myconfig.env.monitoring.email}";
110 };
111
112 # needed since extraResource is not in the closure
113 systemd.services.naemon.path = [ myplugins ];
114 services.naemon = {
115 enable = true;
116 extraConfig = ''
117 broker_module=${pkgs.naemon-livestatus}/lib/naemon-livestatus/livestatus.so ${config.services.naemon.runDir}/live
118 use_syslog=1
119 log_initial_states=1
120 date_format=iso8601
121 admin_email=${myconfig.env.monitoring.email}
122
123 obsess_over_services=1
124 ocsp_command=notify-master
125 '';
126 extraResource = ''
127 $USER2$=${myplugins}
128 $USER200$=${myconfig.env.monitoring.status_url}
129 $USER201$=${myconfig.env.monitoring.status_token}
130 '';
131 objectDefs = toObjects commonObjects + toObjects hostObjects;
132 };
133 };
134 }