From 326d3996248346353894f50bc85133c4e045be92 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 26 Nov 2025 14:35:21 +0100 Subject: check_mrtg: implement modern output --- plugins/check_mrtg.c | 138 +++++++++++++++++++++++++++++++----------- plugins/check_mrtg.d/config.h | 17 +++--- 2 files changed, 110 insertions(+), 45 deletions(-) (limited to 'plugins') diff --git a/plugins/check_mrtg.c b/plugins/check_mrtg.c index 4a17049a..cdc2a035 100644 --- a/plugins/check_mrtg.c +++ b/plugins/check_mrtg.c @@ -29,14 +29,18 @@ * *****************************************************************************/ -const char *progname = "check_mrtg"; -const char *copyright = "1999-2024"; -const char *email = "devel@monitoring-plugins.org"; - #include "common.h" +#include "output.h" +#include "perfdata.h" +#include "states.h" +#include "thresholds.h" #include "utils.h" #include "check_mrtg.d/config.h" +const char *progname = "check_mrtg"; +const char *copyright = "1999-2024"; +const char *email = "devel@monitoring-plugins.org"; + typedef struct { int errorcode; check_mrtg_config config; @@ -62,11 +66,24 @@ int main(int argc, char **argv) { const check_mrtg_config config = tmp_config.config; + if (config.output_format_is_set) { + mp_set_format(config.output_format); + } + + mp_check overall = mp_check_init(); + /* open the MRTG log file for reading */ + mp_subcheck sc_open_mrtg_log_file = mp_subcheck_init(); FILE *mtrg_log_file = fopen(config.log_file, "r"); if (mtrg_log_file == NULL) { - printf(_("Unable to open MRTG log file\n")); - return STATE_UNKNOWN; + xasprintf(&sc_open_mrtg_log_file.output, "unable to open MRTG log file"); + sc_open_mrtg_log_file = mp_set_subcheck_state(sc_open_mrtg_log_file, STATE_UNKNOWN); + mp_add_subcheck_to_check(&overall, sc_open_mrtg_log_file); + mp_exit(overall); + } else { + xasprintf(&sc_open_mrtg_log_file.output, "opened MRTG log file"); + sc_open_mrtg_log_file = mp_set_subcheck_state(sc_open_mrtg_log_file, STATE_OK); + mp_add_subcheck_to_check(&overall, sc_open_mrtg_log_file); } time_t timestamp = 0; @@ -120,18 +137,32 @@ int main(int argc, char **argv) { fclose(mtrg_log_file); /* if we couldn't read enough data, return an unknown error */ + mp_subcheck sc_process_mrtg_log_file = mp_subcheck_init(); if (line <= 2) { - printf(_("Unable to process MRTG log file\n")); - return STATE_UNKNOWN; + xasprintf(&sc_process_mrtg_log_file.output, "unable to process MRTG log file"); + sc_process_mrtg_log_file = mp_set_subcheck_state(sc_process_mrtg_log_file, STATE_UNKNOWN); + mp_exit(overall); + } else { + xasprintf(&sc_process_mrtg_log_file.output, "processed MRTG log file"); + sc_process_mrtg_log_file = mp_set_subcheck_state(sc_process_mrtg_log_file, STATE_OK); + mp_add_subcheck_to_check(&overall, sc_process_mrtg_log_file); } /* make sure the MRTG data isn't too old */ time_t current_time; time(¤t_time); + mp_subcheck sc_data_expired = mp_subcheck_init(); if (config.expire_minutes > 0 && (current_time - timestamp) > (config.expire_minutes * 60)) { - printf(_("MRTG data has expired (%d minutes old)\n"), - (int)((current_time - timestamp) / 60)); - return STATE_WARNING; + xasprintf(&sc_data_expired.output, "MRTG data has expired (%d minutes old)", + (int)((current_time - timestamp) / 60)); + sc_data_expired = mp_set_subcheck_state(sc_data_expired, STATE_WARNING); + mp_add_subcheck_to_check(&overall, sc_data_expired); + mp_exit(overall); + } else { + xasprintf(&sc_data_expired.output, "MRTG data should be valid (%d minutes old)", + (int)((current_time - timestamp) / 60)); + sc_data_expired = mp_set_subcheck_state(sc_data_expired, STATE_OK); + mp_add_subcheck_to_check(&overall, sc_data_expired); } unsigned long rate = 0L; @@ -142,24 +173,27 @@ int main(int argc, char **argv) { rate = maximum_value_rate; } - int result = STATE_OK; - if (config.value_critical_threshold_set && rate > config.value_critical_threshold) { - result = STATE_CRITICAL; - } else if (config.value_warning_threshold_set && rate > config.value_warning_threshold) { - result = STATE_WARNING; - } + mp_subcheck sc_values = mp_subcheck_init(); + mp_perfdata pd_value = perfdata_init(); + pd_value = mp_set_pd_value(pd_value, rate); + pd_value.label = config.label; + pd_value = mp_pd_set_thresholds(pd_value, config.values_threshold); - printf("%s. %s = %lu %s|%s\n", (config.use_average) ? _("Avg") : _("Max"), config.label, rate, - config.units, - perfdata(config.label, (long)rate, config.units, config.value_warning_threshold_set, - (long)config.value_warning_threshold, config.value_critical_threshold_set, - (long)config.value_critical_threshold, 0, 0, 0, 0)); + sc_values = mp_set_subcheck_state(sc_values, mp_get_pd_status(pd_value)); + xasprintf(&sc_values.output, "%s. %s = %lu %s", (config.use_average) ? _("Avg") : _("Max"), + config.label, rate, config.units); - return result; + mp_add_subcheck_to_check(&overall, sc_values); + + mp_exit(overall); } /* process command-line arguments */ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { + enum { + output_format_index, + }; + static struct option longopts[] = {{"logfile", required_argument, 0, 'F'}, {"expires", required_argument, 0, 'e'}, {"aggregation", required_argument, 0, 'a'}, @@ -171,6 +205,7 @@ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { {"variable", required_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, + {"output-format", required_argument, 0, output_format_index}, {0, 0, 0, 0}}; check_mrtg_config_wrapper result = { @@ -218,14 +253,22 @@ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { usage4(_("Invalid variable number")); } break; - case 'w': /* critical time threshold */ - result.config.value_warning_threshold_set = true; - result.config.value_warning_threshold = strtoul(optarg, NULL, 10); - break; - case 'c': /* warning time threshold */ - result.config.value_critical_threshold_set = true; - result.config.value_critical_threshold = strtoul(optarg, NULL, 10); - break; + case 'w': /* critical time threshold */ { + mp_range_parsed tmp = mp_parse_range_string(optarg); + if (tmp.error != MP_PARSING_SUCCES) { + die(STATE_UNKNOWN, "failed to parse warning threshold"); + } + result.config.values_threshold = + mp_thresholds_set_warn(result.config.values_threshold, tmp.range); + } break; + case 'c': /* warning time threshold */ { + mp_range_parsed tmp = mp_parse_range_string(optarg); + if (tmp.error != MP_PARSING_SUCCES) { + die(STATE_UNKNOWN, "failed to parse critical threshold"); + } + result.config.values_threshold = + mp_thresholds_set_crit(result.config.values_threshold, tmp.range); + } break; case 'l': /* label */ result.config.label = optarg; break; @@ -240,6 +283,17 @@ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { exit(STATE_UNKNOWN); case '?': /* help */ usage5(); + 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; + } } } @@ -274,14 +328,22 @@ check_mrtg_config_wrapper process_arguments(int argc, char **argv) { } } - if (argc > option_char && !result.config.value_warning_threshold_set) { - result.config.value_warning_threshold_set = true; - result.config.value_warning_threshold = strtoul(argv[option_char++], NULL, 10); + if (argc > option_char && !result.config.values_threshold.warning_is_set) { + mp_range_parsed tmp = mp_parse_range_string(argv[option_char++]); + if (tmp.error != MP_PARSING_SUCCES) { + die(STATE_UNKNOWN, "failed to parse warning threshold"); + } + result.config.values_threshold = + mp_thresholds_set_warn(result.config.values_threshold, tmp.range); } - if (argc > option_char && !result.config.value_critical_threshold_set) { - result.config.value_critical_threshold_set = true; - result.config.value_critical_threshold = strtoul(argv[option_char++], NULL, 10); + if (argc > option_char && !result.config.values_threshold.critical_is_set) { + mp_range_parsed tmp = mp_parse_range_string(argv[option_char++]); + if (tmp.error != MP_PARSING_SUCCES) { + die(STATE_UNKNOWN, "failed to parse critical threshold"); + } + result.config.values_threshold = + mp_thresholds_set_crit(result.config.values_threshold, tmp.range); } if (argc > option_char && strlen(result.config.label) == 0) { @@ -345,6 +407,8 @@ void print_help(void) { printf(" %s\n", _("Option units label for data (Example: Packets/Sec, Errors/Sec,")); printf(" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")")); + printf(UT_OUTPUT_FORMAT); + printf("\n"); printf(" %s\n", _("If the value exceeds the threshold, a WARNING status is returned. If")); diff --git a/plugins/check_mrtg.d/config.h b/plugins/check_mrtg.d/config.h index 96b849a2..4a5b5595 100644 --- a/plugins/check_mrtg.d/config.h +++ b/plugins/check_mrtg.d/config.h @@ -1,6 +1,8 @@ #pragma once #include "../../config.h" +#include "output.h" +#include "thresholds.h" #include #include @@ -12,10 +14,10 @@ typedef struct { char *units; char *log_file; - bool value_warning_threshold_set; - unsigned long value_warning_threshold; - bool value_critical_threshold_set; - unsigned long value_critical_threshold; + mp_thresholds values_threshold; + + bool output_format_is_set; + mp_output_format output_format; } check_mrtg_config; check_mrtg_config check_mrtg_config_init() { @@ -27,10 +29,9 @@ check_mrtg_config check_mrtg_config_init() { .units = NULL, .log_file = NULL, - .value_warning_threshold_set = false, - .value_warning_threshold = 0, - .value_critical_threshold_set = false, - .value_critical_threshold = 0, + .values_threshold = mp_thresholds_init(), + + .output_format_is_set = false, }; return tmp; } -- cgit v1.2.3-74-g34f1