summaryrefslogtreecommitdiffstats
path: root/plugins/check_mysql_query.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_mysql_query.c')
-rw-r--r--plugins/check_mysql_query.c396
1 files changed, 179 insertions, 217 deletions
diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c
index 842b7a2..79b6e2f 100644
--- a/plugins/check_mysql_query.c
+++ b/plugins/check_mysql_query.c
@@ -1,36 +1,36 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_mysql_query plugin 3 * Monitoring check_mysql_query plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2006-2009 Monitoring Plugins Development Team 6 * Copyright (c) 2006-2024 Monitoring Plugins Development Team
7* Original code from check_mysql, copyright 1999 Didi Rieder 7 * Original code from check_mysql, copyright 1999 Didi Rieder
8* 8 *
9* Description: 9 * Description:
10* 10 *
11* This file contains the check_mysql_query plugin 11 * This file contains the check_mysql_query plugin
12* 12 *
13* This plugin is for running arbitrary SQL and checking the results 13 * This plugin is for running arbitrary SQL and checking the results
14* 14 *
15* 15 *
16* This program is free software: you can redistribute it and/or modify 16 * This program is free software: you can redistribute it and/or modify
17* it under the terms of the GNU General Public License as published by 17 * it under the terms of the GNU General Public License as published by
18* the Free Software Foundation, either version 3 of the License, or 18 * the Free Software Foundation, either version 3 of the License, or
19* (at your option) any later version. 19 * (at your option) any later version.
20* 20 *
21* This program is distributed in the hope that it will be useful, 21 * This program is distributed in the hope that it will be useful,
22* but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24* GNU General Public License for more details. 24 * GNU General Public License for more details.
25* 25 *
26* You should have received a copy of the GNU General Public License 26 * You should have received a copy of the GNU General Public License
27* along with this program. If not, see <http://www.gnu.org/licenses/>. 27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28* 28 *
29* 29 *
30*****************************************************************************/ 30 *****************************************************************************/
31 31
32const char *progname = "check_mysql_query"; 32const char *progname = "check_mysql_query";
33const char *copyright = "1999-2007"; 33const char *copyright = "1999-2024";
34const char *email = "devel@monitoring-plugins.org"; 34const char *email = "devel@monitoring-plugins.org";
35 35
36#include "common.h" 36#include "common.h"
@@ -41,117 +41,109 @@ const char *email = "devel@monitoring-plugins.org";
41#include <mysql.h> 41#include <mysql.h>
42#include <errmsg.h> 42#include <errmsg.h>
43 43
44char *db_user = NULL; 44static char *db_user = NULL;
45char *db_host = NULL; 45static char *db_host = NULL;
46char *db_socket = NULL; 46static char *db_socket = NULL;
47char *db_pass = NULL; 47static char *db_pass = NULL;
48char *db = NULL; 48static char *db = NULL;
49char *opt_file = NULL; 49static char *opt_file = NULL;
50char *opt_group = NULL; 50static char *opt_group = NULL;
51unsigned int db_port = MYSQL_PORT; 51static unsigned int db_port = MYSQL_PORT;
52 52
53int process_arguments (int, char **); 53static int process_arguments(int /*argc*/, char ** /*argv*/);
54int validate_arguments (void); 54static int validate_arguments(void);
55void print_help (void); 55static void print_help(void);
56void print_usage (void); 56void print_usage(void);
57 57
58char *sql_query = NULL; 58static char *sql_query = NULL;
59int verbose = 0; 59static int verbose = 0;
60thresholds *my_thresholds = NULL; 60static thresholds *my_thresholds = NULL;
61 61
62 62int main(int argc, char **argv) {
63int 63 setlocale(LC_ALL, "");
64main (int argc, char **argv) 64 bindtextdomain(PACKAGE, LOCALEDIR);
65{ 65 textdomain(PACKAGE);
66
67 MYSQL mysql;
68 MYSQL_RES *res;
69 MYSQL_ROW row;
70
71 double value;
72 char *error = NULL;
73 int status;
74
75 setlocale (LC_ALL, "");
76 bindtextdomain (PACKAGE, LOCALEDIR);
77 textdomain (PACKAGE);
78 66
79 /* Parse extra opts if any */ 67 /* Parse extra opts if any */
80 argv=np_extra_opts (&argc, argv, progname); 68 argv = np_extra_opts(&argc, argv, progname);
81 69
82 if (process_arguments (argc, argv) == ERROR) 70 if (process_arguments(argc, argv) == ERROR)
83 usage4 (_("Could not parse arguments")); 71 usage4(_("Could not parse arguments"));
84 72
73 MYSQL mysql;
85 /* initialize mysql */ 74 /* initialize mysql */
86 mysql_init (&mysql); 75 mysql_init(&mysql);
87 76
88 if (opt_file != NULL) 77 if (opt_file != NULL)
89 mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file); 78 mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, opt_file);
90 79
91 if (opt_group != NULL) 80 if (opt_group != NULL)
92 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group); 81 mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, opt_group);
93 else 82 else
94 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); 83 mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client");
95 84
96 /* establish a connection to the server and error checking */ 85 /* establish a connection to the server and error checking */
97 if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) { 86 if (!mysql_real_connect(&mysql, db_host, db_user, db_pass, db, db_port, db_socket, 0)) {
98 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) 87 if (mysql_errno(&mysql) == CR_UNKNOWN_HOST)
99 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 88 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
100 else if (mysql_errno (&mysql) == CR_VERSION_ERROR) 89 else if (mysql_errno(&mysql) == CR_VERSION_ERROR)
101 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 90 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
102 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) 91 else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY)
103 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 92 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
104 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) 93 else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR)
105 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 94 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
106 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) 95 else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR)
107 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 96 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
108 else 97 else
109 die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql)); 98 die(STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error(&mysql));
110 } 99 }
111 100
112 if (mysql_query (&mysql, sql_query) != 0) { 101 char *error = NULL;
102 if (mysql_query(&mysql, sql_query) != 0) {
113 error = strdup(mysql_error(&mysql)); 103 error = strdup(mysql_error(&mysql));
114 mysql_close (&mysql); 104 mysql_close(&mysql);
115 die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); 105 die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
116 } 106 }
117 107
108 MYSQL_RES *res;
118 /* store the result */ 109 /* store the result */
119 if ( (res = mysql_store_result (&mysql)) == NULL) { 110 if ((res = mysql_store_result(&mysql)) == NULL) {
120 error = strdup(mysql_error(&mysql)); 111 error = strdup(mysql_error(&mysql));
121 mysql_close (&mysql); 112 mysql_close(&mysql);
122 die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error); 113 die(STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error);
123 } 114 }
124 115
125 /* Check there is some data */ 116 /* Check there is some data */
126 if (mysql_num_rows(res) == 0) { 117 if (mysql_num_rows(res) == 0) {
127 mysql_close(&mysql); 118 mysql_close(&mysql);
128 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned")); 119 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
129 } 120 }
130 121
122 MYSQL_ROW row;
131 /* fetch the first row */ 123 /* fetch the first row */
132 if ( (row = mysql_fetch_row (res)) == NULL) { 124 if ((row = mysql_fetch_row(res)) == NULL) {
133 error = strdup(mysql_error(&mysql)); 125 error = strdup(mysql_error(&mysql));
134 mysql_free_result (res); 126 mysql_free_result(res);
135 mysql_close (&mysql); 127 mysql_close(&mysql);
136 die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error); 128 die(STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
137 } 129 }
138 130
139 if (! is_numeric(row[0])) { 131 if (!is_numeric(row[0])) {
140 die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]); 132 die(STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
141 } 133 }
142 134
143 value = strtod(row[0], NULL); 135 double value = strtod(row[0], NULL);
144 136
145 /* free the result */ 137 /* free the result */
146 mysql_free_result (res); 138 mysql_free_result(res);
147 139
148 /* close the connection */ 140 /* close the connection */
149 mysql_close (&mysql); 141 mysql_close(&mysql);
150 142
151 if (verbose >= 3) 143 if (verbose >= 3)
152 printf("mysql result: %f\n", value); 144 printf("mysql result: %f\n", value);
153 145
154 status = get_status(value, my_thresholds); 146 int status = get_status(value, my_thresholds);
155 147
156 if (status == STATE_OK) { 148 if (status == STATE_OK) {
157 printf("QUERY %s: ", _("OK")); 149 printf("QUERY %s: ", _("OK"));
@@ -161,73 +153,54 @@ main (int argc, char **argv)
161 printf("QUERY %s: ", _("CRITICAL")); 153 printf("QUERY %s: ", _("CRITICAL"));
162 } 154 }
163 printf(_("'%s' returned %f | %s"), sql_query, value, 155 printf(_("'%s' returned %f | %s"), sql_query, value,
164 fperfdata("result", value, "", 156 fperfdata("result", value, "", my_thresholds->warning ? true : false, my_thresholds->warning ? my_thresholds->warning->end : 0,
165 my_thresholds->warning?true:false, my_thresholds->warning?my_thresholds->warning->end:0, 157 my_thresholds->critical ? true : false, my_thresholds->critical ? my_thresholds->critical->end : 0, false, 0, false,
166 my_thresholds->critical?true:false, my_thresholds->critical?my_thresholds->critical->end:0, 158 0));
167 false, 0,
168 false, 0)
169 );
170 printf("\n"); 159 printf("\n");
171 160
172 return status; 161 return status;
173} 162}
174 163
175
176/* process command-line arguments */ 164/* process command-line arguments */
177int 165int process_arguments(int argc, char **argv) {
178process_arguments (int argc, char **argv)
179{
180 int c;
181 char *warning = NULL;
182 char *critical = NULL;
183
184 int option = 0;
185 static struct option longopts[] = { 166 static struct option longopts[] = {
186 {"hostname", required_argument, 0, 'H'}, 167 {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'},
187 {"socket", required_argument, 0, 's'}, 168 {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'},
188 {"database", required_argument, 0, 'd'}, 169 {"group", required_argument, 0, 'g'}, {"port", required_argument, 0, 'P'}, {"verbose", no_argument, 0, 'v'},
189 {"username", required_argument, 0, 'u'}, 170 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"query", required_argument, 0, 'q'},
190 {"password", required_argument, 0, 'p'}, 171 {"warning", required_argument, 0, 'w'}, {"critical", required_argument, 0, 'c'}, {0, 0, 0, 0}};
191 {"file", required_argument, 0, 'f'},
192 {"group", required_argument, 0, 'g'},
193 {"port", required_argument, 0, 'P'},
194 {"verbose", no_argument, 0, 'v'},
195 {"version", no_argument, 0, 'V'},
196 {"help", no_argument, 0, 'h'},
197 {"query", required_argument, 0, 'q'},
198 {"warning", required_argument, 0, 'w'},
199 {"critical", required_argument, 0, 'c'},
200 {0, 0, 0, 0}
201 };
202 172
203 if (argc < 1) 173 if (argc < 1)
204 return ERROR; 174 return ERROR;
205 175
206 while (1) { 176 char *warning = NULL;
207 c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option); 177 char *critical = NULL;
178
179 while (true) {
180 int option = 0;
181 int option_char = getopt_long(argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option);
208 182
209 if (c == -1 || c == EOF) 183 if (option_char == -1 || option_char == EOF)
210 break; 184 break;
211 185
212 switch (c) { 186 switch (option_char) {
213 case 'H': /* hostname */ 187 case 'H': /* hostname */
214 if (is_host (optarg)) { 188 if (is_host(optarg)) {
215 db_host = optarg; 189 db_host = optarg;
216 } 190 } else {
217 else { 191 usage2(_("Invalid hostname/address"), optarg);
218 usage2 (_("Invalid hostname/address"), optarg);
219 } 192 }
220 break; 193 break;
221 case 's': /* socket */ 194 case 's': /* socket */
222 db_socket = optarg; 195 db_socket = optarg;
223 break; 196 break;
224 case 'd': /* database */ 197 case 'd': /* database */
225 db = optarg; 198 db = optarg;
226 break; 199 break;
227 case 'u': /* username */ 200 case 'u': /* username */
228 db_user = optarg; 201 db_user = optarg;
229 break; 202 break;
230 case 'p': /* authentication information: password */ 203 case 'p': /* authentication information: password */
231 db_pass = strdup(optarg); 204 db_pass = strdup(optarg);
232 205
233 /* Delete the password from process list */ 206 /* Delete the password from process list */
@@ -236,24 +209,24 @@ process_arguments (int argc, char **argv)
236 optarg++; 209 optarg++;
237 } 210 }
238 break; 211 break;
239 case 'f': /* client options file */ 212 case 'f': /* client options file */
240 opt_file = optarg; 213 opt_file = optarg;
241 break; 214 break;
242 case 'g': /* client options group */ 215 case 'g': /* client options group */
243 opt_group = optarg; 216 opt_group = optarg;
244 break; 217 break;
245 case 'P': /* critical time threshold */ 218 case 'P': /* critical time threshold */
246 db_port = atoi (optarg); 219 db_port = atoi(optarg);
247 break; 220 break;
248 case 'v': 221 case 'v':
249 verbose++; 222 verbose++;
250 break; 223 break;
251 case 'V': /* version */ 224 case 'V': /* version */
252 print_revision (progname, NP_VERSION); 225 print_revision(progname, NP_VERSION);
253 exit (STATE_UNKNOWN); 226 exit(STATE_UNKNOWN);
254 case 'h': /* help */ 227 case 'h': /* help */
255 print_help (); 228 print_help();
256 exit (STATE_UNKNOWN); 229 exit(STATE_UNKNOWN);
257 case 'q': 230 case 'q':
258 xasprintf(&sql_query, "%s", optarg); 231 xasprintf(&sql_query, "%s", optarg);
259 break; 232 break;
@@ -263,22 +236,17 @@ process_arguments (int argc, char **argv)
263 case 'c': 236 case 'c':
264 critical = optarg; 237 critical = optarg;
265 break; 238 break;
266 case '?': /* help */ 239 case '?': /* help */
267 usage5 (); 240 usage5();
268 } 241 }
269 } 242 }
270 243
271 c = optind;
272
273 set_thresholds(&my_thresholds, warning, critical); 244 set_thresholds(&my_thresholds, warning, critical);
274 245
275 return validate_arguments (); 246 return validate_arguments();
276} 247}
277 248
278 249int validate_arguments(void) {
279int
280validate_arguments (void)
281{
282 if (sql_query == NULL) 250 if (sql_query == NULL)
283 usage("Must specify a SQL query to run"); 251 usage("Must specify a SQL query to run");
284 252
@@ -294,61 +262,55 @@ validate_arguments (void)
294 return OK; 262 return OK;
295} 263}
296 264
297 265void print_help(void) {
298void
299print_help (void)
300{
301 char *myport; 266 char *myport;
302 xasprintf (&myport, "%d", MYSQL_PORT); 267 xasprintf(&myport, "%d", MYSQL_PORT);
303 268
304 print_revision (progname, NP_VERSION); 269 print_revision(progname, NP_VERSION);
305 270
306 printf (_(COPYRIGHT), copyright, email); 271 printf(_(COPYRIGHT), copyright, email);
307 272
308 printf ("%s\n", _("This program checks a query result against threshold levels")); 273 printf("%s\n", _("This program checks a query result against threshold levels"));
309 274
310 printf ("\n\n"); 275 printf("\n\n");
311 276
312 print_usage (); 277 print_usage();
313 278
314 printf (UT_HELP_VRSN); 279 printf(UT_HELP_VRSN);
315 printf (UT_EXTRA_OPTS); 280 printf(UT_EXTRA_OPTS);
316 printf (" -q, --query=STRING\n"); 281 printf(" -q, --query=STRING\n");
317 printf (" %s\n", _("SQL query to run. Only first column in first row will be read")); 282 printf(" %s\n", _("SQL query to run. Only first column in first row will be read"));
318 printf (UT_WARN_CRIT_RANGE); 283 printf(UT_WARN_CRIT_RANGE);
319 printf (UT_HOST_PORT, 'P', myport); 284 printf(UT_HOST_PORT, 'P', myport);
320 printf (" %s\n", "-s, --socket=STRING"); 285 printf(" %s\n", "-s, --socket=STRING");
321 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)")); 286 printf(" %s\n", _("Use the specified socket (has no effect if -H is used)"));
322 printf (" -d, --database=STRING\n"); 287 printf(" -d, --database=STRING\n");
323 printf (" %s\n", _("Database to check")); 288 printf(" %s\n", _("Database to check"));
324 printf (" %s\n", "-f, --file=STRING"); 289 printf(" %s\n", "-f, --file=STRING");
325 printf (" %s\n", _("Read from the specified client options file")); 290 printf(" %s\n", _("Read from the specified client options file"));
326 printf (" %s\n", "-g, --group=STRING"); 291 printf(" %s\n", "-g, --group=STRING");
327 printf (" %s\n", _("Use a client options group")); 292 printf(" %s\n", _("Use a client options group"));
328 printf (" -u, --username=STRING\n"); 293 printf(" -u, --username=STRING\n");
329 printf (" %s\n", _("Username to login with")); 294 printf(" %s\n", _("Username to login with"));
330 printf (" -p, --password=STRING\n"); 295 printf(" -p, --password=STRING\n");
331 printf (" %s\n", _("Password to login with")); 296 printf(" %s\n", _("Password to login with"));
332 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); 297 printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
333 printf (" %s\n", _("Your clear-text password could be visible as a process table entry")); 298 printf(" %s\n", _("Your clear-text password could be visible as a process table entry"));
334 299
335 printf ("\n"); 300 printf("\n");
336 printf (" %s\n", _("A query is required. The result from the query should be numeric.")); 301 printf(" %s\n", _("A query is required. The result from the query should be numeric."));
337 printf (" %s\n", _("For extra security, create a user with minimal access.")); 302 printf(" %s\n", _("For extra security, create a user with minimal access."));
338 303
339 printf ("\n"); 304 printf("\n");
340 printf ("%s\n", _("Notes:")); 305 printf("%s\n", _("Notes:"));
341 printf (" %s\n", _("You must specify -p with an empty string to force an empty password,")); 306 printf(" %s\n", _("You must specify -p with an empty string to force an empty password,"));
342 printf (" %s\n", _("overriding any my.cnf settings.")); 307 printf(" %s\n", _("overriding any my.cnf settings."));
343
344 printf (UT_SUPPORT);
345}
346 308
309 printf(UT_SUPPORT);
310}
347 311
348void 312void print_usage(void) {
349print_usage (void) 313 printf("%s\n", _("Usage:"));
350{ 314 printf(" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n", progname);
351 printf ("%s\n", _("Usage:")); 315 printf(" [-d database] [-u user] [-p password] [-f optfile] [-g group]\n");
352 printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname);
353 printf (" [-d database] [-u user] [-p password] [-f optfile] [-g group]\n");
354} 316}