summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/monit.nix
blob: cc4a1b2086248fff3df0c48fc3d75aca0fe306ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{ config, lib, pkgs, ... }:

with builtins;
with import <stockholm/lib>;

let
  cfg = config.krebs.monit;

  out = {
    options.krebs.monit = api;
    config = mkIf cfg.enable imp;
  };

  api = {
    enable = mkEnableOption "monit";
    http = {
      enable = mkEnableOption "monit http server";
      port = mkOption {
        type = types.int;
        default = 9093;
      };
      user = mkOption {
        type = types.str;
        default = "krebs";
      };
      pass = mkOption {
        type = types.str;
        default = "bob";
      };
    };
    user = mkOption {
      type = types.user;
      default = {
        name = "monit";
      };
    };
    group = mkOption {
      type = types.group;
      default = {
        name = "monitor";
      };
    };
    extraConfig = mkOption {
      type = types.attrs;
      default = {};
    };
    alarms = mkOption {
      default = {};
      type = with types; attrsOf (submodule {
        options = {
          test = mkOption {
            type = either path str;
          };
          alarm = mkOption {
            type = either path str;
          };
          interval = mkOption {
            type = str;
            default = "10";
          };
        };
      });
    };
  };

  imp = let
    configFile = pkgs.writeText "monit.cfg" ''
      ${optionalString cfg.http.enable ''
        set httpd port ${toString cfg.http.port}
          allow ${cfg.http.user}:${cfg.http.pass}
      ''}
      set daemon 10

      ${concatStringsSep "\n" (mapAttrsToList (name: alarm: ''
        check program ${name} with path "${alarm.test}"
          every ${alarm.interval} cycles
          if status != 0 then exec "${alarm.alarm}"
      '') cfg.alarms)}
    '';
  in {
    environment.etc = [
      {
        source = configFile;
        target = "monit.conf";
        mode = "0400";
        uid = config.users.users.${cfg.user.name}.uid;
      }
    ];
    users = {
      groups.${cfg.group.name} = {
        inherit (cfg.group) name gid;
      };
      users.${cfg.user.name} = {
        inherit (cfg.user) home name uid;
        createHome = true;
        group = cfg.group.name;
      };
    };

    systemd.services.monit = {
      description = "monit";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      restartIfChanged = true;

      serviceConfig = {
        Restart = "always";
        User = cfg.user.name;
        ExecStart = "${pkgs.monit}/bin/monit -I -c /etc/monit.conf";
        # Monit should restart when the config changes
        ExecStartPre = "${pkgs.coreutils}/bin/echo ${configFile}";
      };
    };
  };
in out