Code

9a58d78ab3fb9f8bcd0eb1ed523dfc285cf03387
[nagiosplug.git] / plugins-scripts / check_ntp.pl
1 #! /usr/bin/perl -wT
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 
51 #
54 require 5.004;
55 use POSIX;
56 use strict;
57 use Getopt::Long;
58 use vars qw($opt_V $opt_h $opt_H $opt_w $opt_c $verbose $PROGNAME);
59 use lib utils.pm ;
60 use utils qw($TIMEOUT %ERRORS &print_revision &support);
62 $PROGNAME="check_ntp";
64 sub print_help ();
65 sub print_usage ();
67 $ENV{'PATH'}='';
68 $ENV{'BASH_ENV'}='';
69 $ENV{'ENV'}='';
71 Getopt::Long::Configure('bundling');
72 GetOptions
73         ("V"   => \$opt_V, "version"    => \$opt_V,
74          "h"   => \$opt_h, "help"       => \$opt_h,
75          "v" => \$verbose, "verbose"  => \$verbose,
76          "w=s" => \$opt_w, "warning=s"  => \$opt_w,
77          "c=s" => \$opt_c, "critical=s" => \$opt_c,
78          "H=s" => \$opt_H, "hostname=s" => \$opt_H);
80 if ($opt_V) {
81         print_revision($PROGNAME,'$Revision$ ');
82         exit $ERRORS{'OK'};
83 }
85 if ($opt_h) {
86         print_help();
87         exit $ERRORS{'OK'};
88 }
90 $opt_H = shift unless ($opt_H);
91 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]+)*)$/);
92 unless ($host) {
93         print_usage();
94         exit $ERRORS{'UNKNOWN'};
95 }
97 ($opt_w) || ($opt_w = shift) || ($opt_w = 60);
98 my $warning = $1 if ($opt_w =~ /([0-9]+)/);
100 ($opt_c) || ($opt_c = shift) || ($opt_c = 120);
101 my $critical = $1 if ($opt_c =~ /([0-9]+)/);
103 my $answer = undef;
104 my $offset = undef;
105 my $msg; # first line of output to print if format is invalid
107 my $state = $ERRORS{'UNKNOWN'};
108 my $ntpdate_error = $ERRORS{'UNKNOWN'};
109 my $dispersion_error = $ERRORS{'UNKNOWN'};
111 my $key = undef;
113 # Just in case of problems, let's not hang Nagios
114 $SIG{'ALRM'} = sub {
115         print ("ERROR: No response from ntp server (alarm)\n");
116         exit $ERRORS{"UNKNOWN"};
117 };
118 alarm($TIMEOUT);
121 ###
122 ###
123 ### First, check ntpdate
124 ###
125 ###
127 if (!open (NTPDATE, "/usr/local/sbin/ntpdate -q $host 2>&1 |")) {
128         print "Could not open ntpdate\n";
129         exit $ERRORS{"UNKNOWN"};
132 while (<NTPDATE>) {
133         print if ($verbose);
134         $msg = $_ unless ($msg);
135         if (/(offset|adjust)\s+([-.\d]+)/i) {
136                 $offset = $2;
137                 last;
138         }
141 # soak up remaining output; check for error
142 while (<NTPDATE>) {
143         if (/no server suitable for synchronization found/) {
144                 $ntpdate_error = $ERRORS{"CRITICAL"};
145         }
148 close(NTPDATE);
150 # only declare an error if we also get a non-zero return code from ntpdate
151 $ntpdate_error = ($? >> 8) || $ntpdate_error;
153 ###
154 ###
155 ### Then scan xntpdc if it exists
156 ###
157 ###
159 if (#open(NTPDC,"/usr/sbin/ntpdc -c $host 2>&1 |") ||
160     open(NTPDC,"/usr/sbin/xntpdc -c $host 2>&1 |") ) {
161         while (<NTPDC>) {
162                 print if ($verbose);
163                 if (/([^\s]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
164                         if ($8>15) {
165                                 $dispersion_error = $ERRORS{'CRITICAL'};
166                         } elsif ($8>5 && $dispersion_error<$ERRORS{'CRITICAL'}) {
167                                 $dispersion_error = $ERRORS{'WARNING'};
168                         }
169                 }
170         }
171         close NTPDC;
174 # An offset of 0.000000 with an error is probably bogus. Actually,
175 # it's probably always bogus, but let's be paranoid here.
176 if ($ntpdate_error && $offset && ($offset == 0)) { undef $offset;}
178 if ($ntpdate_error > $ERRORS{'OK'}) {
179         $state = $ntpdate_error;
180         $answer = "Server for ntp probably down\n";
181         if (defined($offset) && abs($offset) > $critical) {
182                 $state = $ERRORS{'CRITICAL'};
183                 $answer = "Server Error and time difference $offset seconds greater than +/- $critical sec\n";
184         } elsif (defined($offset) && abs($offset) > $warning) {
185                 $answer = "Server error and time difference $offset seconds greater than +/- $warning sec\n";
186         }
188 } elsif ($dispersion_error > $ERRORS{'OK'}) {
189         $state = $dispersion_error;
190         $answer = "Dispersion too high\n";
191         if (defined($offset) && abs($offset) > $critical) {
192                 $state = $ERRORS{'CRITICAL'};
193                 $answer = "Dispersion error and time difference $offset seconds greater than +/- $critical sec\n";
194         } elsif (defined($offset) && abs($offset) > $warning) {
195                 $answer = "Dispersion error and time difference $offset seconds greater than +/- $warning sec\n";
196         }
198 } else { # no errors from ntpdate or xntpdc
199         if (defined $offset) {
200                 if (abs($offset) > $critical) {
201                         $state = $ERRORS{'CRITICAL'};
202                         $answer = "Time difference $offset seconds greater than +/- $critical sec\n";
203                 } elsif (abs($offset) > $warning) {
204                         $state = $ERRORS{'WARNING'};
205                         $answer = "Time difference $offset seconds greater than +/- $warning sec\n";
206                 } elsif (abs($offset) <= $warning) {
207                         $state = $ERRORS{'OK'};
208                         $answer = "Time difference $offset seconds\n";
209                 }
210         } else { # no offset defined
211                 $state = $ERRORS{'UNKNOWN'};
212                 $answer = "Invalid format returned from ntpdate ($msg)\n";
213         }
216 foreach $key (keys %ERRORS) {
217         if ($state==$ERRORS{$key}) {
218                 print ("$key: $answer");
219                 last;
220         }
222 exit $state;
224 sub print_usage () {
225         print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>]\n";
228 sub print_help () {
229         print_revision($PROGNAME,'$Revision$');
230         print "Copyright (c) 2000 Bo Kersey/Karl DeBisschop\n";
231         print "\n";
232         print_usage();
233         print "\n";
234         print "<warn> = Clock offset in seconds at which a warning message will be generated.\n Defaults to 60.\n";
235         print "<crit> = Clock offset in seconds at which a critical message will be generated.\n        Defaults to 120.\n\n";
236         support();