1import ./make-test-python.nix (
2 { pkgs, ... }:
3 {
4 name = "nagios";
5 meta = with pkgs.lib.maintainers; {
6 maintainers = [ symphorien ];
7 };
8
9 nodes.machine =
10 { lib, ... }:
11 let
12 writer = pkgs.writeShellScript "write" ''
13 set -x
14 echo "$@" >> /tmp/notifications
15 '';
16 in
17 {
18 # tested service
19 services.sshd.enable = true;
20 # nagios
21 services.nagios = {
22 enable = true;
23 # make state transitions faster
24 extraConfig.interval_length = "5";
25 objectDefs =
26 (map (x: "${pkgs.nagios}/etc/objects/${x}.cfg") [
27 "templates"
28 "timeperiods"
29 "commands"
30 ])
31 ++ [
32 (pkgs.writeText "objects.cfg" ''
33 # notifications are written to /tmp/notifications
34 define command {
35 command_name notify-host-by-file
36 command_line ${writer} "$HOSTNAME is $HOSTSTATE$"
37 }
38 define command {
39 command_name notify-service-by-file
40 command_line ${writer} "$SERVICEDESC$ is $SERVICESTATE$"
41 }
42
43 # nagios boilerplate
44 define contact {
45 contact_name alice
46 alias alice
47 host_notifications_enabled 1
48 service_notifications_enabled 1
49 service_notification_period 24x7
50 host_notification_period 24x7
51 service_notification_options w,u,c,r,f,s
52 host_notification_options d,u,r,f,s
53 service_notification_commands notify-service-by-file
54 host_notification_commands notify-host-by-file
55 email foo@example.com
56 }
57 define contactgroup {
58 contactgroup_name admins
59 alias Admins
60 members alice
61 }
62 define hostgroup{
63 hostgroup_name allhosts
64 alias All hosts
65 }
66
67 # monitored objects
68 define host {
69 use generic-host
70 host_name localhost
71 alias localhost
72 address localhost
73 hostgroups allhosts
74 contact_groups admins
75 # make state transitions faster.
76 max_check_attempts 2
77 check_interval 1
78 retry_interval 1
79 }
80 define service {
81 use generic-service
82 host_name localhost
83 service_description ssh
84 check_command check_ssh
85 # make state transitions faster.
86 max_check_attempts 2
87 check_interval 1
88 retry_interval 1
89 }
90 '')
91 ];
92 };
93 };
94
95 testScript =
96 { ... }:
97 ''
98 with subtest("ensure sshd starts"):
99 machine.wait_for_unit("sshd.service")
100
101
102 with subtest("ensure nagios starts"):
103 machine.wait_for_file("/var/log/nagios/current")
104
105
106 def assert_notify(text):
107 machine.wait_for_file("/tmp/notifications")
108 real = machine.succeed("cat /tmp/notifications").strip()
109 print(f"got {real!r}, expected {text!r}")
110 assert text == real
111
112
113 with subtest("ensure we get a notification when sshd is down"):
114 machine.succeed("systemctl stop sshd")
115 assert_notify("ssh is CRITICAL")
116
117
118 with subtest("ensure tests can succeed"):
119 machine.succeed("systemctl start sshd")
120 machine.succeed("rm /tmp/notifications")
121 assert_notify("ssh is OK")
122 '';
123 }
124)