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