diff options
Diffstat (limited to 'plugins/check_ntp.c')
| -rw-r--r-- | plugins/check_ntp.c | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/plugins/check_ntp.c b/plugins/check_ntp.c index 75efc289..99537c88 100644 --- a/plugins/check_ntp.c +++ b/plugins/check_ntp.c | |||
| @@ -10,7 +10,7 @@ | |||
| 10 | * | 10 | * |
| 11 | * This file contains the check_ntp plugin | 11 | * This file contains the check_ntp plugin |
| 12 | * | 12 | * |
| 13 | * This plugin to check ntp servers independant of any commandline | 13 | * This plugin to check ntp servers independent of any commandline |
| 14 | * programs or external libraries. | 14 | * programs or external libraries. |
| 15 | * | 15 | * |
| 16 | * | 16 | * |
| @@ -79,7 +79,7 @@ typedef struct { | |||
| 79 | /* this structure holds data about results from querying offset from a peer */ | 79 | /* this structure holds data about results from querying offset from a peer */ |
| 80 | typedef struct { | 80 | typedef struct { |
| 81 | time_t waiting; /* ts set when we started waiting for a response */ | 81 | time_t waiting; /* ts set when we started waiting for a response */ |
| 82 | int num_responses; /* number of successfully recieved responses */ | 82 | int num_responses; /* number of successfully received responses */ |
| 83 | uint8_t stratum; /* copied verbatim from the ntp_message */ | 83 | uint8_t stratum; /* copied verbatim from the ntp_message */ |
| 84 | double rtdelay; /* converted from the ntp_message */ | 84 | double rtdelay; /* converted from the ntp_message */ |
| 85 | double rtdisp; /* converted from the ntp_message */ | 85 | double rtdisp; /* converted from the ntp_message */ |
| @@ -100,7 +100,7 @@ typedef struct { | |||
| 100 | /* NB: not necessarily NULL terminated! */ | 100 | /* NB: not necessarily NULL terminated! */ |
| 101 | } ntp_control_message; | 101 | } ntp_control_message; |
| 102 | 102 | ||
| 103 | /* this is an association/status-word pair found in control packet reponses */ | 103 | /* this is an association/status-word pair found in control packet responses */ |
| 104 | typedef struct { | 104 | typedef struct { |
| 105 | uint16_t assoc; | 105 | uint16_t assoc; |
| 106 | uint16_t status; | 106 | uint16_t status; |
| @@ -297,7 +297,7 @@ void setup_request(ntp_message *p){ | |||
| 297 | * this is done by filtering servers based on stratum, dispersion, and | 297 | * this is done by filtering servers based on stratum, dispersion, and |
| 298 | * finally round-trip delay. */ | 298 | * finally round-trip delay. */ |
| 299 | int best_offset_server(const ntp_server_results *slist, int nservers){ | 299 | int best_offset_server(const ntp_server_results *slist, int nservers){ |
| 300 | int i=0, cserver=0, best_server=-1; | 300 | int cserver=0, best_server=-1; |
| 301 | 301 | ||
| 302 | /* for each server */ | 302 | /* for each server */ |
| 303 | for(cserver=0; cserver<nservers; cserver++){ | 303 | for(cserver=0; cserver<nservers; cserver++){ |
| @@ -355,8 +355,8 @@ int best_offset_server(const ntp_server_results *slist, int nservers){ | |||
| 355 | * - we also "manually" handle resolving host names and connecting, because | 355 | * - we also "manually" handle resolving host names and connecting, because |
| 356 | * we have to do it in a way that our lazy macros don't handle currently :( */ | 356 | * we have to do it in a way that our lazy macros don't handle currently :( */ |
| 357 | double offset_request(const char *host, int *status){ | 357 | double offset_request(const char *host, int *status){ |
| 358 | int i=0, j=0, ga_result=0, num_hosts=0, *socklist=NULL, respnum=0; | 358 | int i=0, ga_result=0, num_hosts=0, *socklist=NULL, respnum=0; |
| 359 | int servers_completed=0, one_written=0, one_read=0, servers_readable=0, best_index=-1; | 359 | int servers_completed=0, one_read=0, servers_readable=0, best_index=-1; |
| 360 | time_t now_time=0, start_ts=0; | 360 | time_t now_time=0, start_ts=0; |
| 361 | ntp_message *req=NULL; | 361 | ntp_message *req=NULL; |
| 362 | double avg_offset=0.; | 362 | double avg_offset=0.; |
| @@ -421,7 +421,6 @@ double offset_request(const char *host, int *status){ | |||
| 421 | * been touched in the past second or so and is still lacking | 421 | * been touched in the past second or so and is still lacking |
| 422 | * some responses. for each of these servers, send a new request, | 422 | * some responses. for each of these servers, send a new request, |
| 423 | * and update the "waiting" timestamp with the current time. */ | 423 | * and update the "waiting" timestamp with the current time. */ |
| 424 | one_written=0; | ||
| 425 | now_time=time(NULL); | 424 | now_time=time(NULL); |
| 426 | 425 | ||
| 427 | for(i=0; i<num_hosts; i++){ | 426 | for(i=0; i<num_hosts; i++){ |
| @@ -431,7 +430,6 @@ double offset_request(const char *host, int *status){ | |||
| 431 | setup_request(&req[i]); | 430 | setup_request(&req[i]); |
| 432 | write(socklist[i], &req[i], sizeof(ntp_message)); | 431 | write(socklist[i], &req[i], sizeof(ntp_message)); |
| 433 | servers[i].waiting=now_time; | 432 | servers[i].waiting=now_time; |
| 434 | one_written=1; | ||
| 435 | break; | 433 | break; |
| 436 | } | 434 | } |
| 437 | } | 435 | } |
| @@ -488,9 +486,9 @@ double offset_request(const char *host, int *status){ | |||
| 488 | } | 486 | } |
| 489 | 487 | ||
| 490 | /* cleanup */ | 488 | /* cleanup */ |
| 491 | /* FIXME: Not closing the socket to avoid re-use of the local port | 489 | /* FIXME: Not closing the socket to avoid reuse of the local port |
| 492 | * which can cause old NTP packets to be read instead of NTP control | 490 | * which can cause old NTP packets to be read instead of NTP control |
| 493 | * pactets in jitter_request(). THERE MUST BE ANOTHER WAY... | 491 | * packets in jitter_request(). THERE MUST BE ANOTHER WAY... |
| 494 | * for(j=0; j<num_hosts; j++){ close(socklist[j]); } */ | 492 | * for(j=0; j<num_hosts; j++){ close(socklist[j]); } */ |
| 495 | free(socklist); | 493 | free(socklist); |
| 496 | free(ufds); | 494 | free(ufds); |
| @@ -514,7 +512,7 @@ setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ | |||
| 514 | } | 512 | } |
| 515 | 513 | ||
| 516 | /* XXX handle responses with the error bit set */ | 514 | /* XXX handle responses with the error bit set */ |
| 517 | double jitter_request(const char *host, int *status){ | 515 | double jitter_request(int *status){ |
| 518 | int conn=-1, i, npeers=0, num_candidates=0, syncsource_found=0; | 516 | int conn=-1, i, npeers=0, num_candidates=0, syncsource_found=0; |
| 519 | int run=0, min_peer_sel=PEER_INCLUDED, num_selected=0, num_valid=0; | 517 | int run=0, min_peer_sel=PEER_INCLUDED, num_selected=0, num_valid=0; |
| 520 | int peers_size=0, peer_offset=0; | 518 | int peers_size=0, peer_offset=0; |
| @@ -550,7 +548,7 @@ double jitter_request(const char *host, int *status){ | |||
| 550 | DBG(print_ntp_control_message(&req)); | 548 | DBG(print_ntp_control_message(&req)); |
| 551 | /* Attempt to read the largest size packet possible */ | 549 | /* Attempt to read the largest size packet possible */ |
| 552 | req.count=htons(MAX_CM_SIZE); | 550 | req.count=htons(MAX_CM_SIZE); |
| 553 | DBG(printf("recieving READSTAT response")) | 551 | DBG(printf("receiving READSTAT response")) |
| 554 | read(conn, &req, SIZEOF_NTPCM(req)); | 552 | read(conn, &req, SIZEOF_NTPCM(req)); |
| 555 | DBG(print_ntp_control_message(&req)); | 553 | DBG(print_ntp_control_message(&req)); |
| 556 | /* Each peer identifier is 4 bytes in the data section, which | 554 | /* Each peer identifier is 4 bytes in the data section, which |
| @@ -577,7 +575,7 @@ double jitter_request(const char *host, int *status){ | |||
| 577 | } | 575 | } |
| 578 | } | 576 | } |
| 579 | } | 577 | } |
| 580 | if(verbose) printf("%d candiate peers available\n", num_candidates); | 578 | if(verbose) printf("%d candidate peers available\n", num_candidates); |
| 581 | if(verbose && syncsource_found) printf("synchronization source found\n"); | 579 | if(verbose && syncsource_found) printf("synchronization source found\n"); |
| 582 | if(! syncsource_found){ | 580 | if(! syncsource_found){ |
| 583 | *status = STATE_UNKNOWN; | 581 | *status = STATE_UNKNOWN; |
| @@ -599,7 +597,7 @@ double jitter_request(const char *host, int *status){ | |||
| 599 | /* By spec, putting the variable name "jitter" in the request | 597 | /* By spec, putting the variable name "jitter" in the request |
| 600 | * should cause the server to provide _only_ the jitter value. | 598 | * should cause the server to provide _only_ the jitter value. |
| 601 | * thus reducing net traffic, guaranteeing us only a single | 599 | * thus reducing net traffic, guaranteeing us only a single |
| 602 | * datagram in reply, and making intepretation much simpler | 600 | * datagram in reply, and making interpretation much simpler |
| 603 | */ | 601 | */ |
| 604 | /* Older servers doesn't know what jitter is, so if we get an | 602 | /* Older servers doesn't know what jitter is, so if we get an |
| 605 | * error on the first pass we redo it with "dispersion" */ | 603 | * error on the first pass we redo it with "dispersion" */ |
| @@ -610,7 +608,7 @@ double jitter_request(const char *host, int *status){ | |||
| 610 | DBG(print_ntp_control_message(&req)); | 608 | DBG(print_ntp_control_message(&req)); |
| 611 | 609 | ||
| 612 | req.count = htons(MAX_CM_SIZE); | 610 | req.count = htons(MAX_CM_SIZE); |
| 613 | DBG(printf("recieving READVAR response...\n")); | 611 | DBG(printf("receiving READVAR response...\n")); |
| 614 | read(conn, &req, SIZEOF_NTPCM(req)); | 612 | read(conn, &req, SIZEOF_NTPCM(req)); |
| 615 | DBG(print_ntp_control_message(&req)); | 613 | DBG(print_ntp_control_message(&req)); |
| 616 | 614 | ||
| @@ -805,7 +803,7 @@ int main(int argc, char *argv[]){ | |||
| 805 | * (for example) will result in an error | 803 | * (for example) will result in an error |
| 806 | */ | 804 | */ |
| 807 | if(do_jitter){ | 805 | if(do_jitter){ |
| 808 | jitter=jitter_request(server_address, &jitter_result); | 806 | jitter=jitter_request(&jitter_result); |
| 809 | result = max_state_alt(result, get_status(jitter, jitter_thresholds)); | 807 | result = max_state_alt(result, get_status(jitter, jitter_thresholds)); |
| 810 | /* -1 indicates that we couldn't calculate the jitter | 808 | /* -1 indicates that we couldn't calculate the jitter |
| 811 | * Only overrides STATE_OK from the offset */ | 809 | * Only overrides STATE_OK from the offset */ |
