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=s" => \$opt_w, "warning=s" => \$opt_w, # offset|adjust warning if above this number
80 "c=s" => \$opt_c, "critical=s" => \$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 = shift) || ($opt_w = 60);
102 my $warning = $1 if ($opt_w =~ /([0-9]+)/);
104 ($opt_c) || ($opt_c = shift) || ($opt_c = 120);
105 my $critical = $1 if ($opt_c =~ /([0-9]+)/);
107 if ($opt_c < $opt_w) {
108 print "Critical offset should be larger than warning offset\n";
109 print_usage();
110 exit $ERRORS{"UNKNOWN"};
111 }
113 my $answer = undef;
114 my $offset = undef;
115 my $msg; # first line of output to print if format is invalid
117 my $state = $ERRORS{'UNKNOWN'};
118 my $ntpdate_error = $ERRORS{'UNKNOWN'};
119 my $dispersion_error = $ERRORS{'UNKNOWN'};
121 my $key = undef;
122 # some systems don't have a proper ntpdc/xntpdc
123 my $have_ntpdc = undef;
124 if ($utils::PATH_TO_NTPDC && -x $utils::PATH_TO_NTPDC ) {
125 $have_ntpdc = 1;
126 }else{
127 $have_ntpdc = 0;
128 }
130 # Just in case of problems, let's not hang Nagios
131 $SIG{'ALRM'} = sub {
132 print ("ERROR: No response from ntp server (alarm)\n");
133 exit $ERRORS{"UNKNOWN"};
134 };
135 alarm($TIMEOUT);
138 ###
139 ###$dispersion_error = $ERRORS{'
140 ### First, check ntpdate
141 ###
142 ###
144 if (!open (NTPDATE, "$utils::PATH_TO_NTPDATE -q $host 2>&1 |")) {
145 print "Could not open ntpdate\n";
146 exit $ERRORS{"UNKNOWN"};
147 }
149 while (<NTPDATE>) {
150 print if ($verbose);
151 $msg = $_ unless ($msg);
152 if (/(offset|adjust)\s+([-.\d]+)/i) {
153 $offset = $2;
155 # An offset of 0.000000 with an error is probably bogus. Actually,
156 # it's probably always bogus, but let's be paranoid here.
157 if ($offset == 0) { undef $offset;}
159 $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
160 print "ntperr = $ntpdate_error \n" if $verbose;
162 }
164 if (/no server suitable for synchronization found/) {
165 $ntpdate_error = $ERRORS{"CRITICAL"};
166 $msg = "No suitable peer server found - ";
167 }
169 }
171 close (NTPDATE);
172 # declare an error if we also get a non-zero return code from ntpdate
173 # unless already set to critical
174 if ( $? ) {
175 print "stderr = $? : $! \n" if $verbose;
176 $ntpdate_error = $ntpdate_error == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"} ;
177 print "ntperr = $ntpdate_error : $!\n" if $verbose;
178 }
180 ###
181 ###
182 ### Then scan xntpdc/ntpdc if it exists
183 ### and look in the 8th column for dispersion (ntpd v4) or jitter (ntpd v3)
184 ###
186 if ($have_ntpdc) {
188 if ( open(NTPDC,"$utils::PATH_TO_NTPDC -s $host 2>&1 |") ) {
189 while (<NTPDC>) {
190 print $_ if ($verbose);
191 if (/([^\s]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
192 if ($8>15) {
193 print "Dispersion = $8 \n" if ($verbose);
194 $dispersion_error = $ERRORS{'CRITICAL'};
195 } elsif ($8>5 && $dispersion_error<$ERRORS{'CRITICAL'}) {
196 print "Dispersion = $8 \n" if ($verbose);
197 $dispersion_error = $ERRORS{'WARNING'};
198 } else {
199 $dispersion_error = $ERRORS{'OK'};
200 }
201 }
202 }
203 close NTPDC;
204 }
205 }
208 if ($ntpdate_error != $ERRORS{'OK'}) {
209 $state = $ntpdate_error;
210 $answer = $msg . "Server for ntp probably down\n";
211 if (defined($offset) && abs($offset) > $critical) {
212 $state = $ERRORS{'CRITICAL'};
213 $answer = "Server Error and time difference $offset seconds greater than +/- $critical sec\n";
214 } elsif (defined($offset) && abs($offset) > $warning) {
215 $answer = "Server error and time difference $offset seconds greater than +/- $warning sec\n";
216 }
218 } elsif ($have_ntpdc && $dispersion_error != $ERRORS{'OK'}) {
219 $state = $dispersion_error;
220 $answer = "Dispersion too high\n";
221 if (defined($offset) && abs($offset) > $critical) {
222 $state = $ERRORS{'CRITICAL'};
223 $answer = "Dispersion error and time difference $offset seconds greater than +/- $critical sec\n";
224 } elsif (defined($offset) && abs($offset) > $warning) {
225 $answer = "Dispersion error and time difference $offset seconds greater than +/- $warning sec\n";
226 }
228 } else { # no errors from ntpdate or xntpdc
229 if (defined $offset) {
230 if (abs($offset) > $critical) {
231 $state = $ERRORS{'CRITICAL'};
232 $answer = "Time difference $offset seconds greater than +/- $critical sec\n";
233 } elsif (abs($offset) > $warning) {
234 $state = $ERRORS{'WARNING'};
235 $answer = "Time difference $offset seconds greater than +/- $warning sec\n";
236 } elsif (abs($offset) <= $warning) {
237 $state = $ERRORS{'OK'};
238 $answer = "Time difference $offset seconds\n";
239 }
240 } else { # no offset defined
241 $state = $ERRORS{'UNKNOWN'};
242 $answer = "Invalid format returned from ntpdate ($msg)\n";
243 }
244 }
246 foreach $key (keys %ERRORS) {
247 if ($state==$ERRORS{$key}) {
248 print ("$key: $answer");
249 last;
250 }
251 }
252 exit $state;
254 sub print_usage () {
255 print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>]\n";
256 }
258 sub print_help () {
259 print_revision($PROGNAME,'$Revision$');
260 print "Copyright (c) 2000 Bo Kersey/Karl DeBisschop\n";
261 print "\n";
262 print_usage();
263 print "\n";
264 print "<warn> = Clock offset in seconds at which a warning message will be generated.\n Defaults to 60.\n";
265 print "<crit> = Clock offset in seconds at which a critical message will be generated.\n Defaults to 120.\n\n";
266 support();
267 }