X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fcheck_ntp_peer.c;h=62ac9aedcca12437aa9da9207391a14413aecdc3;hb=refs%2Fheads%2Fsh%2Fcheck_pgsql;hp=00c8e97d14e001919ea0a772e7c14a763d0637bb;hpb=873619fffbb3be54f071f043a2ca24fbf7fac8f8;p=nagiosplug.git diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c index 00c8e97..62ac9ae 100644 --- a/plugins/check_ntp_peer.c +++ b/plugins/check_ntp_peer.c @@ -6,8 +6,6 @@ * Copyright (c) 2006 Sean Finney * Copyright (c) 2006-2008 Nagios Plugins Development Team * -* Last Modified: $Date$ -* * Description: * * This file contains the check_ntp_peer plugin @@ -34,12 +32,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . * -* $Id$ * *****************************************************************************/ const char *progname = "check_ntp_peer"; -const char *revision = "$Revision$"; const char *copyright = "2006-2008"; const char *email = "nagiosplug-devel@lists.sourceforge.net"; @@ -48,6 +44,7 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; #include "utils.h" static char *server_address=NULL; +static int port=123; static int verbose=0; static int quiet=0; static short do_offset=0; @@ -59,6 +56,9 @@ static char *scrit="-1:16"; static short do_jitter=0; static char *jwarn="-1:5000"; static char *jcrit="-1:10000"; +static short do_truechimers=0; +static char *twarn="0:"; +static char *tcrit="0:"; static int syncsource_found=0; static int li_alarm=0; @@ -66,6 +66,7 @@ int process_arguments (int, char **); thresholds *offset_thresholds = NULL; thresholds *jitter_thresholds = NULL; thresholds *stratum_thresholds = NULL; +thresholds *truechimer_thresholds = NULL; void print_help (void); void print_usage (void); @@ -124,13 +125,14 @@ typedef struct { #define OP_READVAR 0x02 /* In peer status bytes, bits 6,7,8 determine clock selection status */ #define PEER_SEL(x) ((ntohs(x)>>8)&0x07) +#define PEER_TRUECHIMER 0x02 #define PEER_INCLUDED 0x04 #define PEER_SYNCSOURCE 0x06 /* NTP control message header is 12 bytes, plus any data in the data * field, plus null padding to the nearest 32-bit boundary per rfc. */ -#define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((m.count)?4-(ntohs(m.count)%4):0)) +#define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0)) /* finally, a little helper or two for debugging: */ #define DBG(x) do{if(verbose>1){ x; }}while(0); @@ -161,74 +163,20 @@ void print_ntp_control_message(const ntp_control_message *p){ if(p->op&REM_RESP && p->op&OP_READSTAT){ peer=(ntp_assoc_status_pair*)p->data; for(i=0;i= PEER_INCLUDED){ - if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){ - printf(" <-- current sync source"); - } else { - printf(" <-- current sync candidate"); - } + if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){ + printf(" <-- current sync source"); + } else if(PEER_SEL(peer[i].status) >= PEER_INCLUDED){ + printf(" <-- current sync candidate"); + } else if(PEER_SEL(peer[i].status) >= PEER_TRUECHIMER){ + printf(" <-- outlyer, but truechimer"); } printf("\n"); } } } -/* - * Extract the value from NTP key/value pairs, or return NULL. - * The value returned can be free()ed. - */ -char *extract_value(const char *varlist, const char *name){ - char *tmp=NULL, *value=NULL; - int i; - - while (1) { - /* Strip any leading space */ - for (varlist; isspace(varlist[0]); varlist++); - - if (strncmp(name, varlist, strlen(name)) == 0) { - varlist += strlen(name); - /* strip trailing spaces */ - for (varlist; isspace(varlist[0]); varlist++); - - if (varlist[0] == '=') { - /* We matched the key, go past the = sign */ - varlist++; - /* strip leading spaces */ - for (varlist; isspace(varlist[0]); varlist++); - - if (tmp = index(varlist, ',')) { - /* Value is delimited by a comma */ - if (tmp-varlist == 0) continue; - value = (char *)malloc(tmp-varlist+1); - strncpy(value, varlist, tmp-varlist); - value[tmp-varlist] = '\0'; - } else { - /* Value is delimited by a \0 */ - if (strlen(varlist) == 0) continue; - value = (char *)malloc(strlen(varlist) + 1); - strncpy(value, varlist, strlen(varlist)); - value[strlen(varlist)] = '\0'; - } - break; - } - } - if (tmp = index(varlist, ',')) { - /* More keys, keep going... */ - varlist = tmp + 1; - } else { - /* We're done */ - break; - } - } - - /* Clean-up trailing spaces/newlines */ - if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0'; - - return value; -} - void setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ memset(p, 0, sizeof(ntp_control_message)); @@ -251,7 +199,7 @@ setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ * status is pretty much useless as syncsource_found is a global variable * used later in main to check is the server was synchronized. It works * so I left it alone */ -int ntp_request(const char *host, double *offset, int *offset_result, double *jitter, int *stratum){ +int ntp_request(const char *host, double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers){ int conn=-1, i, npeers=0, num_candidates=0; double tmp_offset = 0; int min_peer_sel=PEER_INCLUDED; @@ -266,6 +214,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji status = STATE_OK; *offset_result = STATE_UNKNOWN; *jitter = *stratum = -1; + *num_truechimers = 0; /* Long-winded explanation: * Getting the sync peer offset, jitter and stratum requires a number of @@ -283,7 +232,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji * 4) Extract the offset, jitter and stratum value from the data[] * (it's ASCII) */ - my_udp_connect(server_address, 123, &conn); + my_udp_connect(server_address, port, &conn); /* keep sending requests until the server stops setting the * REM_MORE bit, though usually this is only 1 packet. */ @@ -298,6 +247,9 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji if(read(conn, &req, SIZEOF_NTPCM(req)) == -1) die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); DBG(print_ntp_control_message(&req)); + /* discard obviously invalid packets */ + if (ntohs(req.count) > MAX_CM_SIZE) + die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n"); if (LI(req.flags) == LI_ALARM) li_alarm = 1; /* Each peer identifier is 4 bytes in the data section, which * we represent as a ntp_assoc_status_pair datatype. @@ -315,15 +267,18 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji * at least some candidates. In the latter case we'll issue * a warning but go ahead with the check on them. */ for (i = 0; i < npeers; i++){ - if (PEER_SEL(peers[i].status) >= PEER_INCLUDED){ - num_candidates++; - if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){ - syncsource_found=1; - min_peer_sel=PEER_SYNCSOURCE; + if(PEER_SEL(peers[i].status) >= PEER_TRUECHIMER){ + (*num_truechimers)++; + if(PEER_SEL(peers[i].status) >= PEER_INCLUDED){ + num_candidates++; + if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){ + syncsource_found=1; + min_peer_sel=PEER_SYNCSOURCE; + } } } } - if(verbose) printf("%d candiate peers available\n", num_candidates); + if(verbose) printf("%d candidate peers available\n", num_candidates); if(verbose && syncsource_found) printf("synchronization source found\n"); if(! syncsource_found){ status = STATE_WARNING; @@ -387,7 +342,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji if(verbose) printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); - value = extract_value(data, "offset"); + value = np_extract_ntpvar(data, "offset"); nptr=NULL; /* Convert the value if we have one */ if(value != NULL) @@ -411,7 +366,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji if(verbose) { printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc)); } - value = extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); + value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); nptr=NULL; /* Convert the value if we have one */ if(value != NULL) @@ -430,7 +385,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji if(verbose) { printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc)); } - value = extract_value(data, "stratum"); + value = np_extract_ntpvar(data, "stratum"); nptr=NULL; /* Convert the value if we have one */ if(value != NULL) @@ -467,17 +422,20 @@ int process_arguments(int argc, char **argv){ {"scrit", required_argument, 0, 'C'}, {"jwarn", required_argument, 0, 'j'}, {"jcrit", required_argument, 0, 'k'}, + {"twarn", required_argument, 0, 'm'}, + {"tcrit", required_argument, 0, 'n'}, {"timeout", required_argument, 0, 't'}, {"hostname", required_argument, 0, 'H'}, + {"port", required_argument, 0, 'p'}, {0, 0, 0, 0} }; - + if (argc < 2) usage ("\n"); while (1) { - c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:t:H:", longopts, &option); + c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); if (c == -1 || c == EOF || c == 1) break; @@ -487,7 +445,7 @@ int process_arguments(int argc, char **argv){ exit(STATE_OK); break; case 'V': - print_revision(progname, revision); + print_revision(progname, NP_VERSION); exit(STATE_OK); break; case 'v': @@ -520,11 +478,22 @@ int process_arguments(int argc, char **argv){ do_jitter=1; jcrit = optarg; break; + case 'm': + do_truechimers=1; + twarn = optarg; + break; + case 'n': + do_truechimers=1; + tcrit = optarg; + break; case 'H': if(is_host(optarg) == FALSE) usage2(_("Invalid hostname/address"), optarg); server_address = strdup(optarg); break; + case 'p': + port=atoi(optarg); + break; case 't': socket_timeout=atoi(optarg); break; @@ -576,8 +545,16 @@ char *perfd_stratum (int stratum) TRUE, 0, TRUE, 16); } +char *perfd_truechimers (int num_truechimers) +{ + return perfdata ("truechimers", num_truechimers, "", + do_truechimers, (int)truechimer_thresholds->warning->end, + do_truechimers, (int)truechimer_thresholds->critical->end, + TRUE, 0, FALSE, 0); +} + int main(int argc, char *argv[]){ - int result, offset_result, stratum; + int result, offset_result, stratum, num_truechimers; double offset=0, jitter=0; char *result_line, *perfdata_line; @@ -594,6 +571,7 @@ int main(int argc, char *argv[]){ set_thresholds(&offset_thresholds, owarn, ocrit); set_thresholds(&jitter_thresholds, jwarn, jcrit); set_thresholds(&stratum_thresholds, swarn, scrit); + set_thresholds(&truechimer_thresholds, twarn, tcrit); /* initialize alarm signal handling */ signal (SIGALRM, socket_timeout_alarm_handler); @@ -602,7 +580,7 @@ int main(int argc, char *argv[]){ alarm (socket_timeout); /* This returns either OK or WARNING (See comment preceeding ntp_request) */ - result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum); + result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum, &num_truechimers); if(offset_result == STATE_UNKNOWN) { /* if there's no sync peer (this overrides ntp_request output): */ @@ -614,6 +592,9 @@ int main(int argc, char *argv[]){ result = max_state_alt(result, get_status(fabs(offset), offset_thresholds)); } + if(do_truechimers) + result = max_state_alt(result, get_status(num_truechimers, truechimer_thresholds)); + if(do_stratum) result = max_state_alt(result, get_status(stratum, stratum_thresholds)); @@ -654,6 +635,10 @@ int main(int argc, char *argv[]){ asprintf(&result_line, "%s, stratum=%i", result_line, stratum); asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum)); } + if (do_truechimers) { + asprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers); + asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers)); + } printf("%s|%s\n", result_line, perfdata_line); if(server_address!=NULL) free(server_address); @@ -663,7 +648,7 @@ int main(int argc, char *argv[]){ void print_help(void){ - print_revision(progname, revision); + print_revision(progname, NP_VERSION); printf ("Copyright (c) 2006 Sean Finney\n"); printf (COPYRIGHT, copyright, email); @@ -673,9 +658,9 @@ void print_help(void){ printf ("\n\n"); print_usage(); - printf (_(UT_HELP_VRSN)); - printf (_(UT_EXTRA_OPTS)); - printf (_(UT_HOST_PORT), 'p', "123"); + printf (UT_HELP_VRSN); + printf (UT_EXTRA_OPTS); + printf (UT_HOST_PORT, 'p', "123"); printf (" %s\n", "-q, --quiet"); printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized")); printf (" %s\n", "-w, --warning=THRESHOLD"); @@ -690,8 +675,12 @@ void print_help(void){ printf (" %s\n", _("Warning threshold for jitter")); printf (" %s\n", "-k, --jcrit=THRESHOLD"); printf (" %s\n", _("Critical threshold for jitter")); - printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); - printf (_(UT_VERBOSE)); + printf (" %s\n", "-m, --twarn=THRESHOLD"); + printf (" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")")); + printf (" %s\n", "-n, --tcrit=THRESHOLD"); + printf (" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")")); + printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); + printf (UT_VERBOSE); printf("\n"); printf("%s\n", _("This plugin checks an NTP server independent of any commandline")); @@ -703,11 +692,7 @@ void print_help(void){ printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP")); printf(" %s\n", _("server; please use check_ntp_time for that purpose.")); printf("\n"); - printf(_(UT_THRESHOLDS_NOTES)); -#ifdef NP_EXTRA_OPTS - printf("\n"); - printf(_(UT_EXTRA_OPTS_NOTES)); -#endif + printf(UT_THRESHOLDS_NOTES); printf("\n"); printf("%s\n", _("Examples:")); @@ -718,16 +703,19 @@ void print_help(void){ printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); printf("\n"); + printf(" %s\n", _("Only check the number of usable time sources (\"truechimers\"):")); + printf(" %s\n", ("./check_ntp_peer -H ntpserv -m :5 -n :3")); + printf("\n"); printf(" %s\n", _("Check only stratum:")); printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6")); - printf (_(UT_SUPPORT)); + printf (UT_SUPPORT); } void print_usage(void) { - printf (_("Usage:")); + printf ("%s\n", _("Usage:")); printf(" %s -H [-w ] [-c ] [-W ] [-C ]\n", progname); printf(" [-j ] [-k ] [-v verbose]\n"); }