1# Nagios system/network monitoring daemon.
2{ config, lib, pkgs, ... }:
3
4with lib;
5
6let
7 cfg = config.services.nagios;
8
9 nagiosState = "/var/lib/nagios";
10 nagiosLogDir = "/var/log/nagios";
11
12 nagiosObjectDefs = cfg.objectDefs;
13
14 nagiosObjectDefsDir = pkgs.runCommand "nagios-objects" {inherit nagiosObjectDefs;}
15 "mkdir -p $out; ln -s $nagiosObjectDefs $out/";
16
17 nagiosCfgFile = pkgs.writeText "nagios.cfg"
18 ''
19 # Paths for state and logs.
20 log_file=${nagiosLogDir}/current
21 log_archive_path=${nagiosLogDir}/archive
22 status_file=${nagiosState}/status.dat
23 object_cache_file=${nagiosState}/objects.cache
24 temp_file=${nagiosState}/nagios.tmp
25 lock_file=/var/run/nagios.lock # Not used I think.
26 state_retention_file=${nagiosState}/retention.dat
27 query_socket=${nagiosState}/nagios.qh
28 check_result_path=${nagiosState}
29 command_file=${nagiosState}/nagios.cmd
30
31 # Configuration files.
32 #resource_file=resource.cfg
33 cfg_dir=${nagiosObjectDefsDir}
34
35 # Uid/gid that the daemon runs under.
36 nagios_user=nagios
37 nagios_group=nogroup
38
39 # Misc. options.
40 illegal_macro_output_chars=`~$&|'"<>
41 retain_state_information=1
42 ''; # "
43
44 # Plain configuration for the Nagios web-interface with no
45 # authentication.
46 nagiosCGICfgFile = pkgs.writeText "nagios.cgi.conf"
47 ''
48 main_config_file=${cfg.mainConfigFile}
49 use_authentication=0
50 url_html_path=${cfg.urlPath}
51 '';
52
53 extraHttpdConfig =
54 ''
55 ScriptAlias ${cfg.urlPath}/cgi-bin ${pkgs.nagios}/sbin
56
57 <Directory "${pkgs.nagios}/sbin">
58 Options ExecCGI
59 AllowOverride None
60 Order allow,deny
61 Allow from all
62 SetEnv NAGIOS_CGI_CONFIG ${cfg.cgiConfigFile}
63 </Directory>
64
65 Alias ${cfg.urlPath} ${pkgs.nagios}/share
66
67 <Directory "${pkgs.nagios}/share">
68 Options None
69 AllowOverride None
70 Order allow,deny
71 Allow from all
72 </Directory>
73 '';
74
75in
76{
77 options = {
78 services.nagios = {
79 enable = mkOption {
80 default = false;
81 description = "
82 Whether to use <link
83 xlink:href='http://www.nagios.org/'>Nagios</link> to monitor
84 your system or network.
85 ";
86 };
87
88 objectDefs = mkOption {
89 description = "
90 A list of Nagios object configuration files that must define
91 the hosts, host groups, services and contacts for the
92 network that you want Nagios to monitor.
93 ";
94 };
95
96 plugins = mkOption {
97 default = [pkgs.nagiosPluginsOfficial pkgs.ssmtp];
98 description = "
99 Packages to be added to the Nagios <envar>PATH</envar>.
100 Typically used to add plugins, but can be anything.
101 ";
102 };
103
104 mainConfigFile = mkOption {
105 default = nagiosCfgFile;
106 description = "
107 Derivation for the main configuration file of Nagios.
108 ";
109 };
110
111 cgiConfigFile = mkOption {
112 default = nagiosCGICfgFile;
113 description = "
114 Derivation for the configuration file of Nagios CGI scripts
115 that can be used in web servers for running the Nagios web interface.
116 ";
117 };
118
119 enableWebInterface = mkOption {
120 default = false;
121 description = "
122 Whether to enable the Nagios web interface. You should also
123 enable Apache (<option>services.httpd.enable</option>).
124 ";
125 };
126
127 urlPath = mkOption {
128 default = "/nagios";
129 description = "
130 The URL path under which the Nagios web interface appears.
131 That is, you can access the Nagios web interface through
132 <literal>http://<replaceable>server</replaceable>/<replaceable>urlPath</replaceable></literal>.
133 ";
134 };
135 };
136 };
137
138
139 config = mkIf cfg.enable {
140 users.extraUsers.nagios = {
141 description = "Nagios user ";
142 uid = config.ids.uids.nagios;
143 home = nagiosState;
144 createHome = true;
145 };
146
147 # This isn't needed, it's just so that the user can type "nagiostats
148 # -c /etc/nagios.cfg".
149 environment.etc = [
150 { source = cfg.mainConfigFile;
151 target = "nagios.cfg";
152 }
153 ];
154
155 environment.systemPackages = [ pkgs.nagios ];
156 systemd.services.nagios = {
157 description = "Nagios monitoring daemon";
158 path = [ pkgs.nagios ];
159 wantedBy = [ "multi-user.target" ];
160 after = [ "network-interfaces.target" ];
161
162 serviceConfig = {
163 User = "nagios";
164 Restart = "always";
165 RestartSec = 2;
166 PermissionsStartOnly = true;
167 };
168
169 preStart = ''
170 mkdir -m 0755 -p ${nagiosState} ${nagiosLogDir}
171 chown nagios ${nagiosState} ${nagiosLogDir}
172 '';
173
174 script = ''
175 for i in ${toString cfg.plugins}; do
176 export PATH=$i/bin:$i/sbin:$i/libexec:$PATH
177 done
178 exec ${pkgs.nagios}/bin/nagios ${cfg.mainConfigFile}
179 '';
180 };
181
182 services.httpd.extraConfig = optionalString cfg.enableWebInterface extraHttpdConfig;
183 };
184}