Module: monitoring-plugins
 Branch: master
 Commit: 71e0d5e0732225e95affbacd1a08f2a8513d2802
 Author: Lorenz Kästle <[email protected]>
   Date: Wed Nov  5 12:19:20 2025 +0100
    URL: 
https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=71e0d5e0

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(+)

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 <stddef.h>
 
@@ -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();

Reply via email to