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);
42 int
43 main (int argc, char **argv)
44 {
46 MYSQL mysql;
47 MYSQL_RES *res;
48 MYSQL_ROW row;
49 char *result = NULL;
50 char slaveresult[SLAVERESULTSIZE];
52 setlocale (LC_ALL, "");
53 bindtextdomain (PACKAGE, LOCALEDIR);
54 textdomain (PACKAGE);
56 if (process_arguments (argc, argv) != OK)
57 usage (_("Incorrect arguments supplied\n"));
59 /* initialize mysql */
60 mysql_init (&mysql);
62 /* establish a connection to the server and error checking */
63 if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
64 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
65 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
66 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
67 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
68 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
69 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
70 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
71 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
72 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
73 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
74 else
75 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
76 }
78 /* get the server stats */
79 result = strdup (mysql_stat (&mysql));
81 /* error checking once more */
82 if (mysql_error (&mysql)) {
83 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
84 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
85 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
86 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
87 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
88 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
89 }
91 if(check_slave) {
92 /* check the slave status */
93 if (mysql_query (&mysql, "show slave status") != 0) {
94 mysql_close (&mysql);
95 die (STATE_CRITICAL, "slave query error: %s\n", mysql_error (&mysql));
96 }
98 /* store the result */
99 if ( (res = mysql_store_result (&mysql)) == NULL) {
100 mysql_close (&mysql);
101 die (STATE_CRITICAL, "slave store_result error: %s\n", mysql_error (&mysql));
102 }
104 /* fetch the first row */
105 if ( (row = mysql_fetch_row (res)) == NULL) {
106 mysql_free_result (res);
107 mysql_close (&mysql);
108 die (STATE_CRITICAL, "slave fetch row error: %s\n", mysql_error (&mysql));
109 }
111 if (mysql_field_count (&mysql) == 12) {
112 /* mysql 3.23.x */
113 snprintf (slaveresult, SLAVERESULTSIZE, "Slave running: %s", row[6]);
114 if (strcmp (row[6], "Yes") != 0) {
115 mysql_free_result (res);
116 mysql_close (&mysql);
117 die (STATE_CRITICAL, "%s\n", slaveresult);
118 }
120 } else {
121 /* mysql 4.x.x */
122 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[9], row[10]);
123 if (strcmp (row[9], "Yes") != 0 || strcmp (row[10], "Yes") != 0) {
124 mysql_free_result (res);
125 mysql_close (&mysql);
126 die (STATE_CRITICAL, "%s\n", slaveresult);
127 }
128 }
130 /* free the result */
131 mysql_free_result (res);
132 }
134 /* close the connection */
135 mysql_close (&mysql);
137 /* print out the result of stats */
138 if (check_slave) {
139 printf ("%s %s\n", result, slaveresult);
140 } else {
141 printf ("%s\n", result);
142 }
144 return STATE_OK;
145 }
151 /* process command-line arguments */
152 int
153 process_arguments (int argc, char **argv)
154 {
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 usage (_("Invalid host name\n"));
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 usage (_("Invalid argument\n"));
212 }
213 }
215 c = optind;
217 while ( argc > c ) {
219 if (strlen(db_host) == 0)
220 if (is_host (argv[c])) {
221 db_host = argv[c++];
222 }
223 else {
224 usage ("Invalid host name");
225 }
226 else if (strlen(db_user) == 0)
227 db_user = argv[c++];
228 else if (strlen(db_pass) == 0)
229 db_pass = argv[c++];
230 else if (strlen(db) == 0)
231 db = argv[c++];
232 else if (is_intnonneg (argv[c]))
233 db_port = atoi (argv[c++]);
234 else
235 break;
236 }
238 return validate_arguments ();
239 }
245 int
246 validate_arguments (void)
247 {
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;
261 }
267 \f
268 void
269 print_help (void)
270 {
271 char *myport;
272 asprintf (&myport, "%d", MYSQL_PORT);
274 print_revision (progname, revision);
276 printf (_(COPYRIGHT), copyright, email);
278 printf (_("This program tests connections to a mysql server\n"));
280 print_usage ();
282 printf (_(UT_HELP_VRSN));
284 printf (_(UT_HOST_PORT), 'P', myport);
286 printf (_("\
287 -d, --database=STRING\n\
288 Check database with indicated name\n\
289 -u, --username=STRING\n\
290 Connect using the indicated username\n\
291 -p, --password=STRING\n\
292 Use the indicated password to authenticate the connection\n\
293 ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
294 Your clear-text password will be visible as a process table entry\n\
295 -S, --check-slave\n\
296 Check if the slave thread is running properly.\n"));
298 printf (_("\n\
299 There are no required arguments. By default, the local database with\n\
300 a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
302 printf (_(UT_SUPPORT));
303 }
308 void
309 print_usage (void)
310 {
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);
315 }