From 802e46f8ea36c344f112d7e1dd8d64d17a4cc939 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Mon, 15 Sep 2025 12:59:37 +0200 Subject: Run clang-format again --- plugins/check_mysql_query.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'plugins/check_mysql_query.c') diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c index 5e04a94b..c7e84deb 100644 --- a/plugins/check_mysql_query.c +++ b/plugins/check_mysql_query.c @@ -47,7 +47,8 @@ typedef struct { check_mysql_query_config config; } check_mysql_query_config_wrapper; static check_mysql_query_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); -static check_mysql_query_config_wrapper validate_arguments(check_mysql_query_config_wrapper /*config_wrapper*/); +static check_mysql_query_config_wrapper + validate_arguments(check_mysql_query_config_wrapper /*config_wrapper*/); static void print_help(void); void print_usage(void); @@ -83,7 +84,8 @@ int main(int argc, char **argv) { } /* establish a connection to the server and error checking */ - if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, config.db_port, config.db_socket, 0)) { + if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, + config.db_port, config.db_socket, 0)) { if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { @@ -155,8 +157,11 @@ int main(int argc, char **argv) { printf("QUERY %s: ", _("CRITICAL")); } printf(_("'%s' returned %f | %s"), config.sql_query, value, - fperfdata("result", value, "", config.my_thresholds->warning, config.my_thresholds->warning ? config.my_thresholds->warning->end : 0, - config.my_thresholds->critical, config.my_thresholds->critical ? config.my_thresholds->critical->end : 0, false, 0, false, 0)); + fperfdata("result", value, "", config.my_thresholds->warning, + config.my_thresholds->warning ? config.my_thresholds->warning->end : 0, + config.my_thresholds->critical, + config.my_thresholds->critical ? config.my_thresholds->critical->end : 0, + false, 0, false, 0)); printf("\n"); return status; @@ -164,12 +169,21 @@ int main(int argc, char **argv) { /* process command-line arguments */ check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { - static struct option longopts[] = { - {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, - {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'}, - {"group", required_argument, 0, 'g'}, {"port", required_argument, 0, 'P'}, {"verbose", no_argument, 0, 'v'}, - {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"query", required_argument, 0, 'q'}, - {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {0, 0, 0, 0}}; + static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, + {"socket", required_argument, 0, 's'}, + {"database", required_argument, 0, 'd'}, + {"username", required_argument, 0, 'u'}, + {"password", required_argument, 0, 'p'}, + {"file", required_argument, 0, 'f'}, + {"group", required_argument, 0, 'g'}, + {"port", required_argument, 0, 'P'}, + {"verbose", no_argument, 0, 'v'}, + {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, + {"query", required_argument, 0, 'q'}, + {"warning", required_argument, 0, 'w'}, + {"critical", required_argument, 0, 'c'}, + {0, 0, 0, 0}}; check_mysql_query_config_wrapper result = { .errorcode = OK, @@ -255,7 +269,8 @@ check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { return validate_arguments(result); } -check_mysql_query_config_wrapper validate_arguments(check_mysql_query_config_wrapper config_wrapper) { +check_mysql_query_config_wrapper +validate_arguments(check_mysql_query_config_wrapper config_wrapper) { if (config_wrapper.config.sql_query == NULL) { usage("Must specify a SQL query to run"); } -- cgit v1.2.3-74-g34f1 From 9d827acbe1aac0edaa91a8765a87412a189cadf1 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Nov 2025 15:01:36 +0100 Subject: check_mysql_query: implement modern output --- plugins/check_mysql_query.c | 105 +++++++++++++++++++++-------------- plugins/check_mysql_query.d/config.h | 4 +- plugins/t/check_mysql_query.t | 2 +- 3 files changed, 65 insertions(+), 46 deletions(-) (limited to 'plugins/check_mysql_query.c') diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c index c7e84deb..cb79b4b4 100644 --- a/plugins/check_mysql_query.c +++ b/plugins/check_mysql_query.c @@ -29,11 +29,11 @@ * *****************************************************************************/ -const char *progname = "check_mysql_query"; -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 "utils_base.h" #include "netutils.h" @@ -42,6 +42,10 @@ const char *email = "devel@monitoring-plugins.org"; #include #include +const char *progname = "check_mysql_query"; +const char *copyright = "1999-2024"; +const char *email = "devel@monitoring-plugins.org"; + typedef struct { int errorcode; check_mysql_query_config config; @@ -83,27 +87,38 @@ int main(int argc, char **argv) { mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client"); } + mp_check overall = mp_check_init(); + mp_subcheck sc_connect = mp_subcheck_init(); + /* establish a connection to the server and error checking */ if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, config.db_port, config.db_socket, 0)) { + xasprintf(&sc_connect.output, "query failed: %s", mysql_error(&mysql)); + if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { - die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); + sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { - die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); + sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); } else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY) { - die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); + sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); } else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR) { - die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); + sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); } else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR) { - die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); + sc_connect = mp_set_subcheck_state(sc_connect, STATE_WARNING); } else { - die(STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error(&mysql)); + sc_connect = mp_set_subcheck_state(sc_connect, STATE_CRITICAL); } + + mp_add_subcheck_to_check(&overall, sc_connect); + mp_exit(overall); } - char *error = NULL; + sc_connect = mp_set_subcheck_state(sc_connect, STATE_OK); + xasprintf(&sc_connect.output, "query succeeded"); + mp_add_subcheck_to_check(&overall, sc_connect); + if (mysql_query(&mysql, config.sql_query) != 0) { - error = strdup(mysql_error(&mysql)); + char *error = strdup(mysql_error(&mysql)); mysql_close(&mysql); die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); } @@ -111,7 +126,7 @@ int main(int argc, char **argv) { MYSQL_RES *res; /* store the result */ if ((res = mysql_store_result(&mysql)) == NULL) { - error = strdup(mysql_error(&mysql)); + char *error = strdup(mysql_error(&mysql)); mysql_close(&mysql); die(STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error); } @@ -122,17 +137,24 @@ int main(int argc, char **argv) { die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned")); } + mp_subcheck sc_value = mp_subcheck_init(); MYSQL_ROW row; /* fetch the first row */ if ((row = mysql_fetch_row(res)) == NULL) { - error = strdup(mysql_error(&mysql)); + xasprintf(&sc_value.output, "fetch row error - %s", mysql_error(&mysql)); mysql_free_result(res); mysql_close(&mysql); - die(STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error); + + sc_value = mp_set_subcheck_state(sc_value, STATE_CRITICAL); + mp_add_subcheck_to_check(&overall, sc_value); + mp_exit(overall); } if (!is_numeric(row[0])) { - die(STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]); + xasprintf(&sc_value.output, "query result is not numeric"); + sc_value = mp_set_subcheck_state(sc_value, STATE_CRITICAL); + mp_add_subcheck_to_check(&overall, sc_value); + mp_exit(overall); } double value = strtod(row[0], NULL); @@ -147,24 +169,18 @@ int main(int argc, char **argv) { printf("mysql result: %f\n", value); } - int status = get_status(value, config.my_thresholds); + mp_perfdata pd_query_result = perfdata_init(); + pd_query_result = mp_set_pd_value(pd_query_result, value); + pd_query_result = mp_pd_set_thresholds(pd_query_result, config.thresholds); + pd_query_result.label = "result"; + mp_add_perfdata_to_subcheck(&sc_value, pd_query_result); - if (status == STATE_OK) { - printf("QUERY %s: ", _("OK")); - } else if (status == STATE_WARNING) { - printf("QUERY %s: ", _("WARNING")); - } else if (status == STATE_CRITICAL) { - printf("QUERY %s: ", _("CRITICAL")); - } - printf(_("'%s' returned %f | %s"), config.sql_query, value, - fperfdata("result", value, "", config.my_thresholds->warning, - config.my_thresholds->warning ? config.my_thresholds->warning->end : 0, - config.my_thresholds->critical, - config.my_thresholds->critical ? config.my_thresholds->critical->end : 0, - false, 0, false, 0)); - printf("\n"); + sc_value = mp_set_subcheck_state(sc_value, mp_get_pd_status(pd_query_result)); + xasprintf(&sc_value.output, "'%s' returned '%f'", config.sql_query, value); + + mp_add_subcheck_to_check(&overall, sc_value); - return status; + mp_exit(overall); } /* process command-line arguments */ @@ -195,9 +211,6 @@ check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { return result; } - char *warning = NULL; - char *critical = NULL; - while (true) { int option = 0; int option_char = getopt_long(argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option); @@ -253,19 +266,25 @@ check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { case 'q': xasprintf(&result.config.sql_query, "%s", optarg); break; - case 'w': - warning = optarg; - break; - case 'c': - critical = optarg; - break; + case 'w': { + mp_range_parsed tmp = mp_parse_range_string(optarg); + if (tmp.error != MP_PARSING_SUCCES) { + die(STATE_UNKNOWN, "failed to parse warnign threshold"); + } + result.config.thresholds = mp_thresholds_set_warn(result.config.thresholds, tmp.range); + } break; + case 'c': { + 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.thresholds = mp_thresholds_set_crit(result.config.thresholds, tmp.range); + } break; case '?': /* help */ usage5(); } } - set_thresholds(&result.config.my_thresholds, warning, critical); - return validate_arguments(result); } diff --git a/plugins/check_mysql_query.d/config.h b/plugins/check_mysql_query.d/config.h index be019160..1c9952e5 100644 --- a/plugins/check_mysql_query.d/config.h +++ b/plugins/check_mysql_query.d/config.h @@ -15,7 +15,7 @@ typedef struct { unsigned int db_port; char *sql_query; - thresholds *my_thresholds; + mp_thresholds thresholds; } check_mysql_query_config; check_mysql_query_config check_mysql_query_config_init() { @@ -30,7 +30,7 @@ check_mysql_query_config check_mysql_query_config_init() { .db_port = MYSQL_PORT, .sql_query = NULL, - .my_thresholds = NULL, + .thresholds = mp_thresholds_init(), }; return tmp; } diff --git a/plugins/t/check_mysql_query.t b/plugins/t/check_mysql_query.t index c30245b2..6de48bf6 100644 --- a/plugins/t/check_mysql_query.t +++ b/plugins/t/check_mysql_query.t @@ -54,5 +54,5 @@ like( $result->output, "/No rows returned/", "No rows error message"); $result = NPTest->testCmd("./check_mysql_query -q 'SHOW VARIABLES' -H $mysqlserver $mysql_login_details"); cmp_ok( $result->return_code, '==', 2, "Data not numeric"); -like( $result->output, "/Is not a numeric/", "Data not numeric error message"); +like( $result->output, "/is not numeric/", "Data not numeric error message"); -- cgit v1.2.3-74-g34f1 From 5bbfd58105766d453a6d0de5b9afcaf6daf4b460 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Fri, 7 Nov 2025 15:10:39 +0100 Subject: Fix typo --- plugins/check_mysql_query.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins/check_mysql_query.c') diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c index cb79b4b4..8af378d5 100644 --- a/plugins/check_mysql_query.c +++ b/plugins/check_mysql_query.c @@ -269,7 +269,7 @@ check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { case 'w': { mp_range_parsed tmp = mp_parse_range_string(optarg); if (tmp.error != MP_PARSING_SUCCES) { - die(STATE_UNKNOWN, "failed to parse warnign threshold"); + die(STATE_UNKNOWN, "failed to parse warning threshold"); } result.config.thresholds = mp_thresholds_set_warn(result.config.thresholds, tmp.range); } break; -- cgit v1.2.3-74-g34f1 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/check_mysql_query.c') 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