Module: monitoring-plugins
 Branch: master
 Commit: 013b4d64899c22532f9578b4d64fa3b646e4a0c4
 Author: Lorenz Kästle <12514511+rincewinds...@users.noreply.github.com>
   Date: Tue Mar 11 13:44:55 2025 +0100
    URL: 
https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=013b4d64

Refactor check_mysql_query

---

 plugins/Makefile.am                  |  1 +
 plugins/check_mysql_query.c          | 96 ++++++++++++++++++------------------
 plugins/check_mysql_query.d/config.h | 36 ++++++++++++++
 3 files changed, 86 insertions(+), 47 deletions(-)

diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 19b3d172..1e4789ff 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -57,6 +57,7 @@ EXTRA_DIST = t \
                         check_ssh.d \
                         check_dns.d \
                         check_mrtgraf.d \
+                        check_mysql_query.d \
                         check_mrtg.d \
                         check_apt.d \
                         check_by_ssh.d \
diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c
index 480453b1..5e04a94b 100644
--- a/plugins/check_mysql_query.c
+++ b/plugins/check_mysql_query.c
@@ -37,27 +37,21 @@ const char *email = "devel@monitoring-plugins.org";
 #include "utils.h"
 #include "utils_base.h"
 #include "netutils.h"
+#include "check_mysql_query.d/config.h"
 
 #include <mysql.h>
 #include <errmsg.h>
 
-static char *db_user = NULL;
-static char *db_host = NULL;
-static char *db_socket = NULL;
-static char *db_pass = NULL;
-static char *db = NULL;
-static char *opt_file = NULL;
-static char *opt_group = NULL;
-static unsigned int db_port = MYSQL_PORT;
-
-static int process_arguments(int /*argc*/, char ** /*argv*/);
-static int validate_arguments(void);
+typedef struct {
+       int errorcode;
+       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 void print_help(void);
 void print_usage(void);
 
-static char *sql_query = NULL;
 static int verbose = 0;
-static thresholds *my_thresholds = NULL;
 
 int main(int argc, char **argv) {
        setlocale(LC_ALL, "");
@@ -67,26 +61,29 @@ int main(int argc, char **argv) {
        /* Parse extra opts if any */
        argv = np_extra_opts(&argc, argv, progname);
 
-       if (process_arguments(argc, argv) == ERROR) {
+       check_mysql_query_config_wrapper tmp_config = process_arguments(argc, 
argv);
+       if (tmp_config.errorcode == ERROR) {
                usage4(_("Could not parse arguments"));
        }
 
+       const check_mysql_query_config config = tmp_config.config;
+
        MYSQL mysql;
        /* initialize mysql  */
        mysql_init(&mysql);
 
-       if (opt_file != NULL) {
-               mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, opt_file);
+       if (config.opt_file != NULL) {
+               mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, config.opt_file);
        }
 
-       if (opt_group != NULL) {
-               mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, opt_group);
+       if (config.opt_group != NULL) {
+               mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, 
config.opt_group);
        } else {
                mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client");
        }
 
        /* establish a connection to the server and error checking */
-       if (!mysql_real_connect(&mysql, db_host, db_user, db_pass, db, db_port, 
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) {
@@ -103,7 +100,7 @@ int main(int argc, char **argv) {
        }
 
        char *error = NULL;
-       if (mysql_query(&mysql, sql_query) != 0) {
+       if (mysql_query(&mysql, config.sql_query) != 0) {
                error = strdup(mysql_error(&mysql));
                mysql_close(&mysql);
                die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), 
_("Error with query"), error);
@@ -148,7 +145,7 @@ int main(int argc, char **argv) {
                printf("mysql result: %f\n", value);
        }
 
-       int status = get_status(value, my_thresholds);
+       int status = get_status(value, config.my_thresholds);
 
        if (status == STATE_OK) {
                printf("QUERY %s: ", _("OK"));
@@ -157,17 +154,16 @@ int main(int argc, char **argv) {
        } else if (status == STATE_CRITICAL) {
                printf("QUERY %s: ", _("CRITICAL"));
        }
-       printf(_("'%s' returned %f | %s"), sql_query, value,
-                  fperfdata("result", value, "", my_thresholds->warning ? true 
: false, my_thresholds->warning ? my_thresholds->warning->end : 0,
-                                        my_thresholds->critical ? true : 
false, my_thresholds->critical ? my_thresholds->critical->end : 0, false, 0, 
false,
-                                        0));
+       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");
 
        return status;
 }
 
 /* process command-line arguments */
-int process_arguments(int argc, char **argv) {
+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'},
@@ -175,8 +171,14 @@ int process_arguments(int argc, char **argv) {
                {"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,
+               .config = check_mysql_query_config_init(),
+       };
+
        if (argc < 1) {
-               return ERROR;
+               result.errorcode = ERROR;
+               return result;
        }
 
        char *warning = NULL;
@@ -193,22 +195,22 @@ int process_arguments(int argc, char **argv) {
                switch (option_char) {
                case 'H': /* hostname */
                        if (is_host(optarg)) {
-                               db_host = optarg;
+                               result.config.db_host = optarg;
                        } else {
                                usage2(_("Invalid hostname/address"), optarg);
                        }
                        break;
                case 's': /* socket */
-                       db_socket = optarg;
+                       result.config.db_socket = optarg;
                        break;
                case 'd': /* database */
-                       db = optarg;
+                       result.config.db = optarg;
                        break;
                case 'u': /* username */
-                       db_user = optarg;
+                       result.config.db_user = optarg;
                        break;
                case 'p': /* authentication information: password */
-                       db_pass = strdup(optarg);
+                       result.config.db_pass = strdup(optarg);
 
                        /* Delete the password from process list */
                        while (*optarg != '\0') {
@@ -217,13 +219,13 @@ int process_arguments(int argc, char **argv) {
                        }
                        break;
                case 'f': /* client options file */
-                       opt_file = optarg;
+                       result.config.opt_file = optarg;
                        break;
                case 'g': /* client options group */
-                       opt_group = optarg;
+                       result.config.opt_group = optarg;
                        break;
                case 'P': /* critical time threshold */
-                       db_port = atoi(optarg);
+                       result.config.db_port = atoi(optarg);
                        break;
                case 'v':
                        verbose++;
@@ -235,7 +237,7 @@ int process_arguments(int argc, char **argv) {
                        print_help();
                        exit(STATE_UNKNOWN);
                case 'q':
-                       xasprintf(&sql_query, "%s", optarg);
+                       xasprintf(&result.config.sql_query, "%s", optarg);
                        break;
                case 'w':
                        warning = optarg;
@@ -248,29 +250,29 @@ int process_arguments(int argc, char **argv) {
                }
        }
 
-       set_thresholds(&my_thresholds, warning, critical);
+       set_thresholds(&result.config.my_thresholds, warning, critical);
 
-       return validate_arguments();
+       return validate_arguments(result);
 }
 
-int validate_arguments(void) {
-       if (sql_query == NULL) {
+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");
        }
 
-       if (db_user == NULL) {
-               db_user = strdup("");
+       if (config_wrapper.config.db_user == NULL) {
+               config_wrapper.config.db_user = strdup("");
        }
 
-       if (db_host == NULL) {
-               db_host = strdup("");
+       if (config_wrapper.config.db_host == NULL) {
+               config_wrapper.config.db_host = strdup("");
        }
 
-       if (db == NULL) {
-               db = strdup("");
+       if (config_wrapper.config.db == NULL) {
+               config_wrapper.config.db = strdup("");
        }
 
-       return OK;
+       return config_wrapper;
 }
 
 void print_help(void) {
diff --git a/plugins/check_mysql_query.d/config.h 
b/plugins/check_mysql_query.d/config.h
new file mode 100644
index 00000000..be019160
--- /dev/null
+++ b/plugins/check_mysql_query.d/config.h
@@ -0,0 +1,36 @@
+#pragma once
+
+#include "../../config.h"
+#include "thresholds.h"
+#include <mysql.h>
+
+typedef struct {
+       char *db_host;
+       char *db_socket;
+       char *db;
+       char *db_user;
+       char *db_pass;
+       char *opt_file;
+       char *opt_group;
+       unsigned int db_port;
+
+       char *sql_query;
+       thresholds *my_thresholds;
+} check_mysql_query_config;
+
+check_mysql_query_config check_mysql_query_config_init() {
+       check_mysql_query_config tmp = {
+               .db_host = NULL,
+               .db_socket = NULL,
+               .db = NULL,
+               .db_user = NULL,
+               .db_pass = NULL,
+               .opt_file = NULL,
+               .opt_group = NULL,
+               .db_port = MYSQL_PORT,
+
+               .sql_query = NULL,
+               .my_thresholds = NULL,
+       };
+       return tmp;
+}

Reply via email to