Code

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