From b35853ee4e10c4485a9521d77c95aecae6573e64 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 4 Nov 2025 12:08:59 +0100 Subject: check_ntp_time: implement modern output --- plugins/check_ntp_time.d/config.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'plugins/check_ntp_time.d') diff --git a/plugins/check_ntp_time.d/config.h b/plugins/check_ntp_time.d/config.h index 99dabbbd..a62e4ceb 100644 --- a/plugins/check_ntp_time.d/config.h +++ b/plugins/check_ntp_time.d/config.h @@ -11,7 +11,7 @@ typedef struct { bool quiet; int time_offset; - thresholds *offset_thresholds; + mp_thresholds offset_thresholds; } check_ntp_time_config; check_ntp_time_config check_ntp_time_config_init() { @@ -22,7 +22,16 @@ check_ntp_time_config check_ntp_time_config_init() { .quiet = false, .time_offset = 0, - .offset_thresholds = NULL, + .offset_thresholds = mp_thresholds_init(), }; + + mp_range warning = mp_range_init(); + warning = mp_range_set_end(warning, mp_create_pd_value(60)); + tmp.offset_thresholds = mp_thresholds_set_warn(tmp.offset_thresholds, warning); + + mp_range critical = mp_range_init(); + critical = mp_range_set_end(warning, mp_create_pd_value(120)); + tmp.offset_thresholds = mp_thresholds_set_crit(tmp.offset_thresholds, critical); + return tmp; } -- cgit v1.2.3-74-g34f1 From 71e0d5e0732225e95affbacd1a08f2a8513d2802 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 5 Nov 2025 12:19:20 +0100 Subject: check_ntp_time: add cli option for output format --- plugins/check_ntp_time.c | 22 ++++++++++++++++++++++ plugins/check_ntp_time.d/config.h | 6 ++++++ 2 files changed, 28 insertions(+) (limited to 'plugins/check_ntp_time.d') 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, } static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { + + enum { + output_format_index = CHAR_MAX + 1, + }; + static struct option longopts[] = {{"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, @@ -517,6 +522,7 @@ static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { {"timeout", required_argument, 0, 't'}, {"hostname", required_argument, 0, 'H'}, {"port", required_argument, 0, 'p'}, + {"output-format", required_argument, 0, output_format_index}, {0, 0, 0, 0}}; if (argc < 2) { @@ -536,6 +542,17 @@ static check_ntp_time_config_wrapper process_arguments(int argc, char **argv) { } switch (option_char) { + case output_format_index: { + parsed_output_format parser = mp_parse_output_format(optarg); + if (!parser.parsing_success) { + printf("Invalid output format: %s\n", optarg); + exit(STATE_UNKNOWN); + } + + result.config.output_format_is_set = true; + result.config.output_format = parser.output_format; + break; + } case 'h': print_help(); exit(STATE_UNKNOWN); @@ -623,6 +640,10 @@ int main(int argc, char *argv[]) { const check_ntp_time_config config = tmp_config.config; + if (config.output_format_is_set) { + mp_set_format(config.output_format); + } + /* initialize alarm signal handling */ signal(SIGALRM, socket_timeout_alarm_handler); @@ -687,6 +708,7 @@ void print_help(void) { printf(" %s\n", _("Expected offset of the ntp server relative to local server (seconds)")); printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); printf(UT_VERBOSE); + printf(UT_OUTPUT_FORMAT); printf("\n"); 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 @@ #pragma once #include "../../config.h" +#include "output.h" #include "thresholds.h" #include @@ -12,6 +13,9 @@ typedef struct { int time_offset; mp_thresholds offset_thresholds; + + bool output_format_is_set; + mp_output_format output_format; } check_ntp_time_config; check_ntp_time_config check_ntp_time_config_init() { @@ -23,6 +27,8 @@ check_ntp_time_config check_ntp_time_config_init() { .time_offset = 0, .offset_thresholds = mp_thresholds_init(), + + .output_format_is_set = false, }; mp_range warning = mp_range_init(); -- cgit v1.2.3-74-g34f1