check_ntp_peer.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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 verbose=0;
  49. static int quiet=0;
  50. static short do_offset=0;
  51. static char *owarn="60";
  52. static char *ocrit="120";
  53. static short do_stratum=0;
  54. static char *swarn="-1:16";
  55. static char *scrit="-1:16";
  56. static short do_jitter=0;
  57. static char *jwarn="-1:5000";
  58. static char *jcrit="-1:10000";
  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. void print_help (void);
  66. void print_usage (void);
  67. /* max size of control message data */
  68. #define MAX_CM_SIZE 468
  69. /* this structure holds everything in an ntp control message as per rfc1305 */
  70. typedef struct {
  71. uint8_t flags; /* byte with leapindicator,vers,mode. see macros */
  72. uint8_t op; /* R,E,M bits and Opcode */
  73. uint16_t seq; /* Packet sequence */
  74. uint16_t status; /* Clock status */
  75. uint16_t assoc; /* Association */
  76. uint16_t offset; /* Similar to TCP sequence # */
  77. uint16_t count; /* # bytes of data */
  78. char data[MAX_CM_SIZE]; /* ASCII data of the request */
  79. /* NB: not necessarily NULL terminated! */
  80. } ntp_control_message;
  81. /* this is an association/status-word pair found in control packet reponses */
  82. typedef struct {
  83. uint16_t assoc;
  84. uint16_t status;
  85. } ntp_assoc_status_pair;
  86. /* bits 1,2 are the leap indicator */
  87. #define LI_MASK 0xc0
  88. #define LI(x) ((x&LI_MASK)>>6)
  89. #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0)
  90. /* and these are the values of the leap indicator */
  91. #define LI_NOWARNING 0x00
  92. #define LI_EXTRASEC 0x01
  93. #define LI_MISSINGSEC 0x02
  94. #define LI_ALARM 0x03
  95. /* bits 3,4,5 are the ntp version */
  96. #define VN_MASK 0x38
  97. #define VN(x) ((x&VN_MASK)>>3)
  98. #define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0)
  99. #define VN_RESERVED 0x02
  100. /* bits 6,7,8 are the ntp mode */
  101. #define MODE_MASK 0x07
  102. #define MODE(x) (x&MODE_MASK)
  103. #define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0)
  104. /* here are some values */
  105. #define MODE_CLIENT 0x03
  106. #define MODE_CONTROLMSG 0x06
  107. /* In control message, bits 8-10 are R,E,M bits */
  108. #define REM_MASK 0xe0
  109. #define REM_RESP 0x80
  110. #define REM_ERROR 0x40
  111. #define REM_MORE 0x20
  112. /* In control message, bits 11 - 15 are opcode */
  113. #define OP_MASK 0x1f
  114. #define OP_SET(x,y) do{ x |= (y&OP_MASK); }while(0)
  115. #define OP_READSTAT 0x01
  116. #define OP_READVAR 0x02
  117. /* In peer status bytes, bits 6,7,8 determine clock selection status */
  118. #define PEER_SEL(x) ((ntohs(x)>>8)&0x07)
  119. #define PEER_INCLUDED 0x04
  120. #define PEER_SYNCSOURCE 0x06
  121. /* NTP control message header is 12 bytes, plus any data in the data
  122. * field, plus null padding to the nearest 32-bit boundary per rfc.
  123. */
  124. #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((m.count)?4-(ntohs(m.count)%4):0))
  125. /* finally, a little helper or two for debugging: */
  126. #define DBG(x) do{if(verbose>1){ x; }}while(0);
  127. #define PRINTSOCKADDR(x) \
  128. do{ \
  129. printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\
  130. }while(0);
  131. void print_ntp_control_message(const ntp_control_message *p){
  132. int i=0, numpeers=0;
  133. const ntp_assoc_status_pair *peer=NULL;
  134. printf("control packet contents:\n");
  135. printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op);
  136. printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK);
  137. printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK);
  138. printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK);
  139. printf("\t response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP);
  140. printf("\t more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE);
  141. printf("\t error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR);
  142. printf("\t op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK);
  143. printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq));
  144. printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status));
  145. printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc));
  146. printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset));
  147. printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count));
  148. numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair));
  149. if(p->op&REM_RESP && p->op&OP_READSTAT){
  150. peer=(ntp_assoc_status_pair*)p->data;
  151. for(i=0;i<numpeers;i++){
  152. printf("\tpeer id %.2x status %.2x",
  153. ntohs(peer[i].assoc), ntohs(peer[i].status));
  154. if (PEER_SEL(peer[i].status) >= PEER_INCLUDED){
  155. if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){
  156. printf(" <-- current sync source");
  157. } else {
  158. printf(" <-- current sync candidate");
  159. }
  160. }
  161. printf("\n");
  162. }
  163. }
  164. }
  165. /*
  166. * Extract the value from NTP key/value pairs, or return NULL.
  167. * The value returned can be free()ed.
  168. */
  169. char *extract_value(const char *varlist, const char *name){
  170. char *tmp=NULL, *value=NULL;
  171. int i;
  172. while (1) {
  173. /* Strip any leading space */
  174. for (varlist; isspace(varlist[0]); varlist++);
  175. if (strncmp(name, varlist, strlen(name)) == 0) {
  176. varlist += strlen(name);
  177. /* strip trailing spaces */
  178. for (varlist; isspace(varlist[0]); varlist++);
  179. if (varlist[0] == '=') {
  180. /* We matched the key, go past the = sign */
  181. varlist++;
  182. /* strip leading spaces */
  183. for (varlist; isspace(varlist[0]); varlist++);
  184. if (tmp = index(varlist, ',')) {
  185. /* Value is delimited by a comma */
  186. if (tmp-varlist == 0) continue;
  187. value = (char *)malloc(tmp-varlist+1);
  188. strncpy(value, varlist, tmp-varlist);
  189. value[tmp-varlist] = '\0';
  190. } else {
  191. /* Value is delimited by a \0 */
  192. if (strlen(varlist) == 0) continue;
  193. value = (char *)malloc(strlen(varlist) + 1);
  194. strncpy(value, varlist, strlen(varlist));
  195. value[strlen(varlist)] = '\0';
  196. }
  197. break;
  198. }
  199. }
  200. if (tmp = index(varlist, ',')) {
  201. /* More keys, keep going... */
  202. varlist = tmp + 1;
  203. } else {
  204. /* We're done */
  205. break;
  206. }
  207. }
  208. /* Clean-up trailing spaces/newlines */
  209. if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
  210. return value;
  211. }
  212. void
  213. setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
  214. memset(p, 0, sizeof(ntp_control_message));
  215. LI_SET(p->flags, LI_NOWARNING);
  216. VN_SET(p->flags, VN_RESERVED);
  217. MODE_SET(p->flags, MODE_CONTROLMSG);
  218. OP_SET(p->op, opcode);
  219. p->seq = htons(seq);
  220. /* Remaining fields are zero for requests */
  221. }
  222. /* This function does all the actual work; roughly here's what it does
  223. * beside setting the offest, jitter and stratum passed as argument:
  224. * - offset can be negative, so if it cannot get the offset, offset_result
  225. * is set to UNKNOWN, otherwise OK.
  226. * - jitter and stratum are set to -1 if they cannot be retrieved so any
  227. * positive value means a success retrieving the value.
  228. * - status is set to WARNING if there's no sync.peer (otherwise OK) and is
  229. * the return value of the function.
  230. * status is pretty much useless as syncsource_found is a global variable
  231. * used later in main to check is the server was synchronized. It works
  232. * so I left it alone */
  233. int ntp_request(const char *host, double *offset, int *offset_result, double *jitter, int *stratum){
  234. int conn=-1, i, npeers=0, num_candidates=0;
  235. double tmp_offset = 0;
  236. int min_peer_sel=PEER_INCLUDED;
  237. int peers_size=0, peer_offset=0;
  238. int status;
  239. ntp_assoc_status_pair *peers=NULL;
  240. ntp_control_message req;
  241. const char *getvar = "stratum,offset,jitter";
  242. char *data, *value, *nptr;
  243. void *tmp;
  244. status = STATE_OK;
  245. *offset_result = STATE_UNKNOWN;
  246. *jitter = *stratum = -1;
  247. /* Long-winded explanation:
  248. * Getting the sync peer offset, jitter and stratum requires a number of
  249. * steps:
  250. * 1) Send a READSTAT request.
  251. * 2) Interpret the READSTAT reply
  252. * a) The data section contains a list of peer identifiers (16 bits)
  253. * and associated status words (16 bits)
  254. * b) We want the value of 0x06 in the SEL (peer selection) value,
  255. * which means "current synchronizatin source". If that's missing,
  256. * we take anything better than 0x04 (see the rfc for details) but
  257. * set a minimum of warning.
  258. * 3) Send a READVAR request for information on each peer identified
  259. * in 2b greater than the minimum selection value.
  260. * 4) Extract the offset, jitter and stratum value from the data[]
  261. * (it's ASCII)
  262. */
  263. my_udp_connect(server_address, 123, &conn);
  264. /* keep sending requests until the server stops setting the
  265. * REM_MORE bit, though usually this is only 1 packet. */
  266. do{
  267. setup_control_request(&req, OP_READSTAT, 1);
  268. DBG(printf("sending READSTAT request"));
  269. write(conn, &req, SIZEOF_NTPCM(req));
  270. DBG(print_ntp_control_message(&req));
  271. /* Attempt to read the largest size packet possible */
  272. req.count=htons(MAX_CM_SIZE);
  273. DBG(printf("recieving READSTAT response"))
  274. if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
  275. die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
  276. DBG(print_ntp_control_message(&req));
  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. {0, 0, 0, 0}
  435. };
  436. if (argc < 2)
  437. usage ("\n");
  438. while (1) {
  439. c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:t:H:", longopts, &option);
  440. if (c == -1 || c == EOF || c == 1)
  441. break;
  442. switch (c) {
  443. case 'h':
  444. print_help();
  445. exit(STATE_OK);
  446. break;
  447. case 'V':
  448. print_revision(progname, revision);
  449. exit(STATE_OK);
  450. break;
  451. case 'v':
  452. verbose++;
  453. break;
  454. case 'q':
  455. quiet = 1;
  456. break;
  457. case 'w':
  458. do_offset=1;
  459. owarn = optarg;
  460. break;
  461. case 'c':
  462. do_offset=1;
  463. ocrit = optarg;
  464. break;
  465. case 'W':
  466. do_stratum=1;
  467. swarn = optarg;
  468. break;
  469. case 'C':
  470. do_stratum=1;
  471. scrit = optarg;
  472. break;
  473. case 'j':
  474. do_jitter=1;
  475. jwarn = optarg;
  476. break;
  477. case 'k':
  478. do_jitter=1;
  479. jcrit = optarg;
  480. break;
  481. case 'H':
  482. if(is_host(optarg) == FALSE)
  483. usage2(_("Invalid hostname/address"), optarg);
  484. server_address = strdup(optarg);
  485. break;
  486. case 't':
  487. socket_timeout=atoi(optarg);
  488. break;
  489. case '4':
  490. address_family = AF_INET;
  491. break;
  492. case '6':
  493. #ifdef USE_IPV6
  494. address_family = AF_INET6;
  495. #else
  496. usage4 (_("IPv6 support not available"));
  497. #endif
  498. break;
  499. case '?':
  500. /* print short usage statement if args not parsable */
  501. usage5 ();
  502. break;
  503. }
  504. }
  505. if(server_address == NULL){
  506. usage4(_("Hostname was not supplied"));
  507. }
  508. return 0;
  509. }
  510. char *perfd_offset (double offset)
  511. {
  512. return fperfdata ("offset", offset, "s",
  513. TRUE, offset_thresholds->warning->end,
  514. TRUE, offset_thresholds->critical->end,
  515. FALSE, 0, FALSE, 0);
  516. }
  517. char *perfd_jitter (double jitter)
  518. {
  519. return fperfdata ("jitter", jitter, "",
  520. do_jitter, jitter_thresholds->warning->end,
  521. do_jitter, jitter_thresholds->critical->end,
  522. TRUE, 0, FALSE, 0);
  523. }
  524. char *perfd_stratum (int stratum)
  525. {
  526. return perfdata ("stratum", stratum, "",
  527. do_stratum, (int)stratum_thresholds->warning->end,
  528. do_stratum, (int)stratum_thresholds->critical->end,
  529. TRUE, 0, TRUE, 16);
  530. }
  531. int main(int argc, char *argv[]){
  532. int result, offset_result, stratum;
  533. double offset=0, jitter=0;
  534. char *result_line, *perfdata_line;
  535. setlocale (LC_ALL, "");
  536. bindtextdomain (PACKAGE, LOCALEDIR);
  537. textdomain (PACKAGE);
  538. /* Parse extra opts if any */
  539. argv=np_extra_opts (&argc, argv, progname);
  540. if (process_arguments (argc, argv) == ERROR)
  541. usage4 (_("Could not parse arguments"));
  542. set_thresholds(&offset_thresholds, owarn, ocrit);
  543. set_thresholds(&jitter_thresholds, jwarn, jcrit);
  544. set_thresholds(&stratum_thresholds, swarn, scrit);
  545. /* initialize alarm signal handling */
  546. signal (SIGALRM, socket_timeout_alarm_handler);
  547. /* set socket timeout */
  548. alarm (socket_timeout);
  549. /* This returns either OK or WARNING (See comment preceeding ntp_request) */
  550. result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum);
  551. if(offset_result == STATE_UNKNOWN) {
  552. /* if there's no sync peer (this overrides ntp_request output): */
  553. result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
  554. } else {
  555. /* Be quiet if there's no candidates either */
  556. if (quiet == 1 && result == STATE_WARNING)
  557. result = STATE_UNKNOWN;
  558. result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
  559. }
  560. if(do_stratum)
  561. result = max_state_alt(result, get_status(stratum, stratum_thresholds));
  562. if(do_jitter)
  563. result = max_state_alt(result, get_status(jitter, jitter_thresholds));
  564. switch (result) {
  565. case STATE_CRITICAL :
  566. asprintf(&result_line, _("NTP CRITICAL:"));
  567. break;
  568. case STATE_WARNING :
  569. asprintf(&result_line, _("NTP WARNING:"));
  570. break;
  571. case STATE_OK :
  572. asprintf(&result_line, _("NTP OK:"));
  573. break;
  574. default :
  575. asprintf(&result_line, _("NTP UNKNOWN:"));
  576. break;
  577. }
  578. if(!syncsource_found)
  579. asprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
  580. else if(li_alarm)
  581. asprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
  582. if(offset_result == STATE_UNKNOWN){
  583. asprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
  584. asprintf(&perfdata_line, "");
  585. } else {
  586. asprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
  587. asprintf(&perfdata_line, "%s", perfd_offset(offset));
  588. }
  589. if (do_jitter) {
  590. asprintf(&result_line, "%s, jitter=%f", result_line, jitter);
  591. asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
  592. }
  593. if (do_stratum) {
  594. asprintf(&result_line, "%s, stratum=%i", result_line, stratum);
  595. asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
  596. }
  597. printf("%s|%s\n", result_line, perfdata_line);
  598. if(server_address!=NULL) free(server_address);
  599. return result;
  600. }
  601. void print_help(void){
  602. print_revision(progname, revision);
  603. printf ("Copyright (c) 2006 Sean Finney\n");
  604. printf (COPYRIGHT, copyright, email);
  605. printf ("%s\n", _("This plugin checks the selected ntp server"));
  606. printf ("\n\n");
  607. print_usage();
  608. printf (_(UT_HELP_VRSN));
  609. printf (_(UT_EXTRA_OPTS));
  610. printf (_(UT_HOST_PORT), 'p', "123");
  611. printf (" %s\n", "-q, --quiet");
  612. printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
  613. printf (" %s\n", "-w, --warning=THRESHOLD");
  614. printf (" %s\n", _("Offset to result in warning status (seconds)"));
  615. printf (" %s\n", "-c, --critical=THRESHOLD");
  616. printf (" %s\n", _("Offset to result in critical status (seconds)"));
  617. printf (" %s\n", "-W, --swarn=THRESHOLD");
  618. printf (" %s\n", _("Warning threshold for stratum"));
  619. printf (" %s\n", "-C, --scrit=THRESHOLD");
  620. printf (" %s\n", _("Critical threshold for stratum"));
  621. printf (" %s\n", "-j, --jwarn=THRESHOLD");
  622. printf (" %s\n", _("Warning threshold for jitter"));
  623. printf (" %s\n", "-k, --jcrit=THRESHOLD");
  624. printf (" %s\n", _("Critical threshold for jitter"));
  625. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  626. printf (_(UT_VERBOSE));
  627. printf("\n");
  628. printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
  629. printf("%s\n\n", _("programs or external libraries."));
  630. printf("%s\n", _("Notes:"));
  631. printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
  632. printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
  633. printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
  634. printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
  635. printf("\n");
  636. printf(_(UT_THRESHOLDS_NOTES));
  637. #ifdef NP_EXTRA_OPTS
  638. printf("\n");
  639. printf(_(UT_EXTRA_OPTS_NOTES));
  640. #endif
  641. printf("\n");
  642. printf("%s\n", _("Examples:"));
  643. printf(" %s\n", _("Simple NTP server check:"));
  644. printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
  645. printf("\n");
  646. printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
  647. printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
  648. printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
  649. printf("\n");
  650. printf(" %s\n", _("Check only stratum:"));
  651. printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
  652. printf (_(UT_SUPPORT));
  653. }
  654. void
  655. print_usage(void)
  656. {
  657. printf (_("Usage:"));
  658. printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
  659. printf(" [-j <warn>] [-k <crit>] [-v verbose]\n");
  660. }