From e667553b97c358f5d80608f62c291deffc0328d5 Mon Sep 17 00:00:00 2001 From: "M. Sean Finney" Date: Mon, 1 May 2006 21:52:42 +0000 Subject: - check_ntp: - now roughly feature-complete. - various bugfixes, esp. offset calculation. - enhanced the asynchronous offset polling to set requests that haven't recieved a response in >= 1 second to stale and retransmit them, which results in much better performance on unreliable networks. - we only spend timeout/2 seconds polling offsets, and if we don't get everything by that point we work with what we have and set status to warning/critical depending on how much data we have. - set the same defaults as the perl script. - commit changes to configure.in to support automatic building of check_apt (if apt-get is installed and regex libraries available) and check_ntp (unconditionally), now defaulting to check_ntp.c instead of the perl script. if this is an issue we can back out the commit of course. an eye should be kept on check_ntp building and running correctly in different environments, esp. 64-bit and big-endian platforms, and those with more "esoteric" API's (do any of the platforms not have poll()?). - similar changes to Makefile.am's. - common.h: add statement to include sys/poll.h - runcmd.c: exit STATE_UNKNOWN if execve() fails. git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1386 f882894a-f735-0410-b71e-b25c423dba1c --- plugins/check_ntp.c | 220 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 144 insertions(+), 76 deletions(-) (limited to 'plugins/check_ntp.c') diff --git a/plugins/check_ntp.c b/plugins/check_ntp.c index 149ca98f..655dd4fc 100644 --- a/plugins/check_ntp.c +++ b/plugins/check_ntp.c @@ -29,16 +29,15 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net"; #include "common.h" #include "netutils.h" #include "utils.h" -#include static char *server_address=NULL; static int verbose=0; static int zero_offset_bad=0; -static double owarn=0; -static double ocrit=0; +static double owarn=60; +static double ocrit=120; static short do_jitter=0; -static double jwarn=0; -static double jcrit=0; +static double jwarn=5000; +static double jcrit=10000; int process_arguments (int, char **); void print_help (void); @@ -67,8 +66,11 @@ typedef struct { /* this structure holds data about results from querying offset from a peer */ typedef struct { - int waiting; /* we set to 1 to signal waiting for a response */ + time_t waiting; /* ts set when we started waiting for a response */ int num_responses; /* number of successfully recieved responses */ + uint8_t stratum; /* copied verbatim from the ntp_message */ + double rtdelay; /* converted from the ntp_message */ + double rtdisp; /* converted from the ntp_message */ double offset[AVG_NUM]; /* offsets from each response */ } ntp_server_results; @@ -192,13 +194,12 @@ typedef struct { /* calculate the offset of the local clock */ static inline double calc_offset(const ntp_message *m, const struct timeval *t){ - double client_tx, peer_rx, peer_tx, client_rx, rtdelay; + double client_tx, peer_rx, peer_tx, client_rx; client_tx = NTP64asDOUBLE(m->origts); peer_rx = NTP64asDOUBLE(m->rxts); peer_tx = NTP64asDOUBLE(m->txts); client_rx=TVasDOUBLE((*t)); - rtdelay=NTP32asDOUBLE(m->rtdelay); - return (.5*((peer_tx-client_rx)+(peer_rx-client_tx)))-rtdelay; + return (.5*((peer_tx-client_rx)+(peer_rx-client_tx))); } /* print out a ntp packet in human readable/debuggable format */ @@ -279,14 +280,63 @@ void setup_request(ntp_message *p){ TVtoNTP64(t,p->txts); } +/* select the "best" server from a list of servers, and return its index. + * this is done by filtering servers based on stratum, dispersion, and + * finally round-trip delay. */ +int best_offset_server(const ntp_server_results *slist, int nservers){ + int i=0, j=0, cserver=0, candidates[5], csize=0; + + /* for each server */ + for(cserver=0; cserveri; j--){ + candidates[j]=candidates[j-1]; + } + } + /* regardless, if they should be on the list... */ + if(i<5) { + candidates[i]=cserver; + if(csize<5) csize++; + /* otherwise discard the server */ + } else { + DBG(printf("discarding peer id %d\n", cserver)); + } + } + + if(csize>0) { + DBG(printf("best server selected: peer %d\n", candidates[0])); + return candidates[0]; + } else { + DBG(printf("no peers meeting synchronization criteria :(\n")); + return -1; + } +} + /* do everything we need to get the total average offset * - we use a certain amount of parallelization with poll() to ensure * we don't waste time sitting around waiting for single packets. * - we also "manually" handle resolving host names and connecting, because * we have to do it in a way that our lazy macros don't handle currently :( */ -double offset_request(const char *host){ +double offset_request(const char *host, int *status){ int i=0, j=0, ga_result=0, num_hosts=0, *socklist=NULL, respnum=0; - int servers_completed=0, one_written=0, servers_readable=0, offsets_recvd=0; + int servers_completed=0, one_written=0, servers_readable=0, best_index=-1; + time_t now_time=0, start_ts=0; ntp_message *req=NULL; double avg_offset=0.; struct timeval recv_time; @@ -337,28 +387,24 @@ double offset_request(const char *host){ ai_tmp = ai_tmp->ai_next; } - /* now do AVG_NUM checks to each host. */ - while(servers_completed= min_peer_sel){ + num_selected++; setup_control_request(&req, OP_READVAR, 2); req.assoc = peers[i].assoc; /* By spec, putting the variable name "jitter" in the request @@ -514,11 +571,12 @@ double jitter_request(const char *host){ printf("parsing jitter from peer %.2x: ", peers[i].assoc); } startofvalue = strchr(req.data, '=') + 1; - jitter = strtod(startofvalue, &nptr); - num_selected++; - if(jitter == 0 && startofvalue==nptr){ - printf("warning: unable to parse server response.\n"); - /* XXX errors value ... */ + if(startofvalue != NULL) { + jitter = strtod(startofvalue, &nptr); + } + if(startofvalue == NULL || startofvalue==nptr){ + printf("warning: unable to read server jitter response.\n"); + *status = STATE_WARNING; } else { if(verbose) printf("%g\n", jitter); num_valid++; @@ -527,7 +585,7 @@ double jitter_request(const char *host){ } } if(verbose){ - printf("jitter parsed from %d/%d peers\n", num_selected, num_valid); + printf("jitter parsed from %d/%d peers\n", num_valid, num_selected); } } @@ -637,9 +695,11 @@ int process_arguments(int argc, char **argv){ } int main(int argc, char *argv[]){ - int result = STATE_UNKNOWN; + int result, offset_result, jitter_result; double offset=0, jitter=0; + result=offset_result=jitter_result=STATE_UNKNOWN; + if (process_arguments (argc, argv) == ERROR) usage4 (_("Could not parse arguments")); @@ -649,14 +709,15 @@ int main(int argc, char *argv[]){ /* set socket timeout */ alarm (socket_timeout); - offset = offset_request(server_address); - if(offset > ocrit){ + offset = offset_request(server_address, &offset_result); + if(fabs(offset) > ocrit){ result = STATE_CRITICAL; - } else if(offset > owarn) { + } else if(fabs(offset) > owarn) { result = STATE_WARNING; } else { result = STATE_OK; } + result=max_state(result, offset_result); /* If not told to check the jitter, we don't even send packets. * jitter is checked using NTP control packets, which not all @@ -664,7 +725,7 @@ int main(int argc, char *argv[]){ * (for example) will result in an error */ if(do_jitter){ - jitter=jitter_request(server_address); + jitter=jitter_request(server_address, &jitter_result); if(jitter > jcrit){ result = max_state(result, STATE_CRITICAL); } else if(jitter > jwarn) { @@ -675,6 +736,7 @@ int main(int argc, char *argv[]){ result = STATE_UNKNOWN; } } + result=max_state(result, jitter_result); switch (result) { case STATE_CRITICAL : @@ -690,9 +752,15 @@ int main(int argc, char *argv[]){ printf("NTP UNKNOWN: "); break; } - - printf("Offset %g secs|offset=%g", offset, offset); - if (do_jitter) printf("|jitter=%f", jitter); + if(offset_result==STATE_CRITICAL){ + printf("Offset unknown|offset=unknown"); + } else { + if(offset_result==STATE_WARNING){ + printf("Unable to fully sample sync server. "); + } + printf("Offset %.10g secs|offset=%.10g", offset, offset); + } + if (do_jitter) printf(", jitter=%f", jitter); printf("\n"); if(server_address!=NULL) free(server_address); -- cgit v1.2.3