Fix INADDR_NONE value (for systems which don't define it).
[monitoring-plugins.git] / plugins / check_mysql_query.c
blobbb62eecf23dd3afafae7d42f0770de9f4dc991e6
1 /******************************************************************************
3 * Nagios check_mysql_query plugin
5 * License: GPL
6 * Copyright (c) 2006 nagios-plugins team, after Didi Rieder (check_mysql)
8 * Last Modified: $Date$
10 * Description:
12 * This file contains the check_mysql_query plugin
14 * This plugin is for running arbitrary SQL and checking the results
16 * License Information:
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 2 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, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 * CHECK_MYSQL_QUERY.C
33 * $Id$
35 ******************************************************************************/
37 const char *progname = "check_mysql_query";
38 const char *revision = "$Revision$";
39 const char *copyright = "2006";
40 const char *email = "nagiosplug-devel@lists.sourceforge.net";
42 #include "common.h"
43 #include "utils.h"
44 #include "utils_base.h"
45 #include "netutils.h"
47 #include <mysql.h>
48 #include <errmsg.h>
50 char *db_user = NULL;
51 char *db_host = NULL;
52 char *db_pass = NULL;
53 char *db = NULL;
54 unsigned int db_port = MYSQL_PORT;
56 int process_arguments (int, char **);
57 int validate_arguments (void);
58 void print_help (void);
59 void print_usage (void);
61 char *sql_query = NULL;
62 int verbose = 0;
63 thresholds *my_thresholds = NULL;
66 int
67 main (int argc, char **argv)
70 MYSQL mysql;
71 MYSQL_RES *res;
72 MYSQL_ROW row;
74 double value;
75 char *error = NULL;
76 int status;
78 setlocale (LC_ALL, "");
79 bindtextdomain (PACKAGE, LOCALEDIR);
80 textdomain (PACKAGE);
82 if (process_arguments (argc, argv) == ERROR)
83 usage4 (_("Could not parse arguments"));
85 /* initialize mysql */
86 mysql_init (&mysql);
88 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
90 /* establish a connection to the server and error checking */
91 if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
92 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
93 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
94 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
95 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
96 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
97 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
98 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
99 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
100 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
101 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
102 else
103 die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql));
106 if (mysql_query (&mysql, sql_query) != 0) {
107 error = strdup(mysql_error(&mysql));
108 mysql_close (&mysql);
109 die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
112 /* store the result */
113 if ( (res = mysql_store_result (&mysql)) == NULL) {
114 error = strdup(mysql_error(&mysql));
115 mysql_close (&mysql);
116 die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error);
119 /* Check there is some data */
120 if (mysql_num_rows(res) == 0) {
121 mysql_close(&mysql);
122 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
125 /* fetch the first row */
126 if ( (row = mysql_fetch_row (res)) == NULL) {
127 error = strdup(mysql_error(&mysql));
128 mysql_free_result (res);
129 mysql_close (&mysql);
130 die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
133 /* free the result */
134 mysql_free_result (res);
136 /* close the connection */
137 mysql_close (&mysql);
139 if (! is_numeric(row[0])) {
140 die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
143 value = strtod(row[0], NULL);
145 if (verbose >= 3)
146 printf("mysql result: %f\n", value);
148 status = get_status(value, my_thresholds);
150 if (status == STATE_OK) {
151 printf("QUERY %s: ", _("OK"));
152 } else if (status == STATE_WARNING) {
153 printf("QUERY %s: ", _("WARNING"));
154 } else if (status == STATE_CRITICAL) {
155 printf("QUERY %s: ", _("CRITICAL"));
157 printf(_("'%s' returned %f"), sql_query, value);
158 printf("\n");
160 return status;
164 /* process command-line arguments */
166 process_arguments (int argc, char **argv)
168 int c;
169 char *warning = NULL;
170 char *critical = NULL;
172 int option = 0;
173 static struct option longopts[] = {
174 {"hostname", required_argument, 0, 'H'},
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: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 'd': /* hostname */
207 db = optarg;
208 break;
209 case 'u': /* username */
210 db_user = optarg;
211 break;
212 case 'p': /* authentication information: password */
213 asprintf(&db_pass, "%s", optarg);
215 /* Delete the password from process list */
216 while (*optarg != '\0') {
217 *optarg = 'X';
218 optarg++;
220 break;
221 case 'P': /* critical time threshold */
222 db_port = atoi (optarg);
223 break;
224 case 'v':
225 verbose++;
226 break;
227 case 'V': /* version */
228 print_revision (progname, revision);
229 exit (STATE_OK);
230 case 'h': /* help */
231 print_help ();
232 exit (STATE_OK);
233 case 'q':
234 asprintf(&sql_query, "%s", optarg);
235 break;
236 case 'w':
237 warning = optarg;
238 break;
239 case 'c':
240 critical = optarg;
241 break;
242 case '?': /* help */
243 usage5 ();
247 c = optind;
249 set_thresholds(&my_thresholds, warning, critical);
251 return validate_arguments ();
256 validate_arguments (void)
258 if (sql_query == NULL)
259 usage("Must specify a SQL query to run");
261 if (db_user == NULL)
262 db_user = strdup("");
264 if (db_host == NULL)
265 db_host = strdup("");
267 if (db_pass == NULL)
268 db_pass == strdup("");
270 if (db == NULL)
271 db = strdup("");
273 return OK;
277 void
278 print_help (void)
280 char *myport;
281 asprintf (&myport, "%d", MYSQL_PORT);
283 print_revision (progname, revision);
285 printf (_(COPYRIGHT), copyright, email);
287 printf ("%s\n", _("This program checks a query result against threshold levels"));
289 printf ("\n\n");
291 print_usage ();
293 printf (_(UT_HELP_VRSN));
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 (" -d, --database=STRING\n");
299 printf (" %s\n", _("Database to check"));
300 printf (" -u, --username=STRING\n");
301 printf (" %s\n", _("Username to login with"));
302 printf (" -p, --password=STRING\n");
303 printf (" %s\n", _("Password to login with"));
304 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
306 printf ("\n");
308 printf ("%s\n", _("A query is required. The result from the query should be numeric."));
309 printf ("%s\n", _("For extra security, create a user with minimal access."));
311 printf (_(UT_SUPPORT));
315 void
316 print_usage (void)
318 printf (_("Usage:"));
319 printf ("%s -q SQL_query [-w warn] [-c crit]\n",progname);
320 printf ("[-d database] [-H host] [-P port] [-u user] [-p password]\n");