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 # Todo - non-hardcoded dispersion values...
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 $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 Getopt::Long::Configure('bundling');
75 GetOptions
76 ("V" => \$opt_V, "version" => \$opt_V,
77 "h" => \$opt_h, "help" => \$opt_h,
78 "v" => \$verbose, "verbose" => \$verbose,
79 "w=f" => \$opt_w, "warning=f" => \$opt_w, # offset|adjust warning if above this number
80 "c=f" => \$opt_c, "critical=f" => \$opt_c, # offset|adjust critical if above this number
81 "H=s" => \$opt_H, "hostname=s" => \$opt_H);
83 if ($opt_V) {
84 print_revision($PROGNAME,'$Revision$ ');
85 exit $ERRORS{'OK'};
86 }
88 if ($opt_h) {
89 print_help();
90 exit $ERRORS{'OK'};
91 }
93 $opt_H = shift unless ($opt_H);
94 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]+)*)$/);
95 unless ($host) {
96 print "No target host specified\n";
97 print_usage();
98 exit $ERRORS{'UNKNOWN'};
99 }
101 ($opt_w) || ($opt_w = 60);
102 my $warning = $1 if ($opt_w =~ /([0-9.]+)/);
104 ($opt_c) || ($opt_c = 120);
105 my $critical = $1 if ($opt_c =~ /([0-9.]+)/);
108 if ($critical < $warning ) {
109 print "Critical offset should be larger than warning offset\n";
110 print_usage();
111 exit $ERRORS{"UNKNOWN"};
112 }
114 my $answer = undef;
115 my $offset = undef;
116 my $msg; # first line of output to print if format is invalid
118 my $state = $ERRORS{'UNKNOWN'};
119 my $ntpdate_error = $ERRORS{'UNKNOWN'};
120 my $dispersion_error = $ERRORS{'UNKNOWN'};
122 my $key = undef;
123 # some systems don't have a proper ntpdc/xntpdc
124 my $have_ntpdc = undef;
125 if ($utils::PATH_TO_NTPDC && -x $utils::PATH_TO_NTPDC ) {
126 $have_ntpdc = 1;
127 }else{
128 $have_ntpdc = 0;
129 }
131 # Just in case of problems, let's not hang Nagios
132 $SIG{'ALRM'} = sub {
133 print ("ERROR: No response from ntp server (alarm)\n");
134 exit $ERRORS{"UNKNOWN"};
135 };
136 alarm($TIMEOUT);
139 ###
140 ###$dispersion_error = $ERRORS{'
141 ### First, check ntpdate
142 ###
143 ###
145 if (!open (NTPDATE, "$utils::PATH_TO_NTPDATE -q $host 2>&1 |")) {
146 print "Could not open ntpdate\n";
147 exit $ERRORS{"UNKNOWN"};
148 }
150 while (<NTPDATE>) {
151 print if ($verbose);
152 $msg = $_ unless ($msg);
153 if (/(offset|adjust)\s+([-.\d]+)/i) {
154 $offset = $2;
156 # An offset of 0.000000 with an error is probably bogus. Actually,
157 # it's probably always bogus, but let's be paranoid here.
158 if ($offset == 0) { undef $offset;}
160 $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
161 print "ntperr = $ntpdate_error \n" if $verbose;
163 }
165 if (/no server suitable for synchronization found/) {
166 $ntpdate_error = $ERRORS{"CRITICAL"};
167 $msg = "No suitable peer server found - ";
168 }
170 }
172 close (NTPDATE);
173 # declare an error if we also get a non-zero return code from ntpdate
174 # unless already set to critical
175 if ( $? ) {
176 print "stderr = $? : $! \n" if $verbose;
177 $ntpdate_error = $ntpdate_error == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"} ;
178 print "ntperr = $ntpdate_error : $!\n" if $verbose;
179 }
181 ###
182 ###
183 ### Then scan xntpdc/ntpdc if it exists
184 ### and look in the 8th column for dispersion (ntpd v4) or jitter (ntpd v3)
185 ###
187 if ($have_ntpdc) {
189 if ( open(NTPDC,"$utils::PATH_TO_NTPDC -s $host 2>&1 |") ) {
190 while (<NTPDC>) {
191 print $_ if ($verbose);
192 if (/([^\s]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
193 if ($8 gt $critical) {
194 print "Dispersion_crit = $8 :$critical\n" if ($verbose);
195 $dispersion_error = $ERRORS{'CRITICAL'};
196 } elsif ($8 gt $warning ) {
197 print "Dispersion_warn = $8 :$warning \n" if ($verbose);
198 $dispersion_error = $ERRORS{'WARNING'};
199 } else {
200 $dispersion_error = $ERRORS{'OK'};
201 }
202 }
203 }
204 close NTPDC;
205 }
206 }
209 if ($ntpdate_error != $ERRORS{'OK'}) {
210 $state = $ntpdate_error;
211 $answer = $msg . "Server for ntp probably down\n";
212 if (defined($offset) && abs($offset) > $critical) {
213 $state = $ERRORS{'CRITICAL'};
214 $answer = "Server Error and time difference $offset seconds greater than +/- $critical sec\n";
215 } elsif (defined($offset) && abs($offset) > $warning) {
216 $answer = "Server error and time difference $offset seconds greater than +/- $warning sec\n";
217 }
219 } elsif ($have_ntpdc && $dispersion_error != $ERRORS{'OK'}) {
220 $state = $dispersion_error;
221 $answer = "Dispersion too high\n";
222 if (defined($offset) && abs($offset) > $critical) {
223 $state = $ERRORS{'CRITICAL'};
224 $answer = "Dispersion error and time difference $offset seconds greater than +/- $critical sec\n";
225 } elsif (defined($offset) && abs($offset) > $warning) {
226 $answer = "Dispersion error and time difference $offset seconds greater than +/- $warning sec\n";
227 }
229 } else { # no errors from ntpdate or xntpdc
230 if (defined $offset) {
231 if (abs($offset) > $critical) {
232 $state = $ERRORS{'CRITICAL'};
233 $answer = "Time difference $offset seconds greater than +/- $critical sec\n";
234 } elsif (abs($offset) > $warning) {
235 $state = $ERRORS{'WARNING'};
236 $answer = "Time difference $offset seconds greater than +/- $warning sec\n";
237 } elsif (abs($offset) <= $warning) {
238 $state = $ERRORS{'OK'};
239 $answer = "Time difference $offset seconds\n";
240 }
241 } else { # no offset defined
242 $state = $ERRORS{'UNKNOWN'};
243 $answer = "Invalid format returned from ntpdate ($msg)\n";
244 }
245 }
247 foreach $key (keys %ERRORS) {
248 if ($state==$ERRORS{$key}) {
249 print ("$key: $answer");
250 last;
251 }
252 }
253 exit $state;
255 sub print_usage () {
256 print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-v verbose]\n";
257 }
259 sub print_help () {
260 print_revision($PROGNAME,'$Revision$');
261 print "Copyright (c) 2000 Bo Kersey/Karl DeBisschop\n";
262 print "\n";
263 print_usage();
264 print "\n";
265 print "<warn> = Clock offset in seconds at which a warning message will be generated.\n Defaults to 60.\n";
266 print "<crit> = Clock offset in seconds at which a critical message will be generated.\n Defaults to 120.\n\n";
267 print "The same warning and critical values are used to check against the dispersion \n";
268 print "column of ntpdc/xntpdc for the host being queried.\n\n";
269 support();
270 }