1 # Nagios system/network monitoring daemon.
2 { config, lib, pkgs, ... }:
4 cfg = config.services.nagios;
6 nagiosState = "/var/lib/nagios";
7 nagiosLogDir = "/var/log/nagios";
10 nagiosObjectDefs = cfg.objectDefs;
12 nagiosObjectDefsDir = pkgs.runCommand "nagios-objects" {
13 inherit nagiosObjectDefs;
14 preferLocalBuild = true;
15 } "mkdir -p $out; ln -s $nagiosObjectDefs $out/";
19 log_file="${nagiosLogDir}/current";
20 log_archive_path="${nagiosLogDir}/archive";
21 status_file="${nagiosState}/status.dat";
22 object_cache_file="${nagiosState}/objects.cache";
23 temp_file="${nagiosState}/nagios.tmp";
24 lock_file="/run/nagios.lock";
25 state_retention_file="${nagiosState}/retention.dat";
26 query_socket="${nagiosState}/nagios.qh";
27 check_result_path="${nagiosState}";
28 command_file="${nagiosState}/nagios.cmd";
29 cfg_dir="${nagiosObjectDefsDir}";
31 nagios_group="nagios";
32 illegal_macro_output_chars="`~$&|'\"<>";
33 retain_state_information="1";
35 lines = lib.mapAttrsToList (key: value: "${key}=${value}") (default // cfg.extraConfig);
36 content = lib.concatStringsSep "\n" lines;
37 file = pkgs.writeText "nagios.cfg" content;
38 validated = pkgs.runCommand "nagios-checked.cfg" {preferLocalBuild=true;} ''
40 # nagios checks the existence of /var/lib/nagios, but
41 # it does not exist in the build sandbox, so we fake it
43 lib=$(readlink -f lib)
44 sed -i s@=${nagiosState}@=$lib@ nagios.cfg
45 ${pkgs.nagios}/bin/nagios -v nagios.cfg && cp ${file} $out
47 defaultCfgFile = if cfg.validateConfig then validated else file;
49 if cfg.mainConfigFile == null then defaultCfgFile else cfg.mainConfigFile;
51 # Plain configuration for the Nagios web-interface with no
53 nagiosCGICfgFile = pkgs.writeText "nagios.cgi.conf"
55 main_config_file=${cfg.mainConfigFile}
57 url_html_path=${urlPath}
62 ScriptAlias ${urlPath}/cgi-bin ${pkgs.nagios}/sbin
64 <Directory "${pkgs.nagios}/sbin">
67 SetEnv NAGIOS_CGI_CONFIG ${cfg.cgiConfigFile}
70 Alias ${urlPath} ${pkgs.nagios}/share
72 <Directory "${pkgs.nagios}/share">
81 (lib.mkRemovedOptionModule [ "services" "nagios" "urlPath" ] "The urlPath option has been removed as it is hard coded to /nagios in the nagios package.")
84 meta.maintainers = with lib.maintainers; [ symphorien ];
88 enable = lib.mkEnableOption ''[Nagios](https://www.nagios.org/) to monitor your system or network'';
90 objectDefs = lib.mkOption {
92 A list of Nagios object configuration files that must define
93 the hosts, host groups, services and contacts for the
94 network that you want Nagios to monitor.
96 type = lib.types.listOf lib.types.path;
97 example = lib.literalExpression "[ ./objects.cfg ]";
100 plugins = lib.mkOption {
101 type = lib.types.listOf lib.types.package;
102 default = with pkgs; [ monitoring-plugins msmtp mailutils ];
103 defaultText = lib.literalExpression "[pkgs.monitoring-plugins pkgs.msmtp pkgs.mailutils]";
105 Packages to be added to the Nagios {env}`PATH`.
106 Typically used to add plugins, but can be anything.
110 mainConfigFile = lib.mkOption {
111 type = lib.types.nullOr lib.types.package;
114 If non-null, overrides the main configuration file of Nagios.
118 extraConfig = lib.mkOption {
119 type = lib.types.attrsOf lib.types.str;
122 debug_file = "/var/log/nagios/debug.log";
125 description = "Configuration to add to /etc/nagios.cfg";
128 validateConfig = lib.mkOption {
129 type = lib.types.bool;
130 default = pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform;
131 defaultText = lib.literalExpression "pkgs.stdenv.hostPlatform == pkgs.stdenv.buildPlatform";
132 description = "if true, the syntax of the nagios configuration file is checked at build time";
135 cgiConfigFile = lib.mkOption {
136 type = lib.types.package;
137 default = nagiosCGICfgFile;
138 defaultText = lib.literalExpression "nagiosCGICfgFile";
140 Derivation for the configuration file of Nagios CGI scripts
141 that can be used in web servers for running the Nagios web interface.
145 enableWebInterface = lib.mkOption {
146 type = lib.types.bool;
149 Whether to enable the Nagios web interface. You should also
150 enable Apache ({option}`services.httpd.enable`).
154 virtualHost = lib.mkOption {
155 type = lib.types.submodule (import ../web-servers/apache-httpd/vhost-options.nix);
156 example = lib.literalExpression ''
157 { hostName = "example.org";
158 adminAddr = "webmaster@example.org";
160 sslServerCert = "/var/lib/acme/example.org/full.pem";
161 sslServerKey = "/var/lib/acme/example.org/key.pem";
165 Apache configuration can be done by adapting {option}`services.httpd.virtualHosts`.
166 See [](#opt-services.httpd.virtualHosts) for further information.
173 config = lib.mkIf cfg.enable {
174 users.users.nagios = {
175 description = "Nagios user";
176 uid = config.ids.uids.nagios;
181 users.groups.nagios = { };
183 # This isn't needed, it's just so that the user can type "nagiostats
184 # -c /etc/nagios.cfg".
185 environment.etc."nagios.cfg".source = nagiosCfgFile;
187 environment.systemPackages = [ pkgs.nagios ];
188 systemd.services.nagios = {
189 description = "Nagios monitoring daemon";
190 path = [ pkgs.nagios ] ++ cfg.plugins;
191 wantedBy = [ "multi-user.target" ];
192 after = [ "network.target" ];
193 restartTriggers = [ nagiosCfgFile ];
200 LogsDirectory = "nagios";
201 StateDirectory = "nagios";
202 ExecStart = "${pkgs.nagios}/bin/nagios /etc/nagios.cfg";
206 services.httpd.virtualHosts = lib.optionalAttrs cfg.enableWebInterface {
207 ${cfg.virtualHost.hostName} = lib.mkMerge [ cfg.virtualHost { extraConfig = extraHttpdConfig; } ];