diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-11-05 12:04:30 +0100 |
|---|---|---|
| committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-11-05 12:04:30 +0100 |
| commit | a9b63deedb1775271fa1335a5d3eb034d0628e91 (patch) | |
| tree | e9ff510b5ec2f7af3b79f2d9bc12b5f23cb05289 /plugins | |
| parent | 99351f0560a5f457e499417366bdfdec0172a428 (diff) | |
| download | monitoring-plugins-a9b63deedb1775271fa1335a5d3eb034d0628e91.tar.gz | |
check_ntp_peer: add cli param to set output format
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/check_ntp_peer.c | 50 | ||||
| -rw-r--r-- | plugins/check_ntp_peer.d/config.h | 6 |
2 files changed, 46 insertions, 10 deletions
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c index 032f8ea4..f7cad630 100644 --- a/plugins/check_ntp_peer.c +++ b/plugins/check_ntp_peer.c | |||
| @@ -478,16 +478,30 @@ ntp_request_result ntp_request(const check_ntp_peer_config config) { | |||
| 478 | } | 478 | } |
| 479 | 479 | ||
| 480 | check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) { | 480 | check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) { |
| 481 | static struct option longopts[] = { | 481 | |
| 482 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, | 482 | enum { |
| 483 | {"verbose", no_argument, 0, 'v'}, {"use-ipv4", no_argument, 0, '4'}, | 483 | output_format_index = CHAR_MAX + 1, |
| 484 | {"use-ipv6", no_argument, 0, '6'}, {"quiet", no_argument, 0, 'q'}, | 484 | }; |
| 485 | {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, | 485 | |
| 486 | {"swarn", required_argument, 0, 'W'}, {"scrit", required_argument, 0, 'C'}, | 486 | static struct option longopts[] = {{"version", no_argument, 0, 'V'}, |
| 487 | {"jwarn", required_argument, 0, 'j'}, {"jcrit", required_argument, 0, 'k'}, | 487 | {"help", no_argument, 0, 'h'}, |
| 488 | {"twarn", required_argument, 0, 'm'}, {"tcrit", required_argument, 0, 'n'}, | 488 | {"verbose", no_argument, 0, 'v'}, |
| 489 | {"timeout", required_argument, 0, 't'}, {"hostname", required_argument, 0, 'H'}, | 489 | {"use-ipv4", no_argument, 0, '4'}, |
| 490 | {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}}; | 490 | {"use-ipv6", no_argument, 0, '6'}, |
| 491 | {"quiet", no_argument, 0, 'q'}, | ||
| 492 | {"warning", required_argument, 0, 'w'}, | ||
| 493 | {"critical", required_argument, 0, 'c'}, | ||
| 494 | {"swarn", required_argument, 0, 'W'}, | ||
| 495 | {"scrit", required_argument, 0, 'C'}, | ||
| 496 | {"jwarn", required_argument, 0, 'j'}, | ||
| 497 | {"jcrit", required_argument, 0, 'k'}, | ||
| 498 | {"twarn", required_argument, 0, 'm'}, | ||
| 499 | {"tcrit", required_argument, 0, 'n'}, | ||
| 500 | {"timeout", required_argument, 0, 't'}, | ||
| 501 | {"hostname", required_argument, 0, 'H'}, | ||
| 502 | {"port", required_argument, 0, 'p'}, | ||
| 503 | {"output-format", required_argument, 0, output_format_index}, | ||
| 504 | {0, 0, 0, 0}}; | ||
| 491 | 505 | ||
| 492 | if (argc < 2) { | 506 | if (argc < 2) { |
| 493 | usage("\n"); | 507 | usage("\n"); |
| @@ -507,6 +521,17 @@ check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) { | |||
| 507 | } | 521 | } |
| 508 | 522 | ||
| 509 | switch (option_char) { | 523 | switch (option_char) { |
| 524 | case output_format_index: { | ||
| 525 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
| 526 | if (!parser.parsing_success) { | ||
| 527 | printf("Invalid output format: %s\n", optarg); | ||
| 528 | exit(STATE_UNKNOWN); | ||
| 529 | } | ||
| 530 | |||
| 531 | result.config.output_format_is_set = true; | ||
| 532 | result.config.output_format = parser.output_format; | ||
| 533 | break; | ||
| 534 | } | ||
| 510 | case 'h': | 535 | case 'h': |
| 511 | print_help(); | 536 | print_help(); |
| 512 | exit(STATE_UNKNOWN); | 537 | exit(STATE_UNKNOWN); |
| @@ -673,6 +698,10 @@ int main(int argc, char *argv[]) { | |||
| 673 | 698 | ||
| 674 | const check_ntp_peer_config config = tmp_config.config; | 699 | const check_ntp_peer_config config = tmp_config.config; |
| 675 | 700 | ||
| 701 | if (config.output_format_is_set) { | ||
| 702 | mp_set_format(config.output_format); | ||
| 703 | } | ||
| 704 | |||
| 676 | /* initialize alarm signal handling */ | 705 | /* initialize alarm signal handling */ |
| 677 | signal(SIGALRM, socket_timeout_alarm_handler); | 706 | signal(SIGALRM, socket_timeout_alarm_handler); |
| 678 | 707 | ||
| @@ -825,6 +854,7 @@ void print_help(void) { | |||
| 825 | printf(" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")")); | 854 | printf(" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")")); |
| 826 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 855 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 827 | printf(UT_VERBOSE); | 856 | printf(UT_VERBOSE); |
| 857 | printf(UT_OUTPUT_FORMAT); | ||
| 828 | 858 | ||
| 829 | printf("\n"); | 859 | printf("\n"); |
| 830 | printf("%s\n", _("This plugin checks an NTP server independent of any commandline")); | 860 | printf("%s\n", _("This plugin checks an NTP server independent of any commandline")); |
diff --git a/plugins/check_ntp_peer.d/config.h b/plugins/check_ntp_peer.d/config.h index a6dd5c4c..488d936c 100644 --- a/plugins/check_ntp_peer.d/config.h +++ b/plugins/check_ntp_peer.d/config.h | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include "../../config.h" | 3 | #include "../../config.h" |
| 4 | #include "output.h" | ||
| 4 | #include "perfdata.h" | 5 | #include "perfdata.h" |
| 5 | #include "thresholds.h" | 6 | #include "thresholds.h" |
| 6 | #include <stddef.h> | 7 | #include <stddef.h> |
| @@ -29,6 +30,9 @@ typedef struct { | |||
| 29 | // jitter stuff | 30 | // jitter stuff |
| 30 | bool do_jitter; | 31 | bool do_jitter; |
| 31 | mp_thresholds jitter_thresholds; | 32 | mp_thresholds jitter_thresholds; |
| 33 | |||
| 34 | bool output_format_is_set; | ||
| 35 | mp_output_format output_format; | ||
| 32 | } check_ntp_peer_config; | 36 | } check_ntp_peer_config; |
| 33 | 37 | ||
| 34 | check_ntp_peer_config check_ntp_peer_config_init() { | 38 | check_ntp_peer_config check_ntp_peer_config_init() { |
| @@ -47,6 +51,8 @@ check_ntp_peer_config check_ntp_peer_config_init() { | |||
| 47 | 51 | ||
| 48 | .do_jitter = false, | 52 | .do_jitter = false, |
| 49 | .jitter_thresholds = mp_thresholds_init(), | 53 | .jitter_thresholds = mp_thresholds_init(), |
| 54 | |||
| 55 | .output_format_is_set = false, | ||
| 50 | }; | 56 | }; |
| 51 | 57 | ||
| 52 | mp_range stratum_default = mp_range_init(); | 58 | mp_range stratum_default = mp_range_init(); |
