1 # Nagios system/network monitoring daemon.
2 { config, lib, pkgs, ... }:
7 cfg = config.services.nagios;
9 nagiosState = "/var/lib/nagios";
10 nagiosLogDir = "/var/log/nagios";
13 nagiosObjectDefs = cfg.objectDefs;
15 nagiosObjectDefsDir = pkgs.runCommand "nagios-objects" {
16 inherit nagiosObjectDefs;
17 preferLocalBuild = true;
18 } "mkdir -p $out; ln -s $nagiosObjectDefs $out/";
22 log_file="${nagiosLogDir}/current";
23 log_archive_path="${nagiosLogDir}/archive";
24 status_file="${nagiosState}/status.dat";
25 object_cache_file="${nagiosState}/objects.cache";
26 temp_file="${nagiosState}/nagios.tmp";
27 lock_file="/run/nagios.lock";
28 state_retention_file="${nagiosState}/retention.dat";
29 query_socket="${nagiosState}/nagios.qh";
30 check_result_path="${nagiosState}";
31 command_file="${nagiosState}/nagios.cmd";
32 cfg_dir="${nagiosObjectDefsDir}";
34 nagios_group="nagios";
35 illegal_macro_output_chars="`~$&|'\"<>";
36 retain_state_information="1";
38 lines = mapAttrsToList (key: value: "${key}=${value}") (default // cfg.extraConfig);
39 content = concatStringsSep "\n" lines;
40 file = pkgs.writeText "nagios.cfg" content;
41 validated = pkgs.runCommand "nagios-checked.cfg" {preferLocalBuild=true;} ''
43 # nagios checks the existence of /var/lib/nagios, but
44 # it does not exist in the build sandbox, so we fake it
46 lib=$(readlink -f lib)
47 sed -i s@=${nagiosState}@=$lib@ nagios.cfg
48 ${pkgs.nagios}/bin/nagios -v nagios.cfg && cp ${file} $out
50 defaultCfgFile = if cfg.validateConfig then validated else file;
52 if cfg.mainConfigFile == null then defaultCfgFile else cfg.mainConfigFile;
54 # Plain configuration for the Nagios web-interface with no
56 nagiosCGICfgFile = pkgs.writeText "nagios.cgi.conf"
58 main_config_file=${cfg.mainConfigFile}
60 url_html_path=${urlPath}
65 ScriptAlias ${urlPath}/cgi-bin ${pkgs.nagios}/sbin
67 <Directory "${pkgs.nagios}/sbin">
70 SetEnv NAGIOS_CGI_CONFIG ${cfg.cgiConfigFile}
73 Alias ${urlPath} ${pkgs.nagios}/share
75 <Directory "${pkgs.nagios}/share">
84 (mkRemovedOptionModule [ "services" "nagios" "urlPath" ] "The urlPath option has been removed as it is hard coded to /nagios in the nagios package.")
87 meta.maintainers = with lib.maintainers; [ symphorien ];
91 enable = mkEnableOption ''[Nagios](https://www.nagios.org/) to monitor your system or network'';
93 objectDefs = mkOption {
95 A list of Nagios object configuration files that must define
96 the hosts, host groups, services and contacts for the
97 network that you want Nagios to monitor.
99 type = types.listOf types.path;
100 example = literalExpression "[ ./objects.cfg ]";
104 type = types.listOf types.package;
105 default = with pkgs; [ monitoring-plugins msmtp mailutils ];
106 defaultText = literalExpression "[pkgs.monitoring-plugins pkgs.msmtp pkgs.mailutils]";
108 Packages to be added to the Nagios {env}`PATH`.
109 Typically used to add plugins, but can be anything.
113 mainConfigFile = mkOption {
114 type = types.nullOr types.package;
117 If non-null, overrides the main configuration file of Nagios.
121 extraConfig = mkOption {
122 type = types.attrsOf types.str;
125 debug_file = "/var/log/nagios/debug.log";
128 description = "Configuration to add to /etc/nagios.cfg";
131 validateConfig = mkOption {
133 default = pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform;
134 defaultText = literalExpression "pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform";
135 description = "if true, the syntax of the nagios configuration file is checked at build time";
138 cgiConfigFile = mkOption {
139 type = types.package;
140 default = nagiosCGICfgFile;
141 defaultText = literalExpression "nagiosCGICfgFile";
143 Derivation for the configuration file of Nagios CGI scripts
144 that can be used in web servers for running the Nagios web interface.
148 enableWebInterface = mkOption {
152 Whether to enable the Nagios web interface. You should also
153 enable Apache ({option}`services.httpd.enable`).
157 virtualHost = mkOption {
158 type = types.submodule (import ../web-servers/apache-httpd/vhost-options.nix);
159 example = literalExpression ''
160 { hostName = "example.org";
161 adminAddr = "webmaster@example.org";
163 sslServerCert = "/var/lib/acme/example.org/full.pem";
164 sslServerKey = "/var/lib/acme/example.org/key.pem";
168 Apache configuration can be done by adapting {option}`services.httpd.virtualHosts`.
169 See [](#opt-services.httpd.virtualHosts) for further information.
176 config = mkIf cfg.enable {
177 users.users.nagios = {
178 description = "Nagios user";
179 uid = config.ids.uids.nagios;
184 users.groups.nagios = { };
186 # This isn't needed, it's just so that the user can type "nagiostats
187 # -c /etc/nagios.cfg".
188 environment.etc."nagios.cfg".source = nagiosCfgFile;
190 environment.systemPackages = [ pkgs.nagios ];
191 systemd.services.nagios = {
192 description = "Nagios monitoring daemon";
193 path = [ pkgs.nagios ] ++ cfg.plugins;
194 wantedBy = [ "multi-user.target" ];
195 after = [ "network.target" ];
196 restartTriggers = [ nagiosCfgFile ];
203 LogsDirectory = "nagios";
204 StateDirectory = "nagios";
205 ExecStart = "${pkgs.nagios}/bin/nagios /etc/nagios.cfg";
209 services.httpd.virtualHosts = optionalAttrs cfg.enableWebInterface {
210 ${cfg.virtualHost.hostName} = mkMerge [ cfg.virtualHost { extraConfig = extraHttpdConfig; } ];