Code

- Fix check_ntp_peer returning misleading information when an ICMP message came back
[nagiosplug.git] / plugins / t / check_ntp.t
1 #! /usr/bin/perl -w -I ..
2 #
3 # Testing NTP
4 #
5 # $Id$
6 #
8 use strict;
9 use Test::More;
10 use NPTest;
12 my @PLUGINS1 = ('check_ntp', 'check_ntp_peer', 'check_ntp_time');
13 my @PLUGINS2 = ('check_ntp_peer');
15 plan tests => (12 * scalar(@PLUGINS1)) + (6 * scalar(@PLUGINS2));
17 my $res;
19 my $ntp_service = getTestParameter( "NP_GOOD_NTP_SERVICE",
20                 "A host providing NTP service",
21                 "pool.ntp.org");
23 my $no_ntp_service = getTestParameter( "NP_NO_NTP_SERVICE",
24                 "A host NOT providing the NTP service",
25                 "localhost" );
27 my $host_nonresponsive = getTestParameter( "NP_HOST_NONRESPONSIVE", 
28                 "The hostname of system not responsive to network requests",
29                 "10.0.0.1" );
31 my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", 
32                 "An invalid (not known to DNS) hostname",  
33                 "nosuchhost");
35 my $ntp_okmatch1 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/';
36 my $ntp_warnmatch1 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/';
37 my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs/';
38 my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}/';
39 my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}/';
40 my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}/';
41 my $ntp_noresponse = '/^(CRITICAL - Socket timeout after 3 seconds)|(NTP CRITICAL: No response from NTP server)$/';
42 my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/';
45 foreach my $plugin (@PLUGINS1) {
46         SKIP: {
47                 skip "No NTP server defined", 1 unless $ntp_service;
48                 $res = NPTest->testCmd(
49                         "./$plugin -H $ntp_service -w 1000 -c 2000"
50                         );
51                 cmp_ok( $res->return_code, '==', 0, "$plugin: Good NTP result (simple check)" );
52                 like( $res->output, $ntp_okmatch1, "$plugin: Output match OK (simple check)" );
54                 $res = NPTest->testCmd(
55                         "./$plugin -H $ntp_service -w 1000: -c 2000"
56                         );
57                 cmp_ok( $res->return_code, '==', 1, "$plugin: Warning NTP result (simple check)" );
58                 like( $res->output, $ntp_warnmatch1, "$plugin: Output match WARNING (simple check)" );
60                 $res = NPTest->testCmd(
61                         "./$plugin -H $ntp_service -w 1000 -c 2000:"
62                         );
63                 cmp_ok( $res->return_code, '==', 2, "$plugin: Critical NTP result (simple check)" );
64                 like( $res->output, $ntp_critmatch1, "$plugin: Output match CRITICAL (simple check)" );
65         }
67         SKIP: {
68                 skip "No bad NTP server defined", 1 unless $no_ntp_service;
69                 $res = NPTest->testCmd(
70                         "./$plugin -H $no_ntp_service -t 3"
71                         );
72                 cmp_ok( $res->return_code, '==', 2, "$plugin: No NTP service" );
73                 like( $res->output, $ntp_noresponse, "$plugin: Output match no NTP service" );
74         }
76         $res = NPTest->testCmd(
77                 "./$plugin -H $host_nonresponsive -t 3"
78                 );
79         cmp_ok( $res->return_code, '==', 2, "$plugin: Server not responding" );
80         like( $res->output, $ntp_noresponse, "$plugin: Output match non-responsive" );
82         $res = NPTest->testCmd(
83                 "./$plugin -H $hostname_invalid"
84                 );
85         cmp_ok( $res->return_code, '==', 3, "$plugin: Invalid hostname/address" );
86         like( $res->output, $ntp_nosuchhost, "$plugin: Output match invalid hostname/address" );
88 }
90 foreach my $plugin (@PLUGINS2) {
91         SKIP: {
92                 skip "No NTP server defined", 1 unless $ntp_service;
93                 $res = NPTest->testCmd(
94                         "./$plugin -H $ntp_service -w 1000 -c 2000 -W 20 -C 21 -j 100000 -k 200000"
95                         );
96                 cmp_ok( $res->return_code, '==', 0, "$plugin: Good NTP result with jitter and stratum check" );
97                 like( $res->output, $ntp_okmatch2, "$plugin: Output match OK with jitter and stratum" );
99                 $res = NPTest->testCmd(
100                         "./$plugin -H $ntp_service -w 1000 -c 2000 -W \~:-1 -C 21 -j 100000 -k 200000"
101                         );
102                 cmp_ok( $res->return_code, '==', 1, "$plugin: Warning NTP result with jitter and stratum check" );
103                 like( $res->output, $ntp_warnmatch2, "$plugin: Output match WARNING with jitter and stratum" );
105                 $res = NPTest->testCmd(
106                         "./$plugin -H $ntp_service -w 1000 -c 2000 -W 20 -C 21 -j 100000 -k \~:-1"
107                         );
108                 cmp_ok( $res->return_code, '==', 2, "$plugin: Critical NTP result with jitter and stratum check" );
109                 like( $res->output, $ntp_critmatch2, "$plugin: Output match CRITICAL with jitter and stratum" );
110         }