Code

Bulk EOL cleanup
[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;
75         /* should be status */
77         char *result = NULL;
78         char *error = NULL;
79         char slaveresult[SLAVERESULTSIZE];
81         setlocale (LC_ALL, "");
82         bindtextdomain (PACKAGE, LOCALEDIR);
83         textdomain (PACKAGE);
85         /* Parse extra opts if any */
86         argv=np_extra_opts (&argc, argv, progname);
88         if (process_arguments (argc, argv) == ERROR)
89                 usage4 (_("Could not parse arguments"));
91         /* initialize mysql  */
92         mysql_init (&mysql);
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, "%s\n", mysql_error (&mysql));
100                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
101                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
102                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
103                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
104                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
105                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
106                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
107                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
108                 else
109                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
110         }
112         /* get the server stats */
113         result = strdup (mysql_stat (&mysql));
115         /* error checking once more */
116         if (mysql_error (&mysql)) {
117                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
118                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
119                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
120                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
121                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
122                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
123         }
125         if(check_slave) {
126                 /* check the slave status */
127                 if (mysql_query (&mysql, "show slave status") != 0) {
128                         error = strdup(mysql_error(&mysql));
129                         mysql_close (&mysql);
130                         die (STATE_CRITICAL, _("slave query error: %s\n"), error);
131                 }
133                 /* store the result */
134                 if ( (res = mysql_store_result (&mysql)) == NULL) {
135                         error = strdup(mysql_error(&mysql));
136                         mysql_close (&mysql);
137                         die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
138                 }
140                 /* Check there is some data */
141                 if (mysql_num_rows(res) == 0) {
142                         mysql_close(&mysql);
143                         die (STATE_WARNING, "%s\n", _("No slaves defined"));
144                 }
146                 /* fetch the first row */
147                 if ( (row = mysql_fetch_row (res)) == NULL) {
148                         error = strdup(mysql_error(&mysql));
149                         mysql_free_result (res);
150                         mysql_close (&mysql);
151                         die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
152                 }
154                 if (mysql_field_count (&mysql) == 12) {
155                         /* mysql 3.23.x */
156                         snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
157                         if (strcmp (row[6], "Yes") != 0) {
158                                 mysql_free_result (res);
159                                 mysql_close (&mysql);
160                                 die (STATE_CRITICAL, "%s\n", slaveresult);
161                         }
163                 } else {
164                         /* mysql 4.x.x */
165                         int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
166                         MYSQL_FIELD* fields;
168                         num_fields = mysql_num_fields(res);
169                         fields = mysql_fetch_fields(res);
170                         for(i = 0; i < num_fields; i++) {
171                                 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
172                                         slave_io_field = i;
173                                         continue;
174                                 }
175                                 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
176                                         slave_sql_field = i;
177                                         continue;
178                                 }
179                                 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
180                                         seconds_behind_field = i;
181                                         continue;
182                                 }
183                         }
184                         if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
185                                 mysql_free_result (res);
186                                 mysql_close (&mysql);
187                                 die (STATE_CRITICAL, "Slave status unavailable\n");
188                         }
190                         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]);
191                         if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
192                                 mysql_free_result (res);
193                                 mysql_close (&mysql);
194                                 die (STATE_CRITICAL, "%s\n", slaveresult);
195                         }
197                         if (verbose >=3) {
198                                 if (seconds_behind_field == -1) {
199                                         printf("seconds_behind_field not found\n");
200                                 } else {
201                                         printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
202                                 }
203                         }
205                         if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
206                                 double value = atof(row[seconds_behind_field]);
207                                 int status;
209                                 status = get_status(value, my_threshold);
211                                 if (status == STATE_WARNING) {
212                                         printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
213                                         exit(STATE_WARNING);
214                                 } else if (status == STATE_CRITICAL) {
215                                         printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
216                                         exit(STATE_CRITICAL);
217                                 }
218                         }
219                 }
221                 /* free the result */
222                 mysql_free_result (res);
223         }
225         /* close the connection */
226         mysql_close (&mysql);
228         /* print out the result of stats */
229         if (check_slave) {
230                 printf ("%s %s\n", result, slaveresult);
231         } else {
232                 printf ("%s\n", result);
233         }
235         return STATE_OK;
239 /* process command-line arguments */
240 int
241 process_arguments (int argc, char **argv)
243         int c;
244         char *warning = NULL;
245         char *critical = NULL;
247         int option = 0;
248         static struct option longopts[] = {
249                 {"hostname", required_argument, 0, 'H'},
250                 {"socket", required_argument, 0, 's'},
251                 {"database", required_argument, 0, 'd'},
252                 {"username", required_argument, 0, 'u'},
253                 {"password", required_argument, 0, 'p'},
254                 {"port", required_argument, 0, 'P'},
255                 {"critical", required_argument, 0, 'c'},
256                 {"warning", required_argument, 0, 'w'},
257                 {"check-slave", no_argument, 0, 'S'},
258                 {"verbose", no_argument, 0, 'v'},
259                 {"version", no_argument, 0, 'V'},
260                 {"help", no_argument, 0, 'h'},
261                 {0, 0, 0, 0}
262         };
264         if (argc < 1)
265                 return ERROR;
267         while (1) {
268                 c = getopt_long (argc, argv, "hvVSP:p:u:d:H:s:c:w:", longopts, &option);
270                 if (c == -1 || c == EOF)
271                         break;
273                 switch (c) {
274                 case 'H':                                                                       /* hostname */
275                         if (is_host (optarg)) {
276                                 db_host = optarg;
277                         }
278                         else {
279                                 usage2 (_("Invalid hostname/address"), optarg);
280                         }
281                         break;
282                 case 's':                                                                       /* socket */
283                         db_socket = optarg;
284                         break;
285                 case 'd':                                                                       /* database */
286                         db = optarg;
287                         break;
288                 case 'u':                                                                       /* username */
289                         db_user = optarg;
290                         break;
291                 case 'p':                                                                       /* authentication information: password */
292                         db_pass = strdup(optarg);
294                         /* Delete the password from process list */
295                         while (*optarg != '\0') {
296                                 *optarg = 'X';
297                                 optarg++;
298                         }
299                         break;
300                 case 'P':                                                                       /* critical time threshold */
301                         db_port = atoi (optarg);
302                         break;
303                 case 'S':
304                         check_slave = 1;                                                        /* check-slave */
305                         break;
306                 case 'w':
307                         warning = optarg;
308                         break;
309                 case 'c':
310                         critical = optarg;
311                         break;
312                 case 'V':                                                                       /* version */
313                         print_revision (progname, revision);
314                         exit (STATE_OK);
315                 case 'h':                                                                       /* help */
316                         print_help ();
317                         exit (STATE_OK);
318                 case 'v':
319                         verbose++;
320                         break;
321                 case '?':                                                                       /* help */
322                         usage5 ();
323                 }
324         }
326         c = optind;
328         set_thresholds(&my_threshold, warning, critical);
330         while ( argc > c ) {
332                 if (db_host == NULL)
333                         if (is_host (argv[c])) {
334                                 db_host = argv[c++];
335                         }
336                         else {
337                                 usage2 (_("Invalid hostname/address"), argv[c]);
338                         }
339                 else if (db_user == NULL)
340                         db_user = argv[c++];
341                 else if (db_pass == NULL)
342                         db_pass = argv[c++];
343                 else if (db == NULL)
344                         db = argv[c++];
345                 else if (is_intnonneg (argv[c]))
346                         db_port = atoi (argv[c++]);
347                 else
348                         break;
349         }
351         return validate_arguments ();
355 int
356 validate_arguments (void)
358         if (db_user == NULL)
359                 db_user = strdup("");
361         if (db_host == NULL)
362                 db_host = strdup("");
364         if (db_pass == NULL)
365                 db_pass = strdup("");
367         if (db == NULL)
368                 db = strdup("");
370         return OK;
374 void
375 print_help (void)
377         char *myport;
378         asprintf (&myport, "%d", MYSQL_PORT);
380         print_revision (progname, revision);
382         printf (_(COPYRIGHT), copyright, email);
384         printf ("%s\n", _("This program tests connections to a mysql server"));
386   printf ("\n\n");
388         print_usage ();
390   printf (_(UT_HELP_VRSN));
391         printf (_(UT_EXTRA_OPTS));
393   printf (_(UT_HOST_PORT), 'P', myport);
394   printf (" %s\n", "-s, --socket=STRING");
395   printf ("    %s\n", _("Use the specified socket (has no effect if -H is used)"));
397   printf (" %s\n", "-d, --database=STRING");
398   printf ("    %s\n", _("Check database with indicated name"));
399   printf (" %s\n", "-u, --username=STRING");
400   printf ("    %s\n", _("Connect using the indicated username"));
401   printf (" %s\n", "-p, --password=STRING");
402   printf ("    %s\n", _("Use the indicated password to authenticate the connection"));
403   printf ("    ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
404   printf ("    %s\n", _("Your clear-text password could be visible as a process table entry"));
405   printf (" %s\n", "-S, --check-slave");
406   printf ("    %s\n", _("Check if the slave thread is running properly."));
407   printf (" %s\n", "-w, --warning");
408   printf ("    %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds"));
409   printf ("    %s\n", _("behind master"));
410   printf (" %s\n", "-c, --critical");
411   printf ("    %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
412   printf ("    %s\n", _("behind master"));
414   printf ("\n");
415   printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
416   printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
417   printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
419 #ifdef NP_EXTRA_OPTS
420         printf ("\n");
421         printf ("%s\n", _("Notes:"));
422         printf (_(UT_EXTRA_OPTS_NOTES));
423 #endif
425         printf (_(UT_SUPPORT));
429 void
430 print_usage (void)
432         printf (_("Usage:"));
433   printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
434   printf ("       [-u user] [-p password] [-S]\n");