From 317ee266a88bd8752113df39f12e2d133edd6802 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Wed, 26 Nov 2025 13:50:58 +0100 Subject: Add output formatting option where they were forgotten --- plugins/check_dbi.c | 2 ++ plugins/check_mysql.c | 19 +++++++++++++++++++ plugins/check_mysql.d/config.h | 5 +++++ plugins/check_mysql_query.c | 22 ++++++++++++++++++++++ plugins/check_mysql_query.d/config.h | 6 ++++++ 5 files changed, 54 insertions(+) (limited to 'plugins') diff --git a/plugins/check_dbi.c b/plugins/check_dbi.c index 9bc68eb3..81d92952 100644 --- a/plugins/check_dbi.c +++ b/plugins/check_dbi.c @@ -688,6 +688,8 @@ void print_help(void) { printf(UT_VERBOSE); + printf(UT_OUTPUT_FORMAT); + printf("\n"); printf(" %s\n", _("A DBI driver (-d option) is required. If the specified metric operates")); printf(" %s\n\n", _("on a query, one has to be specified (-q option).")); diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index 9d8094c0..009c9908 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c @@ -96,6 +96,10 @@ int main(int argc, char **argv) { const check_mysql_config config = tmp_config.config; + if (config.output_format_is_set) { + mp_set_format(config.output_format); + } + MYSQL mysql; /* initialize mysql */ mysql_init(&mysql); @@ -471,6 +475,7 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) { enum { CHECK_REPLICA_OPT = CHAR_MAX + 1, + output_format_index, }; static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, @@ -495,6 +500,7 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) { {"cert", required_argument, 0, 'a'}, {"ca-dir", required_argument, 0, 'D'}, {"ciphers", required_argument, 0, 'L'}, + {"output-format", required_argument, 0, output_format_index}, {0, 0, 0, 0}}; check_mysql_config_wrapper result = { @@ -605,6 +611,17 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) { break; 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; + } } } @@ -711,6 +728,8 @@ void print_help(void) { printf(" %s\n", "-L, --ciphers=STRING"); printf(" %s\n", _("List of valid SSL ciphers")); + printf(UT_OUTPUT_FORMAT); + printf("\n"); printf(" %s\n", _("There are no required arguments. By default, the local database is checked")); diff --git a/plugins/check_mysql.d/config.h b/plugins/check_mysql.d/config.h index ef086cfc..1d8c82bb 100644 --- a/plugins/check_mysql.d/config.h +++ b/plugins/check_mysql.d/config.h @@ -1,6 +1,7 @@ #pragma once #include "../../config.h" +#include "output.h" #include "thresholds.h" #include #include @@ -26,6 +27,8 @@ typedef struct { mp_thresholds replica_thresholds; + bool output_format_is_set; + mp_output_format output_format; } check_mysql_config; check_mysql_config check_mysql_config_init() { @@ -49,6 +52,8 @@ check_mysql_config check_mysql_config_init() { .ignore_auth = false, .replica_thresholds = mp_thresholds_init(), + + .output_format_is_set = false, }; return tmp; } diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c index 8af378d5..ae6cc15d 100644 --- a/plugins/check_mysql_query.c +++ b/plugins/check_mysql_query.c @@ -73,6 +73,10 @@ int main(int argc, char **argv) { const check_mysql_query_config config = tmp_config.config; + if (config.output_format_is_set) { + mp_set_format(config.output_format); + } + MYSQL mysql; /* initialize mysql */ mysql_init(&mysql); @@ -185,6 +189,10 @@ int main(int argc, char **argv) { /* process command-line arguments */ check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { + enum { + output_format_index = CHAR_MAX + 1, + }; + static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, @@ -199,6 +207,7 @@ check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { {"query", required_argument, 0, 'q'}, {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, + {"output-format", required_argument, 0, output_format_index}, {0, 0, 0, 0}}; check_mysql_query_config_wrapper result = { @@ -282,6 +291,17 @@ check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { } break; 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; + } } } @@ -344,6 +364,8 @@ void print_help(void) { printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); printf(" %s\n", _("Your clear-text password could be visible as a process table entry")); + printf(UT_OUTPUT_FORMAT); + printf("\n"); printf(" %s\n", _("A query is required. The result from the query should be numeric.")); printf(" %s\n", _("For extra security, create a user with minimal access.")); diff --git a/plugins/check_mysql_query.d/config.h b/plugins/check_mysql_query.d/config.h index 1c9952e5..32ab455a 100644 --- a/plugins/check_mysql_query.d/config.h +++ b/plugins/check_mysql_query.d/config.h @@ -1,6 +1,7 @@ #pragma once #include "../../config.h" +#include "output.h" #include "thresholds.h" #include @@ -16,6 +17,9 @@ typedef struct { char *sql_query; mp_thresholds thresholds; + + bool output_format_is_set; + mp_output_format output_format; } check_mysql_query_config; check_mysql_query_config check_mysql_query_config_init() { @@ -31,6 +35,8 @@ check_mysql_query_config check_mysql_query_config_init() { .sql_query = NULL, .thresholds = mp_thresholds_init(), + + .output_format_is_set = false, }; return tmp; } -- cgit v1.2.3-74-g34f1 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 From 2917b8735f1d56211eac0ad1bf7a051a842abd76 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 27 Nov 2025 13:24:30 +0100 Subject: check_curl: abort redir if location is not found This commit changes the behaviour of check_curl slightly. Previously when the redirection method was set to the old 'check_http' style redirection and there was no "location" header in the original answer 'check_curl' segfaulted. Now, at least it dies properly with a message. --- plugins/check_curl.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'plugins') diff --git a/plugins/check_curl.c b/plugins/check_curl.c index fc704171..4eaa5f1d 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c @@ -672,6 +672,11 @@ redir_wrapper redir(curlhelp_write_curlbuf *header_buf, const check_curl_config char *location = get_header_value(headers, nof_headers, "location"); + if (location == NULL) { + // location header not found + die(STATE_UNKNOWN, "HTTP UNKNOWN - could not find \"location\" header\n"); + } + if (verbose >= 2) { printf(_("* Seen redirect location %s\n"), location); } -- cgit v1.2.3-74-g34f1 From 9fc1e24543dc0b79a8f75f078feced6c5ee1fa96 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 27 Nov 2025 14:29:33 +0100 Subject: check_curl: try to be more helpful in check_curls help Trying to be more specific with the check_curl help. The idea is to clarify how the parameters are supposed to be used. --- plugins/check_curl.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'plugins') diff --git a/plugins/check_curl.c b/plugins/check_curl.c index fc704171..2c258f36 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c @@ -1480,7 +1480,9 @@ void print_help(void) { printf(" %s\n", _("Append a port to include it in the header (eg: example.com:5000)")); printf(" %s\n", "-I, --IP-address=ADDRESS"); printf(" %s\n", - _("IP address or name (use numeric address if possible to bypass DNS lookup).")); + "IP address or name (use numeric address if possible to bypass DNS lookup).\n"); + printf(" %s\n", + "This overwrites the network address of the target while leaving everything else (HTTP headers) as they are"); printf(" %s\n", "-p, --port=INTEGER"); printf(" %s", _("Port number (default: ")); printf("%d)\n", HTTP_PORT); @@ -1544,6 +1546,7 @@ void print_help(void) { printf(" %s\n", _("String to expect in the content")); printf(" %s\n", "-u, --url=PATH"); printf(" %s\n", _("URL to GET or POST (default: /)")); + printf(" %s\n", _("This is the part after the address in a URL, so for \"https://example.com/index.html\" it would be '-u /index.html'")); printf(" %s\n", "-P, --post=STRING"); printf(" %s\n", _("URL decoded http POST data")); printf(" %s\n", @@ -1685,7 +1688,7 @@ void print_help(void) { printf(" %s\n", _("It is recommended to use an environment proxy like:")); printf(" %s\n", _("https_proxy=http://192.168.100.35:3128 ./check_curl -H www.verisign.com -S")); - printf(" %s\n", _("legacy proxy requests in check_http style still work:")); + printf(" %s\n", _("legacy proxy requests in check_http style might still work, but are frowned upon, so DONT:")); printf(" %s\n", _("check_curl -I 192.168.100.35 -p 3128 -u https://www.verisign.com/ -S -j " "CONNECT -H www.verisign.com ")); printf(" %s\n", _("all these options are needed: -I -p -u " -- cgit v1.2.3-74-g34f1 From 57043387ea640ecf0a32fded0974f6366a53766f Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 27 Nov 2025 14:42:05 +0100 Subject: Remove some superfluous newlines --- plugins/check_curl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins') diff --git a/plugins/check_curl.c b/plugins/check_curl.c index 2c258f36..a20ce749 100644 --- a/plugins/check_curl.c +++ b/plugins/check_curl.c @@ -1480,7 +1480,7 @@ void print_help(void) { printf(" %s\n", _("Append a port to include it in the header (eg: example.com:5000)")); printf(" %s\n", "-I, --IP-address=ADDRESS"); printf(" %s\n", - "IP address or name (use numeric address if possible to bypass DNS lookup).\n"); + "IP address or name (use numeric address if possible to bypass DNS lookup)."); printf(" %s\n", "This overwrites the network address of the target while leaving everything else (HTTP headers) as they are"); printf(" %s\n", "-p, --port=INTEGER"); @@ -1559,7 +1559,7 @@ void print_help(void) { printf(" %s\n", _("Warn if document is more than SECONDS old. the number can also be of")); printf(" %s\n", _("the form \"10m\" for minutes, \"10h\" for hours, or \"10d\" for days.")); printf(" %s\n", "-T, --content-type=STRING"); - printf(" %s\n", _("specify Content-Type header media type when POSTing\n")); + printf(" %s\n", _("specify Content-Type header media type when POSTing")); printf(" %s\n", "-l, --linespan"); printf(" %s\n", _("Allow regex to span newlines (must precede -r or -R)")); printf(" %s\n", "-r, --regex, --ereg=STRING"); -- cgit v1.2.3-74-g34f1