Code

Ignore extra check on offset 0.00000 (John Warburton - 1150777)
[nagiosplug.git] / plugins-scripts / check_ntp.pl
index 10079b693b4afa2b3cbfe21b8b55bc28df60e161..8b5fe4d032ce738645d926aad06005c7c61ada3b 100755 (executable)
@@ -8,7 +8,7 @@
 # should already have it installed.
 #
 # $Id$
-# 
+#
 # Nothing clever done in this program - its a very simple bare basics hack to
 # get the job done.
 #
@@ -61,8 +61,8 @@ require 5.004;
 use POSIX;
 use strict;
 use Getopt::Long;
-use vars qw($opt_V $opt_h $opt_H $opt_t $opt_w $opt_c $opt_j $opt_k $verbose $PROGNAME $def_jitter);
-use lib utils.pm; 
+use vars qw($opt_V $opt_h $opt_H $opt_t $opt_w $opt_c $opt_j $opt_k $verbose $PROGNAME $def_jitter $ipv4 $ipv6);
+use lib utils.pm;
 use utils qw($TIMEOUT %ERRORS &print_revision &support);
 
 $PROGNAME="check_ntp";
@@ -85,7 +85,9 @@ Getopt::Long::Configure('bundling');
 GetOptions
        ("V"   => \$opt_V, "version"    => \$opt_V,
         "h"   => \$opt_h, "help"       => \$opt_h,
-        "v" => \$verbose, "verbose"    => \$verbose,
+        "v"   => \$verbose, "verbose"  => \$verbose,
+        "4"   => \$ipv4, "use-ipv4"    => \$ipv4,
+        "6"   => \$ipv6, "use-ipv6"    => \$ipv6,
         "w=f" => \$opt_w, "warning=f"  => \$opt_w,   # offset|adjust warning if above this number
         "c=f" => \$opt_c, "critical=f" => \$opt_c,   # offset|adjust critical if above this number
         "j=s" => \$opt_j, "jwarn=i"    => \$opt_j,   # jitter warning if above this number
@@ -154,7 +156,8 @@ my $answer = undef;
 my $offset = undef;
 my $jitter = undef;
 my $syspeer = undef;
-my $candidates = 0;
+my $candidate = 0;
+my @candidates;
 my $msg; # first line of output to print if format is invalid
 
 my $state = $ERRORS{'UNKNOWN'};
@@ -176,6 +179,18 @@ $SIG{'ALRM'} = sub {
 };
 alarm($timeout);
 
+# Determine protocol to be used for ntpdate and ntpq
+my $ntpdate = $utils::PATH_TO_NTPDATE;
+my $ntpq    = $utils::PATH_TO_NTPQ;
+if ($ipv4) {
+        $ntpdate .= " -4";
+        $ntpq .= " -4";
+}
+elsif ($ipv6) {
+        $ntpdate .= " -6";
+        $ntpq .= " -6";
+}
+# else don't use any flags
 
 ###
 ###
@@ -183,7 +198,7 @@ alarm($timeout);
 ###
 ###
 
-if (!open (NTPDATE, "$utils::PATH_TO_NTPDATE -q $host 2>&1 |")) {
+if (!open (NTPDATE, $ntpdate . " -q $host 2>&1 |")) {
        print "Could not open ntpdate\n";
        exit $ERRORS{"UNKNOWN"};
 }
@@ -201,7 +216,9 @@ while (<NTPDATE>) {
 
                # An offset of 0.000000 with an error is probably bogus. Actually,
                # it's probably always bogus, but let's be paranoid here.
-               if ($offset == 0) { undef $offset;}
+               # Has been reported that 0.0000 happens in a production environment
+               # so this check should be taken out - SF tracker 1150777
+               #if ($offset == 0) { undef $offset;}
 
                $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
                print "ntperr = $ntpdate_error \n" if $verbose;
@@ -252,10 +269,13 @@ if ( $? && !$ignoreret ) {
 # Field 10: offset
 # Field 11: dispersion/jitter
 # 
+# According to bug 773588 Some solaris xntpd implementations seemto match on
+# "#" even though the docs say it exceeds maximum distance. Providing patch
+# here which will generate a warining.
 
 if ($have_ntpq) {
 
-       if ( open(NTPQ,"$utils::PATH_TO_NTPQ -np $host 2>&1 |") ) {
+       if ( open(NTPQ, $ntpq . " -np $host 2>&1 |") ) {
                while (<NTPQ>) {
                        print $_ if ($verbose);
                        if ( /timed out/ ){
@@ -264,12 +284,13 @@ if ($have_ntpq) {
                        }
                        # number of candidates on <host> for sys.peer
                        if (/^(\*|\+|\#|o])/) {
-                               ++$candidates;
-                               print "Candiate count= $candidates\n" if ($verbose);
+                               ++$candidate;
+                               push (@candidates, $_);
+                               print "Candiate count= $candidate\n" if ($verbose);
                        }
-
+                       
                        # match sys.peer or pps.peer
-                       if (/^(\*|o)([-0-9.\s]+)\s+([-0-9A-Za-z.]+)\s+([-0-9.]+)\s+([lumb]+)\s+([-0-9m.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
+                       if (/^(\*|o)([-0-9.\s]+)\s+([-0-9A-Za-z.]+)\s+([-0-9.]+)\s+([lumb-]+)\s+([-0-9m.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
                                $syspeer = $2;
                                $stratum = $4;
                                $jitter = $11;
@@ -284,8 +305,34 @@ if ($have_ntpq) {
                                        $jitter_error = $ERRORS{'OK'};
                                }
                        }
+                       
                }
                close NTPQ;
+
+               # if we did not match sys.peer or pps.peer but matched # candidates only
+               # generate a warning 
+               # based on bug id 773588
+               unless (defined $syspeer) {
+                       if ($#candidates >0) {
+                               foreach my $c (@candidates) {
+                                       $c =~ /^(#)([-0-9.\s]+)\s+([-0-9A-Za-z.]+)\s+([-0-9.]+)\s+([lumb-]+)\s+([-0-9m.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/;
+                                       $syspeer = $2;
+                                       $stratum = $4;
+                                       $jitter = $11;
+                                       print "candidate match $c \n" if $verbose;
+                                       if ($jitter > $jcrit) {
+                                               print "Candidate match - Jitter_crit = $11 :$jcrit\n" if ($verbose);
+                                               $jitter_error = $ERRORS{'CRITICAL'};
+                                       }elsif ($jitter > $jwarn ) {
+                                               print "Candidate match - Jitter_warn = $11 :$jwarn \n" if ($verbose);
+                                               $jitter_error = $ERRORS{'WARNING'};
+                                       } else {
+                                               $jitter_error = $ERRORS{'WARNING'};
+                                       }
+                               }
+
+                       }
+               }
        }
 }
 
@@ -327,13 +374,13 @@ if ($ntpdate_error != $ERRORS{'OK'}) {
 } elsif( !$have_ntpq ) { # no errors from ntpdate and no ntpq or ntpq timed out
        if (abs($offset) > $ocrit) {
                $state = $ERRORS{'CRITICAL'};
-               $answer = "Offset $offset msec > +/- $ocrit sec\n";
+               $answer = "Offset $offset sec > +/- $ocrit sec\n";
        } elsif (abs($offset) > $owarn) {
                $state = $ERRORS{'WARNING'};
-               $answer = "Offset $offset msec > +/- $owarn sec\n";
+               $answer = "Offset $offset sec > +/- $owarn sec\n";
        } elsif (( abs($offset) > $owarn) && $def_jitter ) {
                $state = $ERRORS{'WARNING'};
-               $answer = "Offset $offset msec > +/- $owarn sec, ntpq timed out\n";
+               $answer = "Offset $offset sec > +/- $owarn sec, ntpq timed out\n";
        } elsif ( $def_jitter ) {
                $state = $ERRORS{'WARNING'};
                $answer = "Offset $offset secs, ntpq timed out\n";
@@ -347,13 +394,13 @@ if ($ntpdate_error != $ERRORS{'OK'}) {
 } else { # no errors from ntpdate or ntpq
        if (abs($offset) > $ocrit) {
                $state = $ERRORS{'CRITICAL'};
-               $answer = "Offset $offset msec > +/- $ocrit sec, jitter $jitter msec\n";
+               $answer = "Offset $offset sec > +/- $ocrit sec, jitter $jitter msec\n";
        } elsif (abs($jitter) > $jcrit ) {
                $state = $ERRORS{'CRITICAL'};
                $answer = "Jitter $jitter msec> +/- $jcrit msec, offset $offset sec \n";
        } elsif (abs($offset) > $owarn) {
                $state = $ERRORS{'WARNING'};
-               $answer = "Offset $offset msec > +/- $owarn sec, jitter $jitter msec\n";
+               $answer = "Offset $offset sec > +/- $owarn sec, jitter $jitter msec\n";
        } elsif (abs($jitter) > $jwarn ) {
                $state = $ERRORS{'WARNING'};
                $answer = "Jitter $jitter msec> +/- $jwarn msec, offset $offset sec \n";
@@ -378,7 +425,7 @@ exit $state;
 #### subs
 
 sub print_usage () {
-       print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-v verbose]\n";
+       print "Usage: $PROGNAME -H <host> [-46] [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-v verbose]\n";
 }
 
 sub print_help () {
@@ -396,10 +443,14 @@ Checks the jitter/dispersion of clock signal between <host> and its sys.peer wit
 -j (--jwarn)
      Clock jitter in milliseconds at which a warning message will be generated.\n      Defaults to $DEFAULT_JITTER_WARN.
 -k (--jcrit)
-    Clock jitter in milliseconds at which a warning message will be generated.\n       Defaults to $DEFAULT_JITTER_CRIT.\n
+    Clock jitter in milliseconds at which a warning message will be generated.\n       Defaults to $DEFAULT_JITTER_CRIT.
     
     If jitter/dispersion is specified with -j or -k and ntpq times out, then a
-    warning is returned.
-";     
+    warning is returned.\n
+-4 (--use-ipv4)
+    Use IPv4 connection
+-6 (--use-ipv6)
+    Use IPv6 connection
+\n";   
 support();
 }