check_ntp_time.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /******************************************************************************
  2. *
  3. * Nagios check_ntp_time plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2006 sean finney <seanius@seanius.net>
  7. * Copyright (c) 2007 nagios-plugins team
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Description:
  12. *
  13. * This file contains the check_ntp_time plugin
  14. *
  15. * This plugin checks the clock offset between the local host and a
  16. * remote NTP server. It is independent of any commandline programs or
  17. * external libraries.
  18. *
  19. * If you'd rather want to monitor an NTP server, please use
  20. * check_ntp_peer.
  21. *
  22. *
  23. * License Information:
  24. *
  25. * This program is free software; you can redistribute it and/or modify
  26. * it under the terms of the GNU General Public License as published by
  27. * the Free Software Foundation; either version 2 of the License, or
  28. * (at your option) any later version.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU General Public License
  36. * along with this program; if not, write to the Free Software
  37. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  38. $Id$
  39. *****************************************************************************/
  40. const char *progname = "check_ntp_time";
  41. const char *revision = "$Revision$";
  42. const char *copyright = "2007";
  43. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  44. #include "common.h"
  45. #include "netutils.h"
  46. #include "utils.h"
  47. static char *server_address=NULL;
  48. static int verbose=0;
  49. static int quiet=0;
  50. static char *owarn="60";
  51. static char *ocrit="120";
  52. int process_arguments (int, char **);
  53. thresholds *offset_thresholds = NULL;
  54. void print_help (void);
  55. void print_usage (void);
  56. /* number of times to perform each request to get a good average. */
  57. #define AVG_NUM 4
  58. /* max size of control message data */
  59. #define MAX_CM_SIZE 468
  60. /* this structure holds everything in an ntp request/response as per rfc1305 */
  61. typedef struct {
  62. uint8_t flags; /* byte with leapindicator,vers,mode. see macros */
  63. uint8_t stratum; /* clock stratum */
  64. int8_t poll; /* polling interval */
  65. int8_t precision; /* precision of the local clock */
  66. int32_t rtdelay; /* total rt delay, as a fixed point num. see macros */
  67. uint32_t rtdisp; /* like above, but for max err to primary src */
  68. uint32_t refid; /* ref clock identifier */
  69. uint64_t refts; /* reference timestamp. local time local clock */
  70. uint64_t origts; /* time at which request departed client */
  71. uint64_t rxts; /* time at which request arrived at server */
  72. uint64_t txts; /* time at which request departed server */
  73. } ntp_message;
  74. /* this structure holds data about results from querying offset from a peer */
  75. typedef struct {
  76. time_t waiting; /* ts set when we started waiting for a response */
  77. int num_responses; /* number of successfully recieved responses */
  78. uint8_t stratum; /* copied verbatim from the ntp_message */
  79. double rtdelay; /* converted from the ntp_message */
  80. double rtdisp; /* converted from the ntp_message */
  81. double offset[AVG_NUM]; /* offsets from each response */
  82. uint8_t flags; /* byte with leapindicator,vers,mode. see macros */
  83. } ntp_server_results;
  84. /* bits 1,2 are the leap indicator */
  85. #define LI_MASK 0xc0
  86. #define LI(x) ((x&LI_MASK)>>6)
  87. #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0)
  88. /* and these are the values of the leap indicator */
  89. #define LI_NOWARNING 0x00
  90. #define LI_EXTRASEC 0x01
  91. #define LI_MISSINGSEC 0x02
  92. #define LI_ALARM 0x03
  93. /* bits 3,4,5 are the ntp version */
  94. #define VN_MASK 0x38
  95. #define VN(x) ((x&VN_MASK)>>3)
  96. #define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0)
  97. #define VN_RESERVED 0x02
  98. /* bits 6,7,8 are the ntp mode */
  99. #define MODE_MASK 0x07
  100. #define MODE(x) (x&MODE_MASK)
  101. #define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0)
  102. /* here are some values */
  103. #define MODE_CLIENT 0x03
  104. #define MODE_CONTROLMSG 0x06
  105. /* In control message, bits 8-10 are R,E,M bits */
  106. #define REM_MASK 0xe0
  107. #define REM_RESP 0x80
  108. #define REM_ERROR 0x40
  109. #define REM_MORE 0x20
  110. /* In control message, bits 11 - 15 are opcode */
  111. #define OP_MASK 0x1f
  112. #define OP_SET(x,y) do{ x |= (y&OP_MASK); }while(0)
  113. #define OP_READSTAT 0x01
  114. #define OP_READVAR 0x02
  115. /* In peer status bytes, bits 6,7,8 determine clock selection status */
  116. #define PEER_SEL(x) ((ntohs(x)>>8)&0x07)
  117. #define PEER_INCLUDED 0x04
  118. #define PEER_SYNCSOURCE 0x06
  119. /**
  120. ** a note about the 32-bit "fixed point" numbers:
  121. **
  122. they are divided into halves, each being a 16-bit int in network byte order:
  123. - the first 16 bits are an int on the left side of a decimal point.
  124. - the second 16 bits represent a fraction n/(2^16)
  125. likewise for the 64-bit "fixed point" numbers with everything doubled :)
  126. **/
  127. /* macros to access the left/right 16 bits of a 32-bit ntp "fixed point"
  128. number. note that these can be used as lvalues too */
  129. #define L16(x) (((uint16_t*)&x)[0])
  130. #define R16(x) (((uint16_t*)&x)[1])
  131. /* macros to access the left/right 32 bits of a 64-bit ntp "fixed point"
  132. number. these too can be used as lvalues */
  133. #define L32(x) (((uint32_t*)&x)[0])
  134. #define R32(x) (((uint32_t*)&x)[1])
  135. /* ntp wants seconds since 1/1/00, epoch is 1/1/70. this is the difference */
  136. #define EPOCHDIFF 0x83aa7e80UL
  137. /* extract a 32-bit ntp fixed point number into a double */
  138. #define NTP32asDOUBLE(x) (ntohs(L16(x)) + (double)ntohs(R16(x))/65536.0)
  139. /* likewise for a 64-bit ntp fp number */
  140. #define NTP64asDOUBLE(n) (double)(((uint64_t)n)?\
  141. (ntohl(L32(n))-EPOCHDIFF) + \
  142. (.00000001*(0.5+(double)(ntohl(R32(n))/42.94967296))):\
  143. 0)
  144. /* convert a struct timeval to a double */
  145. #define TVasDOUBLE(x) (double)(x.tv_sec+(0.000001*x.tv_usec))
  146. /* convert an ntp 64-bit fp number to a struct timeval */
  147. #define NTP64toTV(n,t) \
  148. do{ if(!n) t.tv_sec = t.tv_usec = 0; \
  149. else { \
  150. t.tv_sec=ntohl(L32(n))-EPOCHDIFF; \
  151. t.tv_usec=(int)(0.5+(double)(ntohl(R32(n))/4294.967296)); \
  152. } \
  153. }while(0)
  154. /* convert a struct timeval to an ntp 64-bit fp number */
  155. #define TVtoNTP64(t,n) \
  156. do{ if(!t.tv_usec && !t.tv_sec) n=0x0UL; \
  157. else { \
  158. L32(n)=htonl(t.tv_sec + EPOCHDIFF); \
  159. R32(n)=htonl((uint64_t)((4294.967296*t.tv_usec)+.5)); \
  160. } \
  161. } while(0)
  162. /* NTP control message header is 12 bytes, plus any data in the data
  163. * field, plus null padding to the nearest 32-bit boundary per rfc.
  164. */
  165. #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((m.count)?4-(ntohs(m.count)%4):0))
  166. /* finally, a little helper or two for debugging: */
  167. #define DBG(x) do{if(verbose>1){ x; }}while(0);
  168. #define PRINTSOCKADDR(x) \
  169. do{ \
  170. printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\
  171. }while(0);
  172. /* calculate the offset of the local clock */
  173. static inline double calc_offset(const ntp_message *m, const struct timeval *t){
  174. double client_tx, peer_rx, peer_tx, client_rx;
  175. client_tx = NTP64asDOUBLE(m->origts);
  176. peer_rx = NTP64asDOUBLE(m->rxts);
  177. peer_tx = NTP64asDOUBLE(m->txts);
  178. client_rx=TVasDOUBLE((*t));
  179. return (.5*((peer_tx-client_rx)+(peer_rx-client_tx)));
  180. }
  181. /* print out a ntp packet in human readable/debuggable format */
  182. void print_ntp_message(const ntp_message *p){
  183. struct timeval ref, orig, rx, tx;
  184. NTP64toTV(p->refts,ref);
  185. NTP64toTV(p->origts,orig);
  186. NTP64toTV(p->rxts,rx);
  187. NTP64toTV(p->txts,tx);
  188. printf("packet contents:\n");
  189. printf("\tflags: 0x%.2x\n", p->flags);
  190. printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK);
  191. printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK);
  192. printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK);
  193. printf("\tstratum = %d\n", p->stratum);
  194. printf("\tpoll = %g\n", pow(2, p->poll));
  195. printf("\tprecision = %g\n", pow(2, p->precision));
  196. printf("\trtdelay = %-.16g\n", NTP32asDOUBLE(p->rtdelay));
  197. printf("\trtdisp = %-.16g\n", NTP32asDOUBLE(p->rtdisp));
  198. printf("\trefid = %x\n", p->refid);
  199. printf("\trefts = %-.16g\n", NTP64asDOUBLE(p->refts));
  200. printf("\torigts = %-.16g\n", NTP64asDOUBLE(p->origts));
  201. printf("\trxts = %-.16g\n", NTP64asDOUBLE(p->rxts));
  202. printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts));
  203. }
  204. void setup_request(ntp_message *p){
  205. struct timeval t;
  206. memset(p, 0, sizeof(ntp_message));
  207. LI_SET(p->flags, LI_ALARM);
  208. VN_SET(p->flags, 4);
  209. MODE_SET(p->flags, MODE_CLIENT);
  210. p->poll=4;
  211. p->precision=(int8_t)0xfa;
  212. L16(p->rtdelay)=htons(1);
  213. L16(p->rtdisp)=htons(1);
  214. gettimeofday(&t, NULL);
  215. TVtoNTP64(t,p->txts);
  216. }
  217. /* select the "best" server from a list of servers, and return its index.
  218. * this is done by filtering servers based on stratum, dispersion, and
  219. * finally round-trip delay. */
  220. int best_offset_server(const ntp_server_results *slist, int nservers){
  221. int i=0, j=0, cserver=0, candidates[5], csize=0;
  222. /* for each server */
  223. for(cserver=0; cserver<nservers; cserver++){
  224. /* sort out servers with error flags */
  225. if ( LI(slist[cserver].flags) != LI_NOWARNING ){
  226. if (verbose) printf("discarding peer id %d: flags=%d\n", cserver, LI(slist[cserver].flags));
  227. break;
  228. }
  229. /* compare it to each of the servers already in the candidate list */
  230. for(i=0; i<csize; i++){
  231. /* does it have an equal or better stratum? */
  232. if(slist[cserver].stratum <= slist[i].stratum){
  233. /* does it have an equal or better dispersion? */
  234. if(slist[cserver].rtdisp <= slist[i].rtdisp){
  235. /* does it have a better rtdelay? */
  236. if(slist[cserver].rtdelay < slist[i].rtdelay){
  237. break;
  238. }
  239. }
  240. }
  241. }
  242. /* if we haven't reached the current list's end, move everyone
  243. * over one to the right, and insert the new candidate */
  244. if(i<csize){
  245. for(j=4; j>i; j--){
  246. candidates[j]=candidates[j-1];
  247. }
  248. }
  249. /* regardless, if they should be on the list... */
  250. if(i<5) {
  251. candidates[i]=cserver;
  252. if(csize<5) csize++;
  253. /* otherwise discard the server */
  254. } else {
  255. DBG(printf("discarding peer id %d\n", cserver));
  256. }
  257. }
  258. if(csize>0) {
  259. DBG(printf("best server selected: peer %d\n", candidates[0]));
  260. return candidates[0];
  261. } else {
  262. DBG(printf("no peers meeting synchronization criteria :(\n"));
  263. return -1;
  264. }
  265. }
  266. /* do everything we need to get the total average offset
  267. * - we use a certain amount of parallelization with poll() to ensure
  268. * we don't waste time sitting around waiting for single packets.
  269. * - we also "manually" handle resolving host names and connecting, because
  270. * we have to do it in a way that our lazy macros don't handle currently :( */
  271. double offset_request(const char *host, int *status){
  272. int i=0, j=0, ga_result=0, num_hosts=0, *socklist=NULL, respnum=0;
  273. int servers_completed=0, one_written=0, one_read=0, servers_readable=0, best_index=-1;
  274. time_t now_time=0, start_ts=0;
  275. ntp_message *req=NULL;
  276. double avg_offset=0.;
  277. struct timeval recv_time;
  278. struct addrinfo *ai=NULL, *ai_tmp=NULL, hints;
  279. struct pollfd *ufds=NULL;
  280. ntp_server_results *servers=NULL;
  281. /* setup hints to only return results from getaddrinfo that we'd like */
  282. memset(&hints, 0, sizeof(struct addrinfo));
  283. hints.ai_family = address_family;
  284. hints.ai_protocol = IPPROTO_UDP;
  285. hints.ai_socktype = SOCK_DGRAM;
  286. /* fill in ai with the list of hosts resolved by the host name */
  287. ga_result = getaddrinfo(host, "123", &hints, &ai);
  288. if(ga_result!=0){
  289. die(STATE_UNKNOWN, "error getting address for %s: %s\n",
  290. host, gai_strerror(ga_result));
  291. }
  292. /* count the number of returned hosts, and allocate stuff accordingly */
  293. for(ai_tmp=ai; ai_tmp!=NULL; ai_tmp=ai_tmp->ai_next){ num_hosts++; }
  294. req=(ntp_message*)malloc(sizeof(ntp_message)*num_hosts);
  295. if(req==NULL) die(STATE_UNKNOWN, "can not allocate ntp message array");
  296. socklist=(int*)malloc(sizeof(int)*num_hosts);
  297. if(socklist==NULL) die(STATE_UNKNOWN, "can not allocate socket array");
  298. ufds=(struct pollfd*)malloc(sizeof(struct pollfd)*num_hosts);
  299. if(ufds==NULL) die(STATE_UNKNOWN, "can not allocate socket array");
  300. servers=(ntp_server_results*)malloc(sizeof(ntp_server_results)*num_hosts);
  301. if(servers==NULL) die(STATE_UNKNOWN, "can not allocate server array");
  302. memset(servers, 0, sizeof(ntp_server_results)*num_hosts);
  303. DBG(printf("Found %d peers to check\n", num_hosts));
  304. /* setup each socket for writing, and the corresponding struct pollfd */
  305. ai_tmp=ai;
  306. for(i=0;ai_tmp;i++){
  307. socklist[i]=socket(ai_tmp->ai_family, SOCK_DGRAM, IPPROTO_UDP);
  308. if(socklist[i] == -1) {
  309. perror(NULL);
  310. die(STATE_UNKNOWN, "can not create new socket");
  311. }
  312. if(connect(socklist[i], ai_tmp->ai_addr, ai_tmp->ai_addrlen)){
  313. die(STATE_UNKNOWN, "can't create socket connection");
  314. } else {
  315. ufds[i].fd=socklist[i];
  316. ufds[i].events=POLLIN;
  317. ufds[i].revents=0;
  318. }
  319. ai_tmp = ai_tmp->ai_next;
  320. }
  321. /* now do AVG_NUM checks to each host. We stop before timeout/2 seconds
  322. * have passed in order to ensure post-processing and jitter time. */
  323. now_time=start_ts=time(NULL);
  324. while(servers_completed<num_hosts && now_time-start_ts <= socket_timeout/2){
  325. /* loop through each server and find each one which hasn't
  326. * been touched in the past second or so and is still lacking
  327. * some responses. For each of these servers, send a new request,
  328. * and update the "waiting" timestamp with the current time. */
  329. one_written=0;
  330. now_time=time(NULL);
  331. for(i=0; i<num_hosts; i++){
  332. if(servers[i].waiting<now_time && servers[i].num_responses<AVG_NUM){
  333. if(verbose && servers[i].waiting != 0) printf("re-");
  334. if(verbose) printf("sending request to peer %d\n", i);
  335. setup_request(&req[i]);
  336. write(socklist[i], &req[i], sizeof(ntp_message));
  337. servers[i].waiting=now_time;
  338. one_written=1;
  339. break;
  340. }
  341. }
  342. /* quickly poll for any sockets with pending data */
  343. servers_readable=poll(ufds, num_hosts, 100);
  344. if(servers_readable==-1){
  345. perror("polling ntp sockets");
  346. die(STATE_UNKNOWN, "communication errors");
  347. }
  348. /* read from any sockets with pending data */
  349. for(i=0; servers_readable && i<num_hosts; i++){
  350. if(ufds[i].revents&POLLIN && servers[i].num_responses < AVG_NUM){
  351. if(verbose) {
  352. printf("response from peer %d: ", i);
  353. }
  354. read(ufds[i].fd, &req[i], sizeof(ntp_message));
  355. gettimeofday(&recv_time, NULL);
  356. DBG(print_ntp_message(&req[i]));
  357. respnum=servers[i].num_responses++;
  358. servers[i].offset[respnum]=calc_offset(&req[i], &recv_time);
  359. if(verbose) {
  360. printf("offset %.10g\n", servers[i].offset[respnum]);
  361. }
  362. servers[i].stratum=req[i].stratum;
  363. servers[i].rtdisp=NTP32asDOUBLE(req[i].rtdisp);
  364. servers[i].rtdelay=NTP32asDOUBLE(req[i].rtdelay);
  365. servers[i].waiting=0;
  366. servers[i].flags=req[i].flags;
  367. servers_readable--;
  368. one_read = 1;
  369. if(servers[i].num_responses==AVG_NUM) servers_completed++;
  370. }
  371. }
  372. /* lather, rinse, repeat. */
  373. }
  374. if (one_read == 0) {
  375. die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
  376. }
  377. /* now, pick the best server from the list */
  378. best_index=best_offset_server(servers, num_hosts);
  379. if(best_index < 0){
  380. *status=STATE_UNKNOWN;
  381. } else {
  382. /* finally, calculate the average offset */
  383. for(i=0; i<servers[best_index].num_responses;i++){
  384. avg_offset+=servers[best_index].offset[j];
  385. }
  386. avg_offset/=servers[best_index].num_responses;
  387. }
  388. /* cleanup */
  389. for(j=0; j<num_hosts; j++){ close(socklist[j]); }
  390. free(socklist);
  391. free(ufds);
  392. free(servers);
  393. free(req);
  394. freeaddrinfo(ai);
  395. if(verbose) printf("overall average offset: %.10g\n", avg_offset);
  396. return avg_offset;
  397. }
  398. int process_arguments(int argc, char **argv){
  399. int c;
  400. int option=0;
  401. static struct option longopts[] = {
  402. {"version", no_argument, 0, 'V'},
  403. {"help", no_argument, 0, 'h'},
  404. {"verbose", no_argument, 0, 'v'},
  405. {"use-ipv4", no_argument, 0, '4'},
  406. {"use-ipv6", no_argument, 0, '6'},
  407. {"quiet", no_argument, 0, 'q'},
  408. {"warning", required_argument, 0, 'w'},
  409. {"critical", required_argument, 0, 'c'},
  410. {"timeout", required_argument, 0, 't'},
  411. {"hostname", required_argument, 0, 'H'},
  412. {0, 0, 0, 0}
  413. };
  414. if (argc < 2)
  415. usage ("\n");
  416. while (1) {
  417. c = getopt_long (argc, argv, "Vhv46qw:c:t:H:", longopts, &option);
  418. if (c == -1 || c == EOF || c == 1)
  419. break;
  420. switch (c) {
  421. case 'h':
  422. print_help();
  423. exit(STATE_OK);
  424. break;
  425. case 'V':
  426. print_revision(progname, revision);
  427. exit(STATE_OK);
  428. break;
  429. case 'v':
  430. verbose++;
  431. break;
  432. case 'q':
  433. quiet = 1;
  434. break;
  435. case 'w':
  436. owarn = optarg;
  437. break;
  438. case 'c':
  439. ocrit = optarg;
  440. break;
  441. case 'H':
  442. if(is_host(optarg) == FALSE)
  443. usage2(_("Invalid hostname/address"), optarg);
  444. server_address = strdup(optarg);
  445. break;
  446. case 't':
  447. socket_timeout=atoi(optarg);
  448. break;
  449. case '4':
  450. address_family = AF_INET;
  451. break;
  452. case '6':
  453. #ifdef USE_IPV6
  454. address_family = AF_INET6;
  455. #else
  456. usage4 (_("IPv6 support not available"));
  457. #endif
  458. break;
  459. case '?':
  460. /* print short usage statement if args not parsable */
  461. usage5 ();
  462. break;
  463. }
  464. }
  465. if(server_address == NULL){
  466. usage4(_("Hostname was not supplied"));
  467. }
  468. return 0;
  469. }
  470. char *perfd_offset (double offset)
  471. {
  472. return fperfdata ("offset", offset, "s",
  473. TRUE, offset_thresholds->warning->end,
  474. TRUE, offset_thresholds->critical->end,
  475. FALSE, 0, FALSE, 0);
  476. }
  477. int main(int argc, char *argv[]){
  478. int result, offset_result;
  479. double offset=0;
  480. char *result_line, *perfdata_line;
  481. setlocale (LC_ALL, "");
  482. bindtextdomain (PACKAGE, LOCALEDIR);
  483. textdomain (PACKAGE);
  484. result = offset_result = STATE_OK;
  485. if (process_arguments (argc, argv) == ERROR)
  486. usage4 (_("Could not parse arguments"));
  487. set_thresholds(&offset_thresholds, owarn, ocrit);
  488. /* initialize alarm signal handling */
  489. signal (SIGALRM, socket_timeout_alarm_handler);
  490. /* set socket timeout */
  491. alarm (socket_timeout);
  492. offset = offset_request(server_address, &offset_result);
  493. if (offset_result == STATE_UNKNOWN) {
  494. result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
  495. } else {
  496. result = get_status(fabs(offset), offset_thresholds);
  497. }
  498. switch (result) {
  499. case STATE_CRITICAL :
  500. asprintf(&result_line, _("NTP CRITICAL:"));
  501. break;
  502. case STATE_WARNING :
  503. asprintf(&result_line, _("NTP WARNING:"));
  504. break;
  505. case STATE_OK :
  506. asprintf(&result_line, _("NTP OK:"));
  507. break;
  508. default :
  509. asprintf(&result_line, _("NTP UNKNOWN:"));
  510. break;
  511. }
  512. if(offset_result == STATE_UNKNOWN){
  513. asprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
  514. asprintf(&perfdata_line, "");
  515. } else {
  516. asprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
  517. asprintf(&perfdata_line, "%s", perfd_offset(offset));
  518. }
  519. printf("%s|%s\n", result_line, perfdata_line);
  520. if(server_address!=NULL) free(server_address);
  521. return result;
  522. }
  523. void print_help(void){
  524. print_revision(progname, revision);
  525. printf ("Copyright (c) 2006 Sean Finney\n");
  526. printf (COPYRIGHT, copyright, email);
  527. printf ("%s\n", _("This plugin checks the clock offset with the ntp server"));
  528. printf ("\n\n");
  529. print_usage();
  530. printf (_(UT_HELP_VRSN));
  531. printf (_(UT_HOST_PORT), 'p', "123");
  532. printf (" %s\n", "-q, --quiet");
  533. printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL if offset cannot be found"));
  534. printf (" %s\n", "-w, --warning=THRESHOLD");
  535. printf (" %s\n", _("Offset to result in warning status (seconds)"));
  536. printf (" %s\n", "-c, --critical=THRESHOLD");
  537. printf (" %s\n", _("Offset to result in critical status (seconds)"));
  538. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  539. printf (_(UT_VERBOSE));
  540. printf("\n");
  541. printf("%s\n", _("Notes:"));
  542. printf(" %s\n", _("This plugin checks the clock offset between the local host and a"));
  543. printf(" %s\n", _("remote NTP server. It is independent of any commandline programs or"));
  544. printf(" %s\n\n", _("external libraries."));
  545. printf(" %s\n", _("If you'd rather want to monitor an NTP server, please use"));
  546. printf(" %s\n\n", _("check_ntp_peer."));
  547. printf(" %s\n", _("See:"));
  548. printf(" %s\n", ("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
  549. printf(" %s\n", _("for THRESHOLD format and examples."));
  550. printf("\n");
  551. printf("%s\n", _("Examples:"));
  552. printf(" %s\n", ("./check_ntp_time -H ntpserv -w 0.5 -c 1"));
  553. printf (_(UT_SUPPORT));
  554. }
  555. void
  556. print_usage(void)
  557. {
  558. printf (_("Usage:"));
  559. printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
  560. printf(" [-j <warn>] [-k <crit>] [-v verbose]\n");
  561. }