Code

fixed regex for stratum1 peer, added logic for failed ntpq call(e.g. sntp host)
[nagiosplug.git] / plugins-scripts / check_ntp.pl
1 #!/usr/bin/perl -w
3 # (c)1999 Ian Cass, Knowledge Matters Ltd.
4 # Read the GNU copyright stuff for all the legalese
5 #
6 # Check NTP time servers plugin. This plugin requires the ntpdate utility to
7 # be installed on the system, however since it's part of the ntp suite, you 
8 # should already have it installed.
9 #
10 # $Id$
11
12 # Nothing clever done in this program - its a very simple bare basics hack to
13 # get the job done.
14 #
15 # Things to do...
16 # check @words[9] for time differences greater than +/- x secs & return a
17 # warning.
18 #
19 # (c) 1999 Mark Jewiss, Knowledge Matters Limited
20 # 22-9-1999, 12:45
21 #
22 # Modified script to accept 2 parameters or set defaults.
23 # Now issues warning or critical alert is time difference is greater than the 
24 # time passed.
25 #
26 # These changes have not been tested completely due to the unavailability of a
27 # server with the incorrect time.
28 #
29 # (c) 1999 Bo Kersey, VirCIO - Managed Server Solutions <bo@vircio.com>
30 # 22-10-99, 12:17
31 #
32 # Modified the script to give useage if no parameters are input.
33 #
34 # Modified the script to check for negative as well as positive 
35 # time differences.
36 #
37 # Modified the script to work with ntpdate 3-5.93e Wed Apr 14 20:23:03 EDT 1999
38 #
39 # Modified the script to work with ntpdate's that return adjust or offset...
40 #
41 #
42 # Script modified 2000 June 01 by William Pietri <william@bianca.com>
43 #
44 # Modified script to handle weird cases:
45 #     o NTP server doesn't respond (e.g., has died)
46 #     o Server has correct time but isn't suitable synchronization
47 #           source. This happens while starting up and if contact
48 #           with master has been lost.
49 #
50 # Modifed to run under Embedded Perl  (sghosh@users.sf.net)
51 #   - combined logic some blocks together..
52
53 # Added ntpdate check for stratum 16 desynch peer (James Fidell) Feb 03, 2003
54 #
55 # ntpdate - offset is in seconds
56 # changed ntpdc to ntpq - jitter/dispersion is in milliseconds
57 #
58 # Patch for for regex for stratum1 refid.
60 require 5.004;
61 use POSIX;
62 use strict;
63 use Getopt::Long;
64 use vars qw($opt_V $opt_h $opt_H $opt_w $opt_c $opt_j $opt_k $verbose $PROGNAME $def_jitter);
65 use lib utils.pm; 
66 use utils qw($TIMEOUT %ERRORS &print_revision &support);
68 $PROGNAME="check_ntp";
70 sub print_help ();
71 sub print_usage ();
73 $ENV{'PATH'}='';
74 $ENV{'BASH_ENV'}='';
75 $ENV{'ENV'}='';
77 # defaults in sec
78 my $DEFAULT_OFFSET_WARN =  60;  # 1 minute
79 my $DEFAULT_OFFSET_CRIT = 120;  # 2 minutes
80 # default in millisec
81 my $DEFAULT_JITTER_WARN =   5000; # 5 sec
82 my $DEFAULT_JITTER_CRIT =  10000; # 10 sec
84 Getopt::Long::Configure('bundling');
85 GetOptions
86         ("V"   => \$opt_V, "version"    => \$opt_V,
87          "h"   => \$opt_h, "help"       => \$opt_h,
88          "v" => \$verbose, "verbose"    => \$verbose,
89          "w=f" => \$opt_w, "warning=f"  => \$opt_w,   # offset|adjust warning if above this number
90          "c=f" => \$opt_c, "critical=f" => \$opt_c,   # offset|adjust critical if above this number
91          "j=s" => \$opt_j, "jwarn=s"    => \$opt_j,   # jitter warning if above this number
92          "k=s" => \$opt_k, "jcrit=s"    => \$opt_k,   # jitter critical if above this number
93          "H=s" => \$opt_H, "hostname=s" => \$opt_H);
95 if ($opt_V) {
96         print_revision($PROGNAME,'$Revision$ ');
97         exit $ERRORS{'OK'};
98 }
100 if ($opt_h) {
101         print_help();
102         exit $ERRORS{'OK'};
105 # jitter test params specified
106 if (defined $opt_j || defined $opt_k ) {
107         $def_jitter = 1;
110 $opt_H = shift unless ($opt_H);
111 my $host = $1 if ($opt_H && $opt_H =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z][-a-zA-Z0-9]+(\.[a-zA-Z][-a-zA-Z0-9]+)*)$/);
112 unless ($host) {
113         print "No target host specified\n";
114         print_usage();
115         exit $ERRORS{'UNKNOWN'};
118 ($opt_w) || ($opt_w = $DEFAULT_OFFSET_WARN);
119 my $owarn = $1 if ($opt_w =~ /([0-9.]+)/);
121 ($opt_c) || ($opt_c = $DEFAULT_OFFSET_CRIT);
122 my $ocrit = $1 if ($opt_c =~ /([0-9.]+)/);
124 ($opt_j) || ($opt_j = $DEFAULT_JITTER_WARN);
125 my $jwarn = $1 if ($opt_j =~ /([0-9]+)/);
127 ($opt_k) || ($opt_k = $DEFAULT_JITTER_CRIT);
128 my $jcrit = $1 if ($opt_k =~ /([0-9]+)/);
130 if ($ocrit < $owarn ) {
131         print "Critical offset should be larger than warning offset\n";
132         print_usage();
133         exit $ERRORS{"UNKNOWN"};
135 if ($opt_k < $opt_j) {
136         print "Critical jitter should be larger than warning jitter\n";
137         print_usage();
138         exit $ERRORS{'UNKNOWN'};
142 my $stratum = -1;
143 my $ignoreret = 0;
144 my $answer = undef;
145 my $offset = undef;
146 my $jitter = undef;
147 my $syspeer = undef;
148 my $candidates = 0;
149 my $msg; # first line of output to print if format is invalid
151 my $state = $ERRORS{'UNKNOWN'};
152 my $ntpdate_error = $ERRORS{'UNKNOWN'};
153 my $jitter_error = $ERRORS{'UNKNOWN'};
155 # some systems don't have a proper ntpq  (migrated from ntpdc)
156 my $have_ntpq = undef;
157 if ($utils::PATH_TO_NTPQ && -x $utils::PATH_TO_NTPQ ) {
158         $have_ntpq = 1;  
159 }else{
160         $have_ntpq = 0;
163 # Just in case of problems, let's not hang Nagios
164 $SIG{'ALRM'} = sub {
165         print ("ERROR: No response from ntp server (alarm)\n");
166         exit $ERRORS{"UNKNOWN"};
167 };
168 alarm($TIMEOUT);
171 ###
172 ###
173 ### First, check ntpdate
174 ###
175 ###
177 if (!open (NTPDATE, "$utils::PATH_TO_NTPDATE -q $host 2>&1 |")) {
178         print "Could not open ntpdate\n";
179         exit $ERRORS{"UNKNOWN"};
182 while (<NTPDATE>) {
183         print if ($verbose);
184         $msg = $_ unless ($msg);
185         
186         if (/stratum\s(\d+)/) {
187                 $stratum = $1;
188         }
189         
190         if (/(offset|adjust)\s+([-.\d]+)/i) {
191                 $offset = $2;
193                 # An offset of 0.000000 with an error is probably bogus. Actually,
194                 # it's probably always bogus, but let's be paranoid here.
195                 if ($offset == 0) { undef $offset;}
197                 $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
198                 print "ntperr = $ntpdate_error \n" if $verbose;
199         
200         }
202         if (/no server suitable for synchronization found/) {
203                 if ($stratum == 16) {
204                         $ntpdate_error = $ERRORS{"WARNING"};
205                         $msg = "Desynchronized peer server found";
206                         $ignoreret=1;
207                 }
208                 else {
209                         $ntpdate_error = $ERRORS{"CRITICAL"};
210                         $msg = "No suitable peer server found - ";
211                 }
212         }
216 close (NTPDATE); 
217 # declare an error if we also get a non-zero return code from ntpdate
218 # unless already set to critical
219 if ( $? && !$ignoreret ) {
220         print "stderr = $? : $! \n" if $verbose;
221         $ntpdate_error = $ntpdate_error == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"}  ;
222         print "ntperr = $ntpdate_error : $!\n" if $verbose;
225 ###
226 ###
227 ### Then scan xntpq/ntpq if it exists
228 ### and look in the 11th column for jitter 
229 ###
230 # Field 1: Tally Code ( Space, 'x','.','-','+','#','*','o')
231 #           Only match for '*' which implies sys.peer 
232 #           or 'o' which implies pps.peer
233 #           If both exist, the last one is picked. 
234 # Field 2: address of the remote peer
235 # Field 3: Refid of the clock (0.0.0.0 if unknown, WWWV/PPS/GPS if Stratum1)
236 # Field 4: stratum (0-15)
237 # Field 5: Type of the peer: local (l), unicast (u), multicast (m) 
238 #          broadcast (b); not sure about multicast/broadcast
239 # Field 6: last packet receive (in seconds)
240 # Field 7: polling interval
241 # Field 8: reachability resgister (octal) 
242 # Field 9: delay
243 # Field 10: offset
244 # Field 11: dispersion/jitter
245
247 if ($have_ntpq) {
249         if ( open(NTPQ,"$utils::PATH_TO_NTPQ -np $host 2>&1 |") ) {
250                 while (<NTPQ>) {
251                         print $_ if ($verbose);
252                         if ( /timed out/ ){
253                                 $have_ntpq = 0 ;
254                                 last ;
255                         }
256                         # number of candidates on <host> for sys.peer
257                         if (/^(\*|\+|\#|o])/) {
258                                 ++$candidates;
259                                 print "Candiate count= $candidates\n" if ($verbose);
260                         }
262                         # match sys.peer or pps.peer
263                         if (/^(\*|o)([-0-9.\s]+)\s+([-0-9WwVvGgPpSs.]+)\s+([-0-9.]+)\s+([lumb]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
264                                 $syspeer = $2;
265                                 $stratum = $4;
266                                 $jitter = $11;
267                                 print "match $_ \n" if $verbose;
268                                 if ($jitter > $jcrit) {
269                                         print "Jitter_crit = $11 :$jcrit\n" if ($verbose);
270                                         $jitter_error = $ERRORS{'CRITICAL'};
271                                 } elsif ($jitter > $jwarn ) {
272                                         print "Jitter_warn = $11 :$jwarn \n" if ($verbose);
273                                         $jitter_error = $ERRORS{'WARNING'};
274                                 } else {
275                                         $jitter_error = $ERRORS{'OK'};
276                                 }
277                         }
278                 }
279                 close NTPQ;
280         }
284 if ($ntpdate_error != $ERRORS{'OK'}) {
285         $state = $ntpdate_error;
286         if ($ntpdate_error == $ERRORS{'WARNING'} ) {
287                 $answer = $msg . "\n";
288         }
289         else {
290                 $answer = $msg . "Server for ntp probably down\n";
291         }
293         if (defined($offset) && abs($offset) > $ocrit) {
294                 $state = $ERRORS{'CRITICAL'};
295                 $answer = "Server Error and offset $offset msec > +/- $ocrit msec\n";
296         } elsif (defined($offset) && abs($offset) > $owarn) {
297                 $answer = "Server error and offset $offset msec > +/- $owarn msec\n";
298         } elsif (defined($jitter) && abs($jitter) > $jcrit) {
299                 $answer = "Server error and jitter $jitter msec > +/- $jcrit msec\n";
300         } elsif (defined($jitter) && abs($jitter) > $jwarn) {
301                 $answer = "Server error and jitter $jitter msec > +/- $jwarn msec\n";
302         }
304 } elsif ($have_ntpq && $jitter_error != $ERRORS{'OK'}) {
305         $state = $jitter_error;
306         $answer = "Jitter $jitter too high\n";
307         if (defined($offset) && abs($offset) > $ocrit) {
308                 $state = $ERRORS{'CRITICAL'};
309                 $answer = "Jitter error and offset $offset sec > +/- $ocrit sec\n";
310         } elsif (defined($offset) && abs($offset) > $owarn) {
311                 $answer = "Jitter error and offset $offset sec > +/- $owarn sec\n";
312         } elsif (defined($jitter) && abs($jitter) > $jcrit) {
313                 $answer = "Jitter error and jitter $jitter msec > +/- $jcrit msec\n";
314         } elsif (defined($jitter) && abs($jitter) > $jwarn) {
315                 $answer = "Jitter error and jitter $jitter msec > +/- $jwarn msec\n";
316         }
318 } elsif( !$have_ntpq ) { # no errors from ntpdate and no ntpq or ntpq timed out
319         if (abs($offset) > $ocrit) {
320                 $state = $ERRORS{'CRITICAL'};
321                 $answer = "Offset $offset msec > +/- $ocrit sec\n";
322         } elsif (abs($offset) > $owarn) {
323                 $state = $ERRORS{'WARNING'};
324                 $answer = "Offset $offset msec > +/- $owarn sec\n";
325         } elsif (( abs($offset) > $owarn) && $def_jitter ) {
326                 $state = $ERRORS{'WARNING'};
327                 $answer = "Offset $offset msec > +/- $owarn sec, ntpq timed out\n";
328         } elsif ( $def_jitter ) {
329                 $state = $ERRORS{'WARNING'};
330                 $answer = "Offset $offset secs, ntpq timed out\n";
331         } else{
332                 $state = $ERRORS{'OK'};
333                 $answer = "Offset $offset secs \n";
334         }
338 } else { # no errors from ntpdate or ntpq
339         if (abs($offset) > $ocrit) {
340                 $state = $ERRORS{'CRITICAL'};
341                 $answer = "Offset $offset msec > +/- $ocrit sec, jitter $jitter msec\n";
342         } elsif (abs($jitter) > $jcrit ) {
343                 $state = $ERRORS{'CRITICAL'};
344                 $answer = "Jitter $jitter msec> +/- $jcrit msec, offset $offset sec \n";
345         } elsif (abs($offset) > $owarn) {
346                 $state = $ERRORS{'WARNING'};
347                 $answer = "Offset $offset msec > +/- $owarn sec, jitter $jitter msec\n";
348         } elsif (abs($jitter) > $jwarn ) {
349                 $state = $ERRORS{'WARNING'};
350                 $answer = "Jitter $jitter msec> +/- $jwarn msec, offset $offset sec \n";
352         } else {
353                 $state = $ERRORS{'OK'};
354                 $answer = "Offset $offset secs, jitter $jitter msec, peer is stratum $stratum\n";
355         }
356         
359 foreach my $key (keys %ERRORS) {
360         if ($state==$ERRORS{$key}) {
361                 print ("NTP $key: $answer");
362                 last;
363         }
365 exit $state;
368 ####
369 #### subs
371 sub print_usage () {
372         print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-v verbose]\n";
375 sub print_help () {
376         print_revision($PROGNAME,'$Revision$');
377         print "Copyright (c) 2003 Bo Kersey/Karl DeBisschop\n";
378         print "\n";
379         print_usage();
380         print "
381 Checks the local timestamp offset versus <host> with ntpdate
382 Checks the jitter/dispersion of clock signal between <host> and its sys.peer with ntpq\n
383 -w ( --warning)
384      Clock offset in seconds at which a warning message will be generated.\n    Defaults to $DEFAULT_OFFSET_WARN.
385 -c (--critical) 
386      Clock offset in seconds at which a critical message will be generated.\n   Defaults to $DEFAULT_OFFSET_CRIT.
387 -j (--jwarn)
388      Clock jitter in milliseconds at which a warning message will be generated.\n       Defaults to $DEFAULT_JITTER_WARN.
389 -k (--jcrit)
390     Clock jitter in milliseconds at which a warning message will be generated.\n        Defaults to $DEFAULT_JITTER_CRIT.\n
391     
392     If jitter/dispersion is specified with -j or -k and ntpq times out, then a
393     warning is returned.
394 ";      
395 support();