check_ntp.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #!/usr/bin/perl -w
  2. #
  3. # (c)1999 Ian Cass, Knowledge Matters Ltd.
  4. # Read the GNU copyright stuff for all the legalese
  5. #
  6. # Check NTP time servers plugin. This plugin requires the ntpdate utility to
  7. # be installed on the system, however since it's part of the ntp suite, you
  8. # should already have it installed.
  9. #
  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. # ntpdate - offset is in seconds
  55. # changed ntpdc to ntpq - jitter/dispersion is in milliseconds
  56. #
  57. # Patch for for regex for stratum1 refid.
  58. require 5.004;
  59. use POSIX;
  60. use strict;
  61. use Getopt::Long;
  62. use vars qw($opt_V $opt_h $opt_H $opt_t $opt_w $opt_c $opt_O $opt_j $opt_k $verbose $PROGNAME $def_jitter $ipv4 $ipv6);
  63. use lib utils.pm;
  64. use utils qw($TIMEOUT %ERRORS &print_revision &support);
  65. $PROGNAME="check_ntp";
  66. sub print_help ();
  67. sub print_usage ();
  68. $ENV{'PATH'}='';
  69. $ENV{'BASH_ENV'}='';
  70. $ENV{'ENV'}='';
  71. # defaults in sec
  72. my $DEFAULT_OFFSET_WARN = 60; # 1 minute
  73. my $DEFAULT_OFFSET_CRIT = 120; # 2 minutes
  74. # default in millisec
  75. my $DEFAULT_JITTER_WARN = 5000; # 5 sec
  76. my $DEFAULT_JITTER_CRIT = 10000; # 10 sec
  77. Getopt::Long::Configure('bundling');
  78. GetOptions
  79. ("V" => \$opt_V, "version" => \$opt_V,
  80. "h" => \$opt_h, "help" => \$opt_h,
  81. "v" => \$verbose, "verbose" => \$verbose,
  82. "4" => \$ipv4, "use-ipv4" => \$ipv4,
  83. "6" => \$ipv6, "use-ipv6" => \$ipv6,
  84. "w=f" => \$opt_w, "warning=f" => \$opt_w, # offset|adjust warning if above this number
  85. "c=f" => \$opt_c, "critical=f" => \$opt_c, # offset|adjust critical if above this number
  86. "O" => \$opt_O, "zero-offset" => \$opt_O, # zero-offset bad
  87. "j=s" => \$opt_j, "jwarn=i" => \$opt_j, # jitter warning if above this number
  88. "k=s" => \$opt_k, "jcrit=i" => \$opt_k, # jitter critical if above this number
  89. "t=s" => \$opt_t, "timeout=i" => \$opt_t,
  90. "H=s" => \$opt_H, "hostname=s" => \$opt_H);
  91. if ($opt_V) {
  92. print_revision($PROGNAME,'@NP_VERSION@');
  93. exit $ERRORS{'OK'};
  94. }
  95. if ($opt_h) {
  96. print_help();
  97. exit $ERRORS{'OK'};
  98. }
  99. # jitter test params specified
  100. if (defined $opt_j || defined $opt_k ) {
  101. $def_jitter = 1;
  102. }
  103. $opt_H = shift unless ($opt_H);
  104. 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]+)*)$/);
  105. unless ($host) {
  106. print "No target host specified\n";
  107. print_usage();
  108. exit $ERRORS{'UNKNOWN'};
  109. }
  110. my ($timeout, $owarn, $ocrit, $jwarn, $jcrit);
  111. $timeout = $TIMEOUT;
  112. ($opt_t) && ($opt_t =~ /^([0-9]+)$/) && ($timeout = $1);
  113. $owarn = $DEFAULT_OFFSET_WARN;
  114. ($opt_w) && ($opt_w =~ /^([0-9.]+)$/) && ($owarn = $1);
  115. $ocrit = $DEFAULT_OFFSET_CRIT;
  116. ($opt_c) && ($opt_c =~ /^([0-9.]+)$/) && ($ocrit = $1);
  117. $jwarn = $DEFAULT_JITTER_WARN;
  118. ($opt_j) && ($opt_j =~ /^([0-9]+)$/) && ($jwarn = $1);
  119. $jcrit = $DEFAULT_JITTER_CRIT;
  120. ($opt_k) && ($opt_k =~ /^([0-9]+)$/) && ($jcrit = $1);
  121. if ($ocrit < $owarn ) {
  122. print "Critical offset should be larger than warning offset\n";
  123. print_usage();
  124. exit $ERRORS{"UNKNOWN"};
  125. }
  126. if ($def_jitter) {
  127. if ($opt_k < $opt_j) {
  128. print "Critical jitter should be larger than warning jitter\n";
  129. print_usage();
  130. exit $ERRORS{'UNKNOWN'};
  131. }
  132. }
  133. my $stratum = -1;
  134. my $ignoreret = 0;
  135. my $answer = undef;
  136. my $offset = undef;
  137. my $jitter = undef;
  138. my $syspeer = undef;
  139. my $candidate = 0;
  140. my @candidates;
  141. my $msg; # first line of output to print if format is invalid
  142. my $state = $ERRORS{'UNKNOWN'};
  143. my $ntpdate_error = $ERRORS{'UNKNOWN'};
  144. my $jitter_error = $ERRORS{'UNKNOWN'};
  145. # some systems don't have a proper ntpq (migrated from ntpdc)
  146. my $have_ntpq = undef;
  147. if ($utils::PATH_TO_NTPQ && -x $utils::PATH_TO_NTPQ ) {
  148. $have_ntpq = 1;
  149. }else{
  150. $have_ntpq = 0;
  151. }
  152. # Just in case of problems, let's not hang Nagios
  153. $SIG{'ALRM'} = sub {
  154. print ("ERROR: No response from ntp server (alarm)\n");
  155. exit $ERRORS{"UNKNOWN"};
  156. };
  157. alarm($timeout);
  158. # Determine protocol to be used for ntpdate and ntpq
  159. my $ntpdate = $utils::PATH_TO_NTPDATE;
  160. my $ntpq = $utils::PATH_TO_NTPQ;
  161. if ($ipv4) {
  162. $ntpdate .= " -4";
  163. $ntpq .= " -4";
  164. }
  165. elsif ($ipv6) {
  166. $ntpdate .= " -6";
  167. $ntpq .= " -6";
  168. }
  169. # else don't use any flags
  170. ###
  171. ###
  172. ### First, check ntpdate
  173. ###
  174. ###
  175. if (!open (NTPDATE, $ntpdate . " -q $host 2>&1 |")) {
  176. print "Could not open $ntpdate: $!\n";
  177. exit $ERRORS{"UNKNOWN"};
  178. }
  179. my $out;
  180. while (<NTPDATE>) {
  181. #print if ($verbose); # noop
  182. $msg = $_ unless ($msg);
  183. $out .= "$_ ";
  184. if (/stratum\s(\d+)/) {
  185. $stratum = $1;
  186. }
  187. if (/(offset|adjust)\s+([-.\d]+)/i) {
  188. $offset = $2;
  189. # An offset of 0.000000 with an error is probably bogus. Actually,
  190. # it's probably always bogus, but let's be paranoid here.
  191. # Has been reported that 0.0000 happens in a production environment
  192. # on Solaris 8 so this check should be taken out - SF tracker 1150777
  193. if (defined $opt_O ) {
  194. if ($offset == 0) { undef $offset;}
  195. }
  196. $ntpdate_error = defined ($offset) ? $ERRORS{"OK"} : $ERRORS{"CRITICAL"};
  197. print "ntperr = $ntpdate_error \n" if $verbose;
  198. }
  199. if (/no server suitable for synchronization found/) {
  200. if ($stratum == 16) {
  201. $ntpdate_error = $ERRORS{"WARNING"};
  202. $msg = "Desynchronized peer server found";
  203. $ignoreret=1;
  204. }
  205. else {
  206. $ntpdate_error = $ERRORS{"CRITICAL"};
  207. $msg = "No suitable peer server found - ";
  208. }
  209. }
  210. }
  211. $out =~ s/\n//g;
  212. close (NTPDATE) ||
  213. die $! ? "$out - Error closing $ntpdate pipe: $!"
  214. : "$out - Exit status: $? from $ntpdate\n";
  215. # declare an error if we also get a non-zero return code from ntpdate
  216. # unless already set to critical
  217. if ( $? && !$ignoreret ) {
  218. print "stderr = $? : $! \n" if $verbose;
  219. $ntpdate_error = $ntpdate_error == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"UNKNOWN"} ;
  220. print "ntperr = $ntpdate_error : $!\n" if $verbose;
  221. }
  222. ###
  223. ###
  224. ### Then scan xntpq/ntpq if it exists
  225. ### and look in the 11th column for jitter
  226. ###
  227. # Field 1: Tally Code ( Space, 'x','.','-','+','#','*','o')
  228. # Only match for '*' which implies sys.peer
  229. # or 'o' which implies pps.peer
  230. # If both exist, the last one is picked.
  231. # Field 2: address of the remote peer
  232. # Field 3: Refid of the clock (0.0.0.0 if unknown, WWWV/PPS/GPS/ACTS/USNO/PCS/... if Stratum1)
  233. # Field 4: stratum (0-15)
  234. # Field 5: Type of the peer: local (l), unicast (u), multicast (m)
  235. # broadcast (b); not sure about multicast/broadcast
  236. # Field 6: last packet receive (in seconds)
  237. # Field 7: polling interval
  238. # Field 8: reachability resgister (octal)
  239. # Field 9: delay
  240. # Field 10: offset
  241. # Field 11: dispersion/jitter
  242. #
  243. # According to bug 773588 Some solaris xntpd implementations seemto match on
  244. # "#" even though the docs say it exceeds maximum distance. Providing patch
  245. # here which will generate a warining.
  246. if ($have_ntpq) {
  247. if ( open(NTPQ, $ntpq . " -np $host 2>&1 |") ) {
  248. while (<NTPQ>) {
  249. print $_ if ($verbose);
  250. if ( /timed out/ ){
  251. $have_ntpq = 0 ;
  252. last ;
  253. }
  254. # number of candidates on <host> for sys.peer
  255. if (/^(\*|\+|\#|o])/) {
  256. ++$candidate;
  257. push (@candidates, $_);
  258. print "Candidate count= $candidate\n" if ($verbose);
  259. }
  260. # match sys.peer or pps.peer
  261. if (/^(\*|o)(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
  262. $syspeer = $2;
  263. $stratum = $4;
  264. $jitter = $11;
  265. print "match $_ \n" if $verbose;
  266. if ($jitter > $jcrit) {
  267. print "Jitter_crit = $11 :$jcrit\n" if ($verbose);
  268. $jitter_error = $ERRORS{'CRITICAL'};
  269. } elsif ($jitter > $jwarn ) {
  270. print "Jitter_warn = $11 :$jwarn\n" if ($verbose);
  271. $jitter_error = $ERRORS{'WARNING'};
  272. } else {
  273. $jitter_error = $ERRORS{'OK'};
  274. }
  275. } else {
  276. print "No match!\n" if $verbose;
  277. $jitter = '(not parsed)';
  278. }
  279. }
  280. close NTPQ ||
  281. die $! ? "Error closing $ntpq pipe: $!"
  282. : "Exit status: $? from $ntpq\n";
  283. # if we did not match sys.peer or pps.peer but matched # candidates only
  284. # generate a warning
  285. # based on bug id 773588
  286. unless (defined $syspeer) {
  287. if ($#candidates >=0) {
  288. foreach my $c (@candidates) {
  289. $c =~ /^(#)([-0-9.\s]+)\s+([-0-9A-Za-z_().]+)\s+([-0-9.]+)\s+([lumb-]+)\s+([-0-9m.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)/;
  290. $syspeer = $2;
  291. $stratum = $4;
  292. $jitter = $11;
  293. print "candidate match $c \n" if $verbose;
  294. if ($jitter > $jcrit) {
  295. print "Candidate match - Jitter_crit = $11 :$jcrit\n" if ($verbose);
  296. $jitter_error = $ERRORS{'CRITICAL'};
  297. }elsif ($jitter > $jwarn ) {
  298. print "Candidate match - Jitter_warn = $11 :$jwarn \n" if ($verbose);
  299. $jitter_error = $ERRORS{'WARNING'};
  300. } else {
  301. $jitter_error = $ERRORS{'WARNING'};
  302. }
  303. }
  304. }
  305. }
  306. }
  307. }
  308. if ($ntpdate_error != $ERRORS{'OK'}) {
  309. $state = $ntpdate_error;
  310. if ($ntpdate_error == $ERRORS{'WARNING'} ) {
  311. $answer = $msg;
  312. }
  313. else {
  314. $answer = $msg . "Server for ntp probably down";
  315. }
  316. if (defined($offset) && abs($offset) > $ocrit) {
  317. $state = $ERRORS{'CRITICAL'};
  318. $answer = "Server Error and offset $offset sec > +/- $ocrit sec";
  319. } elsif (defined($offset) && abs($offset) > $owarn) {
  320. $answer = "Server error and offset $offset sec > +/- $owarn sec";
  321. } elsif (defined($jitter) && abs($jitter) > $jcrit) {
  322. $answer = "Server error and jitter $jitter msec > +/- $jcrit msec";
  323. } elsif (defined($jitter) && abs($jitter) > $jwarn) {
  324. $answer = "Server error and jitter $jitter msec > +/- $jwarn msec";
  325. }
  326. } elsif ($have_ntpq && $jitter_error != $ERRORS{'OK'}) {
  327. $state = $jitter_error;
  328. $answer = "Jitter $jitter too high";
  329. if (defined($offset) && abs($offset) > $ocrit) {
  330. $state = $ERRORS{'CRITICAL'};
  331. $answer = "Jitter error and offset $offset sec > +/- $ocrit sec";
  332. } elsif (defined($offset) && abs($offset) > $owarn) {
  333. $answer = "Jitter error and offset $offset sec > +/- $owarn sec";
  334. } elsif (defined($jitter) && abs($jitter) > $jcrit) {
  335. $answer = "Jitter error and jitter $jitter msec > +/- $jcrit msec";
  336. } elsif (defined($jitter) && abs($jitter) > $jwarn) {
  337. $answer = "Jitter error and jitter $jitter msec > +/- $jwarn msec";
  338. }
  339. } elsif( !$have_ntpq ) { # no errors from ntpdate and no ntpq or ntpq timed out
  340. if (abs($offset) > $ocrit) {
  341. $state = $ERRORS{'CRITICAL'};
  342. $answer = "Offset $offset sec > +/- $ocrit sec";
  343. } elsif (abs($offset) > $owarn) {
  344. $state = $ERRORS{'WARNING'};
  345. $answer = "Offset $offset sec > +/- $owarn sec";
  346. } elsif (( abs($offset) > $owarn) && $def_jitter ) {
  347. $state = $ERRORS{'WARNING'};
  348. $answer = "Offset $offset sec > +/- $owarn sec, ntpq timed out";
  349. } elsif ( $def_jitter ) {
  350. $state = $ERRORS{'WARNING'};
  351. $answer = "Offset $offset secs, ntpq timed out";
  352. } else{
  353. $state = $ERRORS{'OK'};
  354. $answer = "Offset $offset secs";
  355. }
  356. } else { # no errors from ntpdate or ntpq
  357. if (abs($offset) > $ocrit) {
  358. $state = $ERRORS{'CRITICAL'};
  359. $answer = "Offset $offset sec > +/- $ocrit sec, jitter $jitter msec";
  360. } elsif (abs($jitter) > $jcrit ) {
  361. $state = $ERRORS{'CRITICAL'};
  362. $answer = "Jitter $jitter msec> +/- $jcrit msec, offset $offset sec";
  363. } elsif (abs($offset) > $owarn) {
  364. $state = $ERRORS{'WARNING'};
  365. $answer = "Offset $offset sec > +/- $owarn sec, jitter $jitter msec";
  366. } elsif (abs($jitter) > $jwarn ) {
  367. $state = $ERRORS{'WARNING'};
  368. $answer = "Jitter $jitter msec> +/- $jwarn msec, offset $offset sec";
  369. } else {
  370. $state = $ERRORS{'OK'};
  371. $answer = "Offset $offset secs, jitter $jitter msec, peer is stratum $stratum";
  372. }
  373. }
  374. foreach my $key (keys %ERRORS) {
  375. if ($state==$ERRORS{$key}) {
  376. # print ("NTP $key: $answer");
  377. print ("NTP $key: $answer|offset=$offset, jitter=" . $jitter/1000 . ",peer_stratum=$stratum\n");
  378. last;
  379. }
  380. }
  381. exit $state;
  382. ####
  383. #### subs
  384. sub print_usage () {
  385. print "Usage: $PROGNAME -H <host> [-46] [-O] [-w <warn>] [-c <crit>] [-j <warn>] [-k <crit>] [-v verbose]\n";
  386. }
  387. sub print_help () {
  388. print_revision($PROGNAME,'@NP_VERSION@');
  389. print "Copyright (c) 2003 Bo Kersey/Karl DeBisschop\n";
  390. print "\n";
  391. print_usage();
  392. print "
  393. Checks the local timestamp offset versus <host> with ntpdate
  394. Checks the jitter/dispersion of clock signal between <host> and its sys.peer with ntpq\n
  395. -O (--zero-offset)
  396. A zero offset on \"ntpdate\" will generate a CRITICAL.\n
  397. -w (--warning)
  398. Clock offset in seconds at which a warning message will be generated.\n Defaults to $DEFAULT_OFFSET_WARN.
  399. -c (--critical)
  400. Clock offset in seconds at which a critical message will be generated.\n Defaults to $DEFAULT_OFFSET_CRIT.
  401. -j (--jwarn)
  402. Clock jitter in milliseconds at which a warning message will be generated.\n Defaults to $DEFAULT_JITTER_WARN.
  403. -k (--jcrit)
  404. Clock jitter in milliseconds at which a critical message will be generated.\n Defaults to $DEFAULT_JITTER_CRIT.
  405. If jitter/dispersion is specified with -j or -k and ntpq times out, then a
  406. warning is returned.\n
  407. -4 (--use-ipv4)
  408. Use IPv4 connection
  409. -6 (--use-ipv6)
  410. Use IPv6 connection
  411. \n";
  412. support();
  413. }