1 /*****************************************************************************
3 * Nagios check_mysql plugin
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
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
41 #include "utils_base.h"
49 char *db_socket
= NULL
;
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;
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
] = {
72 "Qcache_queries_in_cache",
77 #define LENGTH_METRIC_COUNTER 9
78 static const char *metric_counter
[LENGTH_METRIC_COUNTER
] = {
82 "Qcache_lowmem_prunes",
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);
98 main (int argc
, char **argv
)
105 /* should be status */
109 char slaveresult
[SLAVERESULTSIZE
];
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 */
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
);
133 mysql_options(&mysql
,MYSQL_READ_DEFAULT_GROUP
,"client");
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
));
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
) {
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));
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));
192 /* remove trailing space */
193 if (strlen(perf
) > 0)
194 perf
[strlen(perf
) - 1] = '\0';
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) {
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) {
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
);
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
;
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) {
247 if (strcmp(fields
[i
].name
, "Slave_SQL_Running") == 0) {
251 if (strcmp(fields
[i
].name
, "Seconds_Behind_Master") == 0) {
252 seconds_behind_field
= i
;
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
);
275 if (seconds_behind_field
== -1) {
276 printf("seconds_behind_field not found\n");
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
]);
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
,
295 if (status
== STATE_WARNING
) {
296 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult
, perf
);
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 */
314 printf ("%s %s|%s\n", result
, slaveresult
, perf
);
316 printf ("%s|%s\n", result
, perf
);
323 /* process command-line arguments */
325 process_arguments (int argc
, char **argv
)
328 char *warning
= NULL
;
329 char *critical
= NULL
;
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'},
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
)
366 case 'H': /* hostname */
367 if (is_host (optarg
)) {
371 usage2 (_("Invalid hostname/address"), optarg
);
374 case 's': /* socket */
377 case 'd': /* database */
398 case 'u': /* username */
401 case 'p': /* authentication information: password */
402 db_pass
= strdup(optarg
);
404 /* Delete the password from process list */
405 while (*optarg
!= '\0') {
410 case 'f': /* client options file */
413 case 'g': /* client options group */
416 case 'P': /* critical time threshold */
417 db_port
= atoi (optarg
);
420 check_slave
= 1; /* check-slave */
424 warning_time
= strtod (warning
, NULL
);
428 critical_time
= strtod (critical
, NULL
);
430 case 'V': /* version */
431 print_revision (progname
, NP_VERSION
);
446 set_thresholds(&my_threshold
, warning
, critical
);
451 if (is_host (argv
[c
])) {
455 usage2 (_("Invalid hostname/address"), argv
[c
]);
457 else if (db_user
== NULL
)
459 else if (db_pass
== NULL
)
463 else if (is_intnonneg (argv
[c
]))
464 db_port
= atoi (argv
[c
++]);
469 return validate_arguments ();
474 validate_arguments (void)
477 db_user
= strdup("");
479 if (opt_file
== NULL
)
480 opt_file
= strdup("");
482 if (opt_group
== NULL
)
483 opt_group
= strdup("");
486 db_host
= strdup("");
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"));
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"));
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)."));
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."));
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");