check_ntp_peer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /******************************************************************************
  2. *
  3. * Nagios check_ntp_peer 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_peer plugin
  14. *
  15. * This plugin checks an NTP server independent of any commandline
  16. * programs or external libraries.
  17. *
  18. * Use this plugin to check the health of an NTP server. It supports
  19. * checking the offset with the sync peer, the jitter and stratum. This
  20. * plugin will not check the clock offset between the local host and NTP
  21. * server; please use check_ntp_time for that purpose.
  22. *
  23. *
  24. * License Information:
  25. *
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation; either version 2 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  39. $Id$
  40. *****************************************************************************/
  41. const char *progname = "check_ntp_peer";
  42. const char *revision = "$Revision$";
  43. const char *copyright = "2007";
  44. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  45. #include "common.h"
  46. #include "netutils.h"
  47. #include "utils.h"
  48. static char *server_address=NULL;
  49. static int verbose=0;
  50. static int quiet=0;
  51. static short do_offset=0;
  52. static char *owarn="60";
  53. static char *ocrit="120";
  54. static short do_stratum=0;
  55. static char *swarn="-1:16";
  56. static char *scrit="-1:16";
  57. static short do_jitter=0;
  58. static char *jwarn="-1:5000";
  59. static char *jcrit="-1:10000";
  60. static int syncsource_found=0;
  61. static int li_alarm=0;
  62. int process_arguments (int, char **);
  63. thresholds *offset_thresholds = NULL;
  64. thresholds *jitter_thresholds = NULL;
  65. thresholds *stratum_thresholds = NULL;
  66. void print_help (void);
  67. void print_usage (void);
  68. /* max size of control message data */
  69. #define MAX_CM_SIZE 468
  70. /* this structure holds everything in an ntp control message as per rfc1305 */
  71. typedef struct {
  72. uint8_t flags; /* byte with leapindicator,vers,mode. see macros */
  73. uint8_t op; /* R,E,M bits and Opcode */
  74. uint16_t seq; /* Packet sequence */
  75. uint16_t status; /* Clock status */
  76. uint16_t assoc; /* Association */
  77. uint16_t offset; /* Similar to TCP sequence # */
  78. uint16_t count; /* # bytes of data */
  79. char data[MAX_CM_SIZE]; /* ASCII data of the request */
  80. /* NB: not necessarily NULL terminated! */
  81. } ntp_control_message;
  82. /* this is an association/status-word pair found in control packet reponses */
  83. typedef struct {
  84. uint16_t assoc;
  85. uint16_t status;
  86. } ntp_assoc_status_pair;
  87. /* bits 1,2 are the leap indicator */
  88. #define LI_MASK 0xc0
  89. #define LI(x) ((x&LI_MASK)>>6)
  90. #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0)
  91. /* and these are the values of the leap indicator */
  92. #define LI_NOWARNING 0x00
  93. #define LI_EXTRASEC 0x01
  94. #define LI_MISSINGSEC 0x02
  95. #define LI_ALARM 0x03
  96. /* bits 3,4,5 are the ntp version */
  97. #define VN_MASK 0x38
  98. #define VN(x) ((x&VN_MASK)>>3)
  99. #define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0)
  100. #define VN_RESERVED 0x02
  101. /* bits 6,7,8 are the ntp mode */
  102. #define MODE_MASK 0x07
  103. #define MODE(x) (x&MODE_MASK)
  104. #define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0)
  105. /* here are some values */
  106. #define MODE_CLIENT 0x03
  107. #define MODE_CONTROLMSG 0x06
  108. /* In control message, bits 8-10 are R,E,M bits */
  109. #define REM_MASK 0xe0
  110. #define REM_RESP 0x80
  111. #define REM_ERROR 0x40
  112. #define REM_MORE 0x20
  113. /* In control message, bits 11 - 15 are opcode */
  114. #define OP_MASK 0x1f
  115. #define OP_SET(x,y) do{ x |= (y&OP_MASK); }while(0)
  116. #define OP_READSTAT 0x01
  117. #define OP_READVAR 0x02
  118. /* In peer status bytes, bits 6,7,8 determine clock selection status */
  119. #define PEER_SEL(x) ((ntohs(x)>>8)&0x07)
  120. #define PEER_INCLUDED 0x04
  121. #define PEER_SYNCSOURCE 0x06
  122. /* NTP control message header is 12 bytes, plus any data in the data
  123. * field, plus null padding to the nearest 32-bit boundary per rfc.
  124. */
  125. #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((m.count)?4-(ntohs(m.count)%4):0))
  126. /* finally, a little helper or two for debugging: */
  127. #define DBG(x) do{if(verbose>1){ x; }}while(0);
  128. #define PRINTSOCKADDR(x) \
  129. do{ \
  130. printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\
  131. }while(0);
  132. void print_ntp_control_message(const ntp_control_message *p){
  133. int i=0, numpeers=0;
  134. const ntp_assoc_status_pair *peer=NULL;
  135. printf("control packet contents:\n");
  136. printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op);
  137. printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK);
  138. printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK);
  139. printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK);
  140. printf("\t response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP);
  141. printf("\t more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE);
  142. printf("\t error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR);
  143. printf("\t op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK);
  144. printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq));
  145. printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status));
  146. printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc));
  147. printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset));
  148. printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count));
  149. numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair));
  150. if(p->op&REM_RESP && p->op&OP_READSTAT){
  151. peer=(ntp_assoc_status_pair*)p->data;
  152. for(i=0;i<numpeers;i++){
  153. printf("\tpeer id %.2x status %.2x",
  154. ntohs(peer[i].assoc), ntohs(peer[i].status));
  155. if (PEER_SEL(peer[i].status) >= PEER_INCLUDED){
  156. if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){
  157. printf(" <-- current sync source");
  158. } else {
  159. printf(" <-- current sync candidate");
  160. }
  161. }
  162. printf("\n");
  163. }
  164. }
  165. }
  166. char *extract_value(const char *varlist, const char *name){
  167. char *tmpvarlist=NULL, *tmpkey=NULL, *value=NULL;
  168. int last=0;
  169. /* The following code require a non-empty varlist */
  170. if(strlen(varlist) == 0)
  171. return NULL;
  172. tmpvarlist = strdup(varlist);
  173. tmpkey = strtok(tmpvarlist, "=");
  174. do {
  175. if(strstr(tmpkey, name) != NULL) {
  176. value = strtok(NULL, ",");
  177. last = 1;
  178. }
  179. } while (last == 0 && (tmpkey = strtok(NULL, "=")));
  180. return value;
  181. }
  182. void
  183. setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
  184. memset(p, 0, sizeof(ntp_control_message));
  185. LI_SET(p->flags, LI_NOWARNING);
  186. VN_SET(p->flags, VN_RESERVED);
  187. MODE_SET(p->flags, MODE_CONTROLMSG);
  188. OP_SET(p->op, opcode);
  189. p->seq = htons(seq);
  190. /* Remaining fields are zero for requests */
  191. }
  192. /* This function does all the actual work; roughly here's what it does
  193. * beside setting the offest, jitter and stratum passed as argument:
  194. * - offset can be negative, so if it cannot get the offset, offset_result
  195. * is set to UNKNOWN, otherwise OK.
  196. * - jitter and stratum are set to -1 if they cannot be retrieved so any
  197. * positive value means a success retrieving the value.
  198. * - status is set to WARNING if there's no sync.peer (otherwise OK) and is
  199. * the return value of the function.
  200. * status is pretty much useless as syncsource_found is a global variable
  201. * used later in main to check is the server was synchronized. It works
  202. * so I left it alone */
  203. int ntp_request(const char *host, double *offset, int *offset_result, double *jitter, int *stratum){
  204. int conn=-1, i, npeers=0, num_candidates=0;
  205. double tmp_offset = 0;
  206. int min_peer_sel=PEER_INCLUDED;
  207. int peers_size=0, peer_offset=0;
  208. int status;
  209. ntp_assoc_status_pair *peers=NULL;
  210. ntp_control_message req;
  211. const char *getvar = "stratum,offset,jitter";
  212. char *data, *value, *nptr;
  213. void *tmp;
  214. status = STATE_OK;
  215. *offset_result = STATE_UNKNOWN;
  216. *jitter = *stratum = -1;
  217. /* Long-winded explanation:
  218. * Getting the sync peer offset, jitter and stratum requires a number of
  219. * steps:
  220. * 1) Send a READSTAT request.
  221. * 2) Interpret the READSTAT reply
  222. * a) The data section contains a list of peer identifiers (16 bits)
  223. * and associated status words (16 bits)
  224. * b) We want the value of 0x06 in the SEL (peer selection) value,
  225. * which means "current synchronizatin source". If that's missing,
  226. * we take anything better than 0x04 (see the rfc for details) but
  227. * set a minimum of warning.
  228. * 3) Send a READVAR request for information on each peer identified
  229. * in 2b greater than the minimum selection value.
  230. * 4) Extract the offset, jitter and stratum value from the data[]
  231. * (it's ASCII)
  232. */
  233. my_udp_connect(server_address, 123, &conn);
  234. /* keep sending requests until the server stops setting the
  235. * REM_MORE bit, though usually this is only 1 packet. */
  236. do{
  237. setup_control_request(&req, OP_READSTAT, 1);
  238. DBG(printf("sending READSTAT request"));
  239. write(conn, &req, SIZEOF_NTPCM(req));
  240. DBG(print_ntp_control_message(&req));
  241. /* Attempt to read the largest size packet possible */
  242. req.count=htons(MAX_CM_SIZE);
  243. DBG(printf("recieving READSTAT response"))
  244. if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
  245. die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
  246. DBG(print_ntp_control_message(&req));
  247. if (LI(req.flags) == LI_ALARM) li_alarm = 1;
  248. /* Each peer identifier is 4 bytes in the data section, which
  249. * we represent as a ntp_assoc_status_pair datatype.
  250. */
  251. peers_size+=ntohs(req.count);
  252. if((tmp=realloc(peers, peers_size)) == NULL)
  253. free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
  254. peers=tmp;
  255. memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count));
  256. npeers=peers_size/sizeof(ntp_assoc_status_pair);
  257. peer_offset+=ntohs(req.count);
  258. } while(req.op&REM_MORE);
  259. /* first, let's find out if we have a sync source, or if there are
  260. * at least some candidates. In the latter case we'll issue
  261. * a warning but go ahead with the check on them. */
  262. for (i = 0; i < npeers; i++){
  263. if (PEER_SEL(peers[i].status) >= PEER_INCLUDED){
  264. num_candidates++;
  265. if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){
  266. syncsource_found=1;
  267. min_peer_sel=PEER_SYNCSOURCE;
  268. }
  269. }
  270. }
  271. if(verbose) printf("%d candiate peers available\n", num_candidates);
  272. if(verbose && syncsource_found) printf("synchronization source found\n");
  273. if(! syncsource_found){
  274. status = STATE_WARNING;
  275. if(verbose) printf("warning: no synchronization source found\n");
  276. }
  277. if(li_alarm){
  278. status = STATE_WARNING;
  279. if(verbose) printf("warning: LI_ALARM bit is set\n");
  280. }
  281. for (i = 0; i < npeers; i++){
  282. /* Only query this server if it is the current sync source */
  283. /* If there's no sync.peer, query all candidates and use the best one */
  284. if (PEER_SEL(peers[i].status) >= min_peer_sel){
  285. if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
  286. asprintf(&data, "");
  287. do{
  288. setup_control_request(&req, OP_READVAR, 2);
  289. req.assoc = peers[i].assoc;
  290. /* Putting the wanted variable names in the request
  291. * cause the server to provide _only_ the requested values.
  292. * thus reducing net traffic, guaranteeing us only a single
  293. * datagram in reply, and making intepretation much simpler
  294. */
  295. /* Older servers doesn't know what jitter is, so if we get an
  296. * error on the first pass we redo it with "dispersion" */
  297. strncpy(req.data, getvar, MAX_CM_SIZE-1);
  298. req.count = htons(strlen(getvar));
  299. DBG(printf("sending READVAR request...\n"));
  300. write(conn, &req, SIZEOF_NTPCM(req));
  301. DBG(print_ntp_control_message(&req));
  302. req.count = htons(MAX_CM_SIZE);
  303. DBG(printf("receiving READVAR response...\n"));
  304. read(conn, &req, SIZEOF_NTPCM(req));
  305. DBG(print_ntp_control_message(&req));
  306. if(!(req.op&REM_ERROR))
  307. asprintf(&data, "%s%s", data, req.data);
  308. } while(req.op&REM_MORE);
  309. if(req.op&REM_ERROR) {
  310. if(strstr(getvar, "jitter")) {
  311. if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n");
  312. getvar = "stratum,offset,dispersion";
  313. i--;
  314. continue;
  315. } else if(strlen(getvar)) {
  316. if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n");
  317. getvar = "";
  318. i--;
  319. continue;
  320. }
  321. }
  322. if(verbose > 1)
  323. printf("Server responded: >>>%s<<<\n", data);
  324. /* get the offset */
  325. if(verbose)
  326. printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
  327. value = extract_value(data, "offset");
  328. nptr=NULL;
  329. /* Convert the value if we have one */
  330. if(value != NULL)
  331. tmp_offset = strtod(value, &nptr) / 1000;
  332. /* If value is null or no conversion was performed */
  333. if(value == NULL || value==nptr) {
  334. if(verbose) printf("error: unable to read server offset response.\n");
  335. } else {
  336. if(verbose) printf("%.10g\n", tmp_offset);
  337. if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
  338. *offset = tmp_offset;
  339. *offset_result = STATE_OK;
  340. } else {
  341. /* Skip this one; move to the next */
  342. continue;
  343. }
  344. }
  345. if(do_jitter) {
  346. /* get the jitter */
  347. if(verbose) {
  348. printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
  349. }
  350. value = extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
  351. nptr=NULL;
  352. /* Convert the value if we have one */
  353. if(value != NULL)
  354. *jitter = strtod(value, &nptr);
  355. /* If value is null or no conversion was performed */
  356. if(value == NULL || value==nptr) {
  357. if(verbose) printf("error: unable to read server jitter/dispersion response.\n");
  358. *jitter = -1;
  359. } else if(verbose) {
  360. printf("%.10g\n", *jitter);
  361. }
  362. }
  363. if(do_stratum) {
  364. /* get the stratum */
  365. if(verbose) {
  366. printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
  367. }
  368. value = extract_value(data, "stratum");
  369. nptr=NULL;
  370. /* Convert the value if we have one */
  371. if(value != NULL)
  372. *stratum = strtol(value, &nptr, 10);
  373. if(value == NULL || value==nptr) {
  374. if(verbose) printf("error: unable to read server stratum response.\n");
  375. *stratum = -1;
  376. } else {
  377. if(verbose) printf("%i\n", *stratum);
  378. }
  379. }
  380. } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
  381. } /* for (i = 0; i < npeers; i++) */
  382. close(conn);
  383. if(peers!=NULL) free(peers);
  384. return status;
  385. }
  386. int process_arguments(int argc, char **argv){
  387. int c;
  388. int option=0;
  389. static struct option longopts[] = {
  390. {"version", no_argument, 0, 'V'},
  391. {"help", no_argument, 0, 'h'},
  392. {"verbose", no_argument, 0, 'v'},
  393. {"use-ipv4", no_argument, 0, '4'},
  394. {"use-ipv6", no_argument, 0, '6'},
  395. {"quiet", no_argument, 0, 'q'},
  396. {"warning", required_argument, 0, 'w'},
  397. {"critical", required_argument, 0, 'c'},
  398. {"swarn", required_argument, 0, 'W'},
  399. {"scrit", required_argument, 0, 'C'},
  400. {"jwarn", required_argument, 0, 'j'},
  401. {"jcrit", required_argument, 0, 'k'},
  402. {"timeout", required_argument, 0, 't'},
  403. {"hostname", required_argument, 0, 'H'},
  404. {0, 0, 0, 0}
  405. };
  406. if (argc < 2)
  407. usage ("\n");
  408. while (1) {
  409. c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:t:H:", longopts, &option);
  410. if (c == -1 || c == EOF || c == 1)
  411. break;
  412. switch (c) {
  413. case 'h':
  414. print_help();
  415. exit(STATE_OK);
  416. break;
  417. case 'V':
  418. print_revision(progname, revision);
  419. exit(STATE_OK);
  420. break;
  421. case 'v':
  422. verbose++;
  423. break;
  424. case 'q':
  425. quiet = 1;
  426. break;
  427. case 'w':
  428. do_offset=1;
  429. owarn = optarg;
  430. break;
  431. case 'c':
  432. do_offset=1;
  433. ocrit = optarg;
  434. break;
  435. case 'W':
  436. do_stratum=1;
  437. swarn = optarg;
  438. break;
  439. case 'C':
  440. do_stratum=1;
  441. scrit = optarg;
  442. break;
  443. case 'j':
  444. do_jitter=1;
  445. jwarn = optarg;
  446. break;
  447. case 'k':
  448. do_jitter=1;
  449. jcrit = optarg;
  450. break;
  451. case 'H':
  452. if(is_host(optarg) == FALSE)
  453. usage2(_("Invalid hostname/address"), optarg);
  454. server_address = strdup(optarg);
  455. break;
  456. case 't':
  457. socket_timeout=atoi(optarg);
  458. break;
  459. case '4':
  460. address_family = AF_INET;
  461. break;
  462. case '6':
  463. #ifdef USE_IPV6
  464. address_family = AF_INET6;
  465. #else
  466. usage4 (_("IPv6 support not available"));
  467. #endif
  468. break;
  469. case '?':
  470. /* print short usage statement if args not parsable */
  471. usage5 ();
  472. break;
  473. }
  474. }
  475. if(server_address == NULL){
  476. usage4(_("Hostname was not supplied"));
  477. }
  478. return 0;
  479. }
  480. char *perfd_offset (double offset)
  481. {
  482. return fperfdata ("offset", offset, "s",
  483. TRUE, offset_thresholds->warning->end,
  484. TRUE, offset_thresholds->critical->end,
  485. FALSE, 0, FALSE, 0);
  486. }
  487. char *perfd_jitter (double jitter)
  488. {
  489. return fperfdata ("jitter", jitter, "",
  490. do_jitter, jitter_thresholds->warning->end,
  491. do_jitter, jitter_thresholds->critical->end,
  492. TRUE, 0, FALSE, 0);
  493. }
  494. char *perfd_stratum (int stratum)
  495. {
  496. return perfdata ("stratum", stratum, "",
  497. do_stratum, (int)stratum_thresholds->warning->end,
  498. do_stratum, (int)stratum_thresholds->critical->end,
  499. TRUE, 0, TRUE, 16);
  500. }
  501. int main(int argc, char *argv[]){
  502. int result, offset_result, stratum;
  503. double offset=0, jitter=0;
  504. char *result_line, *perfdata_line;
  505. setlocale (LC_ALL, "");
  506. bindtextdomain (PACKAGE, LOCALEDIR);
  507. textdomain (PACKAGE);
  508. if (process_arguments (argc, argv) == ERROR)
  509. usage4 (_("Could not parse arguments"));
  510. set_thresholds(&offset_thresholds, owarn, ocrit);
  511. set_thresholds(&jitter_thresholds, jwarn, jcrit);
  512. set_thresholds(&stratum_thresholds, swarn, scrit);
  513. /* initialize alarm signal handling */
  514. signal (SIGALRM, socket_timeout_alarm_handler);
  515. /* set socket timeout */
  516. alarm (socket_timeout);
  517. /* This returns either OK or WARNING (See comment preceeding ntp_request) */
  518. result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum);
  519. if(offset_result == STATE_UNKNOWN) {
  520. /* if there's no sync peer (this overrides ntp_request output): */
  521. result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
  522. } else {
  523. /* Be quiet if there's no candidates either */
  524. if (quiet == 1 && result == STATE_WARNING)
  525. result = STATE_UNKNOWN;
  526. result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
  527. }
  528. if(do_stratum)
  529. result = max_state_alt(result, get_status(stratum, stratum_thresholds));
  530. if(do_jitter)
  531. result = max_state_alt(result, get_status(jitter, jitter_thresholds));
  532. switch (result) {
  533. case STATE_CRITICAL :
  534. asprintf(&result_line, _("NTP CRITICAL:"));
  535. break;
  536. case STATE_WARNING :
  537. asprintf(&result_line, _("NTP WARNING:"));
  538. break;
  539. case STATE_OK :
  540. asprintf(&result_line, _("NTP OK:"));
  541. break;
  542. default :
  543. asprintf(&result_line, _("NTP UNKNOWN:"));
  544. break;
  545. }
  546. if(!syncsource_found)
  547. asprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
  548. else if(li_alarm)
  549. asprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
  550. if(offset_result == STATE_UNKNOWN){
  551. asprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
  552. asprintf(&perfdata_line, "");
  553. } else {
  554. asprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
  555. asprintf(&perfdata_line, "%s", perfd_offset(offset));
  556. }
  557. if (do_jitter) {
  558. asprintf(&result_line, "%s, jitter=%f", result_line, jitter);
  559. asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
  560. }
  561. if (do_stratum) {
  562. asprintf(&result_line, "%s, stratum=%i", result_line, stratum);
  563. asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
  564. }
  565. printf("%s|%s\n", result_line, perfdata_line);
  566. if(server_address!=NULL) free(server_address);
  567. return result;
  568. }
  569. void print_help(void){
  570. print_revision(progname, revision);
  571. printf ("Copyright (c) 2006 Sean Finney\n");
  572. printf (COPYRIGHT, copyright, email);
  573. printf ("%s\n", _("This plugin checks the selected ntp server"));
  574. printf ("\n\n");
  575. print_usage();
  576. printf (_(UT_HELP_VRSN));
  577. printf (_(UT_HOST_PORT), 'p', "123");
  578. printf (" %s\n", "-q, --quiet");
  579. printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
  580. printf (" %s\n", "-w, --warning=THRESHOLD");
  581. printf (" %s\n", _("Offset to result in warning status (seconds)"));
  582. printf (" %s\n", "-c, --critical=THRESHOLD");
  583. printf (" %s\n", _("Offset to result in critical status (seconds)"));
  584. printf (" %s\n", "-W, --warning=THRESHOLD");
  585. printf (" %s\n", _("Warning threshold for stratum"));
  586. printf (" %s\n", "-C, --critical=THRESHOLD");
  587. printf (" %s\n", _("Critical threshold for stratum"));
  588. printf (" %s\n", "-j, --warning=THRESHOLD");
  589. printf (" %s\n", _("Warning threshold for jitter"));
  590. printf (" %s\n", "-k, --critical=THRESHOLD");
  591. printf (" %s\n", _("Critical threshold for jitter"));
  592. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  593. printf (_(UT_VERBOSE));
  594. printf("\n");
  595. printf("%s\n", _("Notes:"));
  596. printf(" %s\n", _("This plugin checks an NTP server independent of any commandline"));
  597. printf(" %s\n\n", _("programs or external libraries."));
  598. printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
  599. printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
  600. printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
  601. printf(" %s\n\n", _("server; please use check_ntp_time for that purpose."));
  602. printf(" %s\n", _("See:"));
  603. printf(" %s\n", ("http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT"));
  604. printf(" %s\n", _("for THRESHOLD format and examples."));
  605. printf("\n");
  606. printf("%s\n", _("Examples:"));
  607. printf(" %s\n", _("Simple NTP server check:"));
  608. printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
  609. printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
  610. printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
  611. printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
  612. printf(" %s\n", _("Check only stratum:"));
  613. printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
  614. printf (_(UT_SUPPORT));
  615. }
  616. void
  617. print_usage(void)
  618. {
  619. printf (_("Usage:"));
  620. printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
  621. printf(" [-j <warn>] [-k <crit>] [-v verbose]\n");
  622. }