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$
10 *
11 * Description:
12 *
13 * This file contains the check_mysql_query plugin
14 *
15 * This plugin is for running arbitrary SQL and checking the results
16 *
17 *
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.
22 *
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.
27 *
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/>.
30 *
31 * $Id$
32 *
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_pass = NULL;
51 char *db = NULL;
52 unsigned int db_port = MYSQL_PORT;
54 int process_arguments (int, char **);
55 int validate_arguments (void);
56 void print_help (void);
57 void print_usage (void);
59 char *sql_query = NULL;
60 int verbose = 0;
61 thresholds *my_thresholds = NULL;
64 int
65 main (int argc, char **argv)
66 {
68 MYSQL mysql;
69 MYSQL_RES *res;
70 MYSQL_ROW row;
72 double value;
73 char *error = NULL;
74 int status;
76 setlocale (LC_ALL, "");
77 bindtextdomain (PACKAGE, LOCALEDIR);
78 textdomain (PACKAGE);
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,NULL,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));
102 }
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);
108 }
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);
115 }
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"));
121 }
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);
129 }
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]);
139 }
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"));
154 }
155 printf(_("'%s' returned %f"), sql_query, value);
156 printf("\n");
158 return status;
159 }
162 /* process command-line arguments */
163 int
164 process_arguments (int argc, char **argv)
165 {
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 {"database", required_argument, 0, 'd'},
174 {"username", required_argument, 0, 'u'},
175 {"password", required_argument, 0, 'p'},
176 {"port", required_argument, 0, 'P'},
177 {"verbose", no_argument, 0, 'v'},
178 {"version", no_argument, 0, 'V'},
179 {"help", no_argument, 0, 'h'},
180 {"query", required_argument, 0, 'q'},
181 {"warning", required_argument, 0, 'w'},
182 {"critical", required_argument, 0, 'c'},
183 {0, 0, 0, 0}
184 };
186 if (argc < 1)
187 return ERROR;
189 while (1) {
190 c = getopt_long (argc, argv, "hvVSP:p:u:d:H:q:w:c:", longopts, &option);
192 if (c == -1 || c == EOF)
193 break;
195 switch (c) {
196 case 'H': /* hostname */
197 if (is_host (optarg)) {
198 db_host = optarg;
199 }
200 else {
201 usage2 (_("Invalid hostname/address"), optarg);
202 }
203 break;
204 case 'd': /* hostname */
205 db = optarg;
206 break;
207 case 'u': /* username */
208 db_user = optarg;
209 break;
210 case 'p': /* authentication information: password */
211 asprintf(&db_pass, "%s", optarg);
213 /* Delete the password from process list */
214 while (*optarg != '\0') {
215 *optarg = 'X';
216 optarg++;
217 }
218 break;
219 case 'P': /* critical time threshold */
220 db_port = atoi (optarg);
221 break;
222 case 'v':
223 verbose++;
224 break;
225 case 'V': /* version */
226 print_revision (progname, revision);
227 exit (STATE_OK);
228 case 'h': /* help */
229 print_help ();
230 exit (STATE_OK);
231 case 'q':
232 asprintf(&sql_query, "%s", optarg);
233 break;
234 case 'w':
235 warning = optarg;
236 break;
237 case 'c':
238 critical = optarg;
239 break;
240 case '?': /* help */
241 usage5 ();
242 }
243 }
245 c = optind;
247 set_thresholds(&my_thresholds, warning, critical);
249 return validate_arguments ();
250 }
253 int
254 validate_arguments (void)
255 {
256 if (sql_query == NULL)
257 usage("Must specify a SQL query to run");
259 if (db_user == NULL)
260 db_user = strdup("");
262 if (db_host == NULL)
263 db_host = strdup("");
265 if (db_pass == NULL)
266 db_pass == strdup("");
268 if (db == NULL)
269 db = strdup("");
271 return OK;
272 }
275 void
276 print_help (void)
277 {
278 char *myport;
279 asprintf (&myport, "%d", MYSQL_PORT);
281 print_revision (progname, revision);
283 printf (_(COPYRIGHT), copyright, email);
285 printf ("%s\n", _("This program checks a query result against threshold levels"));
287 printf ("\n\n");
289 print_usage ();
291 printf (_(UT_HELP_VRSN));
292 printf (" -q, --query=STRING\n");
293 printf (" %s\n", _("SQL query to run. Only first column in first row will be read"));
294 printf (_(UT_WARN_CRIT_RANGE));
295 printf (_(UT_HOST_PORT), 'P', myport);
296 printf (" -d, --database=STRING\n");
297 printf (" %s\n", _("Database to check"));
298 printf (" -u, --username=STRING\n");
299 printf (" %s\n", _("Username to login with"));
300 printf (" -p, --password=STRING\n");
301 printf (" %s\n", _("Password to login with"));
302 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
304 printf ("\n");
306 printf ("%s\n", _("A query is required. The result from the query should be numeric."));
307 printf ("%s\n", _("For extra security, create a user with minimal access."));
309 printf (_(UT_SUPPORT));
310 }
313 void
314 print_usage (void)
315 {
316 printf (_("Usage:"));
317 printf ("%s -q SQL_query [-w warn] [-c crit]\n",progname);
318 printf ("[-d database] [-H host] [-P port] [-u user] [-p password]\n");
319 }