/[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 2187 - (show annotations) (download)
Thu Feb 10 11:52:18 2011 UTC (11 years, 4 months ago) by ronalds
File size: 2424 byte(s)
Improved argument handling for --ntp in check_time_offset

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 # permit mulitple occurrences of --ntp or a comma separated argument lists to --ntp
32 @ntpservers = split(/,/,join(',',@ntpservers));
33 my $ntpserverlist = "";
34 foreach my $ntp ( @ntpservers ) {
35 # validate hostname
36 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])$/ ) {
37 print "UNKNOWN: invalid NTP hostname found: $ntp\n";
38 exit $ERRORS{"UNKNOWN"};
39 }
40 $ntpserverlist .= " $ntp";
41 }
42 $debug and print STDERR "[DEBUG] list of NTP servers = $ntpserverlist\n";
43
44
45
46 ##############################################################################
47 # determine the offset of the clock at the host with one or more NTP servers
48 #
49
50 my $offset = 0;
51
52 # get time offset from NTP server(s)
53 # but first determine the command to use
54 if ( -x "/usr/sbin/sntp" ) {
55 my $ntpcmd = "/usr/sbin/sntp $ntpserverlist";
56 my $ntpresult = `$ntpcmd | tail -1`;
57 if ( $? == 0 and $ntpresult =~ /([+-]) (\d+\.\d+) \+\/\-/ ) {
58 $offset = ( $1 eq "-" ? -1*($2) : ($2) );
59 }
60 }
61 elsif ( -x "/usr/sbin/ntpdate" ) {
62 my $ntpcmd = "/usr/sbin/ntpdate -u -q $ntpserverlist";
63 my $ntpresult = `$ntpcmd | tail -1`;
64 if ( $? == 0 and $ntpresult =~ /offset (.*) sec$/ ) {
65 $offset = $1;
66 }
67 }
68 else {
69 print "UNKNOWN: cannot find client command to compare time offset\n";
70 exit $ERRORS{"UNKNOWN"};
71 }
72
73
74 $debug and print STDERR "[DEBUG] NTP offset = $offset\n";
75
76 my $message;
77
78 # Only publish positive values for offset
79 my $perfdata = "'Time offset'=" . abs(${offset}) . "s;${delta};;;";
80
81 if ( abs( $offset ) < $delta ) {
82 $message = "OK: time offset is ${offset}s";
83 $result = $ERRORS{"OK"};
84 }
85 else {
86 $message = "WARNING: time offset is ${offset}s ";
87 $result = $ERRORS{"WARNING"};
88 }
89
90 print "$message|$perfdata\n";
91 exit($result);

Properties

Name Value
svn:executable *
svn:keywords id

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