diff options
-rw-r--r-- | plugins/check_ntpd.c | 8 | ||||
-rw-r--r-- | plugins/check_time_ntp.c | 335 |
2 files changed, 20 insertions, 323 deletions
diff --git a/plugins/check_ntpd.c b/plugins/check_ntpd.c index 2ac2a553..84ebe34d 100644 --- a/plugins/check_ntpd.c +++ b/plugins/check_ntpd.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /****************************************************************************** | 1 | /****************************************************************************** |
2 | * | 2 | * |
3 | * Nagios check_ntp plugin | 3 | * Nagios check_ntpd plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2006 sean finney <seanius@seanius.net> | 6 | * Copyright (c) 2006 sean finney <seanius@seanius.net> |
@@ -593,12 +593,12 @@ void print_help(void){ | |||
593 | printf("\n"); | 593 | printf("\n"); |
594 | printf("%s\n", _("Examples:")); | 594 | printf("%s\n", _("Examples:")); |
595 | printf(" %s\n", _("Normal offset check:")); | 595 | printf(" %s\n", _("Normal offset check:")); |
596 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1")); | 596 | printf(" %s\n", ("./check_ntpd -H ntpserv -w 0.5 -c 1")); |
597 | printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available")); | 597 | printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available")); |
598 | printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); | 598 | printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); |
599 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); | 599 | printf(" %s\n", ("./check_ntpd -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); |
600 | printf(" %s\n", _("Check only stratum:")); | 600 | printf(" %s\n", _("Check only stratum:")); |
601 | printf(" %s\n", ("./check_ntp -H ntpserv -W 4 -C 6")); | 601 | printf(" %s\n", ("./check_ntpd -H ntpserv -W 4 -C 6")); |
602 | 602 | ||
603 | printf (_(UT_SUPPORT)); | 603 | printf (_(UT_SUPPORT)); |
604 | } | 604 | } |
diff --git a/plugins/check_time_ntp.c b/plugins/check_time_ntp.c index d23987ba..ac332acb 100644 --- a/plugins/check_time_ntp.c +++ b/plugins/check_time_ntp.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /****************************************************************************** | 1 | /****************************************************************************** |
2 | * TODO: check offset only (remove all NTP control packet stuff) | 2 | * |
3 | * Nagios check_time_ntp plugin | 3 | * Nagios check_time_ntp plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
@@ -12,8 +12,8 @@ | |||
12 | * | 12 | * |
13 | * This file contains the check_time_ntp plugin | 13 | * This file contains the check_time_ntp plugin |
14 | * | 14 | * |
15 | * This plugin to check ntp servers independant of any commandline | 15 | * This plugin checks the clock offset with an NTP server. It is |
16 | * programs or external libraries. | 16 | * independant of any commandline programs or external libraries. |
17 | * | 17 | * |
18 | * | 18 | * |
19 | * License Information: | 19 | * License Information: |
@@ -36,7 +36,7 @@ | |||
36 | 36 | ||
37 | *****************************************************************************/ | 37 | *****************************************************************************/ |
38 | 38 | ||
39 | const char *progname = "check_ntp"; | 39 | const char *progname = "check_time_ntp"; |
40 | const char *revision = "$Revision$"; | 40 | const char *revision = "$Revision$"; |
41 | const char *copyright = "2007"; | 41 | const char *copyright = "2007"; |
42 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | 42 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; |
@@ -47,20 +47,11 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; | |||
47 | 47 | ||
48 | static char *server_address=NULL; | 48 | static char *server_address=NULL; |
49 | static int verbose=0; | 49 | static int verbose=0; |
50 | static short do_offset=0; | ||
51 | static char *owarn="60"; | 50 | static char *owarn="60"; |
52 | static char *ocrit="120"; | 51 | static char *ocrit="120"; |
53 | static short do_stratum=0; | ||
54 | static char *swarn="16"; | ||
55 | static char *scrit="16"; | ||
56 | static short do_jitter=0; | ||
57 | static char *jwarn="5000"; | ||
58 | static char *jcrit="10000"; | ||
59 | 52 | ||
60 | int process_arguments (int, char **); | 53 | int process_arguments (int, char **); |
61 | thresholds *offset_thresholds = NULL; | 54 | thresholds *offset_thresholds = NULL; |
62 | thresholds *jitter_thresholds = NULL; | ||
63 | thresholds *stratum_thresholds = NULL; | ||
64 | void print_help (void); | 55 | void print_help (void); |
65 | void print_usage (void); | 56 | void print_usage (void); |
66 | 57 | ||
@@ -96,25 +87,6 @@ typedef struct { | |||
96 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | 87 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ |
97 | } ntp_server_results; | 88 | } ntp_server_results; |
98 | 89 | ||
99 | /* this structure holds everything in an ntp control message as per rfc1305 */ | ||
100 | typedef struct { | ||
101 | uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ | ||
102 | uint8_t op; /* R,E,M bits and Opcode */ | ||
103 | uint16_t seq; /* Packet sequence */ | ||
104 | uint16_t status; /* Clock status */ | ||
105 | uint16_t assoc; /* Association */ | ||
106 | uint16_t offset; /* Similar to TCP sequence # */ | ||
107 | uint16_t count; /* # bytes of data */ | ||
108 | char data[MAX_CM_SIZE]; /* ASCII data of the request */ | ||
109 | /* NB: not necessarily NULL terminated! */ | ||
110 | } ntp_control_message; | ||
111 | |||
112 | /* this is an association/status-word pair found in control packet reponses */ | ||
113 | typedef struct { | ||
114 | uint16_t assoc; | ||
115 | uint16_t status; | ||
116 | } ntp_assoc_status_pair; | ||
117 | |||
118 | /* bits 1,2 are the leap indicator */ | 90 | /* bits 1,2 are the leap indicator */ |
119 | #define LI_MASK 0xc0 | 91 | #define LI_MASK 0xc0 |
120 | #define LI(x) ((x&LI_MASK)>>6) | 92 | #define LI(x) ((x&LI_MASK)>>6) |
@@ -250,42 +222,6 @@ void print_ntp_message(const ntp_message *p){ | |||
250 | printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts)); | 222 | printf("\ttxts = %-.16g\n", NTP64asDOUBLE(p->txts)); |
251 | } | 223 | } |
252 | 224 | ||
253 | void print_ntp_control_message(const ntp_control_message *p){ | ||
254 | int i=0, numpeers=0; | ||
255 | const ntp_assoc_status_pair *peer=NULL; | ||
256 | |||
257 | printf("control packet contents:\n"); | ||
258 | printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); | ||
259 | printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK); | ||
260 | printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK); | ||
261 | printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK); | ||
262 | printf("\t response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP); | ||
263 | printf("\t more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE); | ||
264 | printf("\t error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR); | ||
265 | printf("\t op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK); | ||
266 | printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq)); | ||
267 | printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status)); | ||
268 | printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); | ||
269 | printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); | ||
270 | printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); | ||
271 | numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair)); | ||
272 | if(p->op&REM_RESP && p->op&OP_READSTAT){ | ||
273 | peer=(ntp_assoc_status_pair*)p->data; | ||
274 | for(i=0;i<numpeers;i++){ | ||
275 | printf("\tpeer id %.2x status %.2x", | ||
276 | ntohs(peer[i].assoc), ntohs(peer[i].status)); | ||
277 | if (PEER_SEL(peer[i].status) >= PEER_INCLUDED){ | ||
278 | if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){ | ||
279 | printf(" <-- current sync source"); | ||
280 | } else { | ||
281 | printf(" <-- current sync candidate"); | ||
282 | } | ||
283 | } | ||
284 | printf("\n"); | ||
285 | } | ||
286 | } | ||
287 | } | ||
288 | |||
289 | void setup_request(ntp_message *p){ | 225 | void setup_request(ntp_message *p){ |
290 | struct timeval t; | 226 | struct timeval t; |
291 | 227 | ||
@@ -361,7 +297,7 @@ int best_offset_server(const ntp_server_results *slist, int nservers){ | |||
361 | * we don't waste time sitting around waiting for single packets. | 297 | * we don't waste time sitting around waiting for single packets. |
362 | * - we also "manually" handle resolving host names and connecting, because | 298 | * - we also "manually" handle resolving host names and connecting, because |
363 | * we have to do it in a way that our lazy macros don't handle currently :( */ | 299 | * we have to do it in a way that our lazy macros don't handle currently :( */ |
364 | double offset_request(const char *host, int *stratum, int *status){ | 300 | double offset_request(const char *host, int *status){ |
365 | int i=0, j=0, ga_result=0, num_hosts=0, *socklist=NULL, respnum=0; | 301 | int i=0, j=0, ga_result=0, num_hosts=0, *socklist=NULL, respnum=0; |
366 | int servers_completed=0, one_written=0, one_read=0, servers_readable=0, best_index=-1; | 302 | int servers_completed=0, one_written=0, one_read=0, servers_readable=0, best_index=-1; |
367 | time_t now_time=0, start_ts=0; | 303 | time_t now_time=0, start_ts=0; |
@@ -458,7 +394,7 @@ double offset_request(const char *host, int *stratum, int *status){ | |||
458 | respnum=servers[i].num_responses++; | 394 | respnum=servers[i].num_responses++; |
459 | servers[i].offset[respnum]=calc_offset(&req[i], &recv_time); | 395 | servers[i].offset[respnum]=calc_offset(&req[i], &recv_time); |
460 | if(verbose) { | 396 | if(verbose) { |
461 | printf("offset %.10g, stratum %i\n", servers[i].offset[respnum], req[i].stratum); | 397 | printf("offset %.10g\n", servers[i].offset[respnum]); |
462 | } | 398 | } |
463 | servers[i].stratum=req[i].stratum; | 399 | servers[i].stratum=req[i].stratum; |
464 | servers[i].rtdisp=NTP32asDOUBLE(req[i].rtdisp); | 400 | servers[i].rtdisp=NTP32asDOUBLE(req[i].rtdisp); |
@@ -487,14 +423,10 @@ double offset_request(const char *host, int *stratum, int *status){ | |||
487 | avg_offset+=servers[best_index].offset[j]; | 423 | avg_offset+=servers[best_index].offset[j]; |
488 | } | 424 | } |
489 | avg_offset/=servers[best_index].num_responses; | 425 | avg_offset/=servers[best_index].num_responses; |
490 | *stratum = servers[best_index].stratum; | ||
491 | } | 426 | } |
492 | 427 | ||
493 | /* cleanup */ | 428 | /* cleanup */ |
494 | /* FIXME: Not closing the socket to avoid re-use of the local port | 429 | for(j=0; j<num_hosts; j++){ close(socklist[j]); } |
495 | * which can cause old NTP packets to be read instead of NTP control | ||
496 | * pactets in jitter_request(). THERE MUST BE ANOTHER WAY... | ||
497 | * for(j=0; j<num_hosts; j++){ close(socklist[j]); } */ | ||
498 | free(socklist); | 430 | free(socklist); |
499 | free(ufds); | 431 | free(ufds); |
500 | free(servers); | 432 | free(servers); |
@@ -505,155 +437,6 @@ double offset_request(const char *host, int *stratum, int *status){ | |||
505 | return avg_offset; | 437 | return avg_offset; |
506 | } | 438 | } |
507 | 439 | ||
508 | void | ||
509 | setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ | ||
510 | memset(p, 0, sizeof(ntp_control_message)); | ||
511 | LI_SET(p->flags, LI_NOWARNING); | ||
512 | VN_SET(p->flags, VN_RESERVED); | ||
513 | MODE_SET(p->flags, MODE_CONTROLMSG); | ||
514 | OP_SET(p->op, opcode); | ||
515 | p->seq = htons(seq); | ||
516 | /* Remaining fields are zero for requests */ | ||
517 | } | ||
518 | |||
519 | /* XXX handle responses with the error bit set */ | ||
520 | double jitter_request(const char *host, int *status){ | ||
521 | int conn=-1, i, npeers=0, num_candidates=0, syncsource_found=0; | ||
522 | int run=0, min_peer_sel=PEER_INCLUDED, num_selected=0, num_valid=0; | ||
523 | int peers_size=0, peer_offset=0; | ||
524 | ntp_assoc_status_pair *peers=NULL; | ||
525 | ntp_control_message req; | ||
526 | const char *getvar = "jitter"; | ||
527 | double rval = 0.0, jitter = -1.0; | ||
528 | char *startofvalue=NULL, *nptr=NULL; | ||
529 | void *tmp; | ||
530 | |||
531 | /* Long-winded explanation: | ||
532 | * Getting the jitter requires a number of steps: | ||
533 | * 1) Send a READSTAT request. | ||
534 | * 2) Interpret the READSTAT reply | ||
535 | * a) The data section contains a list of peer identifiers (16 bits) | ||
536 | * and associated status words (16 bits) | ||
537 | * b) We want the value of 0x06 in the SEL (peer selection) value, | ||
538 | * which means "current synchronizatin source". If that's missing, | ||
539 | * we take anything better than 0x04 (see the rfc for details) but | ||
540 | * set a minimum of warning. | ||
541 | * 3) Send a READVAR request for information on each peer identified | ||
542 | * in 2b greater than the minimum selection value. | ||
543 | * 4) Extract the jitter value from the data[] (it's ASCII) | ||
544 | */ | ||
545 | my_udp_connect(server_address, 123, &conn); | ||
546 | |||
547 | /* keep sending requests until the server stops setting the | ||
548 | * REM_MORE bit, though usually this is only 1 packet. */ | ||
549 | do{ | ||
550 | setup_control_request(&req, OP_READSTAT, 1); | ||
551 | DBG(printf("sending READSTAT request")); | ||
552 | write(conn, &req, SIZEOF_NTPCM(req)); | ||
553 | DBG(print_ntp_control_message(&req)); | ||
554 | /* Attempt to read the largest size packet possible */ | ||
555 | req.count=htons(MAX_CM_SIZE); | ||
556 | DBG(printf("recieving READSTAT response")) | ||
557 | read(conn, &req, SIZEOF_NTPCM(req)); | ||
558 | DBG(print_ntp_control_message(&req)); | ||
559 | /* Each peer identifier is 4 bytes in the data section, which | ||
560 | * we represent as a ntp_assoc_status_pair datatype. | ||
561 | */ | ||
562 | peers_size+=ntohs(req.count); | ||
563 | if((tmp=realloc(peers, peers_size)) == NULL) | ||
564 | free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); | ||
565 | peers=tmp; | ||
566 | memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count)); | ||
567 | npeers=peers_size/sizeof(ntp_assoc_status_pair); | ||
568 | peer_offset+=ntohs(req.count); | ||
569 | } while(req.op&REM_MORE); | ||
570 | |||
571 | /* first, let's find out if we have a sync source, or if there are | ||
572 | * at least some candidates. in the case of the latter we'll issue | ||
573 | * a warning but go ahead with the check on them. */ | ||
574 | for (i = 0; i < npeers; i++){ | ||
575 | if (PEER_SEL(peers[i].status) >= PEER_INCLUDED){ | ||
576 | num_candidates++; | ||
577 | if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){ | ||
578 | syncsource_found=1; | ||
579 | min_peer_sel=PEER_SYNCSOURCE; | ||
580 | } | ||
581 | } | ||
582 | } | ||
583 | if(verbose) printf("%d candiate peers available\n", num_candidates); | ||
584 | if(verbose && syncsource_found) printf("synchronization source found\n"); | ||
585 | if(! syncsource_found){ | ||
586 | *status = STATE_UNKNOWN; | ||
587 | if(verbose) printf("warning: no synchronization source found\n"); | ||
588 | } | ||
589 | |||
590 | |||
591 | for (run=0; run<AVG_NUM; run++){ | ||
592 | if(verbose) printf("jitter run %d of %d\n", run+1, AVG_NUM); | ||
593 | for (i = 0; i < npeers; i++){ | ||
594 | /* Only query this server if it is the current sync source */ | ||
595 | if (PEER_SEL(peers[i].status) >= min_peer_sel){ | ||
596 | num_selected++; | ||
597 | setup_control_request(&req, OP_READVAR, 2); | ||
598 | req.assoc = peers[i].assoc; | ||
599 | /* By spec, putting the variable name "jitter" in the request | ||
600 | * should cause the server to provide _only_ the jitter value. | ||
601 | * thus reducing net traffic, guaranteeing us only a single | ||
602 | * datagram in reply, and making intepretation much simpler | ||
603 | */ | ||
604 | /* Older servers doesn't know what jitter is, so if we get an | ||
605 | * error on the first pass we redo it with "dispersion" */ | ||
606 | strncpy(req.data, getvar, MAX_CM_SIZE-1); | ||
607 | req.count = htons(strlen(getvar)); | ||
608 | DBG(printf("sending READVAR request...\n")); | ||
609 | write(conn, &req, SIZEOF_NTPCM(req)); | ||
610 | DBG(print_ntp_control_message(&req)); | ||
611 | |||
612 | req.count = htons(MAX_CM_SIZE); | ||
613 | DBG(printf("recieving READVAR response...\n")); | ||
614 | read(conn, &req, SIZEOF_NTPCM(req)); | ||
615 | DBG(print_ntp_control_message(&req)); | ||
616 | |||
617 | if(req.op&REM_ERROR && strstr(getvar, "jitter")) { | ||
618 | if(verbose) printf("The 'jitter' command failed (old ntp server?)\nRestarting with 'dispersion'...\n"); | ||
619 | getvar = "dispersion"; | ||
620 | num_selected--; | ||
621 | i--; | ||
622 | continue; | ||
623 | } | ||
624 | |||
625 | /* get to the float value */ | ||
626 | if(verbose) { | ||
627 | printf("parsing jitter from peer %.2x: ", ntohs(peers[i].assoc)); | ||
628 | } | ||
629 | startofvalue = strchr(req.data, '='); | ||
630 | if(startofvalue != NULL) { | ||
631 | startofvalue++; | ||
632 | jitter = strtod(startofvalue, &nptr); | ||
633 | } | ||
634 | if(startofvalue == NULL || startofvalue==nptr){ | ||
635 | printf("warning: unable to read server jitter response.\n"); | ||
636 | *status = STATE_UNKNOWN; | ||
637 | } else { | ||
638 | if(verbose) printf("%g\n", jitter); | ||
639 | num_valid++; | ||
640 | rval += jitter; | ||
641 | } | ||
642 | } | ||
643 | } | ||
644 | if(verbose){ | ||
645 | printf("jitter parsed from %d/%d peers\n", num_valid, num_selected); | ||
646 | } | ||
647 | } | ||
648 | |||
649 | rval = num_valid ? rval / num_valid : -1.0; | ||
650 | |||
651 | close(conn); | ||
652 | if(peers!=NULL) free(peers); | ||
653 | /* If we return -1.0, it means no synchronization source was found */ | ||
654 | return rval; | ||
655 | } | ||
656 | |||
657 | int process_arguments(int argc, char **argv){ | 440 | int process_arguments(int argc, char **argv){ |
658 | int c; | 441 | int c; |
659 | int option=0; | 442 | int option=0; |
@@ -665,10 +448,6 @@ int process_arguments(int argc, char **argv){ | |||
665 | {"use-ipv6", no_argument, 0, '6'}, | 448 | {"use-ipv6", no_argument, 0, '6'}, |
666 | {"warning", required_argument, 0, 'w'}, | 449 | {"warning", required_argument, 0, 'w'}, |
667 | {"critical", required_argument, 0, 'c'}, | 450 | {"critical", required_argument, 0, 'c'}, |
668 | {"swarn", required_argument, 0, 'W'}, | ||
669 | {"scrit", required_argument, 0, 'C'}, | ||
670 | {"jwarn", required_argument, 0, 'j'}, | ||
671 | {"jcrit", required_argument, 0, 'k'}, | ||
672 | {"timeout", required_argument, 0, 't'}, | 451 | {"timeout", required_argument, 0, 't'}, |
673 | {"hostname", required_argument, 0, 'H'}, | 452 | {"hostname", required_argument, 0, 'H'}, |
674 | {0, 0, 0, 0} | 453 | {0, 0, 0, 0} |
@@ -679,7 +458,7 @@ int process_arguments(int argc, char **argv){ | |||
679 | usage ("\n"); | 458 | usage ("\n"); |
680 | 459 | ||
681 | while (1) { | 460 | while (1) { |
682 | c = getopt_long (argc, argv, "Vhv46w:c:W:C:j:k:t:H:", longopts, &option); | 461 | c = getopt_long (argc, argv, "Vhv46w:c:t:H:", longopts, &option); |
683 | if (c == -1 || c == EOF || c == 1) | 462 | if (c == -1 || c == EOF || c == 1) |
684 | break; | 463 | break; |
685 | 464 | ||
@@ -696,29 +475,11 @@ int process_arguments(int argc, char **argv){ | |||
696 | verbose++; | 475 | verbose++; |
697 | break; | 476 | break; |
698 | case 'w': | 477 | case 'w': |
699 | do_offset=1; | ||
700 | owarn = optarg; | 478 | owarn = optarg; |
701 | break; | 479 | break; |
702 | case 'c': | 480 | case 'c': |
703 | do_offset=1; | ||
704 | ocrit = optarg; | 481 | ocrit = optarg; |
705 | break; | 482 | break; |
706 | case 'W': | ||
707 | do_stratum=1; | ||
708 | swarn = optarg; | ||
709 | break; | ||
710 | case 'C': | ||
711 | do_stratum=1; | ||
712 | scrit = optarg; | ||
713 | break; | ||
714 | case 'j': | ||
715 | do_jitter=1; | ||
716 | jwarn = optarg; | ||
717 | break; | ||
718 | case 'k': | ||
719 | do_jitter=1; | ||
720 | jcrit = optarg; | ||
721 | break; | ||
722 | case 'H': | 483 | case 'H': |
723 | if(is_host(optarg) == FALSE) | 484 | if(is_host(optarg) == FALSE) |
724 | usage2(_("Invalid hostname/address"), optarg); | 485 | usage2(_("Invalid hostname/address"), optarg); |
@@ -759,35 +520,17 @@ char *perfd_offset (double offset) | |||
759 | FALSE, 0, FALSE, 0); | 520 | FALSE, 0, FALSE, 0); |
760 | } | 521 | } |
761 | 522 | ||
762 | char *perfd_jitter (double jitter) | ||
763 | { | ||
764 | return fperfdata ("jitter", jitter, "s", | ||
765 | do_jitter, jitter_thresholds->warning->end, | ||
766 | do_jitter, jitter_thresholds->critical->end, | ||
767 | TRUE, 0, FALSE, 0); | ||
768 | } | ||
769 | |||
770 | char *perfd_stratum (int stratum) | ||
771 | { | ||
772 | return perfdata ("stratum", stratum, "", | ||
773 | do_stratum, (int)stratum_thresholds->warning->end, | ||
774 | do_stratum, (int)stratum_thresholds->critical->end, | ||
775 | TRUE, 0, TRUE, 16); | ||
776 | } | ||
777 | |||
778 | int main(int argc, char *argv[]){ | 523 | int main(int argc, char *argv[]){ |
779 | int result, offset_result, jitter_result, stratum; | 524 | int result, offset_result; |
780 | double offset=0, jitter=0; | 525 | double offset=0; |
781 | char *result_line, *perfdata_line; | 526 | char *result_line, *perfdata_line; |
782 | 527 | ||
783 | result = offset_result = jitter_result = STATE_OK; | 528 | result = offset_result = STATE_OK; |
784 | 529 | ||
785 | if (process_arguments (argc, argv) == ERROR) | 530 | if (process_arguments (argc, argv) == ERROR) |
786 | usage4 (_("Could not parse arguments")); | 531 | usage4 (_("Could not parse arguments")); |
787 | 532 | ||
788 | set_thresholds(&offset_thresholds, owarn, ocrit); | 533 | set_thresholds(&offset_thresholds, owarn, ocrit); |
789 | set_thresholds(&jitter_thresholds, jwarn, jcrit); | ||
790 | set_thresholds(&stratum_thresholds, swarn, scrit); | ||
791 | 534 | ||
792 | /* initialize alarm signal handling */ | 535 | /* initialize alarm signal handling */ |
793 | signal (SIGALRM, socket_timeout_alarm_handler); | 536 | signal (SIGALRM, socket_timeout_alarm_handler); |
@@ -795,30 +538,12 @@ int main(int argc, char *argv[]){ | |||
795 | /* set socket timeout */ | 538 | /* set socket timeout */ |
796 | alarm (socket_timeout); | 539 | alarm (socket_timeout); |
797 | 540 | ||
798 | offset = offset_request(server_address, &stratum, &offset_result); | 541 | offset = offset_request(server_address, &offset_result); |
799 | if (do_offset && offset_result == STATE_UNKNOWN) { | 542 | if (offset_result == STATE_UNKNOWN) { |
800 | result = STATE_CRITICAL; | 543 | result = STATE_CRITICAL; |
801 | } else { | 544 | } else { |
802 | result = get_status(fabs(offset), offset_thresholds); | 545 | result = get_status(fabs(offset), offset_thresholds); |
803 | } | 546 | } |
804 | result = max_state(result, offset_result); | ||
805 | if(do_stratum) | ||
806 | result = max_state(result, get_status(stratum, stratum_thresholds)); | ||
807 | |||
808 | /* If not told to check the jitter, we don't even send packets. | ||
809 | * jitter is checked using NTP control packets, which not all | ||
810 | * servers recognize. Trying to check the jitter on OpenNTPD | ||
811 | * (for example) will result in an error | ||
812 | */ | ||
813 | if(do_jitter){ | ||
814 | jitter=jitter_request(server_address, &jitter_result); | ||
815 | result = max_state(result, get_status(jitter, jitter_thresholds)); | ||
816 | /* -1 indicates that we couldn't calculate the jitter | ||
817 | * Only overrides STATE_OK from the offset */ | ||
818 | if(jitter == -1.0 && result == STATE_OK) | ||
819 | result = STATE_UNKNOWN; | ||
820 | } | ||
821 | result = max_state(result, jitter_result); | ||
822 | 547 | ||
823 | switch (result) { | 548 | switch (result) { |
824 | case STATE_CRITICAL : | 549 | case STATE_CRITICAL : |
@@ -838,37 +563,22 @@ int main(int argc, char *argv[]){ | |||
838 | asprintf(&result_line, "%s %s", result_line, _("Offset unknown")); | 563 | asprintf(&result_line, "%s %s", result_line, _("Offset unknown")); |
839 | asprintf(&perfdata_line, ""); | 564 | asprintf(&perfdata_line, ""); |
840 | } else { | 565 | } else { |
841 | #if 0 /* 2007-10-25 This can't happen. Leftovers or uninplemented? */ | ||
842 | if(offset_result==STATE_WARNING){ | ||
843 | asprintf(&result_line, "%s %s", result_line, _("Unable to fully sample sync server")); | ||
844 | } | ||
845 | #endif | ||
846 | asprintf(&result_line, "%s Offset %.10g secs", result_line, offset); | 566 | asprintf(&result_line, "%s Offset %.10g secs", result_line, offset); |
847 | asprintf(&perfdata_line, "%s", perfd_offset(offset)); | 567 | asprintf(&perfdata_line, "%s", perfd_offset(offset)); |
848 | } | 568 | } |
849 | if (do_jitter) { | ||
850 | asprintf(&result_line, "%s, jitter=%f", result_line, jitter); | ||
851 | asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter)); | ||
852 | } | ||
853 | if (do_stratum) { | ||
854 | asprintf(&result_line, "%s, stratum=%i", result_line, stratum); | ||
855 | asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum)); | ||
856 | } | ||
857 | printf("%s|%s\n", result_line, perfdata_line); | 569 | printf("%s|%s\n", result_line, perfdata_line); |
858 | 570 | ||
859 | if(server_address!=NULL) free(server_address); | 571 | if(server_address!=NULL) free(server_address); |
860 | return result; | 572 | return result; |
861 | } | 573 | } |
862 | 574 | ||
863 | |||
864 | |||
865 | void print_help(void){ | 575 | void print_help(void){ |
866 | print_revision(progname, revision); | 576 | print_revision(progname, revision); |
867 | 577 | ||
868 | printf ("Copyright (c) 2006 Sean Finney\n"); | 578 | printf ("Copyright (c) 2006 Sean Finney\n"); |
869 | printf (COPYRIGHT, copyright, email); | 579 | printf (COPYRIGHT, copyright, email); |
870 | 580 | ||
871 | printf ("%s\n", _("This plugin checks the selected ntp server")); | 581 | printf ("%s\n", _("This plugin checks the clock offset with the ntp server")); |
872 | 582 | ||
873 | printf ("\n\n"); | 583 | printf ("\n\n"); |
874 | 584 | ||
@@ -879,14 +589,6 @@ void print_help(void){ | |||
879 | printf (" %s\n", _("Offset to result in warning status (seconds)")); | 589 | printf (" %s\n", _("Offset to result in warning status (seconds)")); |
880 | printf (" %s\n", "-c, --critical=THRESHOLD"); | 590 | printf (" %s\n", "-c, --critical=THRESHOLD"); |
881 | printf (" %s\n", _("Offset to result in critical status (seconds)")); | 591 | printf (" %s\n", _("Offset to result in critical status (seconds)")); |
882 | printf (" %s\n", "-W, --warning=THRESHOLD"); | ||
883 | printf (" %s\n", _("Warning threshold for stratum")); | ||
884 | printf (" %s\n", "-W, --critical=THRESHOLD"); | ||
885 | printf (" %s\n", _("Critical threshold for stratum")); | ||
886 | printf (" %s\n", "-j, --warning=THRESHOLD"); | ||
887 | printf (" %s\n", _("Warning threshold for jitter")); | ||
888 | printf (" %s\n", "-k, --critical=THRESHOLD"); | ||
889 | printf (" %s\n", _("Critical threshold for jitter")); | ||
890 | printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); | 592 | printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); |
891 | printf (_(UT_VERBOSE)); | 593 | printf (_(UT_VERBOSE)); |
892 | 594 | ||
@@ -898,13 +600,7 @@ void print_help(void){ | |||
898 | 600 | ||
899 | printf("\n"); | 601 | printf("\n"); |
900 | printf("%s\n", _("Examples:")); | 602 | printf("%s\n", _("Examples:")); |
901 | printf(" %s\n", _("Normal offset check:")); | 603 | printf(" %s\n", ("./check_time_ntp -H ntpserv -w 0.5 -c 1")); |
902 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1")); | ||
903 | printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available")); | ||
904 | printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); | ||
905 | printf(" %s\n", ("./check_ntp -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); | ||
906 | printf(" %s\n", _("Check only stratum:")); | ||
907 | printf(" %s\n", ("./check_ntp -H ntpserv -W 4 -C 6")); | ||
908 | 604 | ||
909 | printf (_(UT_SUPPORT)); | 605 | printf (_(UT_SUPPORT)); |
910 | } | 606 | } |
@@ -916,3 +612,4 @@ print_usage(void) | |||
916 | printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname); | 612 | printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname); |
917 | printf(" [-j <warn>] [-k <crit>] [-v verbose]\n"); | 613 | printf(" [-j <warn>] [-k <crit>] [-v verbose]\n"); |
918 | } | 614 | } |
615 | |||