summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-11-05 12:46:21 +0100
committerGitHub <noreply@github.com>2025-11-05 12:46:21 +0100
commitad7acf4618f1ac8a49277a482fb4ca391a26f584 (patch)
tree67ae316fcaa4b72328b7e9666e059439d0b8dca4
parent8062f836756001a59d82049ebb67511b893b8f84 (diff)
parent71e0d5e0732225e95affbacd1a08f2a8513d2802 (diff)
downloadmonitoring-plugins-ad7acf4618f1ac8a49277a482fb4ca391a26f584.tar.gz
Merge pull request #2172 from RincewindsHat/fix/check-ntp-time/output-format-option
check_ntp_time: add cli option for output format
-rw-r--r--plugins/check_ntp_time.c22
-rw-r--r--plugins/check_ntp_time.d/config.h6
2 files changed, 28 insertions, 0 deletions
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c
index 7c3fc24d..602b6010 100644
--- a/plugins/check_ntp_time.c
+++ b/plugins/check_ntp_time.c
@@ -505,6 +505,11 @@ static offset_request_wrapper offset_request(const char *host, const char *port,
505} 505}
506 506
507static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { 507static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) {
508
509 enum {
510 output_format_index = CHAR_MAX + 1,
511 };
512
508 static struct option longopts[] = {{"version", no_argument, 0, 'V'}, 513 static struct option longopts[] = {{"version", no_argument, 0, 'V'},
509 {"help", no_argument, 0, 'h'}, 514 {"help", no_argument, 0, 'h'},
510 {"verbose", no_argument, 0, 'v'}, 515 {"verbose", no_argument, 0, 'v'},
@@ -517,6 +522,7 @@ static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) {
517 {"timeout", required_argument, 0, 't'}, 522 {"timeout", required_argument, 0, 't'},
518 {"hostname", required_argument, 0, 'H'}, 523 {"hostname", required_argument, 0, 'H'},
519 {"port", required_argument, 0, 'p'}, 524 {"port", required_argument, 0, 'p'},
525 {"output-format", required_argument, 0, output_format_index},
520 {0, 0, 0, 0}}; 526 {0, 0, 0, 0}};
521 527
522 if (argc < 2) { 528 if (argc < 2) {
@@ -536,6 +542,17 @@ static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) {
536 } 542 }
537 543
538 switch (option_char) { 544 switch (option_char) {
545 case output_format_index: {
546 parsed_output_format parser = mp_parse_output_format(optarg);
547 if (!parser.parsing_success) {
548 printf("Invalid output format: %s\n", optarg);
549 exit(STATE_UNKNOWN);
550 }
551
552 result.config.output_format_is_set = true;
553 result.config.output_format = parser.output_format;
554 break;
555 }
539 case 'h': 556 case 'h':
540 print_help(); 557 print_help();
541 exit(STATE_UNKNOWN); 558 exit(STATE_UNKNOWN);
@@ -623,6 +640,10 @@ int main(int argc, char *argv[]) {
623 640
624 const check_ntp_time_config config = tmp_config.config; 641 const check_ntp_time_config config = tmp_config.config;
625 642
643 if (config.output_format_is_set) {
644 mp_set_format(config.output_format);
645 }
646
626 /* initialize alarm signal handling */ 647 /* initialize alarm signal handling */
627 signal(SIGALRM, socket_timeout_alarm_handler); 648 signal(SIGALRM, socket_timeout_alarm_handler);
628 649
@@ -687,6 +708,7 @@ void print_help(void) {
687 printf(" %s\n", _("Expected offset of the ntp server relative to local server (seconds)")); 708 printf(" %s\n", _("Expected offset of the ntp server relative to local server (seconds)"));
688 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 709 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
689 printf(UT_VERBOSE); 710 printf(UT_VERBOSE);
711 printf(UT_OUTPUT_FORMAT);
690 712
691 printf("\n"); 713 printf("\n");
692 printf("%s\n", _("This plugin checks the clock offset between the local host and a")); 714 printf("%s\n", _("This plugin checks the clock offset between the local host and a"));
diff --git a/plugins/check_ntp_time.d/config.h b/plugins/check_ntp_time.d/config.h
index a62e4ceb..9bbd82aa 100644
--- a/plugins/check_ntp_time.d/config.h
+++ b/plugins/check_ntp_time.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 "thresholds.h" 5#include "thresholds.h"
5#include <stddef.h> 6#include <stddef.h>
6 7
@@ -12,6 +13,9 @@ typedef struct {
12 int time_offset; 13 int time_offset;
13 14
14 mp_thresholds offset_thresholds; 15 mp_thresholds offset_thresholds;
16
17 bool output_format_is_set;
18 mp_output_format output_format;
15} check_ntp_time_config; 19} check_ntp_time_config;
16 20
17check_ntp_time_config check_ntp_time_config_init() { 21check_ntp_time_config check_ntp_time_config_init() {
@@ -23,6 +27,8 @@ check_ntp_time_config check_ntp_time_config_init() {
23 .time_offset = 0, 27 .time_offset = 0,
24 28
25 .offset_thresholds = mp_thresholds_init(), 29 .offset_thresholds = mp_thresholds_init(),
30
31 .output_format_is_set = false,
26 }; 32 };
27 33
28 mp_range warning = mp_range_init(); 34 mp_range warning = mp_range_init();