diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 13:44:55 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 13:58:56 +0100 |
commit | 013b4d64899c22532f9578b4d64fa3b646e4a0c4 (patch) | |
tree | 0eb3482b29b9168e22390f44943b0dcdfb6918d4 | |
parent | e227016ac79a715301cac7eb41df7c752c882332 (diff) | |
download | monitoring-plugins-013b4d64899c22532f9578b4d64fa3b646e4a0c4.tar.gz |
Refactor check_mysql_query
-rw-r--r-- | plugins/Makefile.am | 1 | ||||
-rw-r--r-- | plugins/check_mysql_query.c | 96 | ||||
-rw-r--r-- | 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 \ | |||
57 | check_ssh.d \ | 57 | check_ssh.d \ |
58 | check_dns.d \ | 58 | check_dns.d \ |
59 | check_mrtgraf.d \ | 59 | check_mrtgraf.d \ |
60 | check_mysql_query.d \ | ||
60 | check_mrtg.d \ | 61 | check_mrtg.d \ |
61 | check_apt.d \ | 62 | check_apt.d \ |
62 | check_by_ssh.d \ | 63 | 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"; | |||
37 | #include "utils.h" | 37 | #include "utils.h" |
38 | #include "utils_base.h" | 38 | #include "utils_base.h" |
39 | #include "netutils.h" | 39 | #include "netutils.h" |
40 | #include "check_mysql_query.d/config.h" | ||
40 | 41 | ||
41 | #include <mysql.h> | 42 | #include <mysql.h> |
42 | #include <errmsg.h> | 43 | #include <errmsg.h> |
43 | 44 | ||
44 | static char *db_user = NULL; | 45 | typedef struct { |
45 | static char *db_host = NULL; | 46 | int errorcode; |
46 | static char *db_socket = NULL; | 47 | check_mysql_query_config config; |
47 | static char *db_pass = NULL; | 48 | } check_mysql_query_config_wrapper; |
48 | static char *db = NULL; | 49 | static check_mysql_query_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
49 | static char *opt_file = NULL; | 50 | static check_mysql_query_config_wrapper validate_arguments(check_mysql_query_config_wrapper /*config_wrapper*/); |
50 | static char *opt_group = NULL; | ||
51 | static unsigned int db_port = MYSQL_PORT; | ||
52 | |||
53 | static int process_arguments(int /*argc*/, char ** /*argv*/); | ||
54 | static int validate_arguments(void); | ||
55 | static void print_help(void); | 51 | static void print_help(void); |
56 | void print_usage(void); | 52 | void print_usage(void); |
57 | 53 | ||
58 | static char *sql_query = NULL; | ||
59 | static int verbose = 0; | 54 | static int verbose = 0; |
60 | static thresholds *my_thresholds = NULL; | ||
61 | 55 | ||
62 | int main(int argc, char **argv) { | 56 | int main(int argc, char **argv) { |
63 | setlocale(LC_ALL, ""); | 57 | setlocale(LC_ALL, ""); |
@@ -67,26 +61,29 @@ int main(int argc, char **argv) { | |||
67 | /* Parse extra opts if any */ | 61 | /* Parse extra opts if any */ |
68 | argv = np_extra_opts(&argc, argv, progname); | 62 | argv = np_extra_opts(&argc, argv, progname); |
69 | 63 | ||
70 | if (process_arguments(argc, argv) == ERROR) { | 64 | check_mysql_query_config_wrapper tmp_config = process_arguments(argc, argv); |
65 | if (tmp_config.errorcode == ERROR) { | ||
71 | usage4(_("Could not parse arguments")); | 66 | usage4(_("Could not parse arguments")); |
72 | } | 67 | } |
73 | 68 | ||
69 | const check_mysql_query_config config = tmp_config.config; | ||
70 | |||
74 | MYSQL mysql; | 71 | MYSQL mysql; |
75 | /* initialize mysql */ | 72 | /* initialize mysql */ |
76 | mysql_init(&mysql); | 73 | mysql_init(&mysql); |
77 | 74 | ||
78 | if (opt_file != NULL) { | 75 | if (config.opt_file != NULL) { |
79 | mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, opt_file); | 76 | mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, config.opt_file); |
80 | } | 77 | } |
81 | 78 | ||
82 | if (opt_group != NULL) { | 79 | if (config.opt_group != NULL) { |
83 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, opt_group); | 80 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, config.opt_group); |
84 | } else { | 81 | } else { |
85 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client"); | 82 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client"); |
86 | } | 83 | } |
87 | 84 | ||
88 | /* establish a connection to the server and error checking */ | 85 | /* establish a connection to the server and error checking */ |
89 | if (!mysql_real_connect(&mysql, db_host, db_user, db_pass, db, db_port, db_socket, 0)) { | 86 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, config.db_port, config.db_socket, 0)) { |
90 | if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { | 87 | if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { |
91 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); | 88 | die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql)); |
92 | } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { | 89 | } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { |
@@ -103,7 +100,7 @@ int main(int argc, char **argv) { | |||
103 | } | 100 | } |
104 | 101 | ||
105 | char *error = NULL; | 102 | char *error = NULL; |
106 | if (mysql_query(&mysql, sql_query) != 0) { | 103 | if (mysql_query(&mysql, config.sql_query) != 0) { |
107 | error = strdup(mysql_error(&mysql)); | 104 | error = strdup(mysql_error(&mysql)); |
108 | mysql_close(&mysql); | 105 | mysql_close(&mysql); |
109 | die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); | 106 | die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); |
@@ -148,7 +145,7 @@ int main(int argc, char **argv) { | |||
148 | printf("mysql result: %f\n", value); | 145 | printf("mysql result: %f\n", value); |
149 | } | 146 | } |
150 | 147 | ||
151 | int status = get_status(value, my_thresholds); | 148 | int status = get_status(value, config.my_thresholds); |
152 | 149 | ||
153 | if (status == STATE_OK) { | 150 | if (status == STATE_OK) { |
154 | printf("QUERY %s: ", _("OK")); | 151 | printf("QUERY %s: ", _("OK")); |
@@ -157,17 +154,16 @@ int main(int argc, char **argv) { | |||
157 | } else if (status == STATE_CRITICAL) { | 154 | } else if (status == STATE_CRITICAL) { |
158 | printf("QUERY %s: ", _("CRITICAL")); | 155 | printf("QUERY %s: ", _("CRITICAL")); |
159 | } | 156 | } |
160 | printf(_("'%s' returned %f | %s"), sql_query, value, | 157 | printf(_("'%s' returned %f | %s"), config.sql_query, value, |
161 | fperfdata("result", value, "", my_thresholds->warning ? true : false, my_thresholds->warning ? my_thresholds->warning->end : 0, | 158 | fperfdata("result", value, "", config.my_thresholds->warning, config.my_thresholds->warning ? config.my_thresholds->warning->end : 0, |
162 | my_thresholds->critical ? true : false, my_thresholds->critical ? my_thresholds->critical->end : 0, false, 0, false, | 159 | config.my_thresholds->critical, config.my_thresholds->critical ? config.my_thresholds->critical->end : 0, false, 0, false, 0)); |
163 | 0)); | ||
164 | printf("\n"); | 160 | printf("\n"); |
165 | 161 | ||
166 | return status; | 162 | return status; |
167 | } | 163 | } |
168 | 164 | ||
169 | /* process command-line arguments */ | 165 | /* process command-line arguments */ |
170 | int process_arguments(int argc, char **argv) { | 166 | check_mysql_query_config_wrapper process_arguments(int argc, char **argv) { |
171 | static struct option longopts[] = { | 167 | static struct option longopts[] = { |
172 | {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, | 168 | {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'}, |
173 | {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'}, | 169 | {"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) { | |||
175 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"query", required_argument, 0, 'q'}, | 171 | {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"query", required_argument, 0, 'q'}, |
176 | {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {0, 0, 0, 0}}; | 172 | {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {0, 0, 0, 0}}; |
177 | 173 | ||
174 | check_mysql_query_config_wrapper result = { | ||
175 | .errorcode = OK, | ||
176 | .config = check_mysql_query_config_init(), | ||
177 | }; | ||
178 | |||
178 | if (argc < 1) { | 179 | if (argc < 1) { |
179 | return ERROR; | 180 | result.errorcode = ERROR; |
181 | return result; | ||
180 | } | 182 | } |
181 | 183 | ||
182 | char *warning = NULL; | 184 | char *warning = NULL; |
@@ -193,22 +195,22 @@ int process_arguments(int argc, char **argv) { | |||
193 | switch (option_char) { | 195 | switch (option_char) { |
194 | case 'H': /* hostname */ | 196 | case 'H': /* hostname */ |
195 | if (is_host(optarg)) { | 197 | if (is_host(optarg)) { |
196 | db_host = optarg; | 198 | result.config.db_host = optarg; |
197 | } else { | 199 | } else { |
198 | usage2(_("Invalid hostname/address"), optarg); | 200 | usage2(_("Invalid hostname/address"), optarg); |
199 | } | 201 | } |
200 | break; | 202 | break; |
201 | case 's': /* socket */ | 203 | case 's': /* socket */ |
202 | db_socket = optarg; | 204 | result.config.db_socket = optarg; |
203 | break; | 205 | break; |
204 | case 'd': /* database */ | 206 | case 'd': /* database */ |
205 | db = optarg; | 207 | result.config.db = optarg; |
206 | break; | 208 | break; |
207 | case 'u': /* username */ | 209 | case 'u': /* username */ |
208 | db_user = optarg; | 210 | result.config.db_user = optarg; |
209 | break; | 211 | break; |
210 | case 'p': /* authentication information: password */ | 212 | case 'p': /* authentication information: password */ |
211 | db_pass = strdup(optarg); | 213 | result.config.db_pass = strdup(optarg); |
212 | 214 | ||
213 | /* Delete the password from process list */ | 215 | /* Delete the password from process list */ |
214 | while (*optarg != '\0') { | 216 | while (*optarg != '\0') { |
@@ -217,13 +219,13 @@ int process_arguments(int argc, char **argv) { | |||
217 | } | 219 | } |
218 | break; | 220 | break; |
219 | case 'f': /* client options file */ | 221 | case 'f': /* client options file */ |
220 | opt_file = optarg; | 222 | result.config.opt_file = optarg; |
221 | break; | 223 | break; |
222 | case 'g': /* client options group */ | 224 | case 'g': /* client options group */ |
223 | opt_group = optarg; | 225 | result.config.opt_group = optarg; |
224 | break; | 226 | break; |
225 | case 'P': /* critical time threshold */ | 227 | case 'P': /* critical time threshold */ |
226 | db_port = atoi(optarg); | 228 | result.config.db_port = atoi(optarg); |
227 | break; | 229 | break; |
228 | case 'v': | 230 | case 'v': |
229 | verbose++; | 231 | verbose++; |
@@ -235,7 +237,7 @@ int process_arguments(int argc, char **argv) { | |||
235 | print_help(); | 237 | print_help(); |
236 | exit(STATE_UNKNOWN); | 238 | exit(STATE_UNKNOWN); |
237 | case 'q': | 239 | case 'q': |
238 | xasprintf(&sql_query, "%s", optarg); | 240 | xasprintf(&result.config.sql_query, "%s", optarg); |
239 | break; | 241 | break; |
240 | case 'w': | 242 | case 'w': |
241 | warning = optarg; | 243 | warning = optarg; |
@@ -248,29 +250,29 @@ int process_arguments(int argc, char **argv) { | |||
248 | } | 250 | } |
249 | } | 251 | } |
250 | 252 | ||
251 | set_thresholds(&my_thresholds, warning, critical); | 253 | set_thresholds(&result.config.my_thresholds, warning, critical); |
252 | 254 | ||
253 | return validate_arguments(); | 255 | return validate_arguments(result); |
254 | } | 256 | } |
255 | 257 | ||
256 | int validate_arguments(void) { | 258 | check_mysql_query_config_wrapper validate_arguments(check_mysql_query_config_wrapper config_wrapper) { |
257 | if (sql_query == NULL) { | 259 | if (config_wrapper.config.sql_query == NULL) { |
258 | usage("Must specify a SQL query to run"); | 260 | usage("Must specify a SQL query to run"); |
259 | } | 261 | } |
260 | 262 | ||
261 | if (db_user == NULL) { | 263 | if (config_wrapper.config.db_user == NULL) { |
262 | db_user = strdup(""); | 264 | config_wrapper.config.db_user = strdup(""); |
263 | } | 265 | } |
264 | 266 | ||
265 | if (db_host == NULL) { | 267 | if (config_wrapper.config.db_host == NULL) { |
266 | db_host = strdup(""); | 268 | config_wrapper.config.db_host = strdup(""); |
267 | } | 269 | } |
268 | 270 | ||
269 | if (db == NULL) { | 271 | if (config_wrapper.config.db == NULL) { |
270 | db = strdup(""); | 272 | config_wrapper.config.db = strdup(""); |
271 | } | 273 | } |
272 | 274 | ||
273 | return OK; | 275 | return config_wrapper; |
274 | } | 276 | } |
275 | 277 | ||
276 | void print_help(void) { | 278 | 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 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "../../config.h" | ||
4 | #include "thresholds.h" | ||
5 | #include <mysql.h> | ||
6 | |||
7 | typedef struct { | ||
8 | char *db_host; | ||
9 | char *db_socket; | ||
10 | char *db; | ||
11 | char *db_user; | ||
12 | char *db_pass; | ||
13 | char *opt_file; | ||
14 | char *opt_group; | ||
15 | unsigned int db_port; | ||
16 | |||
17 | char *sql_query; | ||
18 | thresholds *my_thresholds; | ||
19 | } check_mysql_query_config; | ||
20 | |||
21 | check_mysql_query_config check_mysql_query_config_init() { | ||
22 | check_mysql_query_config tmp = { | ||
23 | .db_host = NULL, | ||
24 | .db_socket = NULL, | ||
25 | .db = NULL, | ||
26 | .db_user = NULL, | ||
27 | .db_pass = NULL, | ||
28 | .opt_file = NULL, | ||
29 | .opt_group = NULL, | ||
30 | .db_port = MYSQL_PORT, | ||
31 | |||
32 | .sql_query = NULL, | ||
33 | .my_thresholds = NULL, | ||
34 | }; | ||
35 | return tmp; | ||
36 | } | ||