check_ntp.pl 8.1 KB

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