/[pdpsoft]/trunk/nl.nikhef.ndpf.tools/nagios-ndpf-sensors/scripts/check_time_offset
ViewVC logotype

Contents of /trunk/nl.nikhef.ndpf.tools/nagios-ndpf-sensors/scripts/check_time_offset

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2186 - (show annotations) (download)
Thu Feb 10 11:43:36 2011 UTC (10 years, 11 months ago) by ronalds
File size: 2291 byte(s)
0.3.14: modified check_time_offset so that every node queries one or more NTP servers via either sntp or ntpdate. Both commands return the offset directly

1 #!/usr/bin/perl -w
2
3 use strict;
4 use Getopt::Long;
5
6 my $verbose=0;
7 my $help;
8 my $delta = 15;
9 my @ntpservers = ();
10 my $debug;
11
12 GetOptions("help" => \$help,
13 "delta|D:i" => \$delta,
14 "debug" => \$debug,
15 "ntp:s" => \@ntpservers,
16 "verbose" => \$verbose );
17
18 my %ERRORS=(OK=>0,
19 WARNING=>1,
20 CRITICAL=>2,
21 UNKNOWN=>3,
22 DEPENDENT=>4);
23
24 my $result = $ERRORS{"UNKNOWN"};
25
26 # sanity check of variable ntpservers
27 if ( scalar @ntpservers == 0 ) {
28 print "UNKNOWN: no list of NTP servers found\n";
29 exit $ERRORS{"UNKNOWN"};
30 }
31 my $ntpserverlist = "";
32 foreach my $ntp ( @ntpservers ) {
33 # validate hostname
34 if ( $ntp !~ /^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/ ) {
35 print "UNKNOWN: invalid NTP hostname found: $ntp\n";
36 exit $ERRORS{"UNKNOWN"};
37 }
38 $ntpserverlist .= " $ntp";
39 }
40 $debug and print STDERR "[DEBUG] list of NTP servers = $ntpserverlist\n";
41
42
43
44 ##############################################################################
45 # determine the offset of the clock at the host with one or more NTP servers
46 #
47
48 my $offset = 0;
49
50 # get time offset from NTP server(s)
51 # but first determine the command to use
52 if ( -x "/usr/sbin/sntp" ) {
53 my $ntpcmd = "/usr/sbin/sntp $ntpserverlist";
54 my $ntpresult = `$ntpcmd | tail -1`;
55 if ( $? == 0 and $ntpresult =~ /([+-]) (\d+\.\d+) \+\/\-/ ) {
56 $offset = ( $1 eq "-" ? -1*($2) : ($2) );
57 }
58 }
59 elsif ( -x "/usr/sbin/ntpdate" ) {
60 my $ntpcmd = "/usr/sbin/ntpdate -u -q $ntpserverlist";
61 my $ntpresult = `$ntpcmd | tail -1`;
62 if ( $? == 0 and $ntpresult =~ /offset (.*) sec$/ ) {
63 $offset = $1;
64 }
65 }
66 else {
67 print "UNKNOWN: cannot find client command to compare time offset\n";
68 exit $ERRORS{"UNKNOWN"};
69 }
70
71
72 $debug and print STDERR "[DEBUG] NTP offset = $offset\n";
73
74 my $message;
75
76 # Only publish positive values for offset
77 my $perfdata = "'Time offset'=" . abs(${offset}) . "s;${delta};;;";
78
79 if ( abs( $offset ) < $delta ) {
80 $message = "OK: time offset is ${offset}s";
81 $result = $ERRORS{"OK"};
82 }
83 else {
84 $message = "WARNING: time offset is ${offset}s ";
85 $result = $ERRORS{"WARNING"};
86 }
87
88 print "$message|$perfdata\n";
89 exit($result);

Properties

Name Value
svn:executable *
svn:keywords id

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28