check_ntp.pl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #! /usr/bin/perl -w
  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 (sghosh@users.sf.net)
  50. # - combined logic some blocks together..
  51. #
  52. # Todo - non-hardcoded dispersion values...
  53. #
  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);
  61. $PROGNAME="check_ntp";
  62. sub print_help ();
  63. sub print_usage ();
  64. $ENV{'PATH'}='';
  65. $ENV{'BASH_ENV'}='';
  66. $ENV{'ENV'}='';
  67. Getopt::Long::Configure('bundling');
  68. GetOptions
  69. ("V" => \$opt_V, "version" => \$opt_V,
  70. "h" => \$opt_h, "help" => \$opt_h,
  71. "v" => \$verbose, "verbose" => \$verbose,
  72. "w=s" => \$opt_w, "warning=s" => \$opt_w, # offset|adjust warning if above this number
  73. "c=s" => \$opt_c, "critical=s" => \$opt_c, # offset|adjust critical if above this number
  74. "H=s" => \$opt_H, "hostname=s" => \$opt_H);
  75. if ($opt_V) {
  76. print_revision($PROGNAME,'$Revision$ ');
  77. exit $ERRORS{'OK'};
  78. }
  79. if ($opt_h) {
  80. print_help();
  81. exit $ERRORS{'OK'};
  82. }
  83. $opt_H = shift unless ($opt_H);
  84. 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]+)*)$/);
  85. unless ($host) {
  86. print "No target host specified\n";
  87. print_usage();
  88. exit $ERRORS{'UNKNOWN'};
  89. }
  90. ($opt_w) || ($opt_w = shift) || ($opt_w = 60);
  91. my $warning = $1 if ($opt_w =~ /([0-9]+)/);
  92. ($opt_c) || ($opt_c = shift) || ($opt_c = 120);
  93. my $critical = $1 if ($opt_c =~ /([0-9]+)/);
  94. if ($opt_c < $opt_w) {
  95. print "Critical offset should be larger than warning offset\n";
  96. print_usage();
  97. exit $ERRORS{"UNKNOWN"};
  98. }
  99. my $answer = undef;
  100. my $offset = undef;
  101. my $msg; # first line of output to print if format is invalid
  102. my $state = $ERRORS{'UNKNOWN'};
  103. my $ntpdate_error = $ERRORS{'UNKNOWN'};
  104. my $dispersion_error = $ERRORS{'UNKNOWN'};
  105. my $key = undef;
  106. # Just in case of problems, let's not hang Nagios
  107. $SIG{'ALRM'} = sub {
  108. print ("ERROR: No response from ntp server (alarm)\n");
  109. exit $ERRORS{"UNKNOWN"};
  110. };
  111. alarm($TIMEOUT);
  112. ###
  113. ###$dispersion_error = $ERRORS{'
  114. ### First, check ntpdate
  115. ###
  116. ###
  117. if (!open (NTPDATE, "$utils::PATH_TO_NTPDATE -q $host 2>&1 |")) {
  118. print "Could not open ntpdate\n";
  119. exit $ERRORS{"UNKNOWN"};
  120. }
  121. while (<NTPDATE>) {
  122. print if ($verbose);
  123. $msg = $_ unless ($msg);
  124. if (/(offset|adjust)\s+([-.\d]+)/i) {
  125. $offset = $2;
  126. # An offset of 0.000000 with an error is probably bogus. Actually,
  127. # it's probably always bogus, but let's be paranoid here.
  128. if ($offset == 0) { undef $offset;}
  129. $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
  130. print "ntperr = $ntpdate_error \n" if $verbose;
  131. }
  132. if (/no server suitable for synchronization found/) {
  133. $ntpdate_error = $ERRORS{"CRITICAL"};
  134. $msg = "No suitable peer server found - ";
  135. }
  136. }
  137. close (NTPDATE);
  138. # declare an error if we also get a non-zero return code from ntpdate
  139. # unless already set to critical
  140. if ( $? ) {
  141. print "stderr = $? : $! \n" if $verbose;
  142. $ntpdate_error = $ntpdate_error == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"} ;
  143. print "ntperr = $ntpdate_error : $!\n" if $verbose;
  144. }
  145. ###
  146. ###
  147. ### Then scan xntpdc/ntpdc if it exists
  148. ### and look in the 8th column for dispersion (ntpd v4) or jitter (ntpd v3)
  149. ###
  150. if ( open(NTPDC,"$utils::PATH_TO_NTPDC -s $host 2>&1 |") ) {
  151. while (<NTPDC>) {
  152. print $_ if ($verbose);
  153. if (/([^\s]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
  154. if ($8>15) {
  155. print "Dispersion = $8 \n" if ($verbose);
  156. $dispersion_error = $ERRORS{'CRITICAL'};
  157. } elsif ($8>5 && $dispersion_error<$ERRORS{'CRITICAL'}) {
  158. print "Dispersion = $8 \n" if ($verbose);
  159. $dispersion_error = $ERRORS{'WARNING'};
  160. } else {
  161. $dispersion_error = $ERRORS{'OK'};
  162. }
  163. }
  164. }
  165. close NTPDC;
  166. }
  167. if ($ntpdate_error != $ERRORS{'OK'}) {
  168. $state = $ntpdate_error;
  169. $answer = $msg . "Server for ntp probably down\n";
  170. if (defined($offset) && abs($offset) > $critical) {
  171. $state = $ERRORS{'CRITICAL'};
  172. $answer = "Server Error and time difference $offset seconds greater than +/- $critical sec\n";
  173. } elsif (defined($offset) && abs($offset) > $warning) {
  174. $answer = "Server error and time difference $offset seconds greater than +/- $warning sec\n";
  175. }
  176. } elsif ($dispersion_error != $ERRORS{'OK'}) {
  177. $state = $dispersion_error;
  178. $answer = "Dispersion too high\n";
  179. if (defined($offset) && abs($offset) > $critical) {
  180. $state = $ERRORS{'CRITICAL'};
  181. $answer = "Dispersion error and time difference $offset seconds greater than +/- $critical sec\n";
  182. } elsif (defined($offset) && abs($offset) > $warning) {
  183. $answer = "Dispersion error and time difference $offset seconds greater than +/- $warning sec\n";
  184. }
  185. } else { # no errors from ntpdate or xntpdc
  186. if (defined $offset) {
  187. if (abs($offset) > $critical) {
  188. $state = $ERRORS{'CRITICAL'};
  189. $answer = "Time difference $offset seconds greater than +/- $critical sec\n";
  190. } elsif (abs($offset) > $warning) {
  191. $state = $ERRORS{'WARNING'};
  192. $answer = "Time difference $offset seconds greater than +/- $warning sec\n";
  193. } elsif (abs($offset) <= $warning) {
  194. $state = $ERRORS{'OK'};
  195. $answer = "Time difference $offset seconds\n";
  196. }
  197. } else { # no offset defined
  198. $state = $ERRORS{'UNKNOWN'};
  199. $answer = "Invalid format returned from ntpdate ($msg)\n";
  200. }
  201. }
  202. foreach $key (keys %ERRORS) {
  203. if ($state==$ERRORS{$key}) {
  204. print ("$key: $answer");
  205. last;
  206. }
  207. }
  208. exit $state;
  209. sub print_usage () {
  210. print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>]\n";
  211. }
  212. sub print_help () {
  213. print_revision($PROGNAME,'$Revision$');
  214. print "Copyright (c) 2000 Bo Kersey/Karl DeBisschop\n";
  215. print "\n";
  216. print_usage();
  217. print "\n";
  218. print "<warn> = Clock offset in seconds at which a warning message will be generated.\n Defaults to 60.\n";
  219. print "<crit> = Clock offset in seconds at which a critical message will be generated.\n Defaults to 120.\n\n";
  220. support();
  221. }