4
0

check_ntp.pl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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=f" => \$opt_w, "warning=f" => \$opt_w, # offset|adjust warning if above this number
  73. "c=f" => \$opt_c, "critical=f" => \$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 = 60);
  91. my $warning = $1 if ($opt_w =~ /([0-9.]+)/);
  92. ($opt_c) || ($opt_c = 120);
  93. my $critical = $1 if ($opt_c =~ /([0-9.]+)/);
  94. if ($critical < $warning ) {
  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. # some systems don't have a proper ntpdc/xntpdc
  107. my $have_ntpdc = undef;
  108. if ($utils::PATH_TO_NTPDC && -x $utils::PATH_TO_NTPDC ) {
  109. $have_ntpdc = 1;
  110. }else{
  111. $have_ntpdc = 0;
  112. }
  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);
  119. ###
  120. ###$dispersion_error = $ERRORS{'
  121. ### First, check ntpdate
  122. ###
  123. ###
  124. if (!open (NTPDATE, "$utils::PATH_TO_NTPDATE -q $host 2>&1 |")) {
  125. print "Could not open ntpdate\n";
  126. exit $ERRORS{"UNKNOWN"};
  127. }
  128. while (<NTPDATE>) {
  129. print if ($verbose);
  130. $msg = $_ unless ($msg);
  131. if (/(offset|adjust)\s+([-.\d]+)/i) {
  132. $offset = $2;
  133. # An offset of 0.000000 with an error is probably bogus. Actually,
  134. # it's probably always bogus, but let's be paranoid here.
  135. if ($offset == 0) { undef $offset;}
  136. $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
  137. print "ntperr = $ntpdate_error \n" if $verbose;
  138. }
  139. if (/no server suitable for synchronization found/) {
  140. $ntpdate_error = $ERRORS{"CRITICAL"};
  141. $msg = "No suitable peer server found - ";
  142. }
  143. }
  144. close (NTPDATE);
  145. # declare an error if we also get a non-zero return code from ntpdate
  146. # unless already set to critical
  147. if ( $? ) {
  148. print "stderr = $? : $! \n" if $verbose;
  149. $ntpdate_error = $ntpdate_error == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"} ;
  150. print "ntperr = $ntpdate_error : $!\n" if $verbose;
  151. }
  152. ###
  153. ###
  154. ### Then scan xntpdc/ntpdc if it exists
  155. ### and look in the 8th column for dispersion (ntpd v4) or jitter (ntpd v3)
  156. ###
  157. if ($have_ntpdc) {
  158. if ( open(NTPDC,"$utils::PATH_TO_NTPDC -s $host 2>&1 |") ) {
  159. while (<NTPDC>) {
  160. print $_ if ($verbose);
  161. if (/([^\s]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/) {
  162. if ($8 gt $critical) {
  163. print "Dispersion_crit = $8 :$critical\n" if ($verbose);
  164. $dispersion_error = $ERRORS{'CRITICAL'};
  165. } elsif ($8 gt $warning ) {
  166. print "Dispersion_warn = $8 :$warning \n" if ($verbose);
  167. $dispersion_error = $ERRORS{'WARNING'};
  168. } else {
  169. $dispersion_error = $ERRORS{'OK'};
  170. }
  171. }
  172. }
  173. close NTPDC;
  174. }
  175. }
  176. if ($ntpdate_error != $ERRORS{'OK'}) {
  177. $state = $ntpdate_error;
  178. $answer = $msg . "Server for ntp probably down\n";
  179. if (defined($offset) && abs($offset) > $critical) {
  180. $state = $ERRORS{'CRITICAL'};
  181. $answer = "Server Error and time difference $offset seconds greater than +/- $critical sec\n";
  182. } elsif (defined($offset) && abs($offset) > $warning) {
  183. $answer = "Server error and time difference $offset seconds greater than +/- $warning sec\n";
  184. }
  185. } elsif ($have_ntpdc && $dispersion_error != $ERRORS{'OK'}) {
  186. $state = $dispersion_error;
  187. $answer = "Dispersion too high\n";
  188. if (defined($offset) && abs($offset) > $critical) {
  189. $state = $ERRORS{'CRITICAL'};
  190. $answer = "Dispersion error and time difference $offset seconds greater than +/- $critical sec\n";
  191. } elsif (defined($offset) && abs($offset) > $warning) {
  192. $answer = "Dispersion error and time difference $offset seconds greater than +/- $warning sec\n";
  193. }
  194. } else { # no errors from ntpdate or xntpdc
  195. if (defined $offset) {
  196. if (abs($offset) > $critical) {
  197. $state = $ERRORS{'CRITICAL'};
  198. $answer = "Time difference $offset seconds greater than +/- $critical sec\n";
  199. } elsif (abs($offset) > $warning) {
  200. $state = $ERRORS{'WARNING'};
  201. $answer = "Time difference $offset seconds greater than +/- $warning sec\n";
  202. } elsif (abs($offset) <= $warning) {
  203. $state = $ERRORS{'OK'};
  204. $answer = "Time difference $offset seconds\n";
  205. }
  206. } else { # no offset defined
  207. $state = $ERRORS{'UNKNOWN'};
  208. $answer = "Invalid format returned from ntpdate ($msg)\n";
  209. }
  210. }
  211. foreach $key (keys %ERRORS) {
  212. if ($state==$ERRORS{$key}) {
  213. print ("$key: $answer");
  214. last;
  215. }
  216. }
  217. exit $state;
  218. sub print_usage () {
  219. print "Usage: $PROGNAME -H <host> [-w <warn>] [-c <crit>] [-v verbose]\n";
  220. }
  221. sub print_help () {
  222. print_revision($PROGNAME,'$Revision$');
  223. print "Copyright (c) 2000 Bo Kersey/Karl DeBisschop\n";
  224. print "\n";
  225. print_usage();
  226. print "\n";
  227. print "<warn> = Clock offset in seconds at which a warning message will be generated.\n Defaults to 60.\n";
  228. print "<crit> = Clock offset in seconds at which a critical message will be generated.\n Defaults to 120.\n\n";
  229. print "The same warning and critical values are used to check against the dispersion \n";
  230. print "column of ntpdc/xntpdc for the host being queried.\n\n";
  231. support();
  232. }