Code

change ntpdc to ntpq (Jonathan Rozes,Thomas Schimpke, bug-656237 )
[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 #
57 require 5.004;
58 use POSIX;
59 use strict;
60 use Getopt::Long;
61 use vars qw($opt_V $opt_h $opt_H $opt_w $opt_c $opt_j $opt_k $verbose $PROGNAME);
62 use lib utils.pm; 
63 use utils qw($TIMEOUT %ERRORS &print_revision &support);
65 $PROGNAME="check_ntp";
67 sub print_help ();
68 sub print_usage ();
70 $ENV{'PATH'}='';
71 $ENV{'BASH_ENV'}='';
72 $ENV{'ENV'}='';
74 # defaults in millisec
75 my $DEFAULT_OFFSET_WARN =  60000; 
76 my $DEFAULT_OFFSET_CRIT = 120000; 
77 my $DEFAULT_JITTER_WARN =   5000;
78 my $DEFAULT_JITTER_CRIT =  10000; 
80 Getopt::Long::Configure('bundling');
81 GetOptions
82         ("V"   => \$opt_V, "version"    => \$opt_V,
83          "h"   => \$opt_h, "help"       => \$opt_h,
84          "v" => \$verbose, "verbose"    => \$verbose,
85          "w=f" => \$opt_w, "warning=f"  => \$opt_w,   # offset|adjust warning if above this number
86          "c=f" => \$opt_c, "critical=f" => \$opt_c,   # offset|adjust critical if above this number
87          "j=s" => \$opt_j, "jwarn=s"    => \$opt_j,   # jitter warning if above this number
88          "k=s" => \$opt_k, "jcrit=s"    => \$opt_k,   # jitter critical if above this number
89          "H=s" => \$opt_H, "hostname=s" => \$opt_H);
91 if ($opt_V) {
92         print_revision($PROGNAME,'$Revision$ ');
93         exit $ERRORS{'OK'};
94 }
96 if ($opt_h) {
97         print_help();
98         exit $ERRORS{'OK'};
99 }
101 $opt_H = shift unless ($opt_H);
102 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]+)*)$/);
103 unless ($host) {
104         print "No target host specified\n";
105         print_usage();
106         exit $ERRORS{'UNKNOWN'};
109 ($opt_w) || ($opt_w = $DEFAULT_OFFSET_WARN);
110 my $owarn = $1 if ($opt_w =~ /([0-9.]+)/);
112 ($opt_c) || ($opt_c = $DEFAULT_OFFSET_CRIT);
113 my $ocrit = $1 if ($opt_c =~ /([0-9.]+)/);
115 ($opt_j) || ($opt_j = $DEFAULT_JITTER_WARN);
116 my $jwarn = $1 if ($opt_j =~ /([0-9]+)/);
118 ($opt_k) || ($opt_k = $DEFAULT_JITTER_CRIT);
119 my $jcrit = $1 if ($opt_k =~ /([0-9]+)/);
121 if ($ocrit < $owarn ) {
122         print "Critical offset should be larger than warning offset\n";
123         print_usage();
124         exit $ERRORS{"UNKNOWN"};
126 if ($opt_k < $opt_j) {
127         print "Critical jitter should be larger than warning jitter\n";
128         print_usage();
129         exit $ERRORS{'UNKNOWN'};
132 my $stratum = -1;
133 my $ignoreret = 0;
134 my $answer = undef;
135 my $offset = undef;
136 my $jitter = undef;
137 my $syspeer = undef;
138 my $candidates = 0;
139 my $msg; # first line of output to print if format is invalid
141 my $state = $ERRORS{'UNKNOWN'};
142 my $ntpdate_error = $ERRORS{'UNKNOWN'};
143 my $jitter_error = $ERRORS{'UNKNOWN'};
145 # some systems don't have a proper ntpq  (migrated from ntpdc)
146 my $have_ntpq = undef;
147 if ($utils::PATH_TO_NTPQ && -x $utils::PATH_TO_NTPQ ) {
148         $have_ntpq = 1;  
149 }else{
150         $have_ntpq = 0;
153 # Just in case of problems, let's not hang Nagios
154 $SIG{'ALRM'} = sub {
155         print ("ERROR: No response from ntp server (alarm)\n");
156         exit $ERRORS{"UNKNOWN"};
157 };
158 alarm($TIMEOUT);
161 ###
162 ###
163 ### First, check ntpdate
164 ###
165 ###
167 if (!open (NTPDATE, "$utils::PATH_TO_NTPDATE -q $host 2>&1 |")) {
168         print "Could not open ntpdate\n";
169         exit $ERRORS{"UNKNOWN"};
172 while (<NTPDATE>) {
173         print if ($verbose);
174         $msg = $_ unless ($msg);
175         
176         if (/stratum\s(\d+)/) {
177                 $stratum = $1;
178         }
179         
180         if (/(offset|adjust)\s+([-.\d]+)/i) {
181                 $offset = $2;
183                 # An offset of 0.000000 with an error is probably bogus. Actually,
184                 # it's probably always bogus, but let's be paranoid here.
185                 if ($offset == 0) { undef $offset;}
187                 $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
188                 print "ntperr = $ntpdate_error \n" if $verbose;
189         
190         }
192         if (/no server suitable for synchronization found/) {
193                 if ($stratum == 16) {
194                         $ntpdate_error = $ERRORS{"WARNING"};
195                         $msg = "Desynchronized peer server found";
196                         $ignoreret=1;
197                 }
198                 else {
199                         $ntpdate_error = $ERRORS{"CRITICAL"};
200                         $msg = "No suitable peer server found - ";
201                 }
202         }
206 close (NTPDATE); 
207 # declare an error if we also get a non-zero return code from ntpdate
208 # unless already set to critical
209 if ( $? && !$ignoreret ) {
210         print "stderr = $? : $! \n" if $verbose;
211         $ntpdate_error = $ntpdate_error == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"}  ;
212         print "ntperr = $ntpdate_error : $!\n" if $verbose;
215 ###
216 ###
217 ### Then scan xntpq/ntpq if it exists
218 ### and look in the 11th column for jitter 
219 ###
220 # Field 1: Tally Code ( Space, 'x','.','-','+','#','*','o')
221 #           Only match for '*' which implies sys.peer 
222 #           or 'o' which implies pps.peer
223 #           If both exist, the last one is picked. 
224 # Field 2: address of the remote peer
225 # Field 3: Refid of the clock (0.0.0.0 if unknown)
226 # Field 4: stratum (0-15)
227 # Field 5: Type of the peer: local (l), unicast (u), multicast (m) 
228 #          broadcast (b); not sure about multicast/broadcast
229 # Field 6: last packet receive (in seconds)
230 # Field 7: polling interval
231 # Field 8: reachability resgister (octal) 
232 # Field 9: delay
233 # Field 10: offset
234 # Field 11: dispersion/jitter
235
237 if ($have_ntpq) {
239         if ( open(NTPQ,"$utils::PATH_TO_NTPQ -np $host 2>&1 |") ) {
240                 while (<NTPQ>) {
241                         print $_ if ($verbose);
242                         # number of candidates on <host> for sys.peer
243                         if (/^(\*|\+|\#|o])/) {
244                                 ++$candidates;
245                                 print "Candiate count= $candidates\n" if ($verbose);
246                         }
248                         # match sys.peer or pps.peer
249                         if (/^(\*|o)([-0-9.\s]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([lumb]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
250                                 $syspeer = $2;                          
251                                 $jitter = $11;
252                                 print "match $_ \n" if $verbose;
253                                 if ($jitter > $jcrit) {
254                                         print "Jitter_crit = $11 :$jcrit\n" if ($verbose);
255                                         $jitter_error = $ERRORS{'CRITICAL'};
256                                 } elsif ($jitter > $jwarn ) {
257                                         print "Jitter_warn = $11 :$jwarn \n" if ($verbose);
258                                         $jitter_error = $ERRORS{'WARNING'};
259                                 } else {
260                                         $jitter_error = $ERRORS{'OK'};
261                                 }
262                         }
263                 }
264                 close NTPQ;
265         }
269 if ($ntpdate_error != $ERRORS{'OK'}) {
270         $state = $ntpdate_error;
271         if ($ntpdate_error == $ERRORS{'WARNING'} ) {
272                 $answer = $msg . "\n";
273         }
274         else {
275                 $answer = $msg . "Server for ntp probably down\n";
276         }
278         if (defined($offset) && abs($offset) > $ocrit) {
279                 $state = $ERRORS{'CRITICAL'};
280                 $answer = "Server Error and offset $offset msec > +/- $ocrit msec\n";
281         } elsif (defined($offset) && abs($offset) > $owarn) {
282                 $answer = "Server error and offset $offset msec > +/- $owarn msec\n";
283         } elsif (defined($jitter) && abs($jitter) > $jcrit) {
284                 $answer = "Server error and jitter $jitter msec > +/- $jcrit msec\n";
285         } elsif (defined($jitter) && abs($jitter) > $jwarn) {
286                 $answer = "Server error and jitter $jitter msec > +/- $jwarn msec\n";
287         }
289 } elsif ($have_ntpq && $jitter_error != $ERRORS{'OK'}) {
290         $state = $jitter_error;
291         $answer = "Jitter $jitter too high\n";
292         if (defined($offset) && abs($offset) > $ocrit) {
293                 $state = $ERRORS{'CRITICAL'};
294                 $answer = "Jitter error and offset $offset msec > +/- $ocrit msec\n";
295         } elsif (defined($offset) && abs($offset) > $owarn) {
296                 $answer = "Jitter error and offset $offset msec > +/- $owarn msec\n";
297         } elsif (defined($jitter) && abs($jitter) > $jcrit) {
298                 $answer = "Jitter error and jitter $jitter msec > +/- $jcrit msec\n";
299         } elsif (defined($jitter) && abs($jitter) > $jwarn) {
300                 $answer = "Jitter error and jitter $jitter msec > +/- $jwarn msec\n";
301         }
303 } else { # no errors from ntpdate or ntpq
304         if (abs($offset) > $ocrit) {
305                 $state = $ERRORS{'CRITICAL'};
306                 $answer = "Offset $offset msec > +/- $ocrit msec, jitter $jitter msec\n";
307         } elsif (abs($jitter) > $jcrit ) {
308                 $state = $ERRORS{'CRITICAL'};
309                 $answer = "Jitter $jitter msec> +/- $jcrit msec, offset $offset msec \n";
310         } elsif (abs($offset) > $owarn) {
311                 $state = $ERRORS{'WARNING'};
312                 $answer = "Offset $offset msec > +/- $owarn msec, jitter $jitter msec\n";
313         } elsif (abs($jitter) > $jwarn ) {
314                 $state = $ERRORS{'WARNING'};
315                 $answer = "Jitter $jitter msec> +/- $jwarn msec, offset $offset msec \n";
317         } else {
318                 $state = $ERRORS{'OK'};
319                 $answer = "Offset $offset msecs, jitter $jitter msec\n";
320         }
321         
322 #        else { # no offset defined
323 #               $state = $ERRORS{'UNKNOWN'};
324 #               $answer = "Invalid format returned from ntpdate ($msg)\n";
325 #       }
329 foreach my $key (keys %ERRORS) {
330         if ($state==$ERRORS{$key}) {
331                 print ("$key: $answer");
332                 last;
333         }
335 exit $state;
338 ####
339 #### subs
341 sub print_usage () {
342         print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-v verbose]\n";
345 sub print_help () {
346         print_revision($PROGNAME,'$Revision$');
347         print "Copyright (c) 2000 Bo Kersey/Karl DeBisschop\n";
348         print "\n";
349         print_usage();
350         print "
351 Checks the local timestamp offset versus <host> with ntpdate
352 Checks the jitter/dispersion of clock signal between <host> and its sys.peer with ntpq\n
353 -w ( --warning)
354      Clock offset in milliseconds at which a warning message will be generated.\n       Defaults to $DEFAULT_OFFSET_WARN.
355 -c (--critical) 
356      Clock offset in milliseconds at which a critical message will be generated.\n      Defaults to $DEFAULT_OFFSET_CRIT.
357 -j (--jwarn)
358      Clock jitter in milliseconds at which a warning message will be generated.\n       Defaults to $DEFAULT_JITTER_WARN.
359 -k (--jcrit)
360     Clock jitter in milliseconds at which a warning message will be generated.\n        Defaults to $DEFAULT_JITTER_CRIT.\n";
361         support();