Code

various fixes for localization
[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         /* 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;
154 /* process command-line arguments */
155 int
156 process_arguments (int argc, char **argv)
158         int c;
160         int option = 0;
161         static struct option longopts[] = {
162                 {"hostname", required_argument, 0, 'H'},
163                 {"database", required_argument, 0, 'd'},
164                 {"username", required_argument, 0, 'u'},
165                 {"password", required_argument, 0, 'p'},
166                 {"port", required_argument, 0, 'P'},
167                 {"check-slave", no_argument, 0, 'S'},
168                 {"verbose", no_argument, 0, 'v'},
169                 {"version", no_argument, 0, 'V'},
170                 {"help", no_argument, 0, 'h'},
171                 {0, 0, 0, 0}
172         };
174         if (argc < 1)
175                 return ERROR;
177         while (1) {
178                 c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option);
180                 if (c == -1 || c == EOF)
181                         break;
183                 switch (c) {
184                 case 'H':                                                                       /* hostname */
185                         if (is_host (optarg)) {
186                                 db_host = optarg;
187                         }
188                         else {
189                                 usage2 (_("Invalid hostname/address"), optarg);
190                         }
191                         break;
192                 case 'd':                                                                       /* hostname */
193                         db = optarg;
194                         break;
195                 case 'u':                                                                       /* username */
196                         db_user = optarg;
197                         break;
198                 case 'p':                                                                       /* authentication information: password */
199                         db_pass = optarg;
200                         break;
201                 case 'P':                                                                       /* critical time threshold */
202                         db_port = atoi (optarg);
203                         break;
204                 case 'S':
205                         check_slave = 1;                                                        /* check-slave */
206                         break;
207                 case 'V':                                                                       /* version */
208                         print_revision (progname, revision);
209                         exit (STATE_OK);
210                 case 'h':                                                                       /* help */
211                         print_help ();
212                         exit (STATE_OK);
213                 case '?':                                                                       /* help */
214                         usage2 (_("Unknown argument"), optarg);
215                 }
216         }
218         c = optind;
220         while ( argc > c ) {
222                 if (strlen(db_host) == 0)
223                         if (is_host (argv[c])) {
224                                 db_host = argv[c++];
225                         }
226                         else {
227                                 usage2 (_("Invalid hostname/address"), optarg);
228                         }
229                 else if (strlen(db_user) == 0)
230                         db_user = argv[c++];
231                 else if (strlen(db_pass) == 0)
232                         db_pass = argv[c++];
233                 else if (strlen(db) == 0)
234                         db = argv[c++];
235                 else if (is_intnonneg (argv[c]))
236                         db_port = atoi (argv[c++]);
237                 else
238                         break;
239         }
241         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;
264 void
265 print_help (void)
267         char *myport;
268         asprintf (&myport, "%d", MYSQL_PORT);
270         print_revision (progname, revision);
272         printf (_(COPYRIGHT), copyright, email);
274         printf (_("This program tests connections to a mysql server\n"));
276         print_usage ();
278         printf (_(UT_HELP_VRSN));
280         printf (_(UT_HOST_PORT), 'P', myport);
282         printf (_("\
283  -d, --database=STRING\n\
284    Check database with indicated name\n\
285  -u, --username=STRING\n\
286    Connect using the indicated username\n\
287  -p, --password=STRING\n\
288    Use the indicated password to authenticate the connection\n\
289    ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
290    Your clear-text password will be visible as a process table entry\n\
291  -S, --check-slave\n\
292    Check if the slave thread is running properly.\n"));
294         printf (_("\n\
295 There are no required arguments. By default, the local database with\n\
296 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
298         printf (_(UT_SUPPORT));
302 void
303 print_usage (void)
305         printf ("\
306 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",
307                 progname);