1 /*****************************************************************************
3 * Nagios check_mysql_query plugin
6 * Copyright (c) 2006-2007 Nagios Plugins Development Team
7 * Original code from check_mysql, copyright 1999 Didi Rieder
9 * Last Modified: $Date$
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/>.
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";
42 #include "utils_base.h"
50 char *db_socket
= 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
;
62 thresholds
*my_thresholds
= NULL
;
66 main (int argc
, char **argv
)
77 setlocale (LC_ALL
, "");
78 bindtextdomain (PACKAGE
, LOCALEDIR
);
81 if (process_arguments (argc
, argv
) == ERROR
)
82 usage4 (_("Could not parse arguments"));
84 /* initialize 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
));
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) {
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
);
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
);
163 /* process command-line arguments */
165 process_arguments (int argc
, char **argv
)
168 char *warning
= NULL
;
169 char *critical
= NULL
;
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'},
192 c
= getopt_long (argc
, argv
, "hvVSP:p:u:d:H:s: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 's': /* socket */
209 case 'd': /* database */
212 case 'u': /* username */
215 case 'p': /* authentication information: password */
216 db_pass
= strdup(optarg
);
218 /* Delete the password from process list */
219 while (*optarg
!= '\0') {
224 case 'P': /* critical time threshold */
225 db_port
= atoi (optarg
);
230 case 'V': /* version */
231 print_revision (progname
, revision
);
237 asprintf(&sql_query
, "%s", optarg
);
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");
265 db_user
= strdup("");
268 db_host
= strdup("");
271 db_pass
== strdup("");
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"));
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"));
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
));
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");