diff options
-rw-r--r-- | plugins/Makefile.am | 1 | ||||
-rw-r--r-- | plugins/check_ntp_time.c | 68 | ||||
-rw-r--r-- | plugins/check_ntp_time.d/config.h | 28 |
3 files changed, 69 insertions, 28 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 5d4449bf..bb117881 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
@@ -64,6 +64,7 @@ EXTRA_DIST = t \ | |||
64 | check_by_ssh.d \ | 64 | check_by_ssh.d \ |
65 | check_smtp.d \ | 65 | check_smtp.d \ |
66 | check_mysql.d \ | 66 | check_mysql.d \ |
67 | check_ntp_time.d \ | ||
67 | check_dig.d \ | 68 | check_dig.d \ |
68 | check_cluster.d \ | 69 | check_cluster.d \ |
69 | check_fping.d | 70 | check_fping.d |
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c index c757bc08..31162883 100644 --- a/plugins/check_ntp_time.c +++ b/plugins/check_ntp_time.c | |||
@@ -42,17 +42,17 @@ const char *email = "devel@monitoring-plugins.org"; | |||
42 | #include "netutils.h" | 42 | #include "netutils.h" |
43 | #include "utils.h" | 43 | #include "utils.h" |
44 | #include "states.h" | 44 | #include "states.h" |
45 | #include "thresholds.h" | ||
46 | #include "check_ntp_time.d/config.h" | ||
45 | 47 | ||
46 | static char *server_address = NULL; | ||
47 | static char *port = "123"; | ||
48 | static int verbose = 0; | 48 | static int verbose = 0; |
49 | static bool quiet = false; | ||
50 | static char *owarn = "60"; | ||
51 | static char *ocrit = "120"; | ||
52 | static int time_offset = 0; | ||
53 | 49 | ||
54 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 50 | typedef struct { |
55 | static thresholds *offset_thresholds = NULL; | 51 | int errorcode; |
52 | check_ntp_time_config config; | ||
53 | } check_ntp_time_config_wrapper; | ||
54 | static check_ntp_time_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
55 | |||
56 | static void print_help(void); | 56 | static void print_help(void); |
57 | void print_usage(void); | 57 | void print_usage(void); |
58 | 58 | ||
@@ -321,7 +321,7 @@ int best_offset_server(const ntp_server_results *slist, int nservers) { | |||
321 | * we don't waste time sitting around waiting for single packets. | 321 | * we don't waste time sitting around waiting for single packets. |
322 | * - we also "manually" handle resolving host names and connecting, because | 322 | * - we also "manually" handle resolving host names and connecting, because |
323 | * we have to do it in a way that our lazy macros don't handle currently :( */ | 323 | * we have to do it in a way that our lazy macros don't handle currently :( */ |
324 | double offset_request(const char *host, mp_state_enum *status) { | 324 | double offset_request(const char *host, const char *port, mp_state_enum *status, int time_offset) { |
325 | /* setup hints to only return results from getaddrinfo that we'd like */ | 325 | /* setup hints to only return results from getaddrinfo that we'd like */ |
326 | struct addrinfo hints; | 326 | struct addrinfo hints; |
327 | memset(&hints, 0, sizeof(struct addrinfo)); | 327 | memset(&hints, 0, sizeof(struct addrinfo)); |
@@ -392,7 +392,7 @@ double offset_request(const char *host, mp_state_enum *status) { | |||
392 | time_t start_ts = 0; | 392 | time_t start_ts = 0; |
393 | time_t now_time = 0; | 393 | time_t now_time = 0; |
394 | now_time = start_ts = time(NULL); | 394 | now_time = start_ts = time(NULL); |
395 | int servers_completed = 0; | 395 | size_t servers_completed = 0; |
396 | bool one_read = false; | 396 | bool one_read = false; |
397 | while (servers_completed < num_hosts && now_time - start_ts <= socket_timeout / 2) { | 397 | while (servers_completed < num_hosts && now_time - start_ts <= socket_timeout / 2) { |
398 | /* loop through each server and find each one which hasn't | 398 | /* loop through each server and find each one which hasn't |
@@ -401,13 +401,13 @@ double offset_request(const char *host, mp_state_enum *status) { | |||
401 | * and update the "waiting" timestamp with the current time. */ | 401 | * and update the "waiting" timestamp with the current time. */ |
402 | now_time = time(NULL); | 402 | now_time = time(NULL); |
403 | 403 | ||
404 | for (int i = 0; i < num_hosts; i++) { | 404 | for (size_t i = 0; i < num_hosts; i++) { |
405 | if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) { | 405 | if (servers[i].waiting < now_time && servers[i].num_responses < AVG_NUM) { |
406 | if (verbose && servers[i].waiting != 0) { | 406 | if (verbose && servers[i].waiting != 0) { |
407 | printf("re-"); | 407 | printf("re-"); |
408 | } | 408 | } |
409 | if (verbose) { | 409 | if (verbose) { |
410 | printf("sending request to peer %d\n", i); | 410 | printf("sending request to peer %zu\n", i); |
411 | } | 411 | } |
412 | setup_request(&req[i]); | 412 | setup_request(&req[i]); |
413 | write(socklist[i], &req[i], sizeof(ntp_message)); | 413 | write(socklist[i], &req[i], sizeof(ntp_message)); |
@@ -488,7 +488,7 @@ double offset_request(const char *host, mp_state_enum *status) { | |||
488 | return avg_offset; | 488 | return avg_offset; |
489 | } | 489 | } |
490 | 490 | ||
491 | int process_arguments(int argc, char **argv) { | 491 | check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { |
492 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, | 492 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, |
493 | {"help", no_argument, 0, 'h'}, | 493 | {"help", no_argument, 0, 'h'}, |
494 | {"verbose", no_argument, 0, 'v'}, | 494 | {"verbose", no_argument, 0, 'v'}, |
@@ -507,6 +507,14 @@ int process_arguments(int argc, char **argv) { | |||
507 | usage("\n"); | 507 | usage("\n"); |
508 | } | 508 | } |
509 | 509 | ||
510 | check_ntp_time_config_wrapper result = { | ||
511 | .errorcode = OK, | ||
512 | .config = check_ntp_time_config_init(), | ||
513 | }; | ||
514 | |||
515 | char *owarn = "60"; | ||
516 | char *ocrit = "120"; | ||
517 | |||
510 | while (true) { | 518 | while (true) { |
511 | int option = 0; | 519 | int option = 0; |
512 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); | 520 | int option_char = getopt_long(argc, argv, "Vhv46qw:c:t:H:p:o:", longopts, &option); |
@@ -527,7 +535,7 @@ int process_arguments(int argc, char **argv) { | |||
527 | verbose++; | 535 | verbose++; |
528 | break; | 536 | break; |
529 | case 'q': | 537 | case 'q': |
530 | quiet = true; | 538 | result.config.quiet = true; |
531 | break; | 539 | break; |
532 | case 'w': | 540 | case 'w': |
533 | owarn = optarg; | 541 | owarn = optarg; |
@@ -539,16 +547,16 @@ int process_arguments(int argc, char **argv) { | |||
539 | if (!is_host(optarg)) { | 547 | if (!is_host(optarg)) { |
540 | usage2(_("Invalid hostname/address"), optarg); | 548 | usage2(_("Invalid hostname/address"), optarg); |
541 | } | 549 | } |
542 | server_address = strdup(optarg); | 550 | result.config.server_address = strdup(optarg); |
543 | break; | 551 | break; |
544 | case 'p': | 552 | case 'p': |
545 | port = strdup(optarg); | 553 | result.config.port = strdup(optarg); |
546 | break; | 554 | break; |
547 | case 't': | 555 | case 't': |
548 | socket_timeout = atoi(optarg); | 556 | socket_timeout = atoi(optarg); |
549 | break; | 557 | break; |
550 | case 'o': | 558 | case 'o': |
551 | time_offset = atoi(optarg); | 559 | result.config.time_offset = atoi(optarg); |
552 | break; | 560 | break; |
553 | case '4': | 561 | case '4': |
554 | address_family = AF_INET; | 562 | address_family = AF_INET; |
@@ -567,14 +575,16 @@ int process_arguments(int argc, char **argv) { | |||
567 | } | 575 | } |
568 | } | 576 | } |
569 | 577 | ||
570 | if (server_address == NULL) { | 578 | if (result.config.server_address == NULL) { |
571 | usage4(_("Hostname was not supplied")); | 579 | usage4(_("Hostname was not supplied")); |
572 | } | 580 | } |
573 | 581 | ||
574 | return 0; | 582 | set_thresholds(&result.config.offset_thresholds, owarn, ocrit); |
583 | |||
584 | return result; | ||
575 | } | 585 | } |
576 | 586 | ||
577 | char *perfd_offset(double offset) { | 587 | char *perfd_offset(double offset, thresholds *offset_thresholds) { |
578 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, | 588 | return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true, offset_thresholds->critical->end, false, 0, false, |
579 | 0); | 589 | 0); |
580 | } | 590 | } |
@@ -587,11 +597,13 @@ int main(int argc, char *argv[]) { | |||
587 | /* Parse extra opts if any */ | 597 | /* Parse extra opts if any */ |
588 | argv = np_extra_opts(&argc, argv, progname); | 598 | argv = np_extra_opts(&argc, argv, progname); |
589 | 599 | ||
590 | if (process_arguments(argc, argv) == ERROR) { | 600 | check_ntp_time_config_wrapper tmp_config = process_arguments(argc, argv); |
601 | |||
602 | if (tmp_config.errorcode == ERROR) { | ||
591 | usage4(_("Could not parse arguments")); | 603 | usage4(_("Could not parse arguments")); |
592 | } | 604 | } |
593 | 605 | ||
594 | set_thresholds(&offset_thresholds, owarn, ocrit); | 606 | const check_ntp_time_config config = tmp_config.config; |
595 | 607 | ||
596 | /* initialize alarm signal handling */ | 608 | /* initialize alarm signal handling */ |
597 | signal(SIGALRM, socket_timeout_alarm_handler); | 609 | signal(SIGALRM, socket_timeout_alarm_handler); |
@@ -601,11 +613,11 @@ int main(int argc, char *argv[]) { | |||
601 | 613 | ||
602 | mp_state_enum offset_result = STATE_OK; | 614 | mp_state_enum offset_result = STATE_OK; |
603 | mp_state_enum result = STATE_OK; | 615 | mp_state_enum result = STATE_OK; |
604 | double offset = offset_request(server_address, &offset_result); | 616 | double offset = offset_request(config.server_address, config.port, &offset_result, config.time_offset); |
605 | if (offset_result == STATE_UNKNOWN) { | 617 | if (offset_result == STATE_UNKNOWN) { |
606 | result = ((!quiet) ? STATE_UNKNOWN : STATE_CRITICAL); | 618 | result = ((!config.quiet) ? STATE_UNKNOWN : STATE_CRITICAL); |
607 | } else { | 619 | } else { |
608 | result = get_status(fabs(offset), offset_thresholds); | 620 | result = get_status(fabs(offset), config.offset_thresholds); |
609 | } | 621 | } |
610 | 622 | ||
611 | char *result_line; | 623 | char *result_line; |
@@ -630,12 +642,12 @@ int main(int argc, char *argv[]) { | |||
630 | xasprintf(&perfdata_line, ""); | 642 | xasprintf(&perfdata_line, ""); |
631 | } else { | 643 | } else { |
632 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); | 644 | xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset); |
633 | xasprintf(&perfdata_line, "%s", perfd_offset(offset)); | 645 | xasprintf(&perfdata_line, "%s", perfd_offset(offset, config.offset_thresholds)); |
634 | } | 646 | } |
635 | printf("%s|%s\n", result_line, perfdata_line); | 647 | printf("%s|%s\n", result_line, perfdata_line); |
636 | 648 | ||
637 | if (server_address != NULL) { | 649 | if (config.server_address != NULL) { |
638 | free(server_address); | 650 | free(config.server_address); |
639 | } | 651 | } |
640 | exit(result); | 652 | exit(result); |
641 | } | 653 | } |
diff --git a/plugins/check_ntp_time.d/config.h b/plugins/check_ntp_time.d/config.h new file mode 100644 index 00000000..99dabbbd --- /dev/null +++ b/plugins/check_ntp_time.d/config.h | |||
@@ -0,0 +1,28 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include "thresholds.h" | ||
5 | #include <stddef.h> | ||
6 | |||
7 | typedef struct { | ||
8 | char *server_address; | ||
9 | char *port; | ||
10 | |||
11 | bool quiet; | ||
12 | int time_offset; | ||
13 | |||
14 | thresholds *offset_thresholds; | ||
15 | } check_ntp_time_config; | ||
16 | |||
17 | check_ntp_time_config check_ntp_time_config_init() { | ||
18 | check_ntp_time_config tmp = { | ||
19 | .server_address = NULL, | ||
20 | .port = "123", | ||
21 | |||
22 | .quiet = false, | ||
23 | .time_offset = 0, | ||
24 | |||
25 | .offset_thresholds = NULL, | ||
26 | }; | ||
27 | return tmp; | ||
28 | } | ||