1 /*****************************************************************************
3 * Monitoring check_mysql_query plugin
6 * Copyright (c) 2006-2009 Monitoring 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
= "devel@monitoring-plugins.org";
38 #include "utils_base.h"
46 char *db_socket
= NULL
;
49 char *opt_file
= NULL
;
50 char *opt_group
= NULL
;
51 unsigned int db_port
= MYSQL_PORT
;
53 int process_arguments (int, char **);
54 int validate_arguments (void);
55 void print_help (void);
56 void print_usage (void);
58 char *sql_query
= NULL
;
60 thresholds
*my_thresholds
= NULL
;
64 main (int argc
, char **argv
)
75 setlocale (LC_ALL
, "");
76 bindtextdomain (PACKAGE
, LOCALEDIR
);
79 /* Parse extra opts if any */
80 argv
=np_extra_opts (&argc
, argv
, progname
);
82 if (process_arguments (argc
, argv
) == ERROR
)
83 usage4 (_("Could not parse arguments"));
85 /* initialize mysql */
89 mysql_options(&mysql
,MYSQL_READ_DEFAULT_FILE
,opt_file
);
91 if (opt_group
!= NULL
)
92 mysql_options(&mysql
,MYSQL_READ_DEFAULT_GROUP
,opt_group
);
94 mysql_options(&mysql
,MYSQL_READ_DEFAULT_GROUP
,"client");
96 /* establish a connection to the server and error checking */
97 if (!mysql_real_connect(&mysql
,db_host
,db_user
,db_pass
,db
,db_port
,db_socket
,0)) {
98 if (mysql_errno (&mysql
) == CR_UNKNOWN_HOST
)
99 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
100 else if (mysql_errno (&mysql
) == CR_VERSION_ERROR
)
101 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
102 else if (mysql_errno (&mysql
) == CR_OUT_OF_MEMORY
)
103 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
104 else if (mysql_errno (&mysql
) == CR_IPSOCK_ERROR
)
105 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
106 else if (mysql_errno (&mysql
) == CR_SOCKET_CREATE_ERROR
)
107 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
109 die (STATE_CRITICAL
, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql
));
112 if (mysql_query (&mysql
, sql_query
) != 0) {
113 error
= strdup(mysql_error(&mysql
));
114 mysql_close (&mysql
);
115 die (STATE_CRITICAL
, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error
);
118 /* store the result */
119 if ( (res
= mysql_store_result (&mysql
)) == NULL
) {
120 error
= strdup(mysql_error(&mysql
));
121 mysql_close (&mysql
);
122 die (STATE_CRITICAL
, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error
);
125 /* Check there is some data */
126 if (mysql_num_rows(res
) == 0) {
128 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
131 /* fetch the first row */
132 if ( (row
= mysql_fetch_row (res
)) == NULL
) {
133 error
= strdup(mysql_error(&mysql
));
134 mysql_free_result (res
);
135 mysql_close (&mysql
);
136 die (STATE_CRITICAL
, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error
);
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 /* free the result */
146 mysql_free_result (res
);
148 /* close the connection */
149 mysql_close (&mysql
);
152 printf("mysql result: %f\n", value
);
154 status
= get_status(value
, my_thresholds
);
156 if (status
== STATE_OK
) {
157 printf("QUERY %s: ", _("OK"));
158 } else if (status
== STATE_WARNING
) {
159 printf("QUERY %s: ", _("WARNING"));
160 } else if (status
== STATE_CRITICAL
) {
161 printf("QUERY %s: ", _("CRITICAL"));
163 printf(_("'%s' returned %f | %s"), sql_query
, value
,
164 fperfdata("result", value
, "",
165 my_thresholds
->warning
?true:false, my_thresholds
->warning
?my_thresholds
->warning
->end
:0,
166 my_thresholds
->critical
?true:false, my_thresholds
->critical
?my_thresholds
->critical
->end
:0,
176 /* process command-line arguments */
178 process_arguments (int argc
, char **argv
)
181 char *warning
= NULL
;
182 char *critical
= NULL
;
185 static struct option longopts
[] = {
186 {"hostname", required_argument
, 0, 'H'},
187 {"socket", required_argument
, 0, 's'},
188 {"database", required_argument
, 0, 'd'},
189 {"username", required_argument
, 0, 'u'},
190 {"password", required_argument
, 0, 'p'},
191 {"file", required_argument
, 0, 'f'},
192 {"group", required_argument
, 0, 'g'},
193 {"port", required_argument
, 0, 'P'},
194 {"verbose", no_argument
, 0, 'v'},
195 {"version", no_argument
, 0, 'V'},
196 {"help", no_argument
, 0, 'h'},
197 {"query", required_argument
, 0, 'q'},
198 {"warning", required_argument
, 0, 'w'},
199 {"critical", required_argument
, 0, 'c'},
207 c
= getopt_long (argc
, argv
, "hvVP:p:u:d:H:s:q:w:c:f:g:", longopts
, &option
);
209 if (c
== -1 || c
== EOF
)
213 case 'H': /* hostname */
214 if (is_host (optarg
)) {
218 usage2 (_("Invalid hostname/address"), optarg
);
221 case 's': /* socket */
224 case 'd': /* database */
227 case 'u': /* username */
230 case 'p': /* authentication information: password */
231 db_pass
= strdup(optarg
);
233 /* Delete the password from process list */
234 while (*optarg
!= '\0') {
239 case 'f': /* client options file */
242 case 'g': /* client options group */
245 case 'P': /* critical time threshold */
246 db_port
= atoi (optarg
);
251 case 'V': /* version */
252 print_revision (progname
, NP_VERSION
);
253 exit (STATE_UNKNOWN
);
256 exit (STATE_UNKNOWN
);
258 xasprintf(&sql_query
, "%s", optarg
);
273 set_thresholds(&my_thresholds
, warning
, critical
);
275 return validate_arguments ();
280 validate_arguments (void)
282 if (sql_query
== NULL
)
283 usage("Must specify a SQL query to run");
286 db_user
= strdup("");
289 db_host
= strdup("");
302 xasprintf (&myport
, "%d", MYSQL_PORT
);
304 print_revision (progname
, NP_VERSION
);
306 printf (_(COPYRIGHT
), copyright
, email
);
308 printf ("%s\n", _("This program checks a query result against threshold levels"));
314 printf (UT_HELP_VRSN
);
315 printf (UT_EXTRA_OPTS
);
316 printf (" -q, --query=STRING\n");
317 printf (" %s\n", _("SQL query to run. Only first column in first row will be read"));
318 printf (UT_WARN_CRIT_RANGE
);
319 printf (UT_HOST_PORT
, 'P', myport
);
320 printf (" %s\n", "-s, --socket=STRING");
321 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
322 printf (" -d, --database=STRING\n");
323 printf (" %s\n", _("Database to check"));
324 printf (" %s\n", "-f, --file=STRING");
325 printf (" %s\n", _("Read from the specified client options file"));
326 printf (" %s\n", "-g, --group=STRING");
327 printf (" %s\n", _("Use a client options group"));
328 printf (" -u, --username=STRING\n");
329 printf (" %s\n", _("Username to login with"));
330 printf (" -p, --password=STRING\n");
331 printf (" %s\n", _("Password to login with"));
332 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
333 printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
336 printf (" %s\n", _("A query is required. The result from the query should be numeric."));
337 printf (" %s\n", _("For extra security, create a user with minimal access."));
340 printf ("%s\n", _("Notes:"));
341 printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
342 printf (" %s\n", _("overriding any my.cnf settings."));
351 printf ("%s\n", _("Usage:"));
352 printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname
);
353 printf (" [-d database] [-u user] [-p password] [-f optfile] [-g group]\n");