check_snmp: Close potential for using uninitialized memory
[monitoring-plugins.git] / plugins / check_mysql_query.c
blob0bb83c3e480c570098506fc614cd526c06e0f0d3
1 /*****************************************************************************
2 *
3 * Nagios check_mysql_query plugin
4 *
5 * License: GPL
6 * Copyright (c) 2006-2009 Nagios 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 = "nagiosplug-devel@lists.sourceforge.net";
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 unsigned int db_port = MYSQL_PORT;
51 int process_arguments (int, char **);
52 int validate_arguments (void);
53 void print_help (void);
54 void print_usage (void);
56 char *sql_query = NULL;
57 int verbose = 0;
58 thresholds *my_thresholds = NULL;
61 int
62 main (int argc, char **argv)
65 MYSQL mysql;
66 MYSQL_RES *res;
67 MYSQL_ROW row;
69 double value;
70 char *error = NULL;
71 int status;
73 setlocale (LC_ALL, "");
74 bindtextdomain (PACKAGE, LOCALEDIR);
75 textdomain (PACKAGE);
77 /* Parse extra opts if any */
78 argv=np_extra_opts (&argc, argv, progname);
80 if (process_arguments (argc, argv) == ERROR)
81 usage4 (_("Could not parse arguments"));
83 /* initialize mysql */
84 mysql_init (&mysql);
86 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
88 /* 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)) {
90 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
91 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
92 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
93 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
94 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
95 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
96 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
97 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
98 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
99 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
100 else
101 die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql));
104 if (mysql_query (&mysql, sql_query) != 0) {
105 error = strdup(mysql_error(&mysql));
106 mysql_close (&mysql);
107 die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
110 /* store the result */
111 if ( (res = mysql_store_result (&mysql)) == NULL) {
112 error = strdup(mysql_error(&mysql));
113 mysql_close (&mysql);
114 die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error);
117 /* Check there is some data */
118 if (mysql_num_rows(res) == 0) {
119 mysql_close(&mysql);
120 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
123 /* fetch the first row */
124 if ( (row = mysql_fetch_row (res)) == NULL) {
125 error = strdup(mysql_error(&mysql));
126 mysql_free_result (res);
127 mysql_close (&mysql);
128 die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
131 /* free the result */
132 mysql_free_result (res);
134 /* close the connection */
135 mysql_close (&mysql);
137 if (! is_numeric(row[0])) {
138 die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
141 value = strtod(row[0], NULL);
143 if (verbose >= 3)
144 printf("mysql result: %f\n", value);
146 status = get_status(value, my_thresholds);
148 if (status == STATE_OK) {
149 printf("QUERY %s: ", _("OK"));
150 } else if (status == STATE_WARNING) {
151 printf("QUERY %s: ", _("WARNING"));
152 } else if (status == STATE_CRITICAL) {
153 printf("QUERY %s: ", _("CRITICAL"));
155 printf(_("'%s' returned %f"), sql_query, value);
156 printf("\n");
158 return status;
162 /* process command-line arguments */
164 process_arguments (int argc, char **argv)
166 int c;
167 char *warning = NULL;
168 char *critical = NULL;
170 int option = 0;
171 static struct option longopts[] = {
172 {"hostname", required_argument, 0, 'H'},
173 {"socket", required_argument, 0, 's'},
174 {"database", required_argument, 0, 'd'},
175 {"username", required_argument, 0, 'u'},
176 {"password", required_argument, 0, 'p'},
177 {"port", required_argument, 0, 'P'},
178 {"verbose", no_argument, 0, 'v'},
179 {"version", no_argument, 0, 'V'},
180 {"help", no_argument, 0, 'h'},
181 {"query", required_argument, 0, 'q'},
182 {"warning", required_argument, 0, 'w'},
183 {"critical", required_argument, 0, 'c'},
184 {0, 0, 0, 0}
187 if (argc < 1)
188 return ERROR;
190 while (1) {
191 c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:", longopts, &option);
193 if (c == -1 || c == EOF)
194 break;
196 switch (c) {
197 case 'H': /* hostname */
198 if (is_host (optarg)) {
199 db_host = optarg;
201 else {
202 usage2 (_("Invalid hostname/address"), optarg);
204 break;
205 case 's': /* socket */
206 db_socket = optarg;
207 break;
208 case 'd': /* database */
209 db = optarg;
210 break;
211 case 'u': /* username */
212 db_user = optarg;
213 break;
214 case 'p': /* authentication information: password */
215 db_pass = strdup(optarg);
217 /* Delete the password from process list */
218 while (*optarg != '\0') {
219 *optarg = 'X';
220 optarg++;
222 break;
223 case 'P': /* critical time threshold */
224 db_port = atoi (optarg);
225 break;
226 case 'v':
227 verbose++;
228 break;
229 case 'V': /* version */
230 print_revision (progname, NP_VERSION);
231 exit (STATE_OK);
232 case 'h': /* help */
233 print_help ();
234 exit (STATE_OK);
235 case 'q':
236 xasprintf(&sql_query, "%s", optarg);
237 break;
238 case 'w':
239 warning = optarg;
240 break;
241 case 'c':
242 critical = optarg;
243 break;
244 case '?': /* help */
245 usage5 ();
249 c = optind;
251 set_thresholds(&my_thresholds, warning, critical);
253 return validate_arguments ();
258 validate_arguments (void)
260 if (sql_query == NULL)
261 usage("Must specify a SQL query to run");
263 if (db_user == NULL)
264 db_user = strdup("");
266 if (db_host == NULL)
267 db_host = strdup("");
269 if (db == NULL)
270 db = strdup("");
272 return OK;
276 void
277 print_help (void)
279 char *myport;
280 xasprintf (&myport, "%d", MYSQL_PORT);
282 print_revision (progname, NP_VERSION);
284 printf (_(COPYRIGHT), copyright, email);
286 printf ("%s\n", _("This program checks a query result against threshold levels"));
288 printf ("\n\n");
290 print_usage ();
292 printf (UT_HELP_VRSN);
293 printf (UT_EXTRA_OPTS);
294 printf (" -q, --query=STRING\n");
295 printf (" %s\n", _("SQL query to run. Only first column in first row will be read"));
296 printf (UT_WARN_CRIT_RANGE);
297 printf (UT_HOST_PORT, 'P', myport);
298 printf (" %s\n", "-s, --socket=STRING");
299 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
300 printf (" -d, --database=STRING\n");
301 printf (" %s\n", _("Database to check"));
302 printf (" -u, --username=STRING\n");
303 printf (" %s\n", _("Username to login with"));
304 printf (" -p, --password=STRING\n");
305 printf (" %s\n", _("Password to login with"));
306 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
307 printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
309 printf ("\n");
310 printf (" %s\n", _("A query is required. The result from the query should be numeric."));
311 printf (" %s\n", _("For extra security, create a user with minimal access."));
313 printf ("\n");
314 printf ("%s\n", _("Notes:"));
315 printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
316 printf (" %s\n", _("overriding any my.cnf settings."));
318 printf (UT_SUPPORT);
322 void
323 print_usage (void)
325 printf ("%s\n", _("Usage:"));
326 printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname);
327 printf (" [-d database] [-u user] [-p password]\n");