Fix INADDR_NONE value (for systems which don't define it).
[monitoring-plugins.git] / plugins / t / check_nagios.t
blobbdb444f499726d42282c286ec6ba6513c6301a34
1 #! /usr/bin/perl -w -I ..
3 # check_nagios tests
5 # $Id$
8 use strict;
9 use Test::More tests => 13;
10 use NPTest;
12 my $successOutput = '/^NAGIOS OK: /';
13 my $warningOutput = '/^NAGIOS WARNING: /';
14 my $failureOutput = '/^NAGIOS CRITICAL: /';
16 my $nagios1 = "t/check_nagios.nagios1.status.log";
17 my $nagios2 = "t/check_nagios.nagios2.status.dat";
19 my $result;
21 # Did use init, but MacOSX 10.4 replaces init with launchd
22 # Alternative is to insist that nagios is running to run this test
23 # Reasonable to expect cron because build servers will
24 # invoke cron to run a build
25 my $procname = "cron";
27 $result = NPTest->testCmd(
28 "./check_nagios -F $nagios1 -e 5 -C $procname"
30 cmp_ok( $result->return_code, '==', 1, "Log over 5 minutes old" );
31 like ( $result->output, $warningOutput, "Output for warning correct" );
33 my $now = time;
34 # This substitution is dependant on the testcase
35 system( "perl -pe 's/1133537544/$now/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
37 $result = NPTest->testCmd(
38 "./check_nagios -F $nagios1.tmp -e 1 -C $procname"
40 cmp_ok( $result->return_code, "==", 0, "Log up to date" );
41 like ( $result->output, $successOutput, "Output for success correct" );
43 my $later = $now - 61;
44 system( "perl -pe 's/1133537544/$later/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
46 $result = NPTest->testCmd(
47 "./check_nagios -F $nagios1.tmp -e 1 -C $procname"
49 cmp_ok( $result->return_code, "==", 1, "Log correctly seen as over 1 minute old" );
50 my ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
51 like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
53 $result = NPTest->testCmd(
54 "./check_nagios -F $nagios1.tmp -e 5 -C unlikely_command_string"
56 cmp_ok( $result->return_code, "==", 2, "Nagios command not found" );
57 like ( $result->output, $failureOutput, "Output for failure correct" );
59 $result = NPTest->testCmd(
60 "./check_nagios -F $nagios2 -e 5 -C $procname"
62 cmp_ok( $result->return_code, "==", 1, "Nagios2 for logfile over 5 mins old" );
64 $now = time;
65 system( "perl -pe 's/1133537302/$now/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
67 $result = NPTest->testCmd(
68 "./check_nagios -F $nagios2.tmp -e 1 -C $procname"
70 cmp_ok( $result->return_code, "==", 0, "Nagios2 log up to date" );
72 $later = $now - 61;
73 system( "perl -pe 's/1133537302/$later/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
75 $result = NPTest->testCmd(
76 "./check_nagios -F $nagios2.tmp -e 1 -C $procname"
78 cmp_ok( $result->return_code, "==", 1, "Nagios2 log correctly seen as over 1 minute old" );
79 ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
80 like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
82 $result = NPTest->testCmd(
83 "./check_nagios -F t/check_nagios.t -e 1 -C $procname"
85 cmp_ok( $result->return_code, "==", 2, "Invalid log file" );