File: ntpdate.monitor

package info (click to toggle)
mon 1.2.0-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,860 kB
  • ctags: 538
  • sloc: perl: 15,847; ansic: 774; sh: 330; makefile: 46
file content (415 lines) | stat: -rwxr-xr-x 10,787 bytes parent folder | download | duplicates (9)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/perl
#
# ntpdate.monitor Verify that NTP is running and times are within tolerance
# ntpdate will do most of the work for us
#

=head1 NAME

B<ntpdate.monitor> - ntp monitor using ntpdate to do most of the work

=head1 DESCRIPTION

A mon monitor to verify that ntp is running on multiple servers, those
servers have synchronized time, and that the times are within
specified limits.

The mon server should be running ntp since the times are reported
relative to the system performing the query.

=head1 SYNOPSIS

B<ntpdate.monitor -d -l log_file_YYYYMM.log --maxstratum nn --maxoffset n.nn>

=head1 OPTIONS

=over 5

=item B<--maxstratum> Maximum stratum number, default is 10. Stratum
16 indicates that ntp is running on a system, but the clock is not
synchronized. An alarm will be triggered if this value is exceeded.

=item B<--maxoffset> Maximum value of the clock offset in seconds,
default is 800 ms (a large value, ntp typically keeps clocks within
milliseconds of each other).  An alarm will be triggered if this value
is exceeded.

=item B<-l log_file_template> or B<--log log_file_template>

/path/to/logs/internet_web_YYYYMM.log Current year & month are substituted
for YYYYMM, that is the only possible template at this time. The format of the
log file is:

 time server stratum offset delay

time is in UNIX seconds, offset, and delay are in seconds. Note that
offset and delay times in mon detail, and the optional HTML page are
in milliseconds.

=item B<-shortalerts>

Use only hostname in alert list. For organizations with long FQDNs
this will make mail and pager alerts more readable.

=item B<--htmlfile /full/path/to/file.html>

Optional location to write the formated results from the current
test. Be sure that the directory is writeable by the user under whom
mon is running.

=item B<-d> or B<--debug>

Debug/Test/Verbose, for manual testing only.

=item B<--ntpdate>

Specify the location of ntpdate, the default is /usr/sbin/ntpdate

=back

=head1 MON CONFIGURATION EXAMPLE

 hostgroup ntp ntp1.somedomain.org ntp2.somedomain.org ntp3.somedomain.org

 watch ntp
   service ntpdate
   interval 30m
   monitor ntpdate.monitor --maxoffset 0.100 --log /usr/local/mon/logs/gv-ntp-YYYYMM.log
      period wd {Sun-Sat}
      alert mail.alert user@somedomain.org
      alertevery 1h summary

=head1 BUGS

Listing a server twice can cause ntpdate to report that server as
Stratum 0. This can happen even if an alias name is used.

The shortalerts option only reports the hostname, it could be extended
to provide a configurable number of FQND fields.

ntpdate will be removed from the NTP distribution at some point. This
monitor will need to be modified to use some form of ntpd -q instead.

Check the first line of this file to be sure that it points to an
appropriate perl executable.

=head1 AUTHOR

Jon Meek, meekj at ieee.org

=head1 SEE ALSO

ntp.monitor by  Daniel Hagerty <hag at linnaean.org>

=cut

$RCSid = q{$Id: ntpdate.monitor,v 1.3.2.1 2007/06/03 13:14:27 trockij Exp $ };

#
# Jon Meek
# Lawrenceville, NJ
# meekj at ieee.org
#
#
# $Id: ntpdate.monitor,v 1.3.2.1 2007/06/03 13:14:27 trockij Exp $
#
#    Copyright (C) 2002-2006, Jon Meek
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

use Getopt::Long;
GetOptions(
	   "maxstratum=i" => \$MaxStratum,
	   "maxoffset=f" => \$MaxOffset,
#	   "dns" => \$UseDNS,
	   "d|debug" => \$Debug,
	   "l=s" => \$LogFile,
	   "log=s" => \$LogFile,
	   "htmlfile=s" => \$HtmlFile,
	   "shortalerts" => \$ShortAlerts,
	   "ntpdate=s" => \$NTPDATE,
	  );

use Net::DNS;
use Sys::Hostname;
use POSIX qw(strftime);

#
# Set Defaults
#
# ntpdate reports stratum 16 if ntp is running, but time is not synchronized
#                 stratum 0 will be reported if ntp is not running
#
$MaxStratum = 10 unless $MaxStratum;
$MinStratum = 1;

# Use the first occurrence of this stratum as the reference time for alarms
$ReferenceStratum = 1 unless $ReferenceStratum;

#
# Trigger alarm if the time is ever off by this much
#
$MaxOffset  = 0.800 unless $MaxOffset; # seconds
$NTPDATE = '/usr/sbin/ntpdate' unless $NTPDATE;

$HtmlFileHandle = &HTMLheader($HtmlFile) if $HtmlFile;

@Failures = ();
@Hosts = @ARGV; # Host names are left on the command line after Getopt

%NameByIP = &DNSlookups(\@Hosts);

$TimeOfDay = time;       # Current time
print "TimeOfDay: $TimeOfDay\n" if $Debug;

$cmd = qq{$NTPDATE -q @Hosts |};

$pid = open(NTP, $cmd) || die "Couldn't run $cmd: $!\n";

while ($in = <NTP>) {
  # print $in if $Debug;
  chomp $in;
  #
  # Pick out server strings
  #
  if ($in =~ /^server\s+([\d\.]+),\s+stratum\s+(\d+),\s+offset\s+([\d\.\-\+]+),\s+delay\s+([\d\.\-\+]+)/) {
    $ip = $1;
    $stratum = $2;
    $offset = $3;
    $delay = $4;
    $name = $NameByIP{$ip};
    print "$in   Name: $name  Stratum: $stratum\n" if $Debug;

    if (exists $NameByIP{$ip}) { # Use system name if we have it
      $HostName = $NameByIP{$ip};
    } else {
      $HostName = $ip;           # Otherwise use IP address
    }

    $IP{$HostName}      = $ip;
    $Stratum{$HostName} = $stratum;
    $Offset{$HostName}  = $offset;
    $Delay{$HostName}   = $delay;
    $Detail{$HostName}  = $in;

    if ((!defined $ReferenceOffset) && ($stratum == 1)) { # Save offset from first stratum 1 server seen
      $ReferenceOffset = $offset;
    }

    #
    # Prepare log entries
    #
    if ($LogFile or $Debug) {
      $LogString{$HostName} = qq{$TimeOfDay $HostName $stratum $offset $delay};
    }
  }
}

#1234567890123456789012345678901234567890123456789012345678901234567890
#fwmon-gv.gv.us.pri.wyeth.com             0.276   2      0.413     61.050


#
# Build formatted results and check alarm limits
#
$FmtDetail = qq{NTP Server      times in milliseconds   Delta Stratum   Rel    Delay\n};

&HTMLtableHeader($HtmlFileHandle, 'NTP Server', 'Delta, ms', 'Stratum', 'Rel, ms', 'Delay, ms', 'Status') if $HtmlFile;

foreach $hostname (sort keys %Stratum) {
  $DeltaTime = $Offset{$hostname} - $ReferenceOffset;
  $DeltaTimeByHost{$hostname} = $DeltaTime;

  $msDeltaTime = 1000 * $DeltaTime;
  $msOffset = 1000 * $Offset{$hostname};
  $msDelay = 1000 * $Delay{$hostname};

  $FmtDetail .= sprintf ("%-35s %9.3f %3d %9.3f %9.3f",
    $hostname,  $msDeltaTime,  $Stratum{$hostname},  $msOffset,  $msDelay);

  $fail_string = '&nbsp;';
  if (($Stratum{$hostname} > $MaxStratum) || ($Stratum{$hostname} < $MinStratum)
      || (abs($DeltaTime) > $MaxOffset)) {
    $ip = $IP{$hostname};
    $FailureDetail{$hostname} = $Detail{$hostname};
    push(@Failures, $hostname);
    $FmtDetail .= q{  Fail};
    $fail_string = 'Fail';
  }
  $FmtDetail .= "\n";

  if ($HtmlFile) {
    $fDeltaTime = sprintf("%12.3f", $msDeltaTime);
    $fOffset = sprintf("%12.3f", $msOffset);
    $fDelay = sprintf("%12.3f", $msDelay);
    &HTMLtableRow($HtmlFileHandle, $hostname,  $fDeltaTime, $Stratum{$hostname}, $fOffset, $fDelay, $fail_string);
  }


}
print "\n$FmtDetail\n" if $Debug;

#
# Write results to logfile, if -l
#
if ($LogFile) {

  $LogFile = $LogFile;
  ($sec, $min, $hour, $mday, $Month, $Year, $wday, $yday, $isdst) =
    localtime($TimeOfDay);
  $Month++; $Year += 1900;
  $YYYYMM = sprintf('%04d%02d', $Year, $Month);
  $LogFile =~ s/YYYYMM/$YYYYMM/; # Fill in current year and month

  if (-e $LogFile) {		# Check for existing log file
    $NewLogFile = 0;
  } else {
    $NewLogFile = 1;
  }

  open(LOG, ">>$LogFile") || warn "$0 Can't open logfile: $LogFile\n";

  foreach $ip (sort keys %LogString) {
    print LOG "$LogString{$ip}\n";
  }

  close LOG;
}

if ($Debug) {
  foreach $ip (sort keys %LogString) {
    print "LOG: $LogString{$ip}\n";
  }
}

&HTMLtrailer($HtmlFileHandle) if $HtmlFile;

if (@Failures == 0) {		# Indicate "all OK" to mon
  exit 0;
}

#
# Otherwise we have one or more failures
#

if ($ShortAlerts) {
  foreach $host (sort @Failures) {
    if ($host =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IP address, don't shorten
      push(@SortedFailures, $host);
    } else {
      $host =~ /(.*?)\./;
      push(@SortedFailures, $1);
    }
  }
} else {
  @SortedFailures = sort @Failures;
}


print "------- Have Failures -------\n" if $Debug;

print "@SortedFailures\n";

print "------- Details -------\n" if $Debug;
print $FmtDetail;

#foreach $hostname (sort keys %FailureDetail) {
#    print "$NameByIP{$hostname} $hostname $FailureDetail{$hostname}    $DeltaTimeByHost{$hostname} s\n";
#}

exit 1; # Indicate failure to mon

#
# Get the IP addresses for the hosts (because ntpdate returns IP addresses)
#
sub DNSlookups {
  my ($Hosts) = @_;

  $res = new Net::DNS::Resolver;
  for (my $i = 0; $i < @$Hosts; $i++) {
    $target = $Hosts->[$i];
    $query = $res->search($target);

    if ($query) {
      foreach $rr ($query->answer) {
	#print "$target  Type: ", $rr->type, "\n" if $Debug;
	if ($rr->type eq "A") {
	  print $rr->address . ' ' if $Debug;
	  $NameByIP{$rr->address} = $target;
	}
      }
    }
  }
  return %NameByIP;
}

sub HTMLheader {
  #
  # Print basic standard header for this application
  #
    my($FileName) = @_;
    local *F;
    open(F, ">$FileName") || warn "$$ can't open $FileName, check permissions";

$Title = "NTP Server Status";
$MonitorHostname = hostname;
$FmtTimeNow = strftime("%A %d-%b-%Y  %H:%M:%S %Z", localtime(time));

print F <<"EndOfHeader";
<HTML>
<HEAD>
<TITLE>$Title</TITLE>
</HEAD>
<BODY bgcolor="#ffffff" text="#000000">
<H1>$Title from $MonitorHostname</H1>
<p>$FmtTimeNow</p>
<table border=2 cellpadding=3>
EndOfHeader

    return *F;
}


sub HTMLtableHeader {
 my($FileHandle, @Headers) = @_;
 print $FileHandle "<TR>\n";
 foreach $h (@Headers) {
   print $FileHandle "<TH>$h</TH>\n";
 }
 print $FileHandle "</TR>\n";
}

sub HTMLtableRow {
 my ($FileHandle, @Fields) = @_;
 my ($align, $f);
 $align = '';
 print $FileHandle "<TR>\n";
 foreach $f (@Fields) {
   print $FileHandle "<TD$align>$f</TD>\n";
   $align = ' align=right';
 }
 print $FileHandle "</TR>\n";
}

sub HTMLtrailer {
  #
  # Print basic standard trailer for this application
  #
    my($FileHandle) = @_;

    print $FileHandle "</table>\n</body>\n</html>\n";
    close $FileHandle;
}