4
0

check_ntp.pl 6.5 KB

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