Revert "Trying out a patch for IRIX 11"
[monitoring-plugins.git] / plugins / check_mysql.c
bloba1a63769f2386c46a0fc7a232466a75a17e09e71
1 /*****************************************************************************
2 *
3 * Nagios check_mysql plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
7 * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
8 * Copyright (c) 1999-2007 Nagios Plugins Development Team
9 *
10 * Description:
12 * This file contains the check_mysql plugin
14 * This program tests connections to a mysql server
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 *****************************************************************************/
33 const char *progname = "check_mysql";
34 const char *copyright = "1999-2007";
35 const char *email = "nagiosplug-devel@lists.sourceforge.net";
37 #define SLAVERESULTSIZE 70
39 #include "common.h"
40 #include "utils.h"
41 #include "utils_base.h"
42 #include "netutils.h"
44 #include <mysql.h>
45 #include <errmsg.h>
47 char *db_user = NULL;
48 char *db_host = NULL;
49 char *db_socket = NULL;
50 char *db_pass = NULL;
51 char *db = NULL;
52 unsigned int db_port = MYSQL_PORT;
53 int check_slave = 0, warn_sec = 0, crit_sec = 0;
54 int verbose = 0;
56 thresholds *my_threshold = NULL;
58 int process_arguments (int, char **);
59 int validate_arguments (void);
60 void print_help (void);
61 void print_usage (void);
63 int
64 main (int argc, char **argv)
67 MYSQL mysql;
68 MYSQL_RES *res;
69 MYSQL_ROW row;
71 /* should be status */
73 char *result = NULL;
74 char *error = NULL;
75 char slaveresult[SLAVERESULTSIZE];
77 setlocale (LC_ALL, "");
78 bindtextdomain (PACKAGE, LOCALEDIR);
79 textdomain (PACKAGE);
81 /* Parse extra opts if any */
82 argv=np_extra_opts (&argc, argv, progname);
84 if (process_arguments (argc, argv) == ERROR)
85 usage4 (_("Could not parse arguments"));
87 /* initialize mysql */
88 mysql_init (&mysql);
90 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
92 /* establish a connection to the server and error checking */
93 if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
94 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
95 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
96 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
97 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
98 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
99 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
100 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
101 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
102 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
103 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
104 else
105 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
108 /* get the server stats */
109 result = strdup (mysql_stat (&mysql));
111 /* error checking once more */
112 if (mysql_error (&mysql)) {
113 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
114 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
115 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
116 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
117 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
118 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
121 if(check_slave) {
122 /* check the slave status */
123 if (mysql_query (&mysql, "show slave status") != 0) {
124 error = strdup(mysql_error(&mysql));
125 mysql_close (&mysql);
126 die (STATE_CRITICAL, _("slave query error: %s\n"), error);
129 /* store the result */
130 if ( (res = mysql_store_result (&mysql)) == NULL) {
131 error = strdup(mysql_error(&mysql));
132 mysql_close (&mysql);
133 die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
136 /* Check there is some data */
137 if (mysql_num_rows(res) == 0) {
138 mysql_close(&mysql);
139 die (STATE_WARNING, "%s\n", _("No slaves defined"));
142 /* fetch the first row */
143 if ( (row = mysql_fetch_row (res)) == NULL) {
144 error = strdup(mysql_error(&mysql));
145 mysql_free_result (res);
146 mysql_close (&mysql);
147 die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
150 if (mysql_field_count (&mysql) == 12) {
151 /* mysql 3.23.x */
152 snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
153 if (strcmp (row[6], "Yes") != 0) {
154 mysql_free_result (res);
155 mysql_close (&mysql);
156 die (STATE_CRITICAL, "%s\n", slaveresult);
159 } else {
160 /* mysql 4.x.x */
161 int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
162 MYSQL_FIELD* fields;
164 num_fields = mysql_num_fields(res);
165 fields = mysql_fetch_fields(res);
166 for(i = 0; i < num_fields; i++) {
167 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
168 slave_io_field = i;
169 continue;
171 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
172 slave_sql_field = i;
173 continue;
175 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
176 seconds_behind_field = i;
177 continue;
180 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
181 mysql_free_result (res);
182 mysql_close (&mysql);
183 die (STATE_CRITICAL, "Slave status unavailable\n");
186 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], row[seconds_behind_field]);
187 if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
188 mysql_free_result (res);
189 mysql_close (&mysql);
190 die (STATE_CRITICAL, "%s\n", slaveresult);
193 if (verbose >=3) {
194 if (seconds_behind_field == -1) {
195 printf("seconds_behind_field not found\n");
196 } else {
197 printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
201 if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
202 double value = atof(row[seconds_behind_field]);
203 int status;
205 status = get_status(value, my_threshold);
207 if (status == STATE_WARNING) {
208 printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
209 exit(STATE_WARNING);
210 } else if (status == STATE_CRITICAL) {
211 printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
212 exit(STATE_CRITICAL);
217 /* free the result */
218 mysql_free_result (res);
221 /* close the connection */
222 mysql_close (&mysql);
224 /* print out the result of stats */
225 if (check_slave) {
226 printf ("%s %s\n", result, slaveresult);
227 } else {
228 printf ("%s\n", result);
231 return STATE_OK;
235 /* process command-line arguments */
237 process_arguments (int argc, char **argv)
239 int c;
240 char *warning = NULL;
241 char *critical = NULL;
243 int option = 0;
244 static struct option longopts[] = {
245 {"hostname", required_argument, 0, 'H'},
246 {"socket", required_argument, 0, 's'},
247 {"database", required_argument, 0, 'd'},
248 {"username", required_argument, 0, 'u'},
249 {"password", required_argument, 0, 'p'},
250 {"port", required_argument, 0, 'P'},
251 {"critical", required_argument, 0, 'c'},
252 {"warning", required_argument, 0, 'w'},
253 {"check-slave", no_argument, 0, 'S'},
254 {"verbose", no_argument, 0, 'v'},
255 {"version", no_argument, 0, 'V'},
256 {"help", no_argument, 0, 'h'},
257 {0, 0, 0, 0}
260 if (argc < 1)
261 return ERROR;
263 while (1) {
264 c = getopt_long (argc, argv, "hvVSP:p:u:d:H:s:c:w:", longopts, &option);
266 if (c == -1 || c == EOF)
267 break;
269 switch (c) {
270 case 'H': /* hostname */
271 if (is_host (optarg)) {
272 db_host = optarg;
274 else {
275 usage2 (_("Invalid hostname/address"), optarg);
277 break;
278 case 's': /* socket */
279 db_socket = optarg;
280 break;
281 case 'd': /* database */
282 db = optarg;
283 break;
284 case 'u': /* username */
285 db_user = optarg;
286 break;
287 case 'p': /* authentication information: password */
288 db_pass = strdup(optarg);
290 /* Delete the password from process list */
291 while (*optarg != '\0') {
292 *optarg = 'X';
293 optarg++;
295 break;
296 case 'P': /* critical time threshold */
297 db_port = atoi (optarg);
298 break;
299 case 'S':
300 check_slave = 1; /* check-slave */
301 break;
302 case 'w':
303 warning = optarg;
304 break;
305 case 'c':
306 critical = optarg;
307 break;
308 case 'V': /* version */
309 print_revision (progname, NP_VERSION);
310 exit (STATE_OK);
311 case 'h': /* help */
312 print_help ();
313 exit (STATE_OK);
314 case 'v':
315 verbose++;
316 break;
317 case '?': /* help */
318 usage5 ();
322 c = optind;
324 set_thresholds(&my_threshold, warning, critical);
326 while ( argc > c ) {
328 if (db_host == NULL)
329 if (is_host (argv[c])) {
330 db_host = argv[c++];
332 else {
333 usage2 (_("Invalid hostname/address"), argv[c]);
335 else if (db_user == NULL)
336 db_user = argv[c++];
337 else if (db_pass == NULL)
338 db_pass = argv[c++];
339 else if (db == NULL)
340 db = argv[c++];
341 else if (is_intnonneg (argv[c]))
342 db_port = atoi (argv[c++]);
343 else
344 break;
347 return validate_arguments ();
352 validate_arguments (void)
354 if (db_user == NULL)
355 db_user = strdup("");
357 if (db_host == NULL)
358 db_host = strdup("");
360 if (db_pass == NULL)
361 db_pass = strdup("");
363 if (db == NULL)
364 db = strdup("");
366 return OK;
370 void
371 print_help (void)
373 char *myport;
374 asprintf (&myport, "%d", MYSQL_PORT);
376 print_revision (progname, NP_VERSION);
378 printf (_(COPYRIGHT), copyright, email);
380 printf ("%s\n", _("This program tests connections to a mysql server"));
382 printf ("\n\n");
384 print_usage ();
386 printf (_(UT_HELP_VRSN));
387 printf (_(UT_EXTRA_OPTS));
389 printf (_(UT_HOST_PORT), 'P', myport);
390 printf (" %s\n", "-s, --socket=STRING");
391 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
393 printf (" %s\n", "-d, --database=STRING");
394 printf (" %s\n", _("Check database with indicated name"));
395 printf (" %s\n", "-u, --username=STRING");
396 printf (" %s\n", _("Connect using the indicated username"));
397 printf (" %s\n", "-p, --password=STRING");
398 printf (" %s\n", _("Use the indicated password to authenticate the connection"));
399 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
400 printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
401 printf (" %s\n", "-S, --check-slave");
402 printf (" %s\n", _("Check if the slave thread is running properly."));
403 printf (" %s\n", "-w, --warning");
404 printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds"));
405 printf (" %s\n", _("behind master"));
406 printf (" %s\n", "-c, --critical");
407 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
408 printf (" %s\n", _("behind master"));
410 printf ("\n");
411 printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
412 printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
413 printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
415 #ifdef NP_EXTRA_OPTS
416 printf ("\n");
417 printf ("%s\n", _("Notes:"));
418 printf (_(UT_EXTRA_OPTS_NOTES));
419 #endif
421 printf (_(UT_SUPPORT));
425 void
426 print_usage (void)
428 printf (_("Usage:"));
429 printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
430 printf (" [-u user] [-p password] [-S]\n");