Code

5863e1a764a5195a362ea55485e457f4bcfd53af
[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 slaveresult[SLAVERESULTSIZE];
58         setlocale (LC_ALL, "");
59         bindtextdomain (PACKAGE, LOCALEDIR);
60         textdomain (PACKAGE);
62         if (process_arguments (argc, argv) != TRUE)
63                 usage4 (_("Could not parse arguments"));
65         /* initialize mysql  */
66         mysql_init (&mysql);
68         /* establish a connection to the server and error checking */
69         if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
70                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
71                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
72                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
73                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
74                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
75                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
76                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
77                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
78                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
79                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
80                 else
81                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
82         }
84         /* get the server stats */
85         result = strdup (mysql_stat (&mysql));
87         /* error checking once more */
88         if (mysql_error (&mysql)) {
89                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
90                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
91                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
92                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
93                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
94                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
95         }
97         if(check_slave) {
98                 /* check the slave status */
99                 if (mysql_query (&mysql, "show slave status") != 0) {
100                         mysql_close (&mysql);
101                         die (STATE_CRITICAL, "slave query error: %s\n", mysql_error (&mysql));
102                 }
104                 /* store the result */
105                 if ( (res = mysql_store_result (&mysql)) == NULL) {
106                         mysql_close (&mysql);
107                         die (STATE_CRITICAL, "slave store_result error: %s\n", mysql_error (&mysql));
108                 }
110                 /* fetch the first row */
111                 if ( (row = mysql_fetch_row (res)) == NULL) {
112                         mysql_free_result (res);
113                         mysql_close (&mysql);
114                         die (STATE_CRITICAL, "slave fetch row error: %s\n", mysql_error (&mysql));
115                 }
117                 if (mysql_field_count (&mysql) == 12) {
118                         /* mysql 3.23.x */
119                         snprintf (slaveresult, SLAVERESULTSIZE, "Slave running: %s", row[6]);
120                         if (strcmp (row[6], "Yes") != 0) {
121                                 mysql_free_result (res);
122                                 mysql_close (&mysql);
123                                 die (STATE_CRITICAL, "%s\n", slaveresult);
124                         }
126                 } else {
127                         /* mysql 4.x.x */
128                         snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[9], row[10]);
129                         if (strcmp (row[9], "Yes") != 0 || strcmp (row[10], "Yes") != 0) {
130                                 mysql_free_result (res);
131                                 mysql_close (&mysql);
132                                 die (STATE_CRITICAL, "%s\n", slaveresult);
133                         }
134                 }
136                 /* free the result */
137                 mysql_free_result (res);
138         }
140         /* close the connection */
141         mysql_close (&mysql);
143         /* print out the result of stats */
144         if (check_slave) {
145                 printf ("%s %s\n", result, slaveresult);
146         } else {
147                 printf ("%s\n", result);
148         }
150         return STATE_OK;
155 /* process command-line arguments */
156 int
157 process_arguments (int argc, char **argv)
159         int c;
161         int option = 0;
162         static struct option longopts[] = {
163                 {"hostname", required_argument, 0, 'H'},
164                 {"database", required_argument, 0, 'd'},
165                 {"username", required_argument, 0, 'u'},
166                 {"password", required_argument, 0, 'p'},
167                 {"port", required_argument, 0, 'P'},
168                 {"check-slave", no_argument, 0, 'S'},
169                 {"verbose", no_argument, 0, 'v'},
170                 {"version", no_argument, 0, 'V'},
171                 {"help", no_argument, 0, 'h'},
172                 {0, 0, 0, 0}
173         };
175         if (argc < 1)
176                 return ERROR;
178         while (1) {
179                 c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option);
181                 if (c == -1 || c == EOF)
182                         break;
184                 switch (c) {
185                 case 'H':                                                                       /* hostname */
186                         if (is_host (optarg)) {
187                                 db_host = optarg;
188                         }
189                         else {
190                                 usage2 (_("Invalid hostname/address"), optarg);
191                         }
192                         break;
193                 case 'd':                                                                       /* hostname */
194                         db = optarg;
195                         break;
196                 case 'u':                                                                       /* username */
197                         db_user = optarg;
198                         break;
199                 case 'p':                                                                       /* authentication information: password */
200                         db_pass = optarg;
201                         break;
202                 case 'P':                                                                       /* critical time threshold */
203                         db_port = atoi (optarg);
204                         break;
205                 case 'S':
206                         check_slave = 1;                                                        /* check-slave */
207                         break;
208                 case 'V':                                                                       /* version */
209                         print_revision (progname, revision);
210                         exit (STATE_OK);
211                 case 'h':                                                                       /* help */
212                         print_help ();
213                         exit (STATE_OK);
214                 case '?':                                                                       /* help */
215                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
216                         print_usage ();
217                         exit (STATE_UNKNOWN);
218                 }
219         }
221         c = optind;
223         while ( argc > c ) {
225                 if (strlen(db_host) == 0)
226                         if (is_host (argv[c])) {
227                                 db_host = argv[c++];
228                         }
229                         else {
230                                 usage2 (_("Invalid hostname/address"), optarg);
231                         }
232                 else if (strlen(db_user) == 0)
233                         db_user = argv[c++];
234                 else if (strlen(db_pass) == 0)
235                         db_pass = argv[c++];
236                 else if (strlen(db) == 0)
237                         db = argv[c++];
238                 else if (is_intnonneg (argv[c]))
239                         db_port = atoi (argv[c++]);
240                 else
241                         break;
242         }
244         return validate_arguments ();
249 int
250 validate_arguments (void)
252         if (db_user == NULL)
253                 db_user = strdup("");
255         if (db_host == NULL)
256                 db_host = strdup("");
258         if (db_pass == NULL)
259                 db_pass == strdup("");
261         if (db == NULL)
262                 db = strdup("");
264         return OK;
269 void
270 print_help (void)
272         char *myport;
273         asprintf (&myport, "%d", MYSQL_PORT);
275         print_revision (progname, revision);
277         printf (_(COPYRIGHT), copyright, email);
279         printf (_("This program tests connections to a mysql server\n"));
281         print_usage ();
283         printf (_(UT_HELP_VRSN));
285         printf (_(UT_HOST_PORT), 'P', myport);
287         printf (_("\
288  -d, --database=STRING\n\
289    Check database with indicated name\n\
290  -u, --username=STRING\n\
291    Connect using the indicated username\n\
292  -p, --password=STRING\n\
293    Use the indicated password to authenticate the connection\n\
294    ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
295    Your clear-text password will be visible as a process table entry\n\
296  -S, --check-slave\n\
297    Check if the slave thread is running properly.\n"));
299         printf (_("\n\
300 There are no required arguments. By default, the local database with\n\
301 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
303         printf (_(UT_SUPPORT));
308 void
309 print_usage (void)
311         printf ("\
312 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",
313                 progname);
314         printf (UT_HLP_VRS, progname, progname);