summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/nagios.nix
blob: 6a3b977694620cd4a0d5e60978dcb418e5fc7c90 (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
184
185
186
187
# Nagios system/network monitoring daemon.
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.nagios;

  nagiosState = "/var/lib/nagios";
  nagiosLogDir = "/var/log/nagios";

  nagiosObjectDefs = cfg.objectDefs;

  nagiosObjectDefsDir = pkgs.runCommand "nagios-objects" {
      inherit nagiosObjectDefs;
      preferLocalBuild = true;
    } "mkdir -p $out; ln -s $nagiosObjectDefs $out/";

  nagiosCfgFile = pkgs.writeText "nagios.cfg"
    ''
      # Paths for state and logs.
      log_file=${nagiosLogDir}/current
      log_archive_path=${nagiosLogDir}/archive
      status_file=${nagiosState}/status.dat
      object_cache_file=${nagiosState}/objects.cache
      temp_file=${nagiosState}/nagios.tmp
      lock_file=/run/nagios.lock # Not used I think.
      state_retention_file=${nagiosState}/retention.dat
      query_socket=${nagiosState}/nagios.qh
      check_result_path=${nagiosState}
      command_file=${nagiosState}/nagios.cmd

      # Configuration files.
      #resource_file=resource.cfg
      cfg_dir=${nagiosObjectDefsDir}

      # Uid/gid that the daemon runs under.
      nagios_user=nagios
      nagios_group=nagios

      # Misc. options.
      illegal_macro_output_chars=`~$&|'"<>
      retain_state_information=1
    ''; # "

  # Plain configuration for the Nagios web-interface with no
  # authentication.
  nagiosCGICfgFile = pkgs.writeText "nagios.cgi.conf"
    ''
      main_config_file=${cfg.mainConfigFile}
      use_authentication=0
      url_html_path=${cfg.urlPath}
    '';

  extraHttpdConfig =
    ''
      ScriptAlias ${cfg.urlPath}/cgi-bin ${pkgs.nagios}/sbin

      <Directory "${pkgs.nagios}/sbin">
        Options ExecCGI
        Require all granted
        SetEnv NAGIOS_CGI_CONFIG ${cfg.cgiConfigFile}
      </Directory>

      Alias ${cfg.urlPath} ${pkgs.nagios}/share

      <Directory "${pkgs.nagios}/share">
        Options None
        Require all granted
      </Directory>
    '';

in
{
  options = {
    services.nagios = {
      enable = mkOption {
        default = false;
        description = "
          Whether to use <link
          xlink:href='http://www.nagios.org/'>Nagios</link> to monitor
          your system or network.
        ";
      };

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

      plugins = mkOption {
        type = types.listOf types.package;
        default = [pkgs.nagiosPluginsOfficial pkgs.ssmtp];
        defaultText = "[pkgs.nagiosPluginsOfficial pkgs.ssmtp]";
        description = "
          Packages to be added to the Nagios <envar>PATH</envar>.
          Typically used to add plugins, but can be anything.
        ";
      };

      mainConfigFile = mkOption {
        type = types.package;
        default = nagiosCfgFile;
        defaultText = "nagiosCfgFile";
        description = "
          Derivation for the main configuration file of Nagios.
        ";
      };

      cgiConfigFile = mkOption {
        type = types.package;
        default = nagiosCGICfgFile;
        defaultText = "nagiosCGICfgFile";
        description = "
          Derivation for the configuration file of Nagios CGI scripts
          that can be used in web servers for running the Nagios web interface.
        ";
      };

      enableWebInterface = mkOption {
        default = false;
        description = "
          Whether to enable the Nagios web interface.  You should also
          enable Apache (<option>services.httpd.enable</option>).
        ";
      };

      urlPath = mkOption {
        default = "/nagios";
        description = "
          The URL path under which the Nagios web interface appears.
          That is, you can access the Nagios web interface through
          <literal>http://<replaceable>server</replaceable>/<replaceable>urlPath</replaceable></literal>.
        ";
      };
    };
  };


  config = mkIf cfg.enable {
    users.users.nagios = {
      description = "Nagios user ";
      uid         = config.ids.uids.nagios;
      home        = nagiosState;
      group       = "nagios";
    };

    users.groups.nagios = { };

    # This isn't needed, it's just so that the user can type "nagiostats
    # -c /etc/nagios.cfg".
    environment.etc = [
      { source = cfg.mainConfigFile;
        target = "nagios.cfg";
      }
    ];

    environment.systemPackages = [ pkgs.nagios ];
    systemd.services.nagios = {
      description = "Nagios monitoring daemon";
      path     = [ pkgs.nagios ];
      wantedBy = [ "multi-user.target" ];
      after    = [ "network.target" ];

      serviceConfig = {
        User = "nagios";
        Group = "nagios";
        Restart = "always";
        RestartSec = 2;
        LogsDirectory = "nagios";
        StateDirectory = "nagios";
      };

      script = ''
        for i in ${toString cfg.plugins}; do
          export PATH=$i/bin:$i/sbin:$i/libexec:$PATH
        done
        exec ${pkgs.nagios}/bin/nagios ${cfg.mainConfigFile}
      '';
    };

    services.httpd.extraConfig = optionalString cfg.enableWebInterface extraHttpdConfig;
  };
}