Code

changed Error: by CRITICAL -
[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-2002";
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"
27 #include <mysql/mysql.h>
28 #include <mysql/errmsg.h>
30 char *db_user = NULL;
31 char *db_host = NULL;
32 char *db_pass = NULL;
33 char *db = NULL;
34 unsigned int db_port = MYSQL_PORT;
35 int check_slave = 0;
37 int process_arguments (int, char **);
38 int validate_arguments (void);
39 void print_help (void);
40 void print_usage (void);
44 int
45 main (int argc, char **argv)
46 {
48         MYSQL mysql;
49         MYSQL_RES *res;
50         MYSQL_ROW row;
51         char *result = NULL;
52         char slaveresult[SLAVERESULTSIZE];
54         setlocale (LC_ALL, "");
55         bindtextdomain (PACKAGE, LOCALEDIR);
56         textdomain (PACKAGE);
58         if (process_arguments (argc, argv) != OK)
59                 usage (_("check_mysql: could not parse arguments\n"));
61         /* initialize mysql  */
62         mysql_init (&mysql);
64         /* establish a connection to the server and error checking */
65         if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
66                 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
67                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
68                 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
69                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
70                 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
71                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
72                 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
73                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
74                 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
75                         die (STATE_WARNING, "%s\n", mysql_error (&mysql));
76                 else
77                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
78         }
80         /* get the server stats */
81         result = strdup (mysql_stat (&mysql));
83         /* error checking once more */
84         if (mysql_error (&mysql)) {
85                 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
86                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
87                 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
88                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
89                 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
90                         die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
91         }
93         if(check_slave) {
94                 /* check the slave status */
95                 if (mysql_query (&mysql, "show slave status") != 0) {
96                         mysql_close (&mysql);
97                         die (STATE_CRITICAL, "slave query error: %s\n", mysql_error (&mysql));
98                 }
100                 /* store the result */
101                 if ( (res = mysql_store_result (&mysql)) == NULL) {
102                         mysql_close (&mysql);
103                         die (STATE_CRITICAL, "slave store_result error: %s\n", mysql_error (&mysql));
104                 }
106                 /* fetch the first row */
107                 if ( (row = mysql_fetch_row (res)) == NULL) {
108                         mysql_free_result (res);
109                         mysql_close (&mysql);
110                         die (STATE_CRITICAL, "slave fetch row error: %s\n", mysql_error (&mysql));
111                 }
113                 if (mysql_field_count (&mysql) == 12) {
114                         /* mysql 3.23.x */
115                         snprintf (slaveresult, SLAVERESULTSIZE, "Slave running: %s", row[6]);
116                         if (strcmp (row[6], "Yes") != 0) {
117                                 mysql_free_result (res);
118                                 mysql_close (&mysql);
119                                 die (STATE_CRITICAL, "%s\n", slaveresult);
120                         }
122                 } else {
123                         /* mysql 4.x.x */
124                         snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[9], row[10]);
125                         if (strcmp (row[9], "Yes") != 0 || strcmp (row[10], "Yes") != 0) {
126                                 mysql_free_result (res);
127                                 mysql_close (&mysql);
128                                 die (STATE_CRITICAL, "%s\n", slaveresult);
129                         }
130                 }
132                 /* free the result */
133                 mysql_free_result (res);
134         }
136         /* close the connection */
137         mysql_close (&mysql);
139         /* print out the result of stats */
140         if (check_slave) {
141                 printf ("%s %s\n", result, slaveresult);
142         } else {
143                 printf ("%s\n", result);
144         }
146         return STATE_OK;
151 /* process command-line arguments */
152 int
153 process_arguments (int argc, char **argv)
155         int c;
157         int option = 0;
158         static struct option longopts[] = {
159                 {"hostname", required_argument, 0, 'H'},
160                 {"database", required_argument, 0, 'd'},
161                 {"username", required_argument, 0, 'u'},
162                 {"password", required_argument, 0, 'p'},
163                 {"port", required_argument, 0, 'P'},
164                 {"check-slave", no_argument, 0, 'S'},
165                 {"verbose", no_argument, 0, 'v'},
166                 {"version", no_argument, 0, 'V'},
167                 {"help", no_argument, 0, 'h'},
168                 {0, 0, 0, 0}
169         };
171         if (argc < 1)
172                 return ERROR;
174         while (1) {
175                 c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option);
177                 if (c == -1 || c == EOF)
178                         break;
180                 switch (c) {
181                 case 'H':                                                                       /* hostname */
182                         if (is_host (optarg)) {
183                                 db_host = optarg;
184                         }
185                         else {
186                                 usage2 (_("Invalid hostname/address"), optarg);
187                         }
188                         break;
189                 case 'd':                                                                       /* hostname */
190                         db = optarg;
191                         break;
192                 case 'u':                                                                       /* username */
193                         db_user = optarg;
194                         break;
195                 case 'p':                                                                       /* authentication information: password */
196                         db_pass = optarg;
197                         break;
198                 case 'P':                                                                       /* critical time threshold */
199                         db_port = atoi (optarg);
200                         break;
201                 case 'S':
202                         check_slave = 1;                                                        /* check-slave */
203                         break;
204                 case 'V':                                                                       /* version */
205                         print_revision (progname, revision);
206                         exit (STATE_OK);
207                 case 'h':                                                                       /* help */
208                         print_help ();
209                         exit (STATE_OK);
210                 case '?':                                                                       /* help */
211                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
212                         print_usage ();
213                         exit (STATE_UNKNOWN);
214                 }
215         }
217         c = optind;
219         while ( argc > c ) {
221                 if (strlen(db_host) == 0)
222                         if (is_host (argv[c])) {
223                                 db_host = argv[c++];
224                         }
225                         else {
226                                 usage2 (_("Invalid hostname/address"), optarg);
227                         }
228                 else if (strlen(db_user) == 0)
229                         db_user = argv[c++];
230                 else if (strlen(db_pass) == 0)
231                         db_pass = argv[c++];
232                 else if (strlen(db) == 0)
233                         db = argv[c++];
234                 else if (is_intnonneg (argv[c]))
235                         db_port = atoi (argv[c++]);
236                 else
237                         break;
238         }
240         return validate_arguments ();
245 int
246 validate_arguments (void)
248         if (db_user == NULL)
249                 db_user = strdup("");
251         if (db_host == NULL)
252                 db_host = strdup("");
254         if (db_pass == NULL)
255                 db_pass == strdup("");
257         if (db == NULL)
258                 db = strdup("");
260         return OK;
265 void
266 print_help (void)
268         char *myport;
269         asprintf (&myport, "%d", MYSQL_PORT);
271         print_revision (progname, revision);
273         printf (_(COPYRIGHT), copyright, email);
275         printf (_("This program tests connections to a mysql server\n"));
277         print_usage ();
279         printf (_(UT_HELP_VRSN));
281         printf (_(UT_HOST_PORT), 'P', myport);
283         printf (_("\
284  -d, --database=STRING\n\
285    Check database with indicated name\n\
286  -u, --username=STRING\n\
287    Connect using the indicated username\n\
288  -p, --password=STRING\n\
289    Use the indicated password to authenticate the connection\n\
290    ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
291    Your clear-text password will be visible as a process table entry\n\
292  -S, --check-slave\n\
293    Check if the slave thread is running properly.\n"));
295         printf (_("\n\
296 There are no required arguments. By default, the local database with\n\
297 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
299         printf (_(UT_SUPPORT));
304 void
305 print_usage (void)
307         printf (_("\
308 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n"),
309                 progname);
310         printf (_(UT_HLP_VRS), progname, progname);