aboutsummaryrefslogtreecommitdiff
path: root/modules/naemon/default.nix
blob: 60a75b3f30de048ab4bd696135a79eaa82a8c568 (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.naemon;

  naemonConfig = pkgs.runCommand "naemon-config" {
    objectsFile = pkgs.writeText "naemon_objects.cfg" cfg.objectDefs;
    resourceFile = config.secrets.fullPaths."naemon/resources.cfg";
    extraConfig = pkgs.writeText "extra.cfg" cfg.extraConfig;
    inherit (cfg) logDir varDir runDir cacheDir;
  } ''
     substituteAll ${./naemon.cfg} $out
     cat $extraConfig >> $out
    '';
in
{
  options = {
    services.naemon = {
      enable = mkOption {
        default = false;
        description = "
          Whether to use <link
          xlink:href='http://www.naemon.org/'>Naemon</link> to monitor
          your system or network.
        ";
      };

      objectDefs = mkOption {
        type = types.lines;
        default = "";
        description = "
          A list of Naemon object configuration that must define
          the hosts, host groups, services and contacts for the
          network that you want Naemon to monitor.
        ";
      };

      extraResource = mkOption {
        type = types.lines;
        default = "";
        example = ''
            # Sets $USER2$ to be the path to event handlers
            #$USER2$=/usr/lib/monitoring-plugins/eventhandlers

            # Store some usernames and passwords (hidden from the CGIs)
            #$USER3$=someuser
            #$USER4$=somepassword
          '';
        description = "
          Lines to add to the resource file
            # You can define $USERx$ macros in this file, which can in turn be used
            # in command definitions in your host config file(s).  $USERx$ macros are
            # useful for storing sensitive information such as usernames, passwords,
            # etc.  They are also handy for specifying the path to plugins and
            # event handlers - if you decide to move the plugins or event handlers to
            # a different directory in the future, you can just update one or two
            # $USERx$ macros, instead of modifying a lot of command definitions.
            #
            # Naemon supports up to 256 $USERx$ macros ($USER1$ through $USER256$)
            #
            # Resource files may also be used to store configuration directives for
            # external data sources like MySQL...
            #
        ";
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = "
          Extra config to append to main config
        ";
      };

      user = mkOption {
        type = types.str;
        default = "naemon";
        description = "User for naemon";
      };

      group = mkOption {
        type = types.str;
        default = "naemon";
        description = "Group for naemon";
      };

      varDir = mkOption {
        type = types.path;
        default = "/var/lib/naemon";
        description = "The directory where naemon stores its data";
      };

      cacheDir = mkOption {
        type = types.path;
        default = "/var/cache/naemon";
        description = "The directory where naemon stores its cache";
      };

      runDir = mkOption {
        type = types.path;
        default = "/run/naemon";
        description = "The directory where naemon stores its runtime files";
      };

      logDir = mkOption {
        type = types.path;
        default = "/var/log/naemon";
        description = "The directory where naemon stores its log files";
      };

      package = mkOption {
        type = types.package;
        default = pkgs.naemon.override {
          inherit (cfg) varDir cacheDir logDir runDir user group;
        };
        description  = ''
          Naemon package to use
          '';
      };
    };
  };


  config = mkIf cfg.enable {
    secrets.keys = {
      "naemon/resources.cfg" = {
        user = cfg.user;
        group = cfg.group;
        permissions = "0400";
        text = ''
          $USER1$=${pkgs.monitoring-plugins}/libexec
          ${cfg.extraResource}
          '';
      };
    };

    users.users = optionalAttrs (cfg.user == "naemon") {
      naemon = {
        group = cfg.group;
        uid   = config.ids.uids.nagios;
        extraGroups = [ "keys" ];
      };
    };
    users.groups = optionalAttrs (cfg.user == "naemon") {
      naemon = {
        gid = config.ids.gids.nagios;
      };
    };

    services.filesWatcher.naemon = {
      paths = [ config.secrets.fullPaths."naemon/resources.cfg" ];
    };
    systemd.services.naemon = {
      description = "Naemon monitoring daemon";
      path     = [ cfg.package pkgs.monitoring-plugins ];
      wantedBy = [ "multi-user.target" ];
      after    = [ "network.target" ];

      preStart = "${cfg.package}/bin/naemon -vp ${naemonConfig}";
      script = "${cfg.package}/bin/naemon --daemon ${naemonConfig}";
      reload = "${pkgs.utillinux}/bin/kill -HUP $MAINPID";
      serviceConfig = {
        User = cfg.user;
        Restart = "always";
        RestartSec = 2;
        StandardOutput = "journal";
        StandardError = "inherit";
        PIDFile = "${cfg.runDir}/naemon.pid";
        LogsDirectory = assert lib.strings.hasPrefix "/var/log/" cfg.logDir;
          lib.strings.removePrefix "/var/log/" cfg.logDir;
        CacheDirectory = assert lib.strings.hasPrefix "/var/cache/" cfg.cacheDir;
          let unprefixed = lib.strings.removePrefix "/var/cache/" cfg.cacheDir;
          in [ unprefixed "${unprefixed}/checkresults" ];
        StateDirectory = assert lib.strings.hasPrefix "/var/lib/" cfg.varDir;
          lib.strings.removePrefix "/var/lib/" cfg.varDir;
        RuntimeDirectory = assert lib.strings.hasPrefix "/run/" cfg.runDir;
          lib.strings.removePrefix "/run/" cfg.runDir;
      };
    };
  };
}