check_ntp_peer.c 23 KB

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