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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2185 by ronalds, Fri Feb 4 11:50:12 2011 UTC revision 2186 by ronalds, Thu Feb 10 11:43:36 2011 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use strict;  use strict;
4  use Getopt::Long;  use Getopt::Long;
5    
6  my $verbose=0;  my $verbose=0;
7  my $help;  my $help;
 my $reftime;  
8  my $delta = 15;  my $delta = 15;
9    my @ntpservers = ();
10  my $debug;  my $debug;
11    
12  GetOptions("help" => \$help,  GetOptions("help" => \$help,
            "reference-time|ref-time|r:i" => \$reftime,  
13             "delta|D:i" => \$delta,             "delta|D:i" => \$delta,
14             "debug" => \$debug,             "debug" => \$debug,
15               "ntp:s" => \@ntpservers,
16             "verbose" => \$verbose );             "verbose" => \$verbose );
17    
18  my %ERRORS=(OK=>0,  my %ERRORS=(OK=>0,
# Line 23  my %ERRORS=(OK=>0, Line 23  my %ERRORS=(OK=>0,
23    
24  my $result = $ERRORS{"UNKNOWN"};  my $result = $ERRORS{"UNKNOWN"};
25    
26  if ( ! defined( $reftime ) ) {  # sanity check of variable ntpservers
27      print "UNKNOWN: No reference time provided\n";  if ( scalar @ntpservers == 0 ) {
28      exit $result;      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  # compare reference time (provided via parameter) with the time on the host  # determine the offset of the clock at the host with one or more NTP servers
46  #  #
47    
48  # get current time  my $offset = 0;
49  my $now = time;  
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] current time at host = $now\n";  $debug and print STDERR "[DEBUG] NTP offset = $offset\n";
73    
74  my $message;  my $message;
75    
76  # try to get positive results for offset  # Only publish positive values for offset
 # now is likely to be larger than reftime because it is determined later  
 # reftime at Nagios server before calling this script, now in this script  
 my $offset = $now - $reftime;  
77  my $perfdata = "'Time offset'=" . abs(${offset}) . "s;${delta};;;";  my $perfdata = "'Time offset'=" . abs(${offset}) . "s;${delta};;;";
78    
79  if ( abs( $offset ) < $delta ) {  if ( abs( $offset ) < $delta ) {

Legend:
Removed from v.2185  
changed lines
  Added in v.2186

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