]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/monitoring/default.nix
Add mysql and redis monitoring
[perso/Immae/Config/Nix.git] / modules / private / monitoring / default.nix
CommitLineData
9f202523 1{ config, myconfig, pkgs, lib, name, hostFQDN, ... }:
3bc32d9e
IB
2let
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 [
b11f0e17 11 pkgs.curl pkgs.jq
3bc32d9e
IB
12 ]}
13 wrapProgram $out/check_mem.sh --prefix PATH : ${lib.makeBinPath [
14 pkgs.gnugrep pkgs.gawk pkgs.procps-ng
15 ]}
9f202523
IB
16 wrapProgram $out/check_postgres_replication --prefix PATH : ${lib.makeBinPath [
17 pkgs.postgresql
18 ]}
6015a3b5
IB
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 ]}
3bc32d9e 25 '';
eb071dd4
IB
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";
9f202523 44 in
eb071dd4 45 lib.attrsets.optionalAttrs (builtins.pathExists specific_file) (pkgs.callPackage specific_file {});
3bc32d9e
IB
46in
47{
48 options = {
9f202523
IB
49 myServices.monitoring = {
50 enable = lib.mkOption {
51 type = lib.types.bool;
52 default = false;
53 description = ''
54 Whether to enable monitoring.
55 '';
56 };
3bc32d9e
IB
57 };
58 };
59
60 config = lib.mkIf config.myServices.monitoring.enable {
d2e703c5 61 services.duplyBackup.profiles.monitoring = {
6a8252b1
IB
62 rootDir = config.services.naemon.varDir;
63 };
3bc32d9e
IB
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 }
9f202523
IB
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 = [
6015a3b5 83 { command = "${myplugins}/check_mysql_replication *"; options = [ "NOPASSWD" ]; }
9f202523 84 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
9f6a7862
IB
85 ];
86 users = [ "naemon" ];
87 runAs = "mysql";
88 }
6015a3b5
IB
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 }
9f6a7862
IB
97 {
98 commands = [
99 { command = "${myplugins}/check_last_file_date /backup2/*"; options = [ "NOPASSWD" ]; }
9f202523
IB
100 ];
101 users = [ "naemon" ];
102 runAs = "backup";
103 }
3bc32d9e
IB
104 ];
105 environment.etc."mdadm.conf" = {
106 enable = true;
107 mode = "0644";
108 user = "root";
5ea246ba 109 text = "MAILADDR ${myconfig.env.monitoring.email}";
3bc32d9e
IB
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
5ea246ba 121 admin_email=${myconfig.env.monitoring.email}
3bc32d9e
IB
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 '';
eb071dd4 131 objectDefs = toObjects commonObjects + toObjects hostObjects;
3bc32d9e
IB
132 };
133 };
134}