Code

Bump plugins/ to GPLv3 (check_apt to check_nwstat)
[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_pass = NULL;
54 char *db = NULL;
55 unsigned int db_port = MYSQL_PORT;
56 int check_slave = 0, warn_sec = 0, crit_sec = 0;
57 int verbose = 0;
59 thresholds *my_threshold = NULL;
61 int process_arguments (int, char **);
62 int validate_arguments (void);
63 void print_help (void);
64 void print_usage (void);
66 int
67 main (int argc, char **argv)
68 {
70         MYSQL mysql;
71         MYSQL_RES *res;
72         MYSQL_ROW row;
73         
74         /* should be status */
75         
76         char *result = NULL;
77         char *error = NULL;
78         char slaveresult[SLAVERESULTSIZE];
80         setlocale (LC_ALL, "");
81         bindtextdomain (PACKAGE, LOCALEDIR);
82         textdomain (PACKAGE);
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,NULL,0)) {
94                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
95                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
96                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
97                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
98                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
99                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
100                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
101                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
102                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
103                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
104                 else
105                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
106         }
108         /* get the server stats */
109         result = strdup (mysql_stat (&mysql));
111         /* error checking once more */
112         if (mysql_error (&mysql)) {
113                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
114                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
115                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
116                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
117                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
118                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
119         }
121         if(check_slave) {
122                 /* check the slave status */
123                 if (mysql_query (&mysql, "show slave status") != 0) {
124                         error = strdup(mysql_error(&mysql));
125                         mysql_close (&mysql);
126                         die (STATE_CRITICAL, _("slave query error: %s\n"), error);
127                 }
129                 /* store the result */
130                 if ( (res = mysql_store_result (&mysql)) == NULL) {
131                         error = strdup(mysql_error(&mysql));
132                         mysql_close (&mysql);
133                         die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
134                 }
136                 /* Check there is some data */
137                 if (mysql_num_rows(res) == 0) {
138                         mysql_close(&mysql);
139                         die (STATE_WARNING, "%s\n", _("No slaves defined"));
140                 }
142                 /* fetch the first row */
143                 if ( (row = mysql_fetch_row (res)) == NULL) {
144                         error = strdup(mysql_error(&mysql));
145                         mysql_free_result (res);
146                         mysql_close (&mysql);
147                         die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
148                 }
150                 if (mysql_field_count (&mysql) == 12) {
151                         /* mysql 3.23.x */
152                         snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
153                         if (strcmp (row[6], "Yes") != 0) {
154                                 mysql_free_result (res);
155                                 mysql_close (&mysql);
156                                 die (STATE_CRITICAL, "%s\n", slaveresult);
157                         }
159                 } else {
160                         /* mysql 4.x.x */
161                         int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
162                         MYSQL_FIELD* fields;
164                         num_fields = mysql_num_fields(res);
165                         fields = mysql_fetch_fields(res);
166                         for(i = 0; i < num_fields; i++) {
167                                 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
168                                         slave_io_field = i;
169                                         continue;
170                                 }
171                                 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
172                                         slave_sql_field = i;
173                                         continue;
174                                 }
175                                 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
176                                         seconds_behind_field = i;
177                                         continue;
178                                 }
179                         }
180                         if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
181                                 mysql_free_result (res);
182                                 mysql_close (&mysql);
183                                 die (STATE_CRITICAL, "Slave status unavailable\n");
184                         }
185                         
186                         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]);
187                         if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
188                                 mysql_free_result (res);
189                                 mysql_close (&mysql);
190                                 die (STATE_CRITICAL, "%s\n", slaveresult);
191                         }
193                         if (verbose >=3) {
194                                 if (seconds_behind_field == -1) {
195                                         printf("seconds_behind_field not found\n");
196                                 } else {
197                                         printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
198                                 }
199                         }
201                         if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
202                                 double value = atof(row[seconds_behind_field]);
203                                 int status;
205                                 status = get_status(value, my_threshold);
207                                 if (status == STATE_WARNING) {
208                                         printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
209                                         exit(STATE_WARNING);
210                                 } else if (status == STATE_CRITICAL) {
211                                         printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
212                                         exit(STATE_CRITICAL);
213                                 }
214                         }
215                 }
217                 /* free the result */
218                 mysql_free_result (res);
219         }
221         /* close the connection */
222         mysql_close (&mysql);
224         /* print out the result of stats */
225         if (check_slave) {
226                 printf ("%s %s\n", result, slaveresult);
227         } else {
228                 printf ("%s\n", result);
229         }
231         return STATE_OK;
235 /* process command-line arguments */
236 int
237 process_arguments (int argc, char **argv)
239         int c;
240         char *warning = NULL;
241         char *critical = NULL;
243         int option = 0;
244         static struct option longopts[] = {
245                 {"hostname", required_argument, 0, 'H'},
246                 {"database", required_argument, 0, 'd'},
247                 {"username", required_argument, 0, 'u'},
248                 {"password", required_argument, 0, 'p'},
249                 {"port", required_argument, 0, 'P'},
250                 {"critical", required_argument, 0, 'c'},
251                 {"warning", required_argument, 0, 'w'},
252                 {"check-slave", no_argument, 0, 'S'},
253                 {"verbose", no_argument, 0, 'v'},
254                 {"version", no_argument, 0, 'V'},
255                 {"help", no_argument, 0, 'h'},
256                 {0, 0, 0, 0}
257         };
259         if (argc < 1)
260                 return ERROR;
262         while (1) {
263                 c = getopt_long (argc, argv, "hvVSP:p:u:d:H:c:w:", longopts, &option);
265                 if (c == -1 || c == EOF)
266                         break;
268                 switch (c) {
269                 case 'H':                                                                       /* hostname */
270                         if (is_host (optarg)) {
271                                 db_host = optarg;
272                         }
273                         else {
274                                 usage2 (_("Invalid hostname/address"), optarg);
275                         }
276                         break;
277                 case 'd':                                                                       /* hostname */
278                         db = optarg;
279                         break;
280                 case 'u':                                                                       /* username */
281                         db_user = optarg;
282                         break;
283                 case 'p':                                                                       /* authentication information: password */
284                         db_pass = optarg;
285                         break;
286                 case 'P':                                                                       /* critical time threshold */
287                         db_port = atoi (optarg);
288                         break;
289                 case 'S':
290                         check_slave = 1;                                                        /* check-slave */
291                         break;
292                 case 'w':
293                         warning = optarg;
294                         break;
295                 case 'c':
296                         critical = optarg;
297                         break;
298                 case 'V':                                                                       /* version */
299                         print_revision (progname, revision);
300                         exit (STATE_OK);
301                 case 'h':                                                                       /* help */
302                         print_help ();
303                         exit (STATE_OK);
304                 case 'v':
305                         verbose++;
306                         break;
307                 case '?':                                                                       /* help */
308                         usage5 ();
309                 }
310         }
312         c = optind;
314         set_thresholds(&my_threshold, warning, critical);
316         while ( argc > c ) {
318                 if (db_host == NULL)
319                         if (is_host (argv[c])) {
320                                 db_host = argv[c++];
321                         }
322                         else {
323                                 usage2 (_("Invalid hostname/address"), argv[c]);
324                         }
325                 else if (db_user == NULL)
326                         db_user = argv[c++];
327                 else if (db_pass == NULL)
328                         db_pass = argv[c++];
329                 else if (db == NULL)
330                         db = argv[c++];
331                 else if (is_intnonneg (argv[c]))
332                         db_port = atoi (argv[c++]);
333                 else
334                         break;
335         }
337         return validate_arguments ();
341 int
342 validate_arguments (void)
344         if (db_user == NULL)
345                 db_user = strdup("");
347         if (db_host == NULL)
348                 db_host = strdup("");
350         if (db_pass == NULL)
351                 db_pass == strdup("");
353         if (db == NULL)
354                 db = strdup("");
356         return OK;
360 void
361 print_help (void)
363         char *myport;
364         asprintf (&myport, "%d", MYSQL_PORT);
366         print_revision (progname, revision);
368         printf (_(COPYRIGHT), copyright, email);
370         printf ("%s\n", _("This program tests connections to a mysql server"));
372   printf ("\n\n");
373   
374         print_usage ();
376         printf (_(UT_HELP_VRSN));
378         printf (_(UT_HOST_PORT), 'P', myport);
380         printf (" %s\n", "-d, --database=STRING");
381   printf ("    %s\n", _("Check database with indicated name"));
382   printf (" %s\n", "-u, --username=STRING");
383   printf ("    %s\n", _("Connect using the indicated username"));
384   printf (" %s\n", "-p, --password=STRING");
385   printf ("    %s\n", _("Use the indicated password to authenticate the connection"));
386   printf ("    %s\n", _("==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <=="));
387   printf ("    %s\n", _("Your clear-text password will be visible as a process table entry"));
388   printf (" %s\n", "-S, --check-slave");
389   printf ("    %s\n", _("Check if the slave thread is running properly."));
390   printf (" %s\n", "-w, --warning");
391   printf ("    %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds behind master"));
392   printf (" %s\n", "-c, --critical");
393   printf ("    %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds behind master"));
394   printf (" %s\n", _("There are no required arguments. By default, the local database with"));
395   printf (_("a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
397         printf (_(UT_SUPPORT));
401 void
402 print_usage (void)
404         printf (_("Usage:"));
405   printf ("%s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",progname);