Code

45f86a9c6319e49e88f4f152f5824f73ea4d2f2e
[nagiosplug.git] / plugins / check_mysql.c
1 /******************************************************************************
2 *
3 * CHECK_MYSQL.C
4 *
5 * Program: Mysql plugin for Nagios
6 * License: GPL
7 * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
8 *  portions (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
9
10 * $Id$
11 *
12 * Description:
13 *
14 * This plugin is for testing a mysql server.
15 ******************************************************************************/
17 const char *progname = "check_mysql";
18 const char *revision = "$Revision$";
19 const char *copyright = "1999-2004";
20 const char *email = "nagiosplug-devel@lists.sourceforge.net";
22 #define SLAVERESULTSIZE 40
24 #include "common.h"
25 #include "utils.h"
26 #include "netutils.h"
28 #include <mysql/mysql.h>
29 #include <mysql/errmsg.h>
31 char *db_user = NULL;
32 char *db_host = NULL;
33 char *db_pass = NULL;
34 char *db = NULL;
35 unsigned int db_port = MYSQL_PORT;
36 int check_slave = 0;
38 int process_arguments (int, char **);
39 int validate_arguments (void);
40 void print_help (void);
41 void print_usage (void);
45 int
46 main (int argc, char **argv)
47 {
49         MYSQL mysql;
50         MYSQL_RES *res;
51         MYSQL_ROW row;
52         
53         /* should be status */
54         
55         char *result = NULL;
56         char *error = NULL;
57         char slaveresult[SLAVERESULTSIZE];
59         setlocale (LC_ALL, "");
60         bindtextdomain (PACKAGE, LOCALEDIR);
61         textdomain (PACKAGE);
63         if (process_arguments (argc, argv) == ERROR)
64                 usage4 (_("Could not parse arguments"));
66         /* initialize mysql  */
67         mysql_init (&mysql);
69         mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
71         /* establish a connection to the server and error checking */
72         if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
73                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
74                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
75                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
76                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
77                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
78                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
79                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
80                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
81                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
82                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
83                 else
84                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
85         }
87         /* get the server stats */
88         result = strdup (mysql_stat (&mysql));
90         /* error checking once more */
91         if (mysql_error (&mysql)) {
92                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
93                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
94                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
95                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
96                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
97                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
98         }
100         if(check_slave) {
101                 /* check the slave status */
102                 if (mysql_query (&mysql, "show slave status") != 0) {
103                         error = strdup(mysql_error(&mysql));
104                         mysql_close (&mysql);
105                         die (STATE_CRITICAL, _("slave query error: %s\n"), error);
106                 }
108                 /* store the result */
109                 if ( (res = mysql_store_result (&mysql)) == NULL) {
110                         error = strdup(mysql_error(&mysql));
111                         mysql_close (&mysql);
112                         die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
113                 }
115                 /* Check there is some data */
116                 if (mysql_num_rows(res) == 0) {
117                         mysql_close(&mysql);
118                         die (STATE_WARNING, "%s\n", _("No slaves defined"));
119                 }
121                 /* fetch the first row */
122                 if ( (row = mysql_fetch_row (res)) == NULL) {
123                         error = strdup(mysql_error(&mysql));
124                         mysql_free_result (res);
125                         mysql_close (&mysql);
126                         die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
127                 }
129                 if (mysql_field_count (&mysql) == 12) {
130                         /* mysql 3.23.x */
131                         snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
132                         if (strcmp (row[6], "Yes") != 0) {
133                                 mysql_free_result (res);
134                                 mysql_close (&mysql);
135                                 die (STATE_CRITICAL, "%s\n", slaveresult);
136                         }
138                 } else {
139                         /* mysql 4.x.x */
140                         int slave_io_field = -1 , slave_sql_field = -1, i, num_fields;
141                         MYSQL_FIELD* fields;
143                         num_fields = mysql_num_fields(res);
144                         fields = mysql_fetch_fields(res);
145                         for(i = 0; i < num_fields; i++)
146                         {
147                                 if (0 == strcmp(fields[i].name, "Slave_IO_Running"))
148                                 {
149                                         slave_io_field = i;
150                                         continue;
151                                 }
152                                 if (0 == strcmp(fields[i].name, "Slave_SQL_Running"))
153                                 {
154                                         slave_sql_field = i;
155                                         continue;
156                                 }
157                         }
158                         if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0))
159                         {
160                                 mysql_free_result (res);
161                                 mysql_close (&mysql);
162                                 die (STATE_CRITICAL, "Slave status unavailable\n");
163                         }
164                          
165                         snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[slave_io_field], row[slave_sql_field]);
166                         if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
167                                 mysql_free_result (res);
168                                 mysql_close (&mysql);
169                                 die (STATE_CRITICAL, "%s\n", slaveresult);
170                         }
171                 }
173                 /* free the result */
174                 mysql_free_result (res);
175         }
177         /* close the connection */
178         mysql_close (&mysql);
180         /* print out the result of stats */
181         if (check_slave) {
182                 printf ("%s %s\n", result, slaveresult);
183         } else {
184                 printf ("%s\n", result);
185         }
187         return STATE_OK;
191 /* process command-line arguments */
192 int
193 process_arguments (int argc, char **argv)
195         int c;
197         int option = 0;
198         static struct option longopts[] = {
199                 {"hostname", required_argument, 0, 'H'},
200                 {"database", required_argument, 0, 'd'},
201                 {"username", required_argument, 0, 'u'},
202                 {"password", required_argument, 0, 'p'},
203                 {"port", required_argument, 0, 'P'},
204                 {"check-slave", no_argument, 0, 'S'},
205                 {"verbose", no_argument, 0, 'v'},
206                 {"version", no_argument, 0, 'V'},
207                 {"help", no_argument, 0, 'h'},
208                 {0, 0, 0, 0}
209         };
211         if (argc < 1)
212                 return ERROR;
214         while (1) {
215                 c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option);
217                 if (c == -1 || c == EOF)
218                         break;
220                 switch (c) {
221                 case 'H':                                                                       /* hostname */
222                         if (is_host (optarg)) {
223                                 db_host = optarg;
224                         }
225                         else {
226                                 usage2 (_("Invalid hostname/address"), optarg);
227                         }
228                         break;
229                 case 'd':                                                                       /* hostname */
230                         db = optarg;
231                         break;
232                 case 'u':                                                                       /* username */
233                         db_user = optarg;
234                         break;
235                 case 'p':                                                                       /* authentication information: password */
236                         db_pass = optarg;
237                         break;
238                 case 'P':                                                                       /* critical time threshold */
239                         db_port = atoi (optarg);
240                         break;
241                 case 'S':
242                         check_slave = 1;                                                        /* check-slave */
243                         break;
244                 case 'V':                                                                       /* version */
245                         print_revision (progname, revision);
246                         exit (STATE_OK);
247                 case 'h':                                                                       /* help */
248                         print_help ();
249                         exit (STATE_OK);
250                 case '?':                                                                       /* help */
251                         usage2 (_("Unknown argument"), optarg);
252                 }
253         }
255         c = optind;
257         while ( argc > c ) {
259                 if (strlen(db_host) == 0)
260                         if (is_host (argv[c])) {
261                                 db_host = argv[c++];
262                         }
263                         else {
264                                 usage2 (_("Invalid hostname/address"), optarg);
265                         }
266                 else if (strlen(db_user) == 0)
267                         db_user = argv[c++];
268                 else if (strlen(db_pass) == 0)
269                         db_pass = argv[c++];
270                 else if (strlen(db) == 0)
271                         db = argv[c++];
272                 else if (is_intnonneg (argv[c]))
273                         db_port = atoi (argv[c++]);
274                 else
275                         break;
276         }
278         return validate_arguments ();
282 int
283 validate_arguments (void)
285         if (db_user == NULL)
286                 db_user = strdup("");
288         if (db_host == NULL)
289                 db_host = strdup("");
291         if (db_pass == NULL)
292                 db_pass == strdup("");
294         if (db == NULL)
295                 db = strdup("");
297         return OK;
301 void
302 print_help (void)
304         char *myport;
305         asprintf (&myport, "%d", MYSQL_PORT);
307         print_revision (progname, revision);
309         printf (_(COPYRIGHT), copyright, email);
311         printf (_("This program tests connections to a mysql server\n"));
313         print_usage ();
315         printf (_(UT_HELP_VRSN));
317         printf (_(UT_HOST_PORT), 'P', myport);
319         printf (_("\
320  -d, --database=STRING\n\
321    Check database with indicated name\n\
322  -u, --username=STRING\n\
323    Connect using the indicated username\n\
324  -p, --password=STRING\n\
325    Use the indicated password to authenticate the connection\n\
326    ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
327    Your clear-text password will be visible as a process table entry\n\
328  -S, --check-slave\n\
329    Check if the slave thread is running properly.\n"));
331         printf (_("\n\
332 There are no required arguments. By default, the local database with\n\
333 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
335         printf (_(UT_SUPPORT));
339 void
340 print_usage (void)
342         printf ("\
343 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",
344                 progname);