Code

negate now has the ability to replace the status text as well (-s, --substitute)
[nagiosplug.git] / plugins / check_mysql.c
1 /*****************************************************************************
2
3 * Nagios check_mysql plugin
4
5 * License: GPL
6 * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
7 * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
8 * Copyright (c) 1999-2007 Nagios Plugins Development Team
9
10 * Last Modified: $Date$
11 *
12 * Description:
13
14 * This file contains the check_mysql plugin
15
16 * This program tests connections to a mysql server
17
18
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
23
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 * GNU General Public License for more details.
28
29 * You should have received a copy of the GNU General Public License
30 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31
32 * $Id$
33
34 *****************************************************************************/
36 const char *progname = "check_mysql";
37 const char *revision = "$Revision$";
38 const char *copyright = "1999-2007";
39 const char *email = "nagiosplug-devel@lists.sourceforge.net";
41 #define SLAVERESULTSIZE 70
43 #include "common.h"
44 #include "utils.h"
45 #include "utils_base.h"
46 #include "netutils.h"
48 #include <mysql.h>
49 #include <errmsg.h>
51 char *db_user = NULL;
52 char *db_host = NULL;
53 char *db_socket = NULL;
54 char *db_pass = NULL;
55 char *db = NULL;
56 unsigned int db_port = MYSQL_PORT;
57 int check_slave = 0, warn_sec = 0, crit_sec = 0;
58 int verbose = 0;
60 thresholds *my_threshold = NULL;
62 int process_arguments (int, char **);
63 int validate_arguments (void);
64 void print_help (void);
65 void print_usage (void);
67 int
68 main (int argc, char **argv)
69 {
71         MYSQL mysql;
72         MYSQL_RES *res;
73         MYSQL_ROW row;
74         
75         /* should be status */
76         
77         char *result = NULL;
78         char *error = NULL;
79         char slaveresult[SLAVERESULTSIZE];
81         setlocale (LC_ALL, "");
82         bindtextdomain (PACKAGE, LOCALEDIR);
83         textdomain (PACKAGE);
85         if (process_arguments (argc, argv) == ERROR)
86                 usage4 (_("Could not parse arguments"));
88         /* initialize mysql  */
89         mysql_init (&mysql);
91         mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
93         /* establish a connection to the server and error checking */
94         if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
95                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
96                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
97                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
98                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
99                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
100                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
101                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
102                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
103                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
104                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
105                 else
106                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
107         }
109         /* get the server stats */
110         result = strdup (mysql_stat (&mysql));
112         /* error checking once more */
113         if (mysql_error (&mysql)) {
114                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
115                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
116                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
117                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
118                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
119                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
120         }
122         if(check_slave) {
123                 /* check the slave status */
124                 if (mysql_query (&mysql, "show slave status") != 0) {
125                         error = strdup(mysql_error(&mysql));
126                         mysql_close (&mysql);
127                         die (STATE_CRITICAL, _("slave query error: %s\n"), error);
128                 }
130                 /* store the result */
131                 if ( (res = mysql_store_result (&mysql)) == NULL) {
132                         error = strdup(mysql_error(&mysql));
133                         mysql_close (&mysql);
134                         die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
135                 }
137                 /* Check there is some data */
138                 if (mysql_num_rows(res) == 0) {
139                         mysql_close(&mysql);
140                         die (STATE_WARNING, "%s\n", _("No slaves defined"));
141                 }
143                 /* fetch the first row */
144                 if ( (row = mysql_fetch_row (res)) == NULL) {
145                         error = strdup(mysql_error(&mysql));
146                         mysql_free_result (res);
147                         mysql_close (&mysql);
148                         die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
149                 }
151                 if (mysql_field_count (&mysql) == 12) {
152                         /* mysql 3.23.x */
153                         snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
154                         if (strcmp (row[6], "Yes") != 0) {
155                                 mysql_free_result (res);
156                                 mysql_close (&mysql);
157                                 die (STATE_CRITICAL, "%s\n", slaveresult);
158                         }
160                 } else {
161                         /* mysql 4.x.x */
162                         int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
163                         MYSQL_FIELD* fields;
165                         num_fields = mysql_num_fields(res);
166                         fields = mysql_fetch_fields(res);
167                         for(i = 0; i < num_fields; i++) {
168                                 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
169                                         slave_io_field = i;
170                                         continue;
171                                 }
172                                 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
173                                         slave_sql_field = i;
174                                         continue;
175                                 }
176                                 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
177                                         seconds_behind_field = i;
178                                         continue;
179                                 }
180                         }
181                         if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
182                                 mysql_free_result (res);
183                                 mysql_close (&mysql);
184                                 die (STATE_CRITICAL, "Slave status unavailable\n");
185                         }
186                         
187                         snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], row[seconds_behind_field]);
188                         if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
189                                 mysql_free_result (res);
190                                 mysql_close (&mysql);
191                                 die (STATE_CRITICAL, "%s\n", slaveresult);
192                         }
194                         if (verbose >=3) {
195                                 if (seconds_behind_field == -1) {
196                                         printf("seconds_behind_field not found\n");
197                                 } else {
198                                         printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
199                                 }
200                         }
202                         if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
203                                 double value = atof(row[seconds_behind_field]);
204                                 int status;
206                                 status = get_status(value, my_threshold);
208                                 if (status == STATE_WARNING) {
209                                         printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
210                                         exit(STATE_WARNING);
211                                 } else if (status == STATE_CRITICAL) {
212                                         printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
213                                         exit(STATE_CRITICAL);
214                                 }
215                         }
216                 }
218                 /* free the result */
219                 mysql_free_result (res);
220         }
222         /* close the connection */
223         mysql_close (&mysql);
225         /* print out the result of stats */
226         if (check_slave) {
227                 printf ("%s %s\n", result, slaveresult);
228         } else {
229                 printf ("%s\n", result);
230         }
232         return STATE_OK;
236 /* process command-line arguments */
237 int
238 process_arguments (int argc, char **argv)
240         int c;
241         char *warning = NULL;
242         char *critical = NULL;
244         int option = 0;
245         static struct option longopts[] = {
246                 {"hostname", required_argument, 0, 'H'},
247                 {"socket", required_argument, 0, 's'},
248                 {"database", required_argument, 0, 'd'},
249                 {"username", required_argument, 0, 'u'},
250                 {"password", required_argument, 0, 'p'},
251                 {"port", required_argument, 0, 'P'},
252                 {"critical", required_argument, 0, 'c'},
253                 {"warning", required_argument, 0, 'w'},
254                 {"check-slave", no_argument, 0, 'S'},
255                 {"verbose", no_argument, 0, 'v'},
256                 {"version", no_argument, 0, 'V'},
257                 {"help", no_argument, 0, 'h'},
258                 {0, 0, 0, 0}
259         };
261         if (argc < 1)
262                 return ERROR;
264         while (1) {
265                 c = getopt_long (argc, argv, "hvVSP:p:u:d:H:s:c:w:", longopts, &option);
267                 if (c == -1 || c == EOF)
268                         break;
270                 switch (c) {
271                 case 'H':                                                                       /* hostname */
272                         if (is_host (optarg)) {
273                                 db_host = optarg;
274                         }
275                         else {
276                                 usage2 (_("Invalid hostname/address"), optarg);
277                         }
278                         break;
279                 case 's':                                                                       /* socket */
280                         db_socket = optarg;
281                         break;
282                 case 'd':                                                                       /* database */
283                         db = optarg;
284                         break;
285                 case 'u':                                                                       /* username */
286                         db_user = optarg;
287                         break;
288                 case 'p':                                                                       /* authentication information: password */
289                         db_pass = strdup(optarg);
291                         /* Delete the password from process list */
292                         while (*optarg != '\0') {
293                                 *optarg = 'X';
294                                 optarg++;
295                         }
296                         break;
297                 case 'P':                                                                       /* critical time threshold */
298                         db_port = atoi (optarg);
299                         break;
300                 case 'S':
301                         check_slave = 1;                                                        /* check-slave */
302                         break;
303                 case 'w':
304                         warning = optarg;
305                         break;
306                 case 'c':
307                         critical = optarg;
308                         break;
309                 case 'V':                                                                       /* version */
310                         print_revision (progname, revision);
311                         exit (STATE_OK);
312                 case 'h':                                                                       /* help */
313                         print_help ();
314                         exit (STATE_OK);
315                 case 'v':
316                         verbose++;
317                         break;
318                 case '?':                                                                       /* help */
319                         usage5 ();
320                 }
321         }
323         c = optind;
325         set_thresholds(&my_threshold, warning, critical);
327         while ( argc > c ) {
329                 if (db_host == NULL)
330                         if (is_host (argv[c])) {
331                                 db_host = argv[c++];
332                         }
333                         else {
334                                 usage2 (_("Invalid hostname/address"), argv[c]);
335                         }
336                 else if (db_user == NULL)
337                         db_user = argv[c++];
338                 else if (db_pass == NULL)
339                         db_pass = argv[c++];
340                 else if (db == NULL)
341                         db = argv[c++];
342                 else if (is_intnonneg (argv[c]))
343                         db_port = atoi (argv[c++]);
344                 else
345                         break;
346         }
348         return validate_arguments ();
352 int
353 validate_arguments (void)
355         if (db_user == NULL)
356                 db_user = strdup("");
358         if (db_host == NULL)
359                 db_host = strdup("");
361         if (db_pass == NULL)
362                 db_pass == strdup("");
364         if (db == NULL)
365                 db = strdup("");
367         return OK;
371 void
372 print_help (void)
374         char *myport;
375         asprintf (&myport, "%d", MYSQL_PORT);
377         print_revision (progname, revision);
379         printf (_(COPYRIGHT), copyright, email);
381         printf ("%s\n", _("This program tests connections to a mysql server"));
383   printf ("\n\n");
385         print_usage ();
387   printf (_(UT_HELP_VRSN));
389   printf (_(UT_HOST_PORT), 'P', myport);
390   printf (" %s\n", "-s, --socket=STRING");
391   printf ("    %s\n", _("Use the specified socket (has no effect if -H is used)"));
393   printf (" %s\n", "-d, --database=STRING");
394   printf ("    %s\n", _("Check database with indicated name"));
395   printf (" %s\n", "-u, --username=STRING");
396   printf ("    %s\n", _("Connect using the indicated username"));
397   printf (" %s\n", "-p, --password=STRING");
398   printf ("    %s\n", _("Use the indicated password to authenticate the connection"));
399   printf ("    ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
400   printf ("    %s\n", _("Your clear-text password could be visible as a process table entry"));
401   printf (" %s\n", "-S, --check-slave");
402   printf ("    %s\n", _("Check if the slave thread is running properly."));
403   printf (" %s\n", "-w, --warning");
404   printf ("    %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds"));
405   printf ("    %s\n", _("behind master"));
406   printf (" %s\n", "-c, --critical");
407   printf ("    %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
408   printf ("    %s\n", _("behind master"));
410   printf ("\n");
411   printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
412   printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
413   printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
415         printf (_(UT_SUPPORT));
419 void
420 print_usage (void)
422         printf (_("Usage:"));
423   printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
424   printf ("       [-u user] [-p password] [-S]\n");