Code

Support for Nagios 1 and Nagios 2 status files (Gerhard Lausser - 1296242)
[nagiosplug.git] / plugins / t / check_nagios.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # check_nagios tests
4 #
5 # $Id$
6 #
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 $result = NPTest->testCmd(
22         "./check_nagios -F $nagios1 -e 5 -C init"
23         );
24 cmp_ok( $result->return_code, '==', 1, "Log over 5 minutes old" );
25 like  ( $result->output, $warningOutput, "Output for warning correct" );
27 my $now = time;
28 # This substitution is dependant on the testcase
29 system( "perl -pe 's/1133537544/$now/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
31 $result = NPTest->testCmd(
32         "./check_nagios -F $nagios1.tmp -e 1 -C init"
33         );
34 cmp_ok( $result->return_code, "==", 0, "Log up to date" );
35 like  ( $result->output, $successOutput, "Output for success correct" );
37 my $later = $now - 61;
38 system( "perl -pe 's/1133537544/$later/' $nagios1 > $nagios1.tmp" ) == 0 or die "Problem with munging $nagios1";
40 $result = NPTest->testCmd(
41         "./check_nagios -F $nagios1.tmp -e 1 -C init"
42         );
43 cmp_ok( $result->return_code, "==", 1, "Log correctly seen as over 1 minute old" );
44 my ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
45 like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
47 $result = NPTest->testCmd(
48         "./check_nagios -F $nagios1.tmp -e 5 -C unlikely_command_string"
49         );
50 cmp_ok( $result->return_code, "==", 2, "Nagios command not found" );
51 like  ( $result->output, $failureOutput, "Output for failure correct" );
53 $result = NPTest->testCmd(
54         "./check_nagios -F $nagios2 -e 5 -C init"
55         );
56 cmp_ok( $result->return_code, "==", 1, "Nagios2 for logfile over 5 mins old" );
58 $now = time;
59 system( "perl -pe 's/1133537302/$now/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
61 $result = NPTest->testCmd(
62         "./check_nagios -F $nagios2.tmp -e 1 -C init"
63         );
64 cmp_ok( $result->return_code, "==", 0, "Nagios2 log up to date" );
66 $later = $now - 61;
67 system( "perl -pe 's/1133537302/$later/' $nagios2 > $nagios2.tmp" ) == 0 or die "Problem with munging $nagios2";
69 $result = NPTest->testCmd(
70         "./check_nagios -F $nagios2.tmp -e 1 -C init"
71         );
72 cmp_ok( $result->return_code, "==", 1, "Nagios2 log correctly seen as over 1 minute old" );
73 ($age) = ($_ = $result->output) =~ /status log updated (\d+) seconds ago/;
74 like( $age, '/^6[0-9]$/', "Log correctly seen as between 60-69 seconds old" );
76 $result = NPTest->testCmd(
77         "./check_nagios -F t/check_nagios.t -e 1 -C init"
78         );
79 cmp_ok( $result->return_code, "==", 2, "Invalid log file" );