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 /* Parse extra opts if any */
82 argv
=np_extra_opts (&argc
, argv
, progname
);
84 if (process_arguments (argc
, argv
) == ERROR
)
85 usage4 (_("Could not parse arguments"));
87 /* initialize mysql */
90 mysql_options(&mysql
,MYSQL_READ_DEFAULT_GROUP
,"client");
92 /* establish a connection to the server and error checking */
93 if (!mysql_real_connect(&mysql
,db_host
,db_user
,db_pass
,db
,db_port
,db_socket
,0)) {
94 if (mysql_errno (&mysql
) == CR_UNKNOWN_HOST
)
95 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
96 else if (mysql_errno (&mysql
) == CR_VERSION_ERROR
)
97 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
98 else if (mysql_errno (&mysql
) == CR_OUT_OF_MEMORY
)
99 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
100 else if (mysql_errno (&mysql
) == CR_IPSOCK_ERROR
)
101 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
102 else if (mysql_errno (&mysql
) == CR_SOCKET_CREATE_ERROR
)
103 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql
));
105 die (STATE_CRITICAL
, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql
));
108 if (mysql_query (&mysql
, sql_query
) != 0) {
109 error
= strdup(mysql_error(&mysql
));
110 mysql_close (&mysql
);
111 die (STATE_CRITICAL
, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error
);
114 /* store the result */
115 if ( (res
= mysql_store_result (&mysql
)) == NULL
) {
116 error
= strdup(mysql_error(&mysql
));
117 mysql_close (&mysql
);
118 die (STATE_CRITICAL
, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error
);
121 /* Check there is some data */
122 if (mysql_num_rows(res
) == 0) {
124 die (STATE_WARNING
, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
127 /* fetch the first row */
128 if ( (row
= mysql_fetch_row (res
)) == NULL
) {
129 error
= strdup(mysql_error(&mysql
));
130 mysql_free_result (res
);
131 mysql_close (&mysql
);
132 die (STATE_CRITICAL
, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error
);
135 /* free the result */
136 mysql_free_result (res
);
138 /* close the connection */
139 mysql_close (&mysql
);
141 if (! is_numeric(row
[0])) {
142 die (STATE_CRITICAL
, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row
[0]);
145 value
= strtod(row
[0], NULL
);
148 printf("mysql result: %f\n", value
);
150 status
= get_status(value
, my_thresholds
);
152 if (status
== STATE_OK
) {
153 printf("QUERY %s: ", _("OK"));
154 } else if (status
== STATE_WARNING
) {
155 printf("QUERY %s: ", _("WARNING"));
156 } else if (status
== STATE_CRITICAL
) {
157 printf("QUERY %s: ", _("CRITICAL"));
159 printf(_("'%s' returned %f"), sql_query
, value
);
166 /* process command-line arguments */
168 process_arguments (int argc
, char **argv
)
171 char *warning
= NULL
;
172 char *critical
= NULL
;
175 static struct option longopts
[] = {
176 {"hostname", required_argument
, 0, 'H'},
177 {"socket", required_argument
, 0, 's'},
178 {"database", required_argument
, 0, 'd'},
179 {"username", required_argument
, 0, 'u'},
180 {"password", required_argument
, 0, 'p'},
181 {"port", required_argument
, 0, 'P'},
182 {"verbose", no_argument
, 0, 'v'},
183 {"version", no_argument
, 0, 'V'},
184 {"help", no_argument
, 0, 'h'},
185 {"query", required_argument
, 0, 'q'},
186 {"warning", required_argument
, 0, 'w'},
187 {"critical", required_argument
, 0, 'c'},
195 c
= getopt_long (argc
, argv
, "hvVSP:p:u:d:H:s:q:w:c:", longopts
, &option
);
197 if (c
== -1 || c
== EOF
)
201 case 'H': /* hostname */
202 if (is_host (optarg
)) {
206 usage2 (_("Invalid hostname/address"), optarg
);
209 case 's': /* socket */
212 case 'd': /* database */
215 case 'u': /* username */
218 case 'p': /* authentication information: password */
219 db_pass
= strdup(optarg
);
221 /* Delete the password from process list */
222 while (*optarg
!= '\0') {
227 case 'P': /* critical time threshold */
228 db_port
= atoi (optarg
);
233 case 'V': /* version */
234 print_revision (progname
, revision
);
240 asprintf(&sql_query
, "%s", optarg
);
255 set_thresholds(&my_thresholds
, warning
, critical
);
257 return validate_arguments ();
262 validate_arguments (void)
264 if (sql_query
== NULL
)
265 usage("Must specify a SQL query to run");
268 db_user
= strdup("");
271 db_host
= strdup("");
274 db_pass
== strdup("");
287 asprintf (&myport
, "%d", MYSQL_PORT
);
289 print_revision (progname
, revision
);
291 printf (_(COPYRIGHT
), copyright
, email
);
293 printf ("%s\n", _("This program checks a query result against threshold levels"));
299 printf (_(UT_HELP_VRSN
));
300 printf (_(UT_EXTRA_OPTS
));
301 printf (" -q, --query=STRING\n");
302 printf (" %s\n", _("SQL query to run. Only first column in first row will be read"));
303 printf (_(UT_WARN_CRIT_RANGE
));
304 printf (_(UT_HOST_PORT
), 'P', myport
);
305 printf (" %s\n", "-s, --socket=STRING");
306 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
307 printf (" -d, --database=STRING\n");
308 printf (" %s\n", _("Database to check"));
309 printf (" -u, --username=STRING\n");
310 printf (" %s\n", _("Username to login with"));
311 printf (" -p, --password=STRING\n");
312 printf (" %s\n", _("Password to login with"));
313 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
314 printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
317 printf (" %s\n", _("A query is required. The result from the query should be numeric."));
318 printf (" %s\n", _("For extra security, create a user with minimal access."));
322 printf ("%s\n", _("Notes:"));
323 printf (_(UT_EXTRA_OPTS_NOTES
));
326 printf (_(UT_SUPPORT
));
333 printf (_("Usage:"));
334 printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname
);
335 printf (" [-d database] [-u user] [-p password]\n");