Revert "check_disk - show all disks if state is ok and option error only is used"
[monitoring-plugins.git] / plugins / check_mysql_query.c
blob49a14dd32b39d06f5cfb8e5440f30701aa3fb027
1 /*****************************************************************************
2 *
3 * Monitoring check_mysql_query plugin
4 *
5 * License: GPL
6 * Copyright (c) 2006-2009 Monitoring Plugins Development Team
7 * Original code from check_mysql, copyright 1999 Didi Rieder
8 *
9 * Description:
11 * This file contains the check_mysql_query plugin
13 * This plugin is for running arbitrary SQL and checking the results
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
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
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/>.
30 *****************************************************************************/
32 const char *progname = "check_mysql_query";
33 const char *copyright = "1999-2007";
34 const char *email = "devel@monitoring-plugins.org";
36 #include "common.h"
37 #include "utils.h"
38 #include "utils_base.h"
39 #include "netutils.h"
41 #include <mysql.h>
42 #include <errmsg.h>
44 char *db_user = NULL;
45 char *db_host = NULL;
46 char *db_socket = NULL;
47 char *db_pass = NULL;
48 char *db = NULL;
49 char *opt_file = NULL;
50 char *opt_group = NULL;
51 unsigned int db_port = MYSQL_PORT;
53 int process_arguments (int, char **);
54 int validate_arguments (void);
55 void print_help (void);
56 void print_usage (void);
58 char *sql_query = NULL;
59 int verbose = 0;
60 thresholds *my_thresholds = NULL;
63 int
64 main (int argc, char **argv)
67 MYSQL mysql;
68 MYSQL_RES *res;
69 MYSQL_ROW row;
71 double value;
72 char *error = NULL;
73 int status;
75 setlocale (LC_ALL, "");
76 bindtextdomain (PACKAGE, LOCALEDIR);
77 textdomain (PACKAGE);
79 /* Parse extra opts if any */
80 argv=np_extra_opts (&argc, argv, progname);
82 if (process_arguments (argc, argv) == ERROR)
83 usage4 (_("Could not parse arguments"));
85 /* initialize mysql */
86 mysql_init (&mysql);
88 if (opt_file != NULL)
89 mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file);
91 if (opt_group != NULL)
92 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group);
93 else
94 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
96 /* 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)) {
98 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
99 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
100 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
101 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
102 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
103 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
104 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
105 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
106 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
107 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
108 else
109 die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql));
112 if (mysql_query (&mysql, sql_query) != 0) {
113 error = strdup(mysql_error(&mysql));
114 mysql_close (&mysql);
115 die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
118 /* store the result */
119 if ( (res = mysql_store_result (&mysql)) == NULL) {
120 error = strdup(mysql_error(&mysql));
121 mysql_close (&mysql);
122 die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error);
125 /* Check there is some data */
126 if (mysql_num_rows(res) == 0) {
127 mysql_close(&mysql);
128 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
131 /* fetch the first row */
132 if ( (row = mysql_fetch_row (res)) == NULL) {
133 error = strdup(mysql_error(&mysql));
134 mysql_free_result (res);
135 mysql_close (&mysql);
136 die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
139 /* free the result */
140 mysql_free_result (res);
142 /* close the connection */
143 mysql_close (&mysql);
145 if (! is_numeric(row[0])) {
146 die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
149 value = strtod(row[0], NULL);
151 if (verbose >= 3)
152 printf("mysql result: %f\n", value);
154 status = get_status(value, my_thresholds);
156 if (status == STATE_OK) {
157 printf("QUERY %s: ", _("OK"));
158 } else if (status == STATE_WARNING) {
159 printf("QUERY %s: ", _("WARNING"));
160 } else if (status == STATE_CRITICAL) {
161 printf("QUERY %s: ", _("CRITICAL"));
163 printf(_("'%s' returned %f | %s"), sql_query, value,
164 fperfdata("result", value, "",
165 my_thresholds->warning?TRUE:FALSE, my_thresholds->warning?my_thresholds->warning->end:0,
166 my_thresholds->critical?TRUE:FALSE, my_thresholds->critical?my_thresholds->critical->end:0,
167 FALSE, 0,
168 FALSE, 0)
170 printf("\n");
172 return status;
176 /* process command-line arguments */
178 process_arguments (int argc, char **argv)
180 int c;
181 char *warning = NULL;
182 char *critical = NULL;
184 int option = 0;
185 static struct option longopts[] = {
186 {"hostname", required_argument, 0, 'H'},
187 {"socket", required_argument, 0, 's'},
188 {"database", required_argument, 0, 'd'},
189 {"username", required_argument, 0, 'u'},
190 {"password", required_argument, 0, 'p'},
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}
203 if (argc < 1)
204 return ERROR;
206 while (1) {
207 c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts, &option);
209 if (c == -1 || c == EOF)
210 break;
212 switch (c) {
213 case 'H': /* hostname */
214 if (is_host (optarg)) {
215 db_host = optarg;
217 else {
218 usage2 (_("Invalid hostname/address"), optarg);
220 break;
221 case 's': /* socket */
222 db_socket = optarg;
223 break;
224 case 'd': /* database */
225 db = optarg;
226 break;
227 case 'u': /* username */
228 db_user = optarg;
229 break;
230 case 'p': /* authentication information: password */
231 db_pass = strdup(optarg);
233 /* Delete the password from process list */
234 while (*optarg != '\0') {
235 *optarg = 'X';
236 optarg++;
238 break;
239 case 'f': /* client options file */
240 opt_file = optarg;
241 break;
242 case 'g': /* client options group */
243 opt_group = optarg;
244 break;
245 case 'P': /* critical time threshold */
246 db_port = atoi (optarg);
247 break;
248 case 'v':
249 verbose++;
250 break;
251 case 'V': /* version */
252 print_revision (progname, NP_VERSION);
253 exit (STATE_UNKNOWN);
254 case 'h': /* help */
255 print_help ();
256 exit (STATE_UNKNOWN);
257 case 'q':
258 xasprintf(&sql_query, "%s", optarg);
259 break;
260 case 'w':
261 warning = optarg;
262 break;
263 case 'c':
264 critical = optarg;
265 break;
266 case '?': /* help */
267 usage5 ();
271 c = optind;
273 set_thresholds(&my_thresholds, warning, critical);
275 return validate_arguments ();
280 validate_arguments (void)
282 if (sql_query == NULL)
283 usage("Must specify a SQL query to run");
285 if (db_user == NULL)
286 db_user = strdup("");
288 if (db_host == NULL)
289 db_host = strdup("");
291 if (db == NULL)
292 db = strdup("");
294 return OK;
298 void
299 print_help (void)
301 char *myport;
302 xasprintf (&myport, "%d", MYSQL_PORT);
304 print_revision (progname, NP_VERSION);
306 printf (_(COPYRIGHT), copyright, email);
308 printf ("%s\n", _("This program checks a query result against threshold levels"));
310 printf ("\n\n");
312 print_usage ();
314 printf (UT_HELP_VRSN);
315 printf (UT_EXTRA_OPTS);
316 printf (" -q, --query=STRING\n");
317 printf (" %s\n", _("SQL query to run. Only first column in first row will be read"));
318 printf (UT_WARN_CRIT_RANGE);
319 printf (UT_HOST_PORT, 'P', myport);
320 printf (" %s\n", "-s, --socket=STRING");
321 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
322 printf (" -d, --database=STRING\n");
323 printf (" %s\n", _("Database to check"));
324 printf (" %s\n", "-f, --file=STRING");
325 printf (" %s\n", _("Read from the specified client options file"));
326 printf (" %s\n", "-g, --group=STRING");
327 printf (" %s\n", _("Use a client options group"));
328 printf (" -u, --username=STRING\n");
329 printf (" %s\n", _("Username to login with"));
330 printf (" -p, --password=STRING\n");
331 printf (" %s\n", _("Password to login with"));
332 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"));
335 printf ("\n");
336 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."));
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."));
344 printf (UT_SUPPORT);
348 void
349 print_usage (void)
351 printf ("%s\n", _("Usage:"));
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");