check_ntp_peer.c 23 KB

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