check_ntp_peer.c 22 KB

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