check_ntp_peer.c 22 KB

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