Code

No floorf on Solaris 9 (Jon Vandegrift - 1374705)
[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) == ERROR)
63                 usage4 (_("Could not parse arguments"));
65         /* initialize mysql  */
66         mysql_init (&mysql);
68         mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
70         /* establish a connection to the server and error checking */
71         if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
72                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
73                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
74                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
75                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
76                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
77                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
78                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
79                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
80                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
81                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
82                 else
83                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
84         }
86         /* get the server stats */
87         result = strdup (mysql_stat (&mysql));
89         /* error checking once more */
90         if (mysql_error (&mysql)) {
91                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
92                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
93                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
94                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
95                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
96                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
97         }
99         if(check_slave) {
100                 /* check the slave status */
101                 if (mysql_query (&mysql, "show slave status") != 0) {
102                         mysql_close (&mysql);
103                         die (STATE_CRITICAL, _("slave query error: %s\n"), mysql_error (&mysql));
104                 }
106                 /* store the result */
107                 if ( (res = mysql_store_result (&mysql)) == NULL) {
108                         mysql_close (&mysql);
109                         die (STATE_CRITICAL, _("slave store_result error: %s\n"), mysql_error (&mysql));
110                 }
112                 /* fetch the first row */
113                 if ( (row = mysql_fetch_row (res)) == NULL) {
114                         mysql_free_result (res);
115                         mysql_close (&mysql);
116                         die (STATE_CRITICAL, _("slave fetch row error: %s\n"), mysql_error (&mysql));
117                 }
119                 if (mysql_field_count (&mysql) == 12) {
120                         /* mysql 3.23.x */
121                         snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
122                         if (strcmp (row[6], "Yes") != 0) {
123                                 mysql_free_result (res);
124                                 mysql_close (&mysql);
125                                 die (STATE_CRITICAL, "%s\n", slaveresult);
126                         }
128                 } else {
129                         /* mysql 4.x.x */
130                         int slave_io_field = -1 , slave_sql_field = -1, i, num_fields;
131                         MYSQL_FIELD* fields;
133                         num_fields = mysql_num_fields(res);
134                         fields = mysql_fetch_fields(res);
135                         for(i = 0; i < num_fields; i++)
136                         {
137                                 if (0 == strcmp(fields[i].name, "Slave_IO_Running"))
138                                 {
139                                         slave_io_field = i;
140                                         continue;
141                                 }
142                                 if (0 == strcmp(fields[i].name, "Slave_SQL_Running"))
143                                 {
144                                         slave_sql_field = i;
145                                         continue;
146                                 }
147                         }
148                         if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0))
149                         {
150                                 mysql_free_result (res);
151                                 mysql_close (&mysql);
152                                 die (STATE_CRITICAL, "Slave status unavailable\n");
153                         }
154                          
155                         snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[slave_io_field], row[slave_sql_field]);
156                         if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
157                                 mysql_free_result (res);
158                                 mysql_close (&mysql);
159                                 die (STATE_CRITICAL, "%s\n", slaveresult);
160                         }
161                 }
163                 /* free the result */
164                 mysql_free_result (res);
165         }
167         /* close the connection */
168         mysql_close (&mysql);
170         /* print out the result of stats */
171         if (check_slave) {
172                 printf ("%s %s\n", result, slaveresult);
173         } else {
174                 printf ("%s\n", result);
175         }
177         return STATE_OK;
181 /* process command-line arguments */
182 int
183 process_arguments (int argc, char **argv)
185         int c;
187         int option = 0;
188         static struct option longopts[] = {
189                 {"hostname", required_argument, 0, 'H'},
190                 {"database", required_argument, 0, 'd'},
191                 {"username", required_argument, 0, 'u'},
192                 {"password", required_argument, 0, 'p'},
193                 {"port", required_argument, 0, 'P'},
194                 {"check-slave", no_argument, 0, 'S'},
195                 {"verbose", no_argument, 0, 'v'},
196                 {"version", no_argument, 0, 'V'},
197                 {"help", no_argument, 0, 'h'},
198                 {0, 0, 0, 0}
199         };
201         if (argc < 1)
202                 return ERROR;
204         while (1) {
205                 c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option);
207                 if (c == -1 || c == EOF)
208                         break;
210                 switch (c) {
211                 case 'H':                                                                       /* hostname */
212                         if (is_host (optarg)) {
213                                 db_host = optarg;
214                         }
215                         else {
216                                 usage2 (_("Invalid hostname/address"), optarg);
217                         }
218                         break;
219                 case 'd':                                                                       /* hostname */
220                         db = optarg;
221                         break;
222                 case 'u':                                                                       /* username */
223                         db_user = optarg;
224                         break;
225                 case 'p':                                                                       /* authentication information: password */
226                         db_pass = optarg;
227                         break;
228                 case 'P':                                                                       /* critical time threshold */
229                         db_port = atoi (optarg);
230                         break;
231                 case 'S':
232                         check_slave = 1;                                                        /* check-slave */
233                         break;
234                 case 'V':                                                                       /* version */
235                         print_revision (progname, revision);
236                         exit (STATE_OK);
237                 case 'h':                                                                       /* help */
238                         print_help ();
239                         exit (STATE_OK);
240                 case '?':                                                                       /* help */
241                         usage2 (_("Unknown argument"), optarg);
242                 }
243         }
245         c = optind;
247         while ( argc > c ) {
249                 if (strlen(db_host) == 0)
250                         if (is_host (argv[c])) {
251                                 db_host = argv[c++];
252                         }
253                         else {
254                                 usage2 (_("Invalid hostname/address"), optarg);
255                         }
256                 else if (strlen(db_user) == 0)
257                         db_user = argv[c++];
258                 else if (strlen(db_pass) == 0)
259                         db_pass = argv[c++];
260                 else if (strlen(db) == 0)
261                         db = argv[c++];
262                 else if (is_intnonneg (argv[c]))
263                         db_port = atoi (argv[c++]);
264                 else
265                         break;
266         }
268         return validate_arguments ();
272 int
273 validate_arguments (void)
275         if (db_user == NULL)
276                 db_user = strdup("");
278         if (db_host == NULL)
279                 db_host = strdup("");
281         if (db_pass == NULL)
282                 db_pass == strdup("");
284         if (db == NULL)
285                 db = strdup("");
287         return OK;
291 void
292 print_help (void)
294         char *myport;
295         asprintf (&myport, "%d", MYSQL_PORT);
297         print_revision (progname, revision);
299         printf (_(COPYRIGHT), copyright, email);
301         printf (_("This program tests connections to a mysql server\n"));
303         print_usage ();
305         printf (_(UT_HELP_VRSN));
307         printf (_(UT_HOST_PORT), 'P', myport);
309         printf (_("\
310  -d, --database=STRING\n\
311    Check database with indicated name\n\
312  -u, --username=STRING\n\
313    Connect using the indicated username\n\
314  -p, --password=STRING\n\
315    Use the indicated password to authenticate the connection\n\
316    ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
317    Your clear-text password will be visible as a process table entry\n\
318  -S, --check-slave\n\
319    Check if the slave thread is running properly.\n"));
321         printf (_("\n\
322 There are no required arguments. By default, the local database with\n\
323 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
325         printf (_(UT_SUPPORT));
329 void
330 print_usage (void)
332         printf ("\
333 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",
334                 progname);