68acdf65e34c7f3f85f649fdc43b66e03aeebb63
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 if ($utils::PATH_TO_NTPDC && -x $utils::PATH_TO_NTPDC ) {
124 my $have_ntpdc = 1;
125 }else{
126 my $have_ntpdc = 0;
127 }
129 # Just in case of problems, let's not hang Nagios
130 $SIG{'ALRM'} = sub {
131 print ("ERROR: No response from ntp server (alarm)\n");
132 exit $ERRORS{"UNKNOWN"};
133 };
134 alarm($TIMEOUT);
137 ###
138 ###$dispersion_error = $ERRORS{'
139 ### First, check ntpdate
140 ###
141 ###
143 if (!open (NTPDATE, "$utils::PATH_TO_NTPDATE -q $host 2>&1 |")) {
144 print "Could not open ntpdate\n";
145 exit $ERRORS{"UNKNOWN"};
146 }
148 while (<NTPDATE>) {
149 print if ($verbose);
150 $msg = $_ unless ($msg);
151 if (/(offset|adjust)\s+([-.\d]+)/i) {
152 $offset = $2;
154 # An offset of 0.000000 with an error is probably bogus. Actually,
155 # it's probably always bogus, but let's be paranoid here.
156 if ($offset == 0) { undef $offset;}
158 $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
159 print "ntperr = $ntpdate_error \n" if $verbose;
161 }
163 if (/no server suitable for synchronization found/) {
164 $ntpdate_error = $ERRORS{"CRITICAL"};
165 $msg = "No suitable peer server found - ";
166 }
168 }
170 close (NTPDATE);
171 # declare an error if we also get a non-zero return code from ntpdate
172 # unless already set to critical
173 if ( $? ) {
174 print "stderr = $? : $! \n" if $verbose;
175 $ntpdate_error = $ntpdate_error == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"} ;
176 print "ntperr = $ntpdate_error : $!\n" if $verbose;
177 }
179 ###
180 ###
181 ### Then scan xntpdc/ntpdc if it exists
182 ### and look in the 8th column for dispersion (ntpd v4) or jitter (ntpd v3)
183 ###
185 if ($have_ntpdc) {
187 if ( open(NTPDC,"$utils::PATH_TO_NTPDC -s $host 2>&1 |") ) {
188 while (<NTPDC>) {
189 print $_ if ($verbose);
190 if (/([^\s]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
191 if ($8>15) {
192 print "Dispersion = $8 \n" if ($verbose);
193 $dispersion_error = $ERRORS{'CRITICAL'};
194 } elsif ($8>5 && $dispersion_error<$ERRORS{'CRITICAL'}) {
195 print "Dispersion = $8 \n" if ($verbose);
196 $dispersion_error = $ERRORS{'WARNING'};
197 } else {
198 $dispersion_error = $ERRORS{'OK'};
199 }
200 }
201 }
202 close NTPDC;
203 }
204 }
207 if ($ntpdate_error != $ERRORS{'OK'}) {
208 $state = $ntpdate_error;
209 $answer = $msg . "Server for ntp probably down\n";
210 if (defined($offset) && abs($offset) > $critical) {
211 $state = $ERRORS{'CRITICAL'};
212 $answer = "Server Error and time difference $offset seconds greater than +/- $critical sec\n";
213 } elsif (defined($offset) && abs($offset) > $warning) {
214 $answer = "Server error and time difference $offset seconds greater than +/- $warning sec\n";
215 }
217 } elsif ($have_ntpdc && $dispersion_error != $ERRORS{'OK'}) {
218 $state = $dispersion_error;
219 $answer = "Dispersion too high\n";
220 if (defined($offset) && abs($offset) > $critical) {
221 $state = $ERRORS{'CRITICAL'};
222 $answer = "Dispersion error and time difference $offset seconds greater than +/- $critical sec\n";
223 } elsif (defined($offset) && abs($offset) > $warning) {
224 $answer = "Dispersion error and time difference $offset seconds greater than +/- $warning sec\n";
225 }
227 } else { # no errors from ntpdate or xntpdc
228 if (defined $offset) {
229 if (abs($offset) > $critical) {
230 $state = $ERRORS{'CRITICAL'};
231 $answer = "Time difference $offset seconds greater than +/- $critical sec\n";
232 } elsif (abs($offset) > $warning) {
233 $state = $ERRORS{'WARNING'};
234 $answer = "Time difference $offset seconds greater than +/- $warning sec\n";
235 } elsif (abs($offset) <= $warning) {
236 $state = $ERRORS{'OK'};
237 $answer = "Time difference $offset seconds\n";
238 }
239 } else { # no offset defined
240 $state = $ERRORS{'UNKNOWN'};
241 $answer = "Invalid format returned from ntpdate ($msg)\n";
242 }
243 }
245 foreach $key (keys %ERRORS) {
246 if ($state==$ERRORS{$key}) {
247 print ("$key: $answer");
248 last;
249 }
250 }
251 exit $state;
253 sub print_usage () {
254 print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>]\n";
255 }
257 sub print_help () {
258 print_revision($PROGNAME,'$Revision$');
259 print "Copyright (c) 2000 Bo Kersey/Karl DeBisschop\n";
260 print "\n";
261 print_usage();
262 print "\n";
263 print "<warn> = Clock offset in seconds at which a warning message will be generated.\n Defaults to 60.\n";
264 print "<crit> = Clock offset in seconds at which a critical message will be generated.\n Defaults to 120.\n\n";
265 support();
266 }