4
0

check_ntp_time.c 21 KB

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