summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_ntp_time.c45
1 files changed, 30 insertions, 15 deletions
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c
index 703b69df..05c3d1e4 100644
--- a/plugins/check_ntp_time.c
+++ b/plugins/check_ntp_time.c
@@ -273,14 +273,16 @@ int best_offset_server(const ntp_server_results *slist, int nservers) {
273 * stratum 0 is for reference clocks so no NTP server should ever report 273 * stratum 0 is for reference clocks so no NTP server should ever report
274 * a stratum 0 */ 274 * a stratum 0 */
275 if (slist[cserver].stratum == 0) { 275 if (slist[cserver].stratum == 0) {
276 if (verbose) 276 if (verbose) {
277 printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum); 277 printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum);
278 }
278 continue; 279 continue;
279 } 280 }
280 /* Sort out servers with error flags */ 281 /* Sort out servers with error flags */
281 if (LI(slist[cserver].flags) == LI_ALARM) { 282 if (LI(slist[cserver].flags) == LI_ALARM) {
282 if (verbose) 283 if (verbose) {
283 printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags)); 284 printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags));
285 }
284 continue; 286 continue;
285 } 287 }
286 288
@@ -345,20 +347,24 @@ double offset_request(const char *host, int *status) {
345 347
346 ntp_message *req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts); 348 ntp_message *req = (ntp_message *)malloc(sizeof(ntp_message) * num_hosts);
347 349
348 if (req == NULL) 350 if (req == NULL) {
349 die(STATE_UNKNOWN, "can not allocate ntp message array"); 351 die(STATE_UNKNOWN, "can not allocate ntp message array");
352 }
350 int *socklist = (int *)malloc(sizeof(int) * num_hosts); 353 int *socklist = (int *)malloc(sizeof(int) * num_hosts);
351 354
352 if (socklist == NULL) 355 if (socklist == NULL) {
353 die(STATE_UNKNOWN, "can not allocate socket array"); 356 die(STATE_UNKNOWN, "can not allocate socket array");
357 }
354 358
355 struct pollfd *ufds = (struct pollfd *)malloc(sizeof(struct pollfd) * num_hosts); 359 struct pollfd *ufds = (struct pollfd *)malloc(sizeof(struct pollfd) * num_hosts);
356 if (ufds == NULL) 360 if (ufds == NULL) {
357 die(STATE_UNKNOWN, "can not allocate socket array"); 361 die(STATE_UNKNOWN, "can not allocate socket array");
362 }
358 363
359 ntp_server_results *servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts); 364 ntp_server_results *servers = (ntp_server_results *)malloc(sizeof(ntp_server_results) * num_hosts);
360 if (servers == NULL) 365 if (servers == NULL) {
361 die(STATE_UNKNOWN, "can not allocate server array"); 366 die(STATE_UNKNOWN, "can not allocate server array");
367 }
362 memset(servers, 0, sizeof(ntp_server_results) * num_hosts); 368 memset(servers, 0, sizeof(ntp_server_results) * num_hosts);
363 DBG(printf("Found %d peers to check\n", num_hosts)); 369 DBG(printf("Found %d peers to check\n", num_hosts));
364 370
@@ -400,10 +406,12 @@ double offset_request(const char *host, int *status) {
400 406
401 for (int i = 0; i < num_hosts; i++) { 407 for (int i = 0; i < num_hosts; i++) {
402 if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) { 408 if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) {
403 if (verbose && servers[i].waiting != 0) 409 if (verbose && servers[i].waiting != 0) {
404 printf("re-"); 410 printf("re-");
405 if (verbose) 411 }
412 if (verbose) {
406 printf("sending request to peer %d\n", i); 413 printf("sending request to peer %d\n", i);
414 }
407 setup_request(&req[i]); 415 setup_request(&req[i]);
408 write(socklist[i], &req[i], sizeof(ntp_message)); 416 write(socklist[i], &req[i], sizeof(ntp_message));
409 servers[i].waiting = now_time; 417 servers[i].waiting = now_time;
@@ -442,8 +450,9 @@ double offset_request(const char *host, int *status) {
442 servers[i].flags = req[i].flags; 450 servers[i].flags = req[i].flags;
443 servers_readable--; 451 servers_readable--;
444 one_read = true; 452 one_read = true;
445 if (servers[i].num_responses == AVG_NUM) 453 if (servers[i].num_responses == AVG_NUM) {
446 servers_completed++; 454 servers_completed++;
455 }
447 } 456 }
448 } 457 }
449 /* lather, rinse, repeat. */ 458 /* lather, rinse, repeat. */
@@ -476,8 +485,9 @@ double offset_request(const char *host, int *status) {
476 free(req); 485 free(req);
477 freeaddrinfo(ai); 486 freeaddrinfo(ai);
478 487
479 if (verbose) 488 if (verbose) {
480 printf("overall average offset: %.10g\n", avg_offset); 489 printf("overall average offset: %.10g\n", avg_offset);
490 }
481 return avg_offset; 491 return avg_offset;
482} 492}
483 493
@@ -496,14 +506,16 @@ int process_arguments(int argc, char **argv) {
496 {"port", required_argument, 0, 'p'}, 506 {"port", required_argument, 0, 'p'},
497 {0, 0, 0, 0}}; 507 {0, 0, 0, 0}};
498 508
499 if (argc < 2) 509 if (argc < 2) {
500 usage("\n"); 510 usage("\n");
511 }
501 512
502 while (true) { 513 while (true) {
503 int option = 0; 514 int option = 0;
504 int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); 515 int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option);
505 if (option_char == -1 || option_char == EOF || option_char == 1) 516 if (option_char == -1 || option_char == EOF || option_char == 1) {
506 break; 517 break;
518 }
507 519
508 switch (option_char) { 520 switch (option_char) {
509 case 'h': 521 case 'h':
@@ -527,8 +539,9 @@ int process_arguments(int argc, char **argv) {
527 ocrit = optarg; 539 ocrit = optarg;
528 break; 540 break;
529 case 'H': 541 case 'H':
530 if (!is_host(optarg)) 542 if (!is_host(optarg)) {
531 usage2(_("Invalid hostname/address"), optarg); 543 usage2(_("Invalid hostname/address"), optarg);
544 }
532 server_address = strdup(optarg); 545 server_address = strdup(optarg);
533 break; 546 break;
534 case 'p': 547 case 'p':
@@ -577,8 +590,9 @@ int main(int argc, char *argv[]) {
577 /* Parse extra opts if any */ 590 /* Parse extra opts if any */
578 argv = np_extra_opts(&argc, argv, progname); 591 argv = np_extra_opts(&argc, argv, progname);
579 592
580 if (process_arguments(argc, argv) == ERROR) 593 if (process_arguments(argc, argv) == ERROR) {
581 usage4(_("Could not parse arguments")); 594 usage4(_("Could not parse arguments"));
595 }
582 596
583 set_thresholds(&offset_thresholds, owarn, ocrit); 597 set_thresholds(&offset_thresholds, owarn, ocrit);
584 598
@@ -623,8 +637,9 @@ int main(int argc, char *argv[]) {
623 } 637 }
624 printf("%s|%s\n", result_line, perfdata_line); 638 printf("%s|%s\n", result_line, perfdata_line);
625 639
626 if (server_address != NULL) 640 if (server_address != NULL) {
627 free(server_address); 641 free(server_address);
642 }
628 return result; 643 return result;
629} 644}
630 645