check_ntp_peer.c 24 KB

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