From be25e45fc86612fc52cc3361800547b3552b0131 Mon Sep 17 00:00:00 2001 From: "symphorien+git@xlumurb.eu" Date: Thu, 26 Dec 2019 12:00:00 +0000 Subject: nagios: various improvements * structured config for main config file allows to launch nagios in debug mode without having to write the whole config file by hand * build time syntax check * all options have types, one more example * I find it misleading that the main nagios config file is linked in /etc but that if you change the link in /etc/ and restart nagios, it has no effect. Have nagios use /etc/nagios.cfg * fix paths in example nagios config files, which allows to reuse it: services.nagios.objectDefs = (map (x: "${pkgs.nagios}/etc/objects/${x}.cfg") [ "templates" "timeperiods" "commands" ]) ++ [ ./main.cfg ] * for the above reason, add mailutils to default plugins Co-Authored-By: Aaron Andersen --- pkgs/servers/monitoring/nagios/default.nix | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'pkgs/servers') diff --git a/pkgs/servers/monitoring/nagios/default.nix b/pkgs/servers/monitoring/nagios/default.nix index 693e67ee6dd..d1654bdf3f7 100644 --- a/pkgs/servers/monitoring/nagios/default.nix +++ b/pkgs/servers/monitoring/nagios/default.nix @@ -20,6 +20,13 @@ stdenv.mkDerivation rec { substituteInPlace Makefile --replace '$(MAKE) install-basic' "" ''; installTargets = "install install-config"; + postInstall = '' + # don't make default files use hardcoded paths to commands + sed -i 's@command_line *[^ ]*/\([^/]*\) @command_line \1 @' $out/etc/objects/commands.cfg + sed -i 's@/usr/bin/@@g' $out/etc/objects/commands.cfg + sed -i 's@/bin/@@g' $out/etc/objects/commands.cfg + ''; + meta = { description = "A host, service and network monitoring program"; -- cgit 1.4.1 From cb38bf33e7fd34dff5a2488600c343762a578ffb Mon Sep 17 00:00:00 2001 From: Symphorien Gibol Date: Mon, 30 Dec 2019 12:00:00 +0000 Subject: nagios: add nixos test --- nixos/tests/all-tests.nix | 1 + nixos/tests/nagios.nix | 116 +++++++++++++++++++++++++++++ pkgs/servers/monitoring/nagios/default.nix | 5 +- 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 nixos/tests/nagios.nix (limited to 'pkgs/servers') diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 111643ad69c..0bbf0d9ab41 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -179,6 +179,7 @@ in mysql = handleTest ./mysql.nix {}; mysqlBackup = handleTest ./mysql-backup.nix {}; mysqlReplication = handleTest ./mysql-replication.nix {}; + nagios = handleTest ./nagios.nix {}; nat.firewall = handleTest ./nat.nix { withFirewall = true; }; nat.firewall-conntrack = handleTest ./nat.nix { withFirewall = true; withConntrackHelpers = true; }; nat.standalone = handleTest ./nat.nix { withFirewall = false; }; diff --git a/nixos/tests/nagios.nix b/nixos/tests/nagios.nix new file mode 100644 index 00000000000..6f5d4447287 --- /dev/null +++ b/nixos/tests/nagios.nix @@ -0,0 +1,116 @@ +import ./make-test-python.nix ( + { pkgs, ... }: { + name = "nagios"; + meta = with pkgs.stdenv.lib.maintainers; { + maintainers = [ symphorien ]; + }; + + machine = { lib, ... }: let + writer = pkgs.writeShellScript "write" '' + set -x + echo "$@" >> /tmp/notifications + ''; + in + { + # tested service + services.sshd.enable = true; + # nagios + services.nagios = { + enable = true; + # make state transitions faster + extraConfig.interval_length = "5"; + objectDefs = + (map (x: "${pkgs.nagios}/etc/objects/${x}.cfg") [ "templates" "timeperiods" "commands" ]) ++ [ + ( + pkgs.writeText "objects.cfg" '' + # notifications are written to /tmp/notifications + define command { + command_name notify-host-by-file + command_line ${writer} "$HOSTNAME is $HOSTSTATE$" + } + define command { + command_name notify-service-by-file + command_line ${writer} "$SERVICEDESC$ is $SERVICESTATE$" + } + + # nagios boilerplate + define contact { + contact_name alice + alias alice + host_notifications_enabled 1 + service_notifications_enabled 1 + service_notification_period 24x7 + host_notification_period 24x7 + service_notification_options w,u,c,r,f,s + host_notification_options d,u,r,f,s + service_notification_commands notify-service-by-file + host_notification_commands notify-host-by-file + email foo@example.com + } + define contactgroup { + contactgroup_name admins + alias Admins + members alice + } + define hostgroup{ + hostgroup_name allhosts + alias All hosts + } + + # monitored objects + define host { + use generic-host + host_name localhost + alias localhost + address localhost + hostgroups allhosts + contact_groups admins + # make state transitions faster. + max_check_attempts 2 + check_interval 1 + retry_interval 1 + } + define service { + use generic-service + host_name localhost + service_description ssh + check_command check_ssh + # make state transitions faster. + max_check_attempts 2 + check_interval 1 + retry_interval 1 + } + '' + ) + ]; + }; + }; + + testScript = { ... }: '' + with subtest("ensure sshd starts"): + machine.wait_for_unit("sshd.service") + + + with subtest("ensure nagios starts"): + machine.wait_for_file("/var/log/nagios/current") + + + def assert_notify(text): + machine.wait_for_file("/tmp/notifications") + real = machine.succeed("cat /tmp/notifications").strip() + print(f"got {real!r}, expected {text!r}") + assert text == real + + + with subtest("ensure we get a notification when sshd is down"): + machine.succeed("systemctl stop sshd") + assert_notify("ssh is CRITICAL") + + + with subtest("ensure tests can succeed"): + machine.succeed("systemctl start sshd") + machine.succeed("rm /tmp/notifications") + assert_notify("ssh is OK") + ''; + } +) diff --git a/pkgs/servers/monitoring/nagios/default.nix b/pkgs/servers/monitoring/nagios/default.nix index d1654bdf3f7..042450941d2 100644 --- a/pkgs/servers/monitoring/nagios/default.nix +++ b/pkgs/servers/monitoring/nagios/default.nix @@ -1,4 +1,4 @@ -{ stdenv, fetchurl, perl, php, gd, libpng, zlib, unzip }: +{ stdenv, fetchurl, perl, php, gd, libpng, zlib, unzip, nixosTests }: stdenv.mkDerivation rec { pname = "nagios"; @@ -27,6 +27,9 @@ stdenv.mkDerivation rec { sed -i 's@/bin/@@g' $out/etc/objects/commands.cfg ''; + passthru.tests = { + inherit (nixosTests) nagios; + }; meta = { description = "A host, service and network monitoring program"; -- cgit 1.4.1