Code

first pass at cleaning localization for new release
[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) 1999-2006 nagios-plugins team
8 *  Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
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 * License Information:
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 *
35 * $Id$
36 *
37 ******************************************************************************/
39 const char *progname = "check_mysql";
40 const char *revision = "$Revision$";
41 const char *copyright = "1999-2006";
42 const char *email = "nagiosplug-devel@lists.sourceforge.net";
44 #define SLAVERESULTSIZE 70
46 #include "common.h"
47 #include "utils.h"
48 #include "utils_base.h"
49 #include "netutils.h"
51 #include <mysql.h>
52 #include <errmsg.h>
54 char *db_user = NULL;
55 char *db_host = NULL;
56 char *db_pass = NULL;
57 char *db = NULL;
58 unsigned int db_port = MYSQL_PORT;
59 int check_slave = 0, warn_sec = 0, crit_sec = 0;
60 int verbose = 0;
62 thresholds *my_threshold = NULL;
64 int process_arguments (int, char **);
65 int validate_arguments (void);
66 void print_help (void);
67 void print_usage (void);
69 int
70 main (int argc, char **argv)
71 {
73         MYSQL mysql;
74         MYSQL_RES *res;
75         MYSQL_ROW row;
76         
77         /* should be status */
78         
79         char *result = NULL;
80         char *error = NULL;
81         char slaveresult[SLAVERESULTSIZE];
83         setlocale (LC_ALL, "");
84         bindtextdomain (PACKAGE, LOCALEDIR);
85         textdomain (PACKAGE);
87         if (process_arguments (argc, argv) == ERROR)
88                 usage4 (_("Could not parse arguments"));
90         /* initialize mysql  */
91         mysql_init (&mysql);
93         mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
95         /* establish a connection to the server and error checking */
96         if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
97                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
98                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
99                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
100                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
101                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
102                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
103                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
104                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
105                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
106                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
107                 else
108                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
109         }
111         /* get the server stats */
112         result = strdup (mysql_stat (&mysql));
114         /* error checking once more */
115         if (mysql_error (&mysql)) {
116                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
117                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
118                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
119                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
120                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
121                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
122         }
124         if(check_slave) {
125                 /* check the slave status */
126                 if (mysql_query (&mysql, "show slave status") != 0) {
127                         error = strdup(mysql_error(&mysql));
128                         mysql_close (&mysql);
129                         die (STATE_CRITICAL, _("slave query error: %s\n"), error);
130                 }
132                 /* store the result */
133                 if ( (res = mysql_store_result (&mysql)) == NULL) {
134                         error = strdup(mysql_error(&mysql));
135                         mysql_close (&mysql);
136                         die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
137                 }
139                 /* Check there is some data */
140                 if (mysql_num_rows(res) == 0) {
141                         mysql_close(&mysql);
142                         die (STATE_WARNING, "%s\n", _("No slaves defined"));
143                 }
145                 /* fetch the first row */
146                 if ( (row = mysql_fetch_row (res)) == NULL) {
147                         error = strdup(mysql_error(&mysql));
148                         mysql_free_result (res);
149                         mysql_close (&mysql);
150                         die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
151                 }
153                 if (mysql_field_count (&mysql) == 12) {
154                         /* mysql 3.23.x */
155                         snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
156                         if (strcmp (row[6], "Yes") != 0) {
157                                 mysql_free_result (res);
158                                 mysql_close (&mysql);
159                                 die (STATE_CRITICAL, "%s\n", slaveresult);
160                         }
162                 } else {
163                         /* mysql 4.x.x */
164                         int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
165                         MYSQL_FIELD* fields;
167                         num_fields = mysql_num_fields(res);
168                         fields = mysql_fetch_fields(res);
169                         for(i = 0; i < num_fields; i++) {
170                                 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
171                                         slave_io_field = i;
172                                         continue;
173                                 }
174                                 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
175                                         slave_sql_field = i;
176                                         continue;
177                                 }
178                                 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
179                                         seconds_behind_field = i;
180                                         continue;
181                                 }
182                         }
183                         if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
184                                 mysql_free_result (res);
185                                 mysql_close (&mysql);
186                                 die (STATE_CRITICAL, "Slave status unavailable\n");
187                         }
188                         
189                         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]);
190                         if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
191                                 mysql_free_result (res);
192                                 mysql_close (&mysql);
193                                 die (STATE_CRITICAL, "%s\n", slaveresult);
194                         }
196                         if (verbose >=3) {
197                                 if (seconds_behind_field == -1) {
198                                         printf("seconds_behind_field not found\n");
199                                 } else {
200                                         printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
201                                 }
202                         }
204                         if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
205                                 double value = atof(row[seconds_behind_field]);
206                                 int status;
208                                 status = get_status(value, my_threshold);
210                                 if (status == STATE_WARNING) {
211                                         printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
212                                         exit(STATE_WARNING);
213                                 } else if (status == STATE_CRITICAL) {
214                                         printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
215                                         exit(STATE_CRITICAL);
216                                 }
217                         }
218                 }
220                 /* free the result */
221                 mysql_free_result (res);
222         }
224         /* close the connection */
225         mysql_close (&mysql);
227         /* print out the result of stats */
228         if (check_slave) {
229                 printf ("%s %s\n", result, slaveresult);
230         } else {
231                 printf ("%s\n", result);
232         }
234         return STATE_OK;
238 /* process command-line arguments */
239 int
240 process_arguments (int argc, char **argv)
242         int c;
243         char *warning = NULL;
244         char *critical = NULL;
246         int option = 0;
247         static struct option longopts[] = {
248                 {"hostname", required_argument, 0, 'H'},
249                 {"database", required_argument, 0, 'd'},
250                 {"username", required_argument, 0, 'u'},
251                 {"password", required_argument, 0, 'p'},
252                 {"port", required_argument, 0, 'P'},
253                 {"critical", required_argument, 0, 'c'},
254                 {"warning", required_argument, 0, 'w'},
255                 {"check-slave", no_argument, 0, 'S'},
256                 {"verbose", no_argument, 0, 'v'},
257                 {"version", no_argument, 0, 'V'},
258                 {"help", no_argument, 0, 'h'},
259                 {0, 0, 0, 0}
260         };
262         if (argc < 1)
263                 return ERROR;
265         while (1) {
266                 c = getopt_long (argc, argv, "hvVSP:p:u:d:H:c:w:", longopts, &option);
268                 if (c == -1 || c == EOF)
269                         break;
271                 switch (c) {
272                 case 'H':                                                                       /* hostname */
273                         if (is_host (optarg)) {
274                                 db_host = optarg;
275                         }
276                         else {
277                                 usage2 (_("Invalid hostname/address"), optarg);
278                         }
279                         break;
280                 case 'd':                                                                       /* hostname */
281                         db = optarg;
282                         break;
283                 case 'u':                                                                       /* username */
284                         db_user = optarg;
285                         break;
286                 case 'p':                                                                       /* authentication information: password */
287                         db_pass = optarg;
288                         break;
289                 case 'P':                                                                       /* critical time threshold */
290                         db_port = atoi (optarg);
291                         break;
292                 case 'S':
293                         check_slave = 1;                                                        /* check-slave */
294                         break;
295                 case 'w':
296                         warning = optarg;
297                         break;
298                 case 'c':
299                         critical = optarg;
300                         break;
301                 case 'V':                                                                       /* version */
302                         print_revision (progname, revision);
303                         exit (STATE_OK);
304                 case 'h':                                                                       /* help */
305                         print_help ();
306                         exit (STATE_OK);
307                 case 'v':
308                         verbose++;
309                         break;
310                 case '?':                                                                       /* help */
311                         usage2 (_("Unknown argument"), optarg);
312                 }
313         }
315         c = optind;
317         set_thresholds(&my_threshold, warning, critical);
319         while ( argc > c ) {
321                 if (strlen(db_host) == 0)
322                         if (is_host (argv[c])) {
323                                 db_host = argv[c++];
324                         }
325                         else {
326                                 usage2 (_("Invalid hostname/address"), optarg);
327                         }
328                 else if (strlen(db_user) == 0)
329                         db_user = argv[c++];
330                 else if (strlen(db_pass) == 0)
331                         db_pass = argv[c++];
332                 else if (strlen(db) == 0)
333                         db = argv[c++];
334                 else if (is_intnonneg (argv[c]))
335                         db_port = atoi (argv[c++]);
336                 else
337                         break;
338         }
340         return validate_arguments ();
344 int
345 validate_arguments (void)
347         if (db_user == NULL)
348                 db_user = strdup("");
350         if (db_host == NULL)
351                 db_host = strdup("");
353         if (db_pass == NULL)
354                 db_pass == strdup("");
356         if (db == NULL)
357                 db = strdup("");
359         return OK;
363 void
364 print_help (void)
366         char *myport;
367         asprintf (&myport, "%d", MYSQL_PORT);
369         print_revision (progname, revision);
371         printf (_(COPYRIGHT), copyright, email);
373         printf ("%s\n", _("This program tests connections to a mysql server"));
375   printf ("\n\n");
376   
377         print_usage ();
379         printf (_(UT_HELP_VRSN));
381         printf (_(UT_HOST_PORT), 'P', myport);
383         printf (" %s\n", "-d, --database=STRING");
384   printf ("    %s\n", _("Check database with indicated name"));
385   printf (" %s\n", "-u, --username=STRING");
386   printf ("    %s\n", _("Connect using the indicated username"));
387   printf (" %s\n", "-p, --password=STRING");
388   printf ("    %s\n", _("Use the indicated password to authenticate the connection"));
389   printf ("    %s\n", _("==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <=="));
390   printf ("    %s\n", _("Your clear-text password will be visible as a process table entry"));
391   printf (" %s\n", "-S, --check-slave");
392   printf ("    %s\n", _("Check if the slave thread is running properly."));
393   printf (" %s\n", "-w, --warning");
394   printf ("    %s\n", _("Exit with WARNING status if slave server is more then INTEGER seconds behind master"));
395   printf (" %s\n", "-c, --critical");
396   printf ("    %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds behind master"));
397   printf (" %s\n", _("There are no required arguments. By default, the local database with"));
398   printf (_("a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
400         printf (_(UT_SUPPORT));
404 void
405 print_usage (void)
407         printf (_("Usage:"));
408   printf ("%s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",progname);