Add socket support to check_mysql*
[monitoring-plugins.git] / plugins / check_mysql_query.c
blob171fc69fab3e8eeeeec60d8c3e345fbdf7be035c
1 /*****************************************************************************
2 *
3 * Nagios check_mysql_query plugin
4 *
5 * License: GPL
6 * Copyright (c) 2006-2007 Nagios Plugins Development Team
7 * Original code from check_mysql, copyright 1999 Didi Rieder
8 *
9 * Last Modified: $Date$
11 * Description:
13 * This file contains the check_mysql_query plugin
15 * This plugin is for running arbitrary SQL and checking the results
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * $Id$
33 *****************************************************************************/
35 const char *progname = "check_mysql_query";
36 const char *revision = "$Revision$";
37 const char *copyright = "1999-2007";
38 const char *email = "nagiosplug-devel@lists.sourceforge.net";
40 #include "common.h"
41 #include "utils.h"
42 #include "utils_base.h"
43 #include "netutils.h"
45 #include <mysql.h>
46 #include <errmsg.h>
48 char *db_user = NULL;
49 char *db_host = NULL;
50 char *db_socket = NULL;
51 char *db_pass = NULL;
52 char *db = NULL;
53 unsigned int db_port = MYSQL_PORT;
55 int process_arguments (int, char **);
56 int validate_arguments (void);
57 void print_help (void);
58 void print_usage (void);
60 char *sql_query = NULL;
61 int verbose = 0;
62 thresholds *my_thresholds = NULL;
65 int
66 main (int argc, char **argv)
69 MYSQL mysql;
70 MYSQL_RES *res;
71 MYSQL_ROW row;
73 double value;
74 char *error = NULL;
75 int status;
77 setlocale (LC_ALL, "");
78 bindtextdomain (PACKAGE, LOCALEDIR);
79 textdomain (PACKAGE);
81 if (process_arguments (argc, argv) == ERROR)
82 usage4 (_("Could not parse arguments"));
84 /* initialize mysql */
85 mysql_init (&mysql);
87 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
89 /* establish a connection to the server and error checking */
90 if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
91 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
92 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
93 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
94 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
95 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
96 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
97 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
98 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
99 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
100 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
101 else
102 die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql));
105 if (mysql_query (&mysql, sql_query) != 0) {
106 error = strdup(mysql_error(&mysql));
107 mysql_close (&mysql);
108 die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
111 /* store the result */
112 if ( (res = mysql_store_result (&mysql)) == NULL) {
113 error = strdup(mysql_error(&mysql));
114 mysql_close (&mysql);
115 die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error);
118 /* Check there is some data */
119 if (mysql_num_rows(res) == 0) {
120 mysql_close(&mysql);
121 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
124 /* fetch the first row */
125 if ( (row = mysql_fetch_row (res)) == NULL) {
126 error = strdup(mysql_error(&mysql));
127 mysql_free_result (res);
128 mysql_close (&mysql);
129 die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
132 /* free the result */
133 mysql_free_result (res);
135 /* close the connection */
136 mysql_close (&mysql);
138 if (! is_numeric(row[0])) {
139 die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
142 value = strtod(row[0], NULL);
144 if (verbose >= 3)
145 printf("mysql result: %f\n", value);
147 status = get_status(value, my_thresholds);
149 if (status == STATE_OK) {
150 printf("QUERY %s: ", _("OK"));
151 } else if (status == STATE_WARNING) {
152 printf("QUERY %s: ", _("WARNING"));
153 } else if (status == STATE_CRITICAL) {
154 printf("QUERY %s: ", _("CRITICAL"));
156 printf(_("'%s' returned %f"), sql_query, value);
157 printf("\n");
159 return status;
163 /* process command-line arguments */
165 process_arguments (int argc, char **argv)
167 int c;
168 char *warning = NULL;
169 char *critical = NULL;
171 int option = 0;
172 static struct option longopts[] = {
173 {"hostname", required_argument, 0, 'H'},
174 {"socket", required_argument, 0, 's'},
175 {"database", required_argument, 0, 'd'},
176 {"username", required_argument, 0, 'u'},
177 {"password", required_argument, 0, 'p'},
178 {"port", required_argument, 0, 'P'},
179 {"verbose", no_argument, 0, 'v'},
180 {"version", no_argument, 0, 'V'},
181 {"help", no_argument, 0, 'h'},
182 {"query", required_argument, 0, 'q'},
183 {"warning", required_argument, 0, 'w'},
184 {"critical", required_argument, 0, 'c'},
185 {0, 0, 0, 0}
188 if (argc < 1)
189 return ERROR;
191 while (1) {
192 c = getopt_long (argc, argv, "hvVSP:p:u:d:H:s:q:w:c:", longopts, &option);
194 if (c == -1 || c == EOF)
195 break;
197 switch (c) {
198 case 'H': /* hostname */
199 if (is_host (optarg)) {
200 db_host = optarg;
202 else {
203 usage2 (_("Invalid hostname/address"), optarg);
205 break;
206 case 's': /* socket */
207 db_socket = optarg;
208 break;
209 case 'd': /* database */
210 db = optarg;
211 break;
212 case 'u': /* username */
213 db_user = optarg;
214 break;
215 case 'p': /* authentication information: password */
216 db_pass = strdup(optarg);
218 /* Delete the password from process list */
219 while (*optarg != '\0') {
220 *optarg = 'X';
221 optarg++;
223 break;
224 case 'P': /* critical time threshold */
225 db_port = atoi (optarg);
226 break;
227 case 'v':
228 verbose++;
229 break;
230 case 'V': /* version */
231 print_revision (progname, revision);
232 exit (STATE_OK);
233 case 'h': /* help */
234 print_help ();
235 exit (STATE_OK);
236 case 'q':
237 asprintf(&sql_query, "%s", optarg);
238 break;
239 case 'w':
240 warning = optarg;
241 break;
242 case 'c':
243 critical = optarg;
244 break;
245 case '?': /* help */
246 usage5 ();
250 c = optind;
252 set_thresholds(&my_thresholds, warning, critical);
254 return validate_arguments ();
259 validate_arguments (void)
261 if (sql_query == NULL)
262 usage("Must specify a SQL query to run");
264 if (db_user == NULL)
265 db_user = strdup("");
267 if (db_host == NULL)
268 db_host = strdup("");
270 if (db_pass == NULL)
271 db_pass == strdup("");
273 if (db == NULL)
274 db = strdup("");
276 return OK;
280 void
281 print_help (void)
283 char *myport;
284 asprintf (&myport, "%d", MYSQL_PORT);
286 print_revision (progname, revision);
288 printf (_(COPYRIGHT), copyright, email);
290 printf ("%s\n", _("This program checks a query result against threshold levels"));
292 printf ("\n\n");
294 print_usage ();
296 printf (_(UT_HELP_VRSN));
297 printf (" -q, --query=STRING\n");
298 printf (" %s\n", _("SQL query to run. Only first column in first row will be read"));
299 printf (_(UT_WARN_CRIT_RANGE));
300 printf (_(UT_HOST_PORT), 'P', myport);
301 printf (" %s\n", "-s, --socket=STRING");
302 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
303 printf (" -d, --database=STRING\n");
304 printf (" %s\n", _("Database to check"));
305 printf (" -u, --username=STRING\n");
306 printf (" %s\n", _("Username to login with"));
307 printf (" -p, --password=STRING\n");
308 printf (" %s\n", _("Password to login with"));
309 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
310 printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
312 printf ("\n");
313 printf (" %s\n", _("A query is required. The result from the query should be numeric."));
314 printf (" %s\n", _("For extra security, create a user with minimal access."));
316 printf (_(UT_SUPPORT));
320 void
321 print_usage (void)
323 printf (_("Usage:"));
324 printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname);
325 printf (" [-d database] [-u user] [-p password]\n");