check_ntp_time.c 20 KB

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