check_ntp_peer.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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-2014 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-2014";
  39. const char *email = "devel@nagios-plugins.org";
  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 responses */
  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. do {
  228. /* Attempt to read the largest size packet possible */
  229. req.count=htons(MAX_CM_SIZE);
  230. DBG(printf("receiving READSTAT response"))
  231. if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
  232. die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
  233. DBG(print_ntp_control_message(&req));
  234. /* discard obviously invalid packets */
  235. if (ntohs(req.count) > MAX_CM_SIZE)
  236. die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
  237. } while (!(req.op&OP_READSTAT && ntohs(req.seq) == 1));
  238. if (LI(req.flags) == LI_ALARM) li_alarm = 1;
  239. /* Each peer identifier is 4 bytes in the data section, which
  240. * we represent as a ntp_assoc_status_pair datatype.
  241. */
  242. peers_size+=ntohs(req.count);
  243. if((tmp=realloc(peers, peers_size)) == NULL)
  244. free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
  245. peers=tmp;
  246. memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count));
  247. npeers=peers_size/sizeof(ntp_assoc_status_pair);
  248. peer_offset+=ntohs(req.count);
  249. } while(req.op&REM_MORE);
  250. /* first, let's find out if we have a sync source, or if there are
  251. * at least some candidates. In the latter case we'll issue
  252. * a warning but go ahead with the check on them. */
  253. for (i = 0; i < npeers; i++){
  254. if(PEER_SEL(peers[i].status) >= PEER_TRUECHIMER){
  255. (*num_truechimers)++;
  256. if(PEER_SEL(peers[i].status) >= PEER_INCLUDED){
  257. num_candidates++;
  258. if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){
  259. syncsource_found=1;
  260. min_peer_sel=PEER_SYNCSOURCE;
  261. }
  262. }
  263. }
  264. }
  265. if(verbose) printf("%d candidate peers available\n", num_candidates);
  266. if(verbose && syncsource_found) printf("synchronization source found\n");
  267. if(! syncsource_found){
  268. status = STATE_WARNING;
  269. if(verbose) printf("warning: no synchronization source found\n");
  270. }
  271. if(li_alarm){
  272. status = STATE_WARNING;
  273. if(verbose) printf("warning: LI_ALARM bit is set\n");
  274. }
  275. for (i = 0; i < npeers; i++){
  276. /* Only query this server if it is the current sync source */
  277. /* If there's no sync.peer, query all candidates and use the best one */
  278. if (PEER_SEL(peers[i].status) >= min_peer_sel){
  279. if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
  280. xasprintf(&data, "");
  281. do{
  282. setup_control_request(&req, OP_READVAR, 2);
  283. req.assoc = peers[i].assoc;
  284. /* Putting the wanted variable names in the request
  285. * cause the server to provide _only_ the requested values.
  286. * thus reducing net traffic, guaranteeing us only a single
  287. * datagram in reply, and making interpretation much simpler
  288. */
  289. /* Older servers doesn't know what jitter is, so if we get an
  290. * error on the first pass we redo it with "dispersion" */
  291. strncpy(req.data, getvar, MAX_CM_SIZE-1);
  292. req.count = htons(strlen(getvar));
  293. DBG(printf("sending READVAR request...\n"));
  294. write(conn, &req, SIZEOF_NTPCM(req));
  295. DBG(print_ntp_control_message(&req));
  296. do {
  297. req.count = htons(MAX_CM_SIZE);
  298. DBG(printf("receiving READVAR response...\n"));
  299. read(conn, &req, SIZEOF_NTPCM(req));
  300. DBG(print_ntp_control_message(&req));
  301. } while (!(req.op&OP_READVAR && ntohs(req.seq) == 2));
  302. if(!(req.op&REM_ERROR))
  303. xasprintf(&data, "%s%s", data, req.data);
  304. } while(req.op&REM_MORE);
  305. if(req.op&REM_ERROR) {
  306. if(strstr(getvar, "jitter")) {
  307. if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n");
  308. getvar = "stratum,offset,dispersion";
  309. i--;
  310. continue;
  311. } else if(strlen(getvar)) {
  312. if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n");
  313. getvar = "";
  314. i--;
  315. continue;
  316. }
  317. }
  318. if(verbose > 1)
  319. printf("Server responded: >>>%s<<<\n", data);
  320. /* get the offset */
  321. if(verbose)
  322. printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
  323. value = np_extract_ntpvar(data, "offset");
  324. nptr=NULL;
  325. /* Convert the value if we have one */
  326. if(value != NULL)
  327. tmp_offset = strtod(value, &nptr) / 1000;
  328. /* If value is null or no conversion was performed */
  329. if(value == NULL || value==nptr) {
  330. if(verbose) printf("error: unable to read server offset response.\n");
  331. } else {
  332. if(verbose) printf("%.10g\n", tmp_offset);
  333. if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
  334. *offset = tmp_offset;
  335. *offset_result = STATE_OK;
  336. } else {
  337. /* Skip this one; move to the next */
  338. continue;
  339. }
  340. }
  341. if(do_jitter) {
  342. /* get the jitter */
  343. if(verbose) {
  344. printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
  345. }
  346. value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
  347. nptr=NULL;
  348. /* Convert the value if we have one */
  349. if(value != NULL)
  350. *jitter = strtod(value, &nptr);
  351. /* If value is null or no conversion was performed */
  352. if(value == NULL || value==nptr) {
  353. if(verbose) printf("error: unable to read server jitter/dispersion response.\n");
  354. *jitter = -1;
  355. } else if(verbose) {
  356. printf("%.10g\n", *jitter);
  357. }
  358. }
  359. if(do_stratum) {
  360. /* get the stratum */
  361. if(verbose) {
  362. printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
  363. }
  364. value = np_extract_ntpvar(data, "stratum");
  365. nptr=NULL;
  366. /* Convert the value if we have one */
  367. if(value != NULL)
  368. *stratum = strtol(value, &nptr, 10);
  369. if(value == NULL || value==nptr) {
  370. if(verbose) printf("error: unable to read server stratum response.\n");
  371. *stratum = -1;
  372. } else {
  373. if(verbose) printf("%i\n", *stratum);
  374. }
  375. }
  376. } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
  377. } /* for (i = 0; i < npeers; i++) */
  378. close(conn);
  379. if(peers!=NULL) free(peers);
  380. return status;
  381. }
  382. int process_arguments(int argc, char **argv){
  383. int c;
  384. int option=0;
  385. static struct option longopts[] = {
  386. {"version", no_argument, 0, 'V'},
  387. {"help", no_argument, 0, 'h'},
  388. {"verbose", no_argument, 0, 'v'},
  389. {"use-ipv4", no_argument, 0, '4'},
  390. {"use-ipv6", no_argument, 0, '6'},
  391. {"quiet", no_argument, 0, 'q'},
  392. {"warning", required_argument, 0, 'w'},
  393. {"critical", required_argument, 0, 'c'},
  394. {"swarn", required_argument, 0, 'W'},
  395. {"scrit", required_argument, 0, 'C'},
  396. {"jwarn", required_argument, 0, 'j'},
  397. {"jcrit", required_argument, 0, 'k'},
  398. {"twarn", required_argument, 0, 'm'},
  399. {"tcrit", required_argument, 0, 'n'},
  400. {"timeout", required_argument, 0, 't'},
  401. {"hostname", required_argument, 0, 'H'},
  402. {"port", required_argument, 0, 'p'},
  403. {0, 0, 0, 0}
  404. };
  405. if (argc < 2)
  406. usage ("\n");
  407. while (1) {
  408. c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option);
  409. if (c == -1 || c == EOF || c == 1)
  410. break;
  411. switch (c) {
  412. case 'h':
  413. print_help();
  414. exit(STATE_OK);
  415. break;
  416. case 'V':
  417. print_revision(progname, NP_VERSION);
  418. exit(STATE_OK);
  419. break;
  420. case 'v':
  421. verbose++;
  422. break;
  423. case 'q':
  424. quiet = 1;
  425. break;
  426. case 'w':
  427. do_offset=1;
  428. owarn = optarg;
  429. break;
  430. case 'c':
  431. do_offset=1;
  432. ocrit = optarg;
  433. break;
  434. case 'W':
  435. do_stratum=1;
  436. swarn = optarg;
  437. break;
  438. case 'C':
  439. do_stratum=1;
  440. scrit = optarg;
  441. break;
  442. case 'j':
  443. do_jitter=1;
  444. jwarn = optarg;
  445. break;
  446. case 'k':
  447. do_jitter=1;
  448. jcrit = optarg;
  449. break;
  450. case 'm':
  451. do_truechimers=1;
  452. twarn = optarg;
  453. break;
  454. case 'n':
  455. do_truechimers=1;
  456. tcrit = optarg;
  457. break;
  458. case 'H':
  459. if(is_host(optarg) == FALSE)
  460. usage2(_("Invalid hostname/address"), optarg);
  461. server_address = strdup(optarg);
  462. break;
  463. case 'p':
  464. port=atoi(optarg);
  465. break;
  466. case 't':
  467. timeout_interval = parse_timeout_string(optarg);
  468. break;
  469. case '4':
  470. address_family = AF_INET;
  471. break;
  472. case '6':
  473. #ifdef USE_IPV6
  474. address_family = AF_INET6;
  475. #else
  476. usage4 (_("IPv6 support not available"));
  477. #endif
  478. break;
  479. case '?':
  480. /* print short usage statement if args not parsable */
  481. usage5 ();
  482. break;
  483. }
  484. }
  485. if(server_address == NULL){
  486. usage4(_("Hostname was not supplied"));
  487. }
  488. return 0;
  489. }
  490. char *perfd_offset (double offset)
  491. {
  492. return sperfdata ("offset", offset, "s",
  493. offset_thresholds->warning_string,
  494. offset_thresholds->critical_string,
  495. FALSE, 0, FALSE, 0);
  496. }
  497. char *perfd_jitter (double jitter)
  498. {
  499. return sperfdata ("jitter", jitter, "",
  500. jitter_thresholds->warning_string,
  501. jitter_thresholds->critical_string,
  502. TRUE, 0, FALSE, 0);
  503. }
  504. char *perfd_stratum (int stratum)
  505. {
  506. return sperfdata_int ("stratum", stratum, "",
  507. stratum_thresholds->warning_string,
  508. stratum_thresholds->critical_string,
  509. TRUE, 0, TRUE, 16);
  510. }
  511. char *perfd_truechimers (int num_truechimers)
  512. {
  513. return sperfdata_int ("truechimers", num_truechimers, "",
  514. truechimer_thresholds->warning_string,
  515. truechimer_thresholds->critical_string,
  516. TRUE, 0, FALSE, 0);
  517. }
  518. int main(int argc, char *argv[]){
  519. int result, offset_result, stratum, num_truechimers, oresult, jresult, sresult, tresult;
  520. double offset=0, jitter=0;
  521. char *result_line, *perfdata_line;
  522. setlocale (LC_ALL, "");
  523. setlocale (LC_NUMERIC, "C");
  524. bindtextdomain (PACKAGE, LOCALEDIR);
  525. textdomain (PACKAGE);
  526. /* Parse extra opts if any */
  527. argv=np_extra_opts (&argc, argv, progname);
  528. if (process_arguments (argc, argv) == ERROR)
  529. usage4 (_("Could not parse arguments"));
  530. set_thresholds(&offset_thresholds, owarn, ocrit);
  531. set_thresholds(&jitter_thresholds, jwarn, jcrit);
  532. set_thresholds(&stratum_thresholds, swarn, scrit);
  533. set_thresholds(&truechimer_thresholds, twarn, tcrit);
  534. /* initialize alarm signal handling */
  535. signal (SIGALRM, socket_timeout_alarm_handler);
  536. /* set socket timeout */
  537. alarm (timeout_interval);
  538. /* This returns either OK or WARNING (See comment preceding ntp_request) */
  539. result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum, &num_truechimers);
  540. if(offset_result == STATE_UNKNOWN) {
  541. /* if there's no sync peer (this overrides ntp_request output): */
  542. result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
  543. } else {
  544. /* Be quiet if there's no candidates either */
  545. if (quiet == 1 && result == STATE_WARNING)
  546. result = STATE_UNKNOWN;
  547. result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
  548. }
  549. oresult = result;
  550. if(do_truechimers) {
  551. tresult = get_status(num_truechimers, truechimer_thresholds);
  552. result = max_state_alt(result, tresult);
  553. }
  554. if(do_stratum) {
  555. sresult = get_status(stratum, stratum_thresholds);
  556. result = max_state_alt(result, sresult);
  557. }
  558. if(do_jitter) {
  559. jresult = get_status(jitter, jitter_thresholds);
  560. result = max_state_alt(result, jresult);
  561. }
  562. switch (result) {
  563. case STATE_CRITICAL :
  564. xasprintf(&result_line, _("NTP CRITICAL:"));
  565. break;
  566. case STATE_WARNING :
  567. xasprintf(&result_line, _("NTP WARNING:"));
  568. break;
  569. case STATE_OK :
  570. xasprintf(&result_line, _("NTP OK:"));
  571. break;
  572. default :
  573. xasprintf(&result_line, _("NTP UNKNOWN:"));
  574. break;
  575. }
  576. if(!syncsource_found)
  577. xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
  578. else if(li_alarm)
  579. xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
  580. if(offset_result == STATE_UNKNOWN){
  581. xasprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
  582. xasprintf(&perfdata_line, "");
  583. } else if (oresult == STATE_WARNING) {
  584. xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), offset);
  585. } else if (oresult == STATE_CRITICAL) {
  586. xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), offset);
  587. } else {
  588. xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
  589. }
  590. xasprintf(&perfdata_line, "%s", perfd_offset(offset));
  591. if (do_jitter) {
  592. if (jresult == STATE_WARNING) {
  593. xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, jitter);
  594. } else if (jresult == STATE_CRITICAL) {
  595. xasprintf(&result_line, "%s, jitter=%f (CRITICAL)", result_line, jitter);
  596. } else {
  597. xasprintf(&result_line, "%s, jitter=%f", result_line, jitter);
  598. }
  599. xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
  600. }
  601. if (do_stratum) {
  602. if (sresult == STATE_WARNING) {
  603. xasprintf(&result_line, "%s, stratum=%i (WARNING)", result_line, stratum);
  604. } else if (sresult == STATE_CRITICAL) {
  605. xasprintf(&result_line, "%s, stratum=%i (CRITICAL)", result_line, stratum);
  606. } else {
  607. xasprintf(&result_line, "%s, stratum=%i", result_line, stratum);
  608. }
  609. xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
  610. }
  611. if (do_truechimers) {
  612. if (tresult == STATE_WARNING) {
  613. xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, num_truechimers);
  614. } else if (tresult == STATE_CRITICAL) {
  615. xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, num_truechimers);
  616. } else {
  617. xasprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers);
  618. }
  619. xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers));
  620. }
  621. printf("%s|%s\n", result_line, perfdata_line);
  622. if(server_address!=NULL) free(server_address);
  623. return result;
  624. }
  625. void print_help(void){
  626. print_revision(progname, NP_VERSION);
  627. printf ("Copyright (c) 2006 Sean Finney\n");
  628. printf (COPYRIGHT, copyright, email);
  629. printf ("%s\n", _("This plugin checks the selected ntp server"));
  630. printf ("\n\n");
  631. print_usage();
  632. printf (UT_HELP_VRSN);
  633. printf (UT_EXTRA_OPTS);
  634. printf (UT_IPv46);
  635. printf (UT_HOST_PORT, 'p', "123");
  636. printf (" %s\n", "-q, --quiet");
  637. printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
  638. printf (" %s\n", "-w, --warning=THRESHOLD");
  639. printf (" %s\n", _("Offset to result in warning status (seconds)"));
  640. printf (" %s\n", "-c, --critical=THRESHOLD");
  641. printf (" %s\n", _("Offset to result in critical status (seconds)"));
  642. printf (" %s\n", "-W, --swarn=THRESHOLD");
  643. printf (" %s\n", _("Warning threshold for stratum of server's synchronization peer"));
  644. printf (" %s\n", "-C, --scrit=THRESHOLD");
  645. printf (" %s\n", _("Critical threshold for stratum of server's synchronization peer"));
  646. printf (" %s\n", "-j, --jwarn=THRESHOLD");
  647. printf (" %s\n", _("Warning threshold for jitter"));
  648. printf (" %s\n", "-k, --jcrit=THRESHOLD");
  649. printf (" %s\n", _("Critical threshold for jitter"));
  650. printf (" %s\n", "-m, --twarn=THRESHOLD");
  651. printf (" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")"));
  652. printf (" %s\n", "-n, --tcrit=THRESHOLD");
  653. printf (" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")"));
  654. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  655. printf (UT_VERBOSE);
  656. printf("\n");
  657. printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
  658. printf("%s\n\n", _("programs or external libraries."));
  659. printf("%s\n", _("Notes:"));
  660. printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
  661. printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
  662. printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
  663. printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
  664. printf("\n");
  665. printf(UT_THRESHOLDS_NOTES);
  666. printf("\n");
  667. printf("%s\n", _("Examples:"));
  668. printf(" %s\n", _("Simple NTP server check:"));
  669. printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
  670. printf("\n");
  671. printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
  672. printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
  673. printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
  674. printf("\n");
  675. printf(" %s\n", _("Only check the number of usable time sources (\"truechimers\"):"));
  676. printf(" %s\n", ("./check_ntp_peer -H ntpserv -m @5 -n @3"));
  677. printf("\n");
  678. printf(" %s\n", _("Check only stratum:"));
  679. printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
  680. printf (UT_SUPPORT);
  681. }
  682. void
  683. print_usage(void)
  684. {
  685. printf ("%s\n", _("Usage:"));
  686. printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
  687. printf(" [-j <warn>] [-k <crit>] [-v verbose]\n");
  688. }