4
0

check_ntp.pl 14 KB

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