1 /******************************************************************************
3 * Nagios check_mysql_query plugin
6 * Copyright (c) 2006 nagios-plugins team, after Didi Rieder (check_mysql)
8 * Last Modified: $Date$
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.
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";
44 #include "utils_base.h"
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
;
63 thresholds
*my_thresholds
= NULL
;
67 main (int argc
, char **argv
)
78 setlocale (LC_ALL
, "");
79 bindtextdomain (PACKAGE
, LOCALEDIR
);
82 if (process_arguments (argc
, argv
) == ERROR
)
83 usage4 (_("Could not parse arguments"));
85 /* initialize 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
));
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) {
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
);
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
);
164 /* process command-line arguments */
166 process_arguments (int argc
, char **argv
)
169 char *warning
= NULL
;
170 char *critical
= NULL
;
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'},
192 c
= getopt_long (argc
, argv
, "hvVSP:p:u:d:H:q:w:c:", longopts
, &option
);
194 if (c
== -1 || c
== EOF
)
198 case 'H': /* hostname */
199 if (is_host (optarg
)) {
203 usage2 (_("Invalid hostname/address"), optarg
);
206 case 'd': /* hostname */
209 case 'u': /* username */
212 case 'p': /* authentication information: password */
213 asprintf(&db_pass
, "%s", optarg
);
215 /* Delete the password from process list */
216 while (*optarg
!= '\0') {
221 case 'P': /* critical time threshold */
222 db_port
= atoi (optarg
);
227 case 'V': /* version */
228 print_revision (progname
, revision
);
234 asprintf(&sql_query
, "%s", optarg
);
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");
262 db_user
= strdup("");
265 db_host
= strdup("");
268 db_pass
== strdup("");
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"));
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!!!"));
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
));
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");