Code

logic reorg, ePN fix and support for utils.pm
[nagiosplug.git] / plugins-scripts / check_ntp.pl
1 #! /usr/bin/perl -w
3 # (c)1999 Ian Cass, Knowledge Matters Ltd.
4 # Read the GNU copyright stuff for all the legalese
5 #
6 # Check NTP time servers plugin. This plugin requires the ntpdate utility to
7 # be installed on the system, however since it's part of the ntp suite, you 
8 # should already have it installed.
9 #
10 # $Id$
11
12 # Nothing clever done in this program - its a very simple bare basics hack to
13 # get the job done.
14 #
15 # Things to do...
16 # check @words[9] for time differences greater than +/- x secs & return a
17 # warning.
18 #
19 # (c) 1999 Mark Jewiss, Knowledge Matters Limited
20 # 22-9-1999, 12:45
21 #
22 # Modified script to accept 2 parameters or set defaults.
23 # Now issues warning or critical alert is time difference is greater than the 
24 # time passed.
25 #
26 # These changes have not been tested completely due to the unavailability of a
27 # server with the incorrect time.
28 #
29 # (c) 1999 Bo Kersey, VirCIO - Managed Server Solutions <bo@vircio.com>
30 # 22-10-99, 12:17
31 #
32 # Modified the script to give useage if no parameters are input.
33 #
34 # Modified the script to check for negative as well as positive 
35 # time differences.
36 #
37 # Modified the script to work with ntpdate 3-5.93e Wed Apr 14 20:23:03 EDT 1999
38 #
39 # Modified the script to work with ntpdate's that return adjust or offset...
40 #
41 #
42 # Script modified 2000 June 01 by William Pietri <william@bianca.com>
43 #
44 # Modified script to handle weird cases:
45 #     o NTP server doesn't respond (e.g., has died)
46 #     o Server has correct time but isn't suitable synchronization
47 #           source. This happens while starting up and if contact
48 #           with master has been lost.
49 #
50 # Modifed to run under Embedded Perl  (sghosh@users.sf.net)
51 #   - combined logic some blocks together..
52
53 # Todo - non-hardcoded dispersion values...
54 #
57 require 5.004;
58 use POSIX;
59 use strict;
60 use Getopt::Long;
61 use vars qw($opt_V $opt_h $opt_H $opt_w $opt_c $verbose $PROGNAME);
62 use lib utils.pm ;
63 use utils qw($TIMEOUT %ERRORS &print_revision &support);
65 $PROGNAME="check_ntp";
67 sub print_help ();
68 sub print_usage ();
70 $ENV{'PATH'}='';
71 $ENV{'BASH_ENV'}='';
72 $ENV{'ENV'}='';
74 Getopt::Long::Configure('bundling');
75 GetOptions
76         ("V"   => \$opt_V, "version"    => \$opt_V,
77          "h"   => \$opt_h, "help"       => \$opt_h,
78          "v" => \$verbose, "verbose"  => \$verbose,
79          "w=s" => \$opt_w, "warning=s"  => \$opt_w,   # offset|adjust warning if above this number
80          "c=s" => \$opt_c, "critical=s" => \$opt_c,   # offset|adjust critical if above this number
81          "H=s" => \$opt_H, "hostname=s" => \$opt_H);
83 if ($opt_V) {
84         print_revision($PROGNAME,'$Revision$ ');
85         exit $ERRORS{'OK'};
86 }
88 if ($opt_h) {
89         print_help();
90         exit $ERRORS{'OK'};
91 }
93 $opt_H = shift unless ($opt_H);
94 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]+)*)$/);
95 unless ($host) {
96         print "No target host specified\n";
97         print_usage();
98         exit $ERRORS{'UNKNOWN'};
99 }
101 ($opt_w) || ($opt_w = shift) || ($opt_w = 60);
102 my $warning = $1 if ($opt_w =~ /([0-9]+)/);
104 ($opt_c) || ($opt_c = shift) || ($opt_c = 120);
105 my $critical = $1 if ($opt_c =~ /([0-9]+)/);
107 if ($opt_c < $opt_w) {
108         print "Critical offset should be larger than warning offset\n";
109         print_usage();
110         exit $ERRORS{"UNKNOWN"};
113 my $answer = undef;
114 my $offset = undef;
115 my $msg; # first line of output to print if format is invalid
117 my $state = $ERRORS{'UNKNOWN'};
118 my $ntpdate_error = $ERRORS{'UNKNOWN'};
119 my $dispersion_error = $ERRORS{'UNKNOWN'};
121 my $key = undef;
123 # Just in case of problems, let's not hang Nagios
124 $SIG{'ALRM'} = sub {
125         print ("ERROR: No response from ntp server (alarm)\n");
126         exit $ERRORS{"UNKNOWN"};
127 };
128 #alarm($TIMEOUT);
131 ###
132 ###$dispersion_error = $ERRORS{'
133 ### First, check ntpdate
134 ###
135 ###
137 if (!open (NTPDATE, "$utils::PATH_TO_NTPDATE -q $host 2>&1 |")) {
138         print "Could not open ntpdate\n";
139         exit $ERRORS{"UNKNOWN"};
142 while (<NTPDATE>) {
143         print if ($verbose);
144         $msg = $_ unless ($msg);
145         if (/(offset|adjust)\s+([-.\d]+)/i) {
146                 $offset = $2;
148                 # An offset of 0.000000 with an error is probably bogus. Actually,
149                 # it's probably always bogus, but let's be paranoid here.
150                 if ($offset == 0) { undef $offset;}
152                 $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
153                 print "ntperr = $ntpdate_error \n" if $verbose;
154         
155         }
157         if (/no server suitable for synchronization found/) {
158                 $ntpdate_error = $ERRORS{"CRITICAL"};
159                 $msg = "No suitable peer server found - ";
160         }
164 close (NTPDATE); 
165 # declare an error if we also get a non-zero return code from ntpdate
166 # unless already set to critical
167 if ( $? ) {
168         print "stderr = $? : $! \n" if $verbose;
169         $ntpdate_error = $ntpdate_error == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"}  ;
170         print "ntperr = $ntpdate_error : $!\n" if $verbose;
173 ###
174 ###
175 ### Then scan xntpdc/ntpdc if it exists
176 ### and look in the 8th column for dispersion (ntpd v4) or jitter (ntpd v3)
177 ###
179 if ( open(NTPDC,"$utils::PATH_TO_NTPDC -s $host 2>&1 |") ) {
180         while (<NTPDC>) {
181                 print $_ if ($verbose);
182                 if (/([^\s]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
183                         if ($8>15) {
184                                 print "Dispersion = $8 \n" if ($verbose);
185                                 $dispersion_error = $ERRORS{'CRITICAL'};
186                         } elsif ($8>5 && $dispersion_error<$ERRORS{'CRITICAL'}) {
187                                 print "Dispersion = $8 \n" if ($verbose);
188                                 $dispersion_error = $ERRORS{'WARNING'};
189                         } else {
190                                 $dispersion_error = $ERRORS{'OK'};
191                         }
192                 }
193         }
194         close NTPDC;
198 if ($ntpdate_error != $ERRORS{'OK'}) {
199         $state = $ntpdate_error;
200         $answer = $msg . "Server for ntp probably down\n";
201         if (defined($offset) && abs($offset) > $critical) {
202                 $state = $ERRORS{'CRITICAL'};
203                 $answer = "Server Error and time difference $offset seconds greater than +/- $critical sec\n";
204         } elsif (defined($offset) && abs($offset) > $warning) {
205                 $answer = "Server error and time difference $offset seconds greater than +/- $warning sec\n";
206         }
208 } elsif ($dispersion_error != $ERRORS{'OK'}) {
209         $state = $dispersion_error;
210         $answer = "Dispersion too high\n";
211         if (defined($offset) && abs($offset) > $critical) {
212                 $state = $ERRORS{'CRITICAL'};
213                 $answer = "Dispersion error and time difference $offset seconds greater than +/- $critical sec\n";
214         } elsif (defined($offset) && abs($offset) > $warning) {
215                 $answer = "Dispersion error and time difference $offset seconds greater than +/- $warning sec\n";
216         }
218 } else { # no errors from ntpdate or xntpdc
219         if (defined $offset) {
220                 if (abs($offset) > $critical) {
221                         $state = $ERRORS{'CRITICAL'};
222                         $answer = "Time difference $offset seconds greater than +/- $critical sec\n";
223                 } elsif (abs($offset) > $warning) {
224                         $state = $ERRORS{'WARNING'};
225                         $answer = "Time difference $offset seconds greater than +/- $warning sec\n";
226                 } elsif (abs($offset) <= $warning) {
227                         $state = $ERRORS{'OK'};
228                         $answer = "Time difference $offset seconds\n";
229                 }
230         } else { # no offset defined
231                 $state = $ERRORS{'UNKNOWN'};
232                 $answer = "Invalid format returned from ntpdate ($msg)\n";
233         }
236 foreach $key (keys %ERRORS) {
237         if ($state==$ERRORS{$key}) {
238                 print ("$key: $answer");
239                 last;
240         }
242 exit $state;
244 sub print_usage () {
245         print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>]\n";
248 sub print_help () {
249         print_revision($PROGNAME,'$Revision$');
250         print "Copyright (c) 2000 Bo Kersey/Karl DeBisschop\n";
251         print "\n";
252         print_usage();
253         print "\n";
254         print "<warn> = Clock offset in seconds at which a warning message will be generated.\n Defaults to 60.\n";
255         print "<crit> = Clock offset in seconds at which a critical message will be generated.\n        Defaults to 120.\n\n";
256         support();