summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_mysql_query.c334
1 files changed, 152 insertions, 182 deletions
diff --git a/plugins/check_mysql_query.c b/plugins/check_mysql_query.c
index 842b7a2f..18057d61 100644
--- a/plugins/check_mysql_query.c
+++ b/plugins/check_mysql_query.c
@@ -1,33 +1,33 @@
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-2009 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-2007";
@@ -50,103 +50,100 @@ char *opt_file = NULL;
50char *opt_group = NULL; 50char *opt_group = NULL;
51unsigned int db_port = MYSQL_PORT; 51unsigned int db_port = MYSQL_PORT;
52 52
53int process_arguments (int, char **); 53int process_arguments(int, char **);
54int validate_arguments (void); 54int validate_arguments(void);
55void print_help (void); 55void print_help(void);
56void print_usage (void); 56void print_usage(void);
57 57
58char *sql_query = NULL; 58char *sql_query = NULL;
59int verbose = 0; 59int verbose = 0;
60thresholds *my_thresholds = NULL; 60thresholds *my_thresholds = NULL;
61 61
62 62int main(int argc, char **argv) {
63int
64main (int argc, char **argv)
65{
66 63
67 MYSQL mysql; 64 MYSQL mysql;
68 MYSQL_RES *res; 65 MYSQL_RES *res;
69 MYSQL_ROW row; 66 MYSQL_ROW row;
70 67
71 double value; 68 double value;
72 char *error = NULL; 69 char *error = NULL;
73 int status; 70 int status;
74 71
75 setlocale (LC_ALL, ""); 72 setlocale(LC_ALL, "");
76 bindtextdomain (PACKAGE, LOCALEDIR); 73 bindtextdomain(PACKAGE, LOCALEDIR);
77 textdomain (PACKAGE); 74 textdomain(PACKAGE);
78 75
79 /* Parse extra opts if any */ 76 /* Parse extra opts if any */
80 argv=np_extra_opts (&argc, argv, progname); 77 argv = np_extra_opts(&argc, argv, progname);
81 78
82 if (process_arguments (argc, argv) == ERROR) 79 if (process_arguments(argc, argv) == ERROR)
83 usage4 (_("Could not parse arguments")); 80 usage4(_("Could not parse arguments"));
84 81
85 /* initialize mysql */ 82 /* initialize mysql */
86 mysql_init (&mysql); 83 mysql_init(&mysql);
87 84
88 if (opt_file != NULL) 85 if (opt_file != NULL)
89 mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file); 86 mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, opt_file);
90 87
91 if (opt_group != NULL) 88 if (opt_group != NULL)
92 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group); 89 mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, opt_group);
93 else 90 else
94 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); 91 mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client");
95 92
96 /* establish a connection to the server and error checking */ 93 /* 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)) { 94 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) 95 if (mysql_errno(&mysql) == CR_UNKNOWN_HOST)
99 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 96 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
100 else if (mysql_errno (&mysql) == CR_VERSION_ERROR) 97 else if (mysql_errno(&mysql) == CR_VERSION_ERROR)
101 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 98 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
102 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) 99 else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY)
103 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 100 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
104 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) 101 else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR)
105 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 102 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
106 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) 103 else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR)
107 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql)); 104 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error(&mysql));
108 else 105 else
109 die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql)); 106 die(STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error(&mysql));
110 } 107 }
111 108
112 if (mysql_query (&mysql, sql_query) != 0) { 109 if (mysql_query(&mysql, sql_query) != 0) {
113 error = strdup(mysql_error(&mysql)); 110 error = strdup(mysql_error(&mysql));
114 mysql_close (&mysql); 111 mysql_close(&mysql);
115 die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error); 112 die(STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
116 } 113 }
117 114
118 /* store the result */ 115 /* store the result */
119 if ( (res = mysql_store_result (&mysql)) == NULL) { 116 if ((res = mysql_store_result(&mysql)) == NULL) {
120 error = strdup(mysql_error(&mysql)); 117 error = strdup(mysql_error(&mysql));
121 mysql_close (&mysql); 118 mysql_close(&mysql);
122 die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error); 119 die(STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error);
123 } 120 }
124 121
125 /* Check there is some data */ 122 /* Check there is some data */
126 if (mysql_num_rows(res) == 0) { 123 if (mysql_num_rows(res) == 0) {
127 mysql_close(&mysql); 124 mysql_close(&mysql);
128 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned")); 125 die(STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
129 } 126 }
130 127
131 /* fetch the first row */ 128 /* fetch the first row */
132 if ( (row = mysql_fetch_row (res)) == NULL) { 129 if ((row = mysql_fetch_row(res)) == NULL) {
133 error = strdup(mysql_error(&mysql)); 130 error = strdup(mysql_error(&mysql));
134 mysql_free_result (res); 131 mysql_free_result(res);
135 mysql_close (&mysql); 132 mysql_close(&mysql);
136 die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error); 133 die(STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
137 } 134 }
138 135
139 if (! is_numeric(row[0])) { 136 if (!is_numeric(row[0])) {
140 die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]); 137 die(STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
141 } 138 }
142 139
143 value = strtod(row[0], NULL); 140 value = strtod(row[0], NULL);
144 141
145 /* free the result */ 142 /* free the result */
146 mysql_free_result (res); 143 mysql_free_result(res);
147 144
148 /* close the connection */ 145 /* close the connection */
149 mysql_close (&mysql); 146 mysql_close(&mysql);
150 147
151 if (verbose >= 3) 148 if (verbose >= 3)
152 printf("mysql result: %f\n", value); 149 printf("mysql result: %f\n", value);
@@ -161,73 +158,55 @@ main (int argc, char **argv)
161 printf("QUERY %s: ", _("CRITICAL")); 158 printf("QUERY %s: ", _("CRITICAL"));
162 } 159 }
163 printf(_("'%s' returned %f | %s"), sql_query, value, 160 printf(_("'%s' returned %f | %s"), sql_query, value,
164 fperfdata("result", value, "", 161 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, 162 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, 163 0));
167 false, 0,
168 false, 0)
169 );
170 printf("\n"); 164 printf("\n");
171 165
172 return status; 166 return status;
173} 167}
174 168
175
176/* process command-line arguments */ 169/* process command-line arguments */
177int 170int process_arguments(int argc, char **argv) {
178process_arguments (int argc, char **argv)
179{
180 int c; 171 int c;
181 char *warning = NULL; 172 char *warning = NULL;
182 char *critical = NULL; 173 char *critical = NULL;
183 174
184 int option = 0; 175 int option = 0;
185 static struct option longopts[] = { 176 static struct option longopts[] = {
186 {"hostname", required_argument, 0, 'H'}, 177 {"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, {"database", required_argument, 0, 'd'},
187 {"socket", required_argument, 0, 's'}, 178 {"username", required_argument, 0, 'u'}, {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'},
188 {"database", required_argument, 0, 'd'}, 179 {"group", required_argument, 0, 'g'}, {"port", required_argument, 0, 'P'}, {"verbose", no_argument, 0, 'v'},
189 {"username", required_argument, 0, 'u'}, 180 {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"query", required_argument, 0, 'q'},
190 {"password", required_argument, 0, 'p'}, 181 {"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 182
203 if (argc < 1) 183 if (argc < 1)
204 return ERROR; 184 return ERROR;
205 185
206 while (1) { 186 while (1) {
207 c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option); 187 c = getopt_long(argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option);
208 188
209 if (c == -1 || c == EOF) 189 if (c == -1 || c == EOF)
210 break; 190 break;
211 191
212 switch (c) { 192 switch (c) {
213 case 'H': /* hostname */ 193 case 'H': /* hostname */
214 if (is_host (optarg)) { 194 if (is_host(optarg)) {
215 db_host = optarg; 195 db_host = optarg;
216 } 196 } else {
217 else { 197 usage2(_("Invalid hostname/address"), optarg);
218 usage2 (_("Invalid hostname/address"), optarg);
219 } 198 }
220 break; 199 break;
221 case 's': /* socket */ 200 case 's': /* socket */
222 db_socket = optarg; 201 db_socket = optarg;
223 break; 202 break;
224 case 'd': /* database */ 203 case 'd': /* database */
225 db = optarg; 204 db = optarg;
226 break; 205 break;
227 case 'u': /* username */ 206 case 'u': /* username */
228 db_user = optarg; 207 db_user = optarg;
229 break; 208 break;
230 case 'p': /* authentication information: password */ 209 case 'p': /* authentication information: password */
231 db_pass = strdup(optarg); 210 db_pass = strdup(optarg);
232 211
233 /* Delete the password from process list */ 212 /* Delete the password from process list */
@@ -236,24 +215,24 @@ process_arguments (int argc, char **argv)
236 optarg++; 215 optarg++;
237 } 216 }
238 break; 217 break;
239 case 'f': /* client options file */ 218 case 'f': /* client options file */
240 opt_file = optarg; 219 opt_file = optarg;
241 break; 220 break;
242 case 'g': /* client options group */ 221 case 'g': /* client options group */
243 opt_group = optarg; 222 opt_group = optarg;
244 break; 223 break;
245 case 'P': /* critical time threshold */ 224 case 'P': /* critical time threshold */
246 db_port = atoi (optarg); 225 db_port = atoi(optarg);
247 break; 226 break;
248 case 'v': 227 case 'v':
249 verbose++; 228 verbose++;
250 break; 229 break;
251 case 'V': /* version */ 230 case 'V': /* version */
252 print_revision (progname, NP_VERSION); 231 print_revision(progname, NP_VERSION);
253 exit (STATE_UNKNOWN); 232 exit(STATE_UNKNOWN);
254 case 'h': /* help */ 233 case 'h': /* help */
255 print_help (); 234 print_help();
256 exit (STATE_UNKNOWN); 235 exit(STATE_UNKNOWN);
257 case 'q': 236 case 'q':
258 xasprintf(&sql_query, "%s", optarg); 237 xasprintf(&sql_query, "%s", optarg);
259 break; 238 break;
@@ -263,8 +242,8 @@ process_arguments (int argc, char **argv)
263 case 'c': 242 case 'c':
264 critical = optarg; 243 critical = optarg;
265 break; 244 break;
266 case '?': /* help */ 245 case '?': /* help */
267 usage5 (); 246 usage5();
268 } 247 }
269 } 248 }
270 249
@@ -272,13 +251,10 @@ process_arguments (int argc, char **argv)
272 251
273 set_thresholds(&my_thresholds, warning, critical); 252 set_thresholds(&my_thresholds, warning, critical);
274 253
275 return validate_arguments (); 254 return validate_arguments();
276} 255}
277 256
278 257int validate_arguments(void) {
279int
280validate_arguments (void)
281{
282 if (sql_query == NULL) 258 if (sql_query == NULL)
283 usage("Must specify a SQL query to run"); 259 usage("Must specify a SQL query to run");
284 260
@@ -294,61 +270,55 @@ validate_arguments (void)
294 return OK; 270 return OK;
295} 271}
296 272
297 273void print_help(void) {
298void
299print_help (void)
300{
301 char *myport; 274 char *myport;
302 xasprintf (&myport, "%d", MYSQL_PORT); 275 xasprintf(&myport, "%d", MYSQL_PORT);
303 276
304 print_revision (progname, NP_VERSION); 277 print_revision(progname, NP_VERSION);
305 278
306 printf (_(COPYRIGHT), copyright, email); 279 printf(_(COPYRIGHT), copyright, email);
307 280
308 printf ("%s\n", _("This program checks a query result against threshold levels")); 281 printf("%s\n", _("This program checks a query result against threshold levels"));
309 282
310 printf ("\n\n"); 283 printf("\n\n");
311 284
312 print_usage (); 285 print_usage();
313 286
314 printf (UT_HELP_VRSN); 287 printf(UT_HELP_VRSN);
315 printf (UT_EXTRA_OPTS); 288 printf(UT_EXTRA_OPTS);
316 printf (" -q, --query=STRING\n"); 289 printf(" -q, --query=STRING\n");
317 printf (" %s\n", _("SQL query to run. Only first column in first row will be read")); 290 printf(" %s\n", _("SQL query to run. Only first column in first row will be read"));
318 printf (UT_WARN_CRIT_RANGE); 291 printf(UT_WARN_CRIT_RANGE);
319 printf (UT_HOST_PORT, 'P', myport); 292 printf(UT_HOST_PORT, 'P', myport);
320 printf (" %s\n", "-s, --socket=STRING"); 293 printf(" %s\n", "-s, --socket=STRING");
321 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)")); 294 printf(" %s\n", _("Use the specified socket (has no effect if -H is used)"));
322 printf (" -d, --database=STRING\n"); 295 printf(" -d, --database=STRING\n");
323 printf (" %s\n", _("Database to check")); 296 printf(" %s\n", _("Database to check"));
324 printf (" %s\n", "-f, --file=STRING"); 297 printf(" %s\n", "-f, --file=STRING");
325 printf (" %s\n", _("Read from the specified client options file")); 298 printf(" %s\n", _("Read from the specified client options file"));
326 printf (" %s\n", "-g, --group=STRING"); 299 printf(" %s\n", "-g, --group=STRING");
327 printf (" %s\n", _("Use a client options group")); 300 printf(" %s\n", _("Use a client options group"));
328 printf (" -u, --username=STRING\n"); 301 printf(" -u, --username=STRING\n");
329 printf (" %s\n", _("Username to login with")); 302 printf(" %s\n", _("Username to login with"));
330 printf (" -p, --password=STRING\n"); 303 printf(" -p, --password=STRING\n");
331 printf (" %s\n", _("Password to login with")); 304 printf(" %s\n", _("Password to login with"));
332 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); 305 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")); 306 printf(" %s\n", _("Your clear-text password could be visible as a process table entry"));
334 307
335 printf ("\n"); 308 printf("\n");
336 printf (" %s\n", _("A query is required. The result from the query should be numeric.")); 309 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.")); 310 printf(" %s\n", _("For extra security, create a user with minimal access."));
338
339 printf ("\n");
340 printf ("%s\n", _("Notes:"));
341 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."));
343
344 printf (UT_SUPPORT);
345}
346 311
312 printf("\n");
313 printf("%s\n", _("Notes:"));
314 printf(" %s\n", _("You must specify -p with an empty string to force an empty password,"));
315 printf(" %s\n", _("overriding any my.cnf settings."));
316
317 printf(UT_SUPPORT);
318}
347 319
348void 320void print_usage(void) {
349print_usage (void) 321 printf("%s\n", _("Usage:"));
350{ 322 printf(" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n", progname);
351 printf ("%s\n", _("Usage:")); 323 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} 324}