Code

a92b85701a56fc879c0ad3b76907ce77c8f43d6d
[nagiosplug.git] / plugins / check_mysql_query.c
1 /*****************************************************************************
2
3 * Nagios check_mysql_query plugin
4
5 * License: GPL
6 * Copyright (c) 2006-2007 Nagios Plugins Development Team
7 * Original code from check_mysql, copyright 1999 Didi Rieder
8
9 * Last Modified: $Date$
10
11 * Description:
12
13 * This file contains the check_mysql_query plugin
14
15 * This plugin is for running arbitrary SQL and checking the results
16
17
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.
22
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.
27
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/>.
30
31 * $Id$
32
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";
40 #include "common.h"
41 #include "utils.h"
42 #include "utils_base.h"
43 #include "netutils.h"
45 #include <mysql.h>
46 #include <errmsg.h>
48 char *db_user = NULL;
49 char *db_host = NULL;
50 char *db_socket = NULL;
51 char *db_pass = NULL;
52 char *db = 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;
61 int verbose = 0;
62 thresholds *my_thresholds = NULL;
65 int
66 main (int argc, char **argv)
67 {
69         MYSQL mysql;
70         MYSQL_RES *res;
71         MYSQL_ROW row;
72         
73         double value;
74         char *error = NULL;
75         int status;
77         setlocale (LC_ALL, "");
78         bindtextdomain (PACKAGE, LOCALEDIR);
79         textdomain (PACKAGE);
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  */
88         mysql_init (&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));
104                 else
105                         die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql));
106         }
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);
112         }
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);
119         }
121         /* Check there is some data */
122         if (mysql_num_rows(res) == 0) {
123                 mysql_close(&mysql);
124                 die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
125         }
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);
133         }
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]);
143         }
145         value = strtod(row[0], NULL);
147         if (verbose >= 3)
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"));
158         }
159         printf(_("'%s' returned %f"), sql_query, value);
160         printf("\n");
162         return status;
166 /* process command-line arguments */
167 int
168 process_arguments (int argc, char **argv)
170         int c;
171         char *warning = NULL;
172         char *critical = NULL;
174         int option = 0;
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'},
188                 {0, 0, 0, 0}
189         };
191         if (argc < 1)
192                 return ERROR;
194         while (1) {
195                 c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:", longopts, &option);
197                 if (c == -1 || c == EOF)
198                         break;
200                 switch (c) {
201                 case 'H':                                                                       /* hostname */
202                         if (is_host (optarg)) {
203                                 db_host = optarg;
204                         }
205                         else {
206                                 usage2 (_("Invalid hostname/address"), optarg);
207                         }
208                         break;
209                 case 's':                                                                       /* socket */
210                         db_socket = optarg;
211                         break;
212                 case 'd':                                                                       /* database */
213                         db = optarg;
214                         break;
215                 case 'u':                                                                       /* username */
216                         db_user = optarg;
217                         break;
218                 case 'p':                                                                       /* authentication information: password */
219                         db_pass = strdup(optarg);
221                         /* Delete the password from process list */
222                         while (*optarg != '\0') {
223                                 *optarg = 'X';
224                                 optarg++;
225                         }
226                         break;
227                 case 'P':                                                                       /* critical time threshold */
228                         db_port = atoi (optarg);
229                         break;
230                 case 'v':
231                         verbose++;
232                         break;
233                 case 'V':                                                                       /* version */
234                         print_revision (progname, revision);
235                         exit (STATE_OK);
236                 case 'h':                                                                       /* help */
237                         print_help ();
238                         exit (STATE_OK);
239                 case 'q':
240                         asprintf(&sql_query, "%s", optarg);
241                         break;
242                 case 'w':
243                         warning = optarg;
244                         break;
245                 case 'c':
246                         critical = optarg;
247                         break;
248                 case '?':                                                                       /* help */
249                         usage5 ();
250                 }
251         }
253         c = optind;
255         set_thresholds(&my_thresholds, warning, critical);
257         return validate_arguments ();
261 int
262 validate_arguments (void)
264         if (sql_query == NULL)
265                 usage("Must specify a SQL query to run");
267         if (db_user == NULL)
268                 db_user = strdup("");
270         if (db_host == NULL)
271                 db_host = strdup("");
273         if (db_pass == NULL)
274                 db_pass = strdup("");
276         if (db == NULL)
277                 db = strdup("");
279         return OK;
283 void
284 print_help (void)
286         char *myport;
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"));
295   printf ("\n\n");
297         print_usage ();
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"));
316         printf ("\n");
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."));
320 #ifdef NP_EXTRA_OPTS
321         printf ("\n");
322         printf ("%s\n", _("Notes:"));
323         printf (_(UT_EXTRA_OPTS_NOTES));
324 #endif
326         printf (_(UT_SUPPORT));
330 void
331 print_usage (void)
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");