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;
53 /* should be status */
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 if (mysql_field_count (&mysql) == 33) {
127 /* mysql >= 4.1.1 */
128 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[10], row[11]);
129 if (strcmp (row[10], "Yes") != 0 || strcmp (row[11], "Yes") != 0) {
130 mysql_free_result (res);
131 mysql_close (&mysql);
132 die (STATE_CRITICAL, "%s\n", slaveresult);
133 }
134 } else {
135 /* mysql 4.0.x or 4.1.0 */
136 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[9], row[10]);
137 if (strcmp (row[9], "Yes") != 0 || strcmp (row[10], "Yes") != 0) {
138 mysql_free_result (res);
139 mysql_close (&mysql);
140 die (STATE_CRITICAL, "%s\n", slaveresult);
141 }
142 }
144 /* free the result */
145 mysql_free_result (res);
146 }
148 /* close the connection */
149 mysql_close (&mysql);
151 /* print out the result of stats */
152 if (check_slave) {
153 printf ("%s %s\n", result, slaveresult);
154 } else {
155 printf ("%s\n", result);
156 }
158 return STATE_OK;
159 }
162 /* process command-line arguments */
163 int
164 process_arguments (int argc, char **argv)
165 {
166 int c;
168 int option = 0;
169 static struct option longopts[] = {
170 {"hostname", required_argument, 0, 'H'},
171 {"database", required_argument, 0, 'd'},
172 {"username", required_argument, 0, 'u'},
173 {"password", required_argument, 0, 'p'},
174 {"port", required_argument, 0, 'P'},
175 {"check-slave", no_argument, 0, 'S'},
176 {"verbose", no_argument, 0, 'v'},
177 {"version", no_argument, 0, 'V'},
178 {"help", no_argument, 0, 'h'},
179 {0, 0, 0, 0}
180 };
182 if (argc < 1)
183 return ERROR;
185 while (1) {
186 c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option);
188 if (c == -1 || c == EOF)
189 break;
191 switch (c) {
192 case 'H': /* hostname */
193 if (is_host (optarg)) {
194 db_host = optarg;
195 }
196 else {
197 usage2 (_("Invalid hostname/address"), optarg);
198 }
199 break;
200 case 'd': /* hostname */
201 db = optarg;
202 break;
203 case 'u': /* username */
204 db_user = optarg;
205 break;
206 case 'p': /* authentication information: password */
207 db_pass = optarg;
208 break;
209 case 'P': /* critical time threshold */
210 db_port = atoi (optarg);
211 break;
212 case 'S':
213 check_slave = 1; /* check-slave */
214 break;
215 case 'V': /* version */
216 print_revision (progname, revision);
217 exit (STATE_OK);
218 case 'h': /* help */
219 print_help ();
220 exit (STATE_OK);
221 case '?': /* help */
222 usage2 (_("Unknown argument"), optarg);
223 }
224 }
226 c = optind;
228 while ( argc > c ) {
230 if (strlen(db_host) == 0)
231 if (is_host (argv[c])) {
232 db_host = argv[c++];
233 }
234 else {
235 usage2 (_("Invalid hostname/address"), optarg);
236 }
237 else if (strlen(db_user) == 0)
238 db_user = argv[c++];
239 else if (strlen(db_pass) == 0)
240 db_pass = argv[c++];
241 else if (strlen(db) == 0)
242 db = argv[c++];
243 else if (is_intnonneg (argv[c]))
244 db_port = atoi (argv[c++]);
245 else
246 break;
247 }
249 return validate_arguments ();
250 }
253 int
254 validate_arguments (void)
255 {
256 if (db_user == NULL)
257 db_user = strdup("");
259 if (db_host == NULL)
260 db_host = strdup("");
262 if (db_pass == NULL)
263 db_pass == strdup("");
265 if (db == NULL)
266 db = strdup("");
268 return OK;
269 }
272 void
273 print_help (void)
274 {
275 char *myport;
276 asprintf (&myport, "%d", MYSQL_PORT);
278 print_revision (progname, revision);
280 printf (_(COPYRIGHT), copyright, email);
282 printf (_("This program tests connections to a mysql server\n"));
284 print_usage ();
286 printf (_(UT_HELP_VRSN));
288 printf (_(UT_HOST_PORT), 'P', myport);
290 printf (_("\
291 -d, --database=STRING\n\
292 Check database with indicated name\n\
293 -u, --username=STRING\n\
294 Connect using the indicated username\n\
295 -p, --password=STRING\n\
296 Use the indicated password to authenticate the connection\n\
297 ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
298 Your clear-text password will be visible as a process table entry\n\
299 -S, --check-slave\n\
300 Check if the slave thread is running properly.\n"));
302 printf (_("\n\
303 There are no required arguments. By default, the local database with\n\
304 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
306 printf (_(UT_SUPPORT));
307 }
310 void
311 print_usage (void)
312 {
313 printf ("\
314 Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",
315 progname);
316 }