check_ntp.pl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #! /usr/bin/perl -wT
  2. # (c)1999 Ian Cass, Knowledge Matters Ltd.
  3. # Read the GNU copyright stuff for all the legalese
  4. #
  5. # Check NTP time servers plugin. This plugin requires the ntpdate utility to
  6. # be installed on the system, however since it's part of the ntp suite, you
  7. # should already have it installed.
  8. #
  9. # $Id$
  10. #
  11. # Nothing clever done in this program - its a very simple bare basics hack to
  12. # get the job done.
  13. #
  14. # Things to do...
  15. # check @words[9] for time differences greater than +/- x secs & return a
  16. # warning.
  17. #
  18. # (c) 1999 Mark Jewiss, Knowledge Matters Limited
  19. # 22-9-1999, 12:45
  20. #
  21. # Modified script to accept 2 parameters or set defaults.
  22. # Now issues warning or critical alert is time difference is greater than the
  23. # time passed.
  24. #
  25. # These changes have not been tested completely due to the unavailability of a
  26. # server with the incorrect time.
  27. #
  28. # (c) 1999 Bo Kersey, VirCIO - Managed Server Solutions <bo@vircio.com>
  29. # 22-10-99, 12:17
  30. #
  31. # Modified the script to give useage if no parameters are input.
  32. #
  33. # Modified the script to check for negative as well as positive
  34. # time differences.
  35. #
  36. # Modified the script to work with ntpdate 3-5.93e Wed Apr 14 20:23:03 EDT 1999
  37. #
  38. # Modified the script to work with ntpdate's that return adjust or offset...
  39. #
  40. #
  41. # Script modified 2000 June 01 by William Pietri <william@bianca.com>
  42. #
  43. # Modified script to handle weird cases:
  44. # o NTP server doesn't respond (e.g., has died)
  45. # o Server has correct time but isn't suitable synchronization
  46. # source. This happens while starting up and if contact
  47. # with master has been lost.
  48. #
  49. # Modifed to run under Embedded Perl
  50. #
  51. require 5.004;
  52. use POSIX;
  53. use strict;
  54. use Getopt::Long;
  55. use vars qw($opt_V $opt_h $opt_H $opt_w $opt_c $verbose $PROGNAME);
  56. use lib utils.pm ;
  57. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  58. $PROGNAME="check_ntp";
  59. sub print_help ();
  60. sub print_usage ();
  61. $ENV{'PATH'}='';
  62. $ENV{'BASH_ENV'}='';
  63. $ENV{'ENV'}='';
  64. Getopt::Long::Configure('bundling');
  65. GetOptions
  66. ("V" => \$opt_V, "version" => \$opt_V,
  67. "h" => \$opt_h, "help" => \$opt_h,
  68. "v" => \$verbose, "verbose" => \$verbose,
  69. "w=s" => \$opt_w, "warning=s" => \$opt_w,
  70. "c=s" => \$opt_c, "critical=s" => \$opt_c,
  71. "H=s" => \$opt_H, "hostname=s" => \$opt_H);
  72. if ($opt_V) {
  73. print_revision($PROGNAME,'$Revision$ ');
  74. exit $ERRORS{'OK'};
  75. }
  76. if ($opt_h) {
  77. print_help();
  78. exit $ERRORS{'OK'};
  79. }
  80. $opt_H = shift unless ($opt_H);
  81. 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]+)*)$/);
  82. unless ($host) {
  83. print_usage();
  84. exit $ERRORS{'UNKNOWN'};
  85. }
  86. ($opt_w) || ($opt_w = shift) || ($opt_w = 60);
  87. my $warning = $1 if ($opt_w =~ /([0-9]+)/);
  88. ($opt_c) || ($opt_c = shift) || ($opt_c = 120);
  89. my $critical = $1 if ($opt_c =~ /([0-9]+)/);
  90. my $answer = undef;
  91. my $offset = undef;
  92. my $msg; # first line of output to print if format is invalid
  93. my $state = $ERRORS{'UNKNOWN'};
  94. my $ntpdate_error = $ERRORS{'UNKNOWN'};
  95. my $dispersion_error = $ERRORS{'UNKNOWN'};
  96. my $key = undef;
  97. # Just in case of problems, let's not hang Nagios
  98. $SIG{'ALRM'} = sub {
  99. print ("ERROR: No response from ntp server (alarm)\n");
  100. exit $ERRORS{"UNKNOWN"};
  101. };
  102. alarm($TIMEOUT);
  103. ###
  104. ###
  105. ### First, check ntpdate
  106. ###
  107. ###
  108. if (!open (NTPDATE, "/usr/local/sbin/ntpdate -q $host 2>&1 |")) {
  109. print "Could not open ntpdate\n";
  110. exit $ERRORS{"UNKNOWN"};
  111. }
  112. while (<NTPDATE>) {
  113. print if ($verbose);
  114. $msg = $_ unless ($msg);
  115. if (/(offset|adjust)\s+([-.\d]+)/i) {
  116. $offset = $2;
  117. last;
  118. }
  119. }
  120. # soak up remaining output; check for error
  121. while (<NTPDATE>) {
  122. if (/no server suitable for synchronization found/) {
  123. $ntpdate_error = $ERRORS{"CRITICAL"};
  124. }
  125. }
  126. close(NTPDATE);
  127. # only declare an error if we also get a non-zero return code from ntpdate
  128. $ntpdate_error = ($? >> 8) || $ntpdate_error;
  129. ###
  130. ###
  131. ### Then scan xntpdc if it exists
  132. ###
  133. ###
  134. if (#open(NTPDC,"/usr/sbin/ntpdc -c $host 2>&1 |") ||
  135. open(NTPDC,"/usr/sbin/xntpdc -c $host 2>&1 |") ) {
  136. while (<NTPDC>) {
  137. print if ($verbose);
  138. if (/([^\s]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
  139. if ($8>15) {
  140. $dispersion_error = $ERRORS{'CRITICAL'};
  141. } elsif ($8>5 && $dispersion_error<$ERRORS{'CRITICAL'}) {
  142. $dispersion_error = $ERRORS{'WARNING'};
  143. }
  144. }
  145. }
  146. close NTPDC;
  147. }
  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 ($ntpdate_error && $offset && ($offset == 0)) { undef $offset;}
  151. if ($ntpdate_error > $ERRORS{'OK'}) {
  152. $state = $ntpdate_error;
  153. $answer = "Server for ntp probably down\n";
  154. if (defined($offset) && abs($offset) > $critical) {
  155. $state = $ERRORS{'CRITICAL'};
  156. $answer = "Server Error and time difference $offset seconds greater than +/- $critical sec\n";
  157. } elsif (defined($offset) && abs($offset) > $warning) {
  158. $answer = "Server error and time difference $offset seconds greater than +/- $warning sec\n";
  159. }
  160. } elsif ($dispersion_error > $ERRORS{'OK'}) {
  161. $state = $dispersion_error;
  162. $answer = "Dispersion too high\n";
  163. if (defined($offset) && abs($offset) > $critical) {
  164. $state = $ERRORS{'CRITICAL'};
  165. $answer = "Dispersion error and time difference $offset seconds greater than +/- $critical sec\n";
  166. } elsif (defined($offset) && abs($offset) > $warning) {
  167. $answer = "Dispersion error and time difference $offset seconds greater than +/- $warning sec\n";
  168. }
  169. } else { # no errors from ntpdate or xntpdc
  170. if (defined $offset) {
  171. if (abs($offset) > $critical) {
  172. $state = $ERRORS{'CRITICAL'};
  173. $answer = "Time difference $offset seconds greater than +/- $critical sec\n";
  174. } elsif (abs($offset) > $warning) {
  175. $state = $ERRORS{'WARNING'};
  176. $answer = "Time difference $offset seconds greater than +/- $warning sec\n";
  177. } elsif (abs($offset) <= $warning) {
  178. $state = $ERRORS{'OK'};
  179. $answer = "Time difference $offset seconds\n";
  180. }
  181. } else { # no offset defined
  182. $state = $ERRORS{'UNKNOWN'};
  183. $answer = "Invalid format returned from ntpdate ($msg)\n";
  184. }
  185. }
  186. foreach $key (keys %ERRORS) {
  187. if ($state==$ERRORS{$key}) {
  188. print ("$key: $answer");
  189. last;
  190. }
  191. }
  192. exit $state;
  193. sub print_usage () {
  194. print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>]\n";
  195. }
  196. sub print_help () {
  197. print_revision($PROGNAME,'$Revision$');
  198. print "Copyright (c) 2000 Bo Kersey/Karl DeBisschop\n";
  199. print "\n";
  200. print_usage();
  201. print "\n";
  202. print "<warn> = Clock offset in seconds at which a warning message will be generated.\n Defaults to 60.\n";
  203. print "<crit> = Clock offset in seconds at which a critical message will be generated.\n Defaults to 120.\n\n";
  204. support();
  205. }