Fix for SSL Versioning when multiple options are used.
[monitoring-plugins.git] / plugins / check_mysql.c
blob3414320643bf787ad3382e1c5b8c2e7cd26c6bcf
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-2011 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-2011";
35 const char *email = "devel@nagios-plugins.org";
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 char *ca_cert = NULL;
53 char *ca_dir = NULL;
54 char *cert = NULL;
55 char *key = NULL;
56 char *ciphers = NULL;
57 bool ssl = false;
58 char *opt_file = NULL;
59 char *opt_group = NULL;
60 unsigned int db_port = MYSQL_PORT;
61 int check_slave = 0, warn_sec = 0, crit_sec = 0;
62 int verbose = 0;
64 static double warning_time = 0;
65 static double critical_time = 0;
67 #define LENGTH_METRIC_UNIT 6
68 static const char *metric_unit[LENGTH_METRIC_UNIT] = {
69 "Open_files",
70 "Open_tables",
71 "Qcache_free_memory",
72 "Qcache_queries_in_cache",
73 "Threads_connected",
74 "Threads_running"
77 #define LENGTH_METRIC_COUNTER 9
78 static const char *metric_counter[LENGTH_METRIC_COUNTER] = {
79 "Connections",
80 "Qcache_hits",
81 "Qcache_inserts",
82 "Qcache_lowmem_prunes",
83 "Qcache_not_cached",
84 "Queries",
85 "Questions",
86 "Table_locks_waited",
87 "Uptime"
90 thresholds *my_threshold = NULL;
92 int process_arguments (int, char **);
93 int validate_arguments (void);
94 void print_help (void);
95 void print_usage (void);
97 int
98 main (int argc, char **argv)
101 MYSQL mysql;
102 MYSQL_RES *res;
103 MYSQL_ROW row;
105 /* should be status */
107 char *result = NULL;
108 char *error = NULL;
109 char slaveresult[SLAVERESULTSIZE];
110 char* perf;
112 perf = strdup ("");
114 setlocale (LC_ALL, "");
115 bindtextdomain (PACKAGE, LOCALEDIR);
116 textdomain (PACKAGE);
118 /* Parse extra opts if any */
119 argv=np_extra_opts (&argc, argv, progname);
121 if (process_arguments (argc, argv) == ERROR)
122 usage4 (_("Could not parse arguments"));
124 /* initialize mysql */
125 mysql_init (&mysql);
127 if (opt_file != NULL)
128 mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file);
130 if (opt_group != NULL)
131 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group);
132 else
133 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
135 if (ssl)
136 mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers);
137 /* establish a connection to the server and error checking */
138 if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
139 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
140 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
141 else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
142 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
143 else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
144 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
145 else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
146 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
147 else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
148 die (STATE_WARNING, "%s\n", mysql_error (&mysql));
149 else
150 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
153 /* get the server stats */
154 result = strdup (mysql_stat (&mysql));
156 /* error checking once more */
157 if (mysql_error (&mysql)) {
158 if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
159 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
160 else if (mysql_errno (&mysql) == CR_SERVER_LOST)
161 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
162 else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
163 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
166 /* try to fetch some perf data */
167 if (mysql_query (&mysql, "show global status") == 0) {
168 if ( (res = mysql_store_result (&mysql)) == NULL) {
169 error = strdup(mysql_error(&mysql));
170 mysql_close (&mysql);
171 die (STATE_CRITICAL, _("status store_result error: %s\n"), error);
174 while ( (row = mysql_fetch_row (res)) != NULL) {
175 int i;
177 for(i = 0; i < LENGTH_METRIC_UNIT; i++) {
178 if (strcmp(row[0], metric_unit[i]) == 0) {
179 xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i],
180 atol(row[1]), "", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0));
181 continue;
184 for(i = 0; i < LENGTH_METRIC_COUNTER; i++) {
185 if (strcmp(row[0], metric_counter[i]) == 0) {
186 xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i],
187 atol(row[1]), "c", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0));
188 continue;
192 /* remove trailing space */
193 if (strlen(perf) > 0)
194 perf[strlen(perf) - 1] = '\0';
197 if(check_slave) {
198 /* check the slave status */
199 if (mysql_query (&mysql, "show slave status") != 0) {
200 error = strdup(mysql_error(&mysql));
201 mysql_close (&mysql);
202 die (STATE_CRITICAL, _("slave query error: %s\n"), error);
205 /* store the result */
206 if ( (res = mysql_store_result (&mysql)) == NULL) {
207 error = strdup(mysql_error(&mysql));
208 mysql_close (&mysql);
209 die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
212 /* Check there is some data */
213 if (mysql_num_rows(res) == 0) {
214 mysql_close(&mysql);
215 die (STATE_WARNING, "%s\n", _("No slaves defined"));
218 /* fetch the first row */
219 if ( (row = mysql_fetch_row (res)) == NULL) {
220 error = strdup(mysql_error(&mysql));
221 mysql_free_result (res);
222 mysql_close (&mysql);
223 die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
226 if (mysql_field_count (&mysql) == 12) {
227 /* mysql 3.23.x */
228 snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
229 if (strcmp (row[6], "Yes") != 0) {
230 mysql_free_result (res);
231 mysql_close (&mysql);
232 die (STATE_CRITICAL, "%s\n", slaveresult);
235 } else {
236 /* mysql 4.x.x and mysql 5.x.x */
237 int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
238 MYSQL_FIELD* fields;
240 num_fields = mysql_num_fields(res);
241 fields = mysql_fetch_fields(res);
242 for(i = 0; i < num_fields; i++) {
243 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
244 slave_io_field = i;
245 continue;
247 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
248 slave_sql_field = i;
249 continue;
251 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
252 seconds_behind_field = i;
253 continue;
257 /* Check if slave status is available */
258 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
259 mysql_free_result (res);
260 mysql_close (&mysql);
261 die (STATE_CRITICAL, "Slave status unavailable\n");
264 /* Save slave status in slaveresult */
265 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], seconds_behind_field!=-1?row[seconds_behind_field]:"Unknown");
267 /* Raise critical error if SQL THREAD or IO THREAD are stopped */
268 if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
269 mysql_free_result (res);
270 mysql_close (&mysql);
271 die (STATE_CRITICAL, "%s\n", slaveresult);
274 if (verbose >=3) {
275 if (seconds_behind_field == -1) {
276 printf("seconds_behind_field not found\n");
277 } else {
278 printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
282 /* Check Seconds Behind against threshold */
283 if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
284 double value = atof(row[seconds_behind_field]);
285 int status;
287 status = get_status(value, my_threshold);
289 xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s",
290 TRUE, (double) warning_time,
291 TRUE, (double) critical_time,
292 FALSE, 0,
293 FALSE, 0));
295 if (status == STATE_WARNING) {
296 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf);
297 exit(STATE_WARNING);
298 } else if (status == STATE_CRITICAL) {
299 printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf);
300 exit(STATE_CRITICAL);
305 /* free the result */
306 mysql_free_result (res);
309 /* close the connection */
310 mysql_close (&mysql);
312 /* print out the result of stats */
313 if (check_slave) {
314 printf ("%s %s|%s\n", result, slaveresult, perf);
315 } else {
316 printf ("%s|%s\n", result, perf);
319 return STATE_OK;
323 /* process command-line arguments */
325 process_arguments (int argc, char **argv)
327 int c;
328 char *warning = NULL;
329 char *critical = NULL;
331 int option = 0;
332 static struct option longopts[] = {
333 {"hostname", required_argument, 0, 'H'},
334 {"socket", required_argument, 0, 's'},
335 {"database", required_argument, 0, 'd'},
336 {"username", required_argument, 0, 'u'},
337 {"password", required_argument, 0, 'p'},
338 {"file", required_argument, 0, 'f'},
339 {"group", required_argument, 0, 'g'},
340 {"port", required_argument, 0, 'P'},
341 {"critical", required_argument, 0, 'c'},
342 {"warning", required_argument, 0, 'w'},
343 {"check-slave", no_argument, 0, 'S'},
344 {"verbose", no_argument, 0, 'v'},
345 {"version", no_argument, 0, 'V'},
346 {"help", no_argument, 0, 'h'},
347 {"ssl", no_argument, 0, 'l'},
348 {"ca-cert", optional_argument, 0, 'C'},
349 {"key", required_argument,0,'k'},
350 {"cert", required_argument,0,'a'},
351 {"ca-dir", required_argument, 0, 'D'},
352 {"ciphers", required_argument, 0, 'L'},
353 {0, 0, 0, 0}
356 if (argc < 1)
357 return ERROR;
359 while (1) {
360 c = getopt_long (argc, argv, "hlvVSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option);
362 if (c == -1 || c == EOF)
363 break;
365 switch (c) {
366 case 'H': /* hostname */
367 if (is_host (optarg)) {
368 db_host = optarg;
370 else {
371 usage2 (_("Invalid hostname/address"), optarg);
373 break;
374 case 's': /* socket */
375 db_socket = optarg;
376 break;
377 case 'd': /* database */
378 db = optarg;
379 break;
380 case 'l':
381 ssl = true;
382 break;
383 case 'C':
384 ca_cert = optarg;
385 break;
386 case 'a':
387 cert = optarg;
388 break;
389 case 'k':
390 key = optarg;
391 break;
392 case 'D':
393 ca_dir = optarg;
394 break;
395 case 'L':
396 ciphers = optarg;
397 break;
398 case 'u': /* username */
399 db_user = optarg;
400 break;
401 case 'p': /* authentication information: password */
402 db_pass = strdup(optarg);
404 /* Delete the password from process list */
405 while (*optarg != '\0') {
406 *optarg = 'X';
407 optarg++;
409 break;
410 case 'f': /* client options file */
411 opt_file = optarg;
412 break;
413 case 'g': /* client options group */
414 opt_group = optarg;
415 break;
416 case 'P': /* critical time threshold */
417 db_port = atoi (optarg);
418 break;
419 case 'S':
420 check_slave = 1; /* check-slave */
421 break;
422 case 'w':
423 warning = optarg;
424 warning_time = strtod (warning, NULL);
425 break;
426 case 'c':
427 critical = optarg;
428 critical_time = strtod (critical, NULL);
429 break;
430 case 'V': /* version */
431 print_revision (progname, NP_VERSION);
432 exit (STATE_OK);
433 case 'h': /* help */
434 print_help ();
435 exit (STATE_OK);
436 case 'v':
437 verbose++;
438 break;
439 case '?': /* help */
440 usage5 ();
444 c = optind;
446 set_thresholds(&my_threshold, warning, critical);
448 while ( argc > c ) {
450 if (db_host == NULL)
451 if (is_host (argv[c])) {
452 db_host = argv[c++];
454 else {
455 usage2 (_("Invalid hostname/address"), argv[c]);
457 else if (db_user == NULL)
458 db_user = argv[c++];
459 else if (db_pass == NULL)
460 db_pass = argv[c++];
461 else if (db == NULL)
462 db = argv[c++];
463 else if (is_intnonneg (argv[c]))
464 db_port = atoi (argv[c++]);
465 else
466 break;
469 return validate_arguments ();
474 validate_arguments (void)
476 if (db_user == NULL)
477 db_user = strdup("");
479 if (opt_file == NULL)
480 opt_file = strdup("");
482 if (opt_group == NULL)
483 opt_group = strdup("");
485 if (db_host == NULL)
486 db_host = strdup("");
488 if (db == NULL)
489 db = strdup("");
491 return OK;
495 void
496 print_help (void)
498 char *myport;
499 xasprintf (&myport, "%d", MYSQL_PORT);
501 print_revision (progname, NP_VERSION);
503 printf (_(COPYRIGHT), copyright, email);
505 printf ("%s\n", _("This program tests connections to a MySQL server"));
507 printf ("\n\n");
509 print_usage ();
511 printf (UT_HELP_VRSN);
512 printf (UT_EXTRA_OPTS);
514 printf (UT_HOST_PORT, 'P', myport);
515 printf (" %s\n", "-s, --socket=STRING");
516 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
518 printf (" %s\n", "-d, --database=STRING");
519 printf (" %s\n", _("Check database with indicated name"));
520 printf (" %s\n", "-f, --file=STRING");
521 printf (" %s\n", _("Read from the specified client options file"));
522 printf (" %s\n", "-g, --group=STRING");
523 printf (" %s\n", _("Use a client options group"));
524 printf (" %s\n", "-u, --username=STRING");
525 printf (" %s\n", _("Connect using the indicated username"));
526 printf (" %s\n", "-p, --password=STRING");
527 printf (" %s\n", _("Use the indicated password to authenticate the connection"));
528 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
529 printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
530 printf (" %s\n", "-S, --check-slave");
531 printf (" %s\n", _("Check if the slave thread is running properly."));
532 printf (" %s\n", "-w, --warning");
533 printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds"));
534 printf (" %s\n", _("behind master"));
535 printf (" %s\n", "-c, --critical");
536 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
537 printf (" %s\n", _("behind master"));
538 printf (" %s\n", "-l, --ssl");
539 printf (" %s\n", _("Use ssl encryptation"));
540 printf (" %s\n", "-C, --ca-cert=STRING");
541 printf (" %s\n", _("Path to CA signing the cert"));
542 printf (" %s\n", "-a, --cert=STRING");
543 printf (" %s\n", _("Path to SSL certificate"));
544 printf (" %s\n", "-k, --key=STRING");
545 printf (" %s\n", _("Path to private SSL key"));
546 printf (" %s\n", "-D, --ca-dir=STRING");
547 printf (" %s\n", _("Path to CA directory"));
548 printf (" %s\n", "-L, --ciphers=STRING");
549 printf (" %s\n", _("List of valid SSL ciphers"));
552 printf ("\n");
553 printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
554 printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
555 printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
557 printf ("\n");
558 printf ("%s\n", _("Notes:"));
559 printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
560 printf (" %s\n", _("overriding any my.cnf settings."));
562 printf (UT_SUPPORT);
566 void
567 print_usage (void)
569 printf ("%s\n", _("Usage:"));
570 printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
571 printf (" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
572 printf (" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n");