1 /*****************************************************************************
3 * Nagios check_mysql_query plugin
6 * Copyright (c) 2006-2009 Nagios Plugins Development Team
7 * Original code from check_mysql, copyright 1999 Didi Rieder
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";
38 #include "utils_base.h"
46 char *db_socket
= 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
;
58 thresholds
*my_thresholds
= NULL
;
62 main (int argc
, char **argv
)
73 setlocale (LC_ALL
, "");
74 bindtextdomain (PACKAGE
, LOCALEDIR
);
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 */
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
));
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) {
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
);
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
);
162 /* process command-line arguments */
164 process_arguments (int argc
, char **argv
)
167 char *warning
= NULL
;
168 char *critical
= NULL
;
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'},
191 c
= getopt_long (argc
, argv
, "hvVP:p:u:d:H:s:q:w:c:", longopts
, &option
);
193 if (c
== -1 || c
== EOF
)
197 case 'H': /* hostname */
198 if (is_host (optarg
)) {
202 usage2 (_("Invalid hostname/address"), optarg
);
205 case 's': /* socket */
208 case 'd': /* database */
211 case 'u': /* username */
214 case 'p': /* authentication information: password */
215 db_pass
= strdup(optarg
);
217 /* Delete the password from process list */
218 while (*optarg
!= '\0') {
223 case 'P': /* critical time threshold */
224 db_port
= atoi (optarg
);
229 case 'V': /* version */
230 print_revision (progname
, NP_VERSION
);
236 xasprintf(&sql_query
, "%s", optarg
);
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");
264 db_user
= strdup("");
267 db_host
= strdup("");
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"));
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"));
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."));
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."));
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");