Code

new option for zero offset - 1159317
[nagiosplug.git] / plugins-scripts / check_ntp.pl
index 17730a59f8f4cfac9b29ae302a0dbb080a10c712..d0167cdedd019e38c3af543bb055382f5407dd5c 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_O $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,9 +85,12 @@ 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
+        "O"   => \$opt_O, "zero-offset" => \$opt_O,  # zero-offset  bad
         "j=s" => \$opt_j, "jwarn=i"    => \$opt_j,   # jitter warning if above this number
         "k=s" => \$opt_k, "jcrit=i"    => \$opt_k,   # jitter critical if above this number
         "t=s" => \$opt_t, "timeout=i"  => \$opt_t,
@@ -138,10 +141,13 @@ if ($ocrit < $owarn ) {
        print_usage();
        exit $ERRORS{"UNKNOWN"};
 }
-if ($opt_k < $opt_j) {
-       print "Critical jitter should be larger than warning jitter\n";
-       print_usage();
-       exit $ERRORS{'UNKNOWN'};
+
+if ($def_jitter) {
+       if ($opt_k < $opt_j) {
+               print "Critical jitter should be larger than warning jitter\n";
+               print_usage();
+               exit $ERRORS{'UNKNOWN'};
+       }
 }
 
 
@@ -151,7 +157,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'};
@@ -173,6 +180,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
 
 ###
 ###
@@ -180,13 +199,13 @@ 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"};
 }
 
 while (<NTPDATE>) {
-       print if ($verbose);
+       #print if ($verbose);  # noop
        $msg = $_ unless ($msg);
        
        if (/stratum\s(\d+)/) {
@@ -198,7 +217,11 @@ 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
+               # on Solaris 8 so this check should be taken out - SF tracker 1150777
+               if (defined $opt_O ) {
+                       if ($offset == 0) { undef $offset;}
+               }
 
                $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
                print "ntperr = $ntpdate_error \n" if $verbose;
@@ -249,10 +272,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/ ){
@@ -261,12 +287,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-9hm.]+)\s+([-0-9.mh]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
                                $syspeer = $2;
                                $stratum = $4;
                                $jitter = $11;
@@ -281,8 +308,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'};
+                                       }
+                               }
+
+                       }
+               }
        }
 }
 
@@ -324,13 +377,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";
@@ -344,13 +397,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";
@@ -375,7 +428,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] [-O] [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-v verbose]\n";
 }
 
 sub print_help () {
@@ -386,17 +439,23 @@ sub print_help () {
        print "
 Checks the local timestamp offset versus <host> with ntpdate
 Checks the jitter/dispersion of clock signal between <host> and its sys.peer with ntpq\n
--w ( --warning)
+-O (--zero-offset)
+     A zero offset on \"ntpdate\" will generate a CRITICAL.\n
+-w (--warning)
      Clock offset in seconds at which a warning message will be generated.\n   Defaults to $DEFAULT_OFFSET_WARN.
 -c (--critical) 
      Clock offset in seconds at which a critical message will be generated.\n  Defaults to $DEFAULT_OFFSET_CRIT.
 -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 critical 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();
 }