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 type = types.listOf types.package;
98 default = [pkgs.nagiosPluginsOfficial pkgs.ssmtp];
99 defaultText = "[pkgs.nagiosPluginsOfficial pkgs.ssmtp]";
100 description = "
101 Packages to be added to the Nagios <envar>PATH</envar>.
102 Typically used to add plugins, but can be anything.
103 ";
104 };
105
106 mainConfigFile = mkOption {
107 type = types.package;
108 default = nagiosCfgFile;
109 defaultText = "nagiosCfgFile";
110 description = "
111 Derivation for the main configuration file of Nagios.
112 ";
113 };
114
115 cgiConfigFile = mkOption {
116 type = types.package;
117 default = nagiosCGICfgFile;
118 defaultText = "nagiosCGICfgFile";
119 description = "
120 Derivation for the configuration file of Nagios CGI scripts
121 that can be used in web servers for running the Nagios web interface.
122 ";
123 };
124
125 enableWebInterface = mkOption {
126 default = false;
127 description = "
128 Whether to enable the Nagios web interface. You should also
129 enable Apache (<option>services.httpd.enable</option>).
130 ";
131 };
132
133 urlPath = mkOption {
134 default = "/nagios";
135 description = "
136 The URL path under which the Nagios web interface appears.
137 That is, you can access the Nagios web interface through
138 <literal>http://<replaceable>server</replaceable>/<replaceable>urlPath</replaceable></literal>.
139 ";
140 };
141 };
142 };
143
144
145 config = mkIf cfg.enable {
146 users.extraUsers.nagios = {
147 description = "Nagios user ";
148 uid = config.ids.uids.nagios;
149 home = nagiosState;
150 createHome = true;
151 };
152
153 # This isn't needed, it's just so that the user can type "nagiostats
154 # -c /etc/nagios.cfg".
155 environment.etc = [
156 { source = cfg.mainConfigFile;
157 target = "nagios.cfg";
158 }
159 ];
160
161 environment.systemPackages = [ pkgs.nagios ];
162 systemd.services.nagios = {
163 description = "Nagios monitoring daemon";
164 path = [ pkgs.nagios ];
165 wantedBy = [ "multi-user.target" ];
166 after = [ "network.target" ];
167
168 serviceConfig = {
169 User = "nagios";
170 Restart = "always";
171 RestartSec = 2;
172 PermissionsStartOnly = true;
173 };
174
175 preStart = ''
176 mkdir -m 0755 -p ${nagiosState} ${nagiosLogDir}
177 chown nagios ${nagiosState} ${nagiosLogDir}
178 '';
179
180 script = ''
181 for i in ${toString cfg.plugins}; do
182 export PATH=$i/bin:$i/sbin:$i/libexec:$PATH
183 done
184 exec ${pkgs.nagios}/bin/nagios ${cfg.mainConfigFile}
185 '';
186 };
187
188 services.httpd.extraConfig = optionalString cfg.enableWebInterface extraHttpdConfig;
189 };
190}