1 /*****************************************************************************
3 * Monitoring 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-2024 Monitoring 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-2024";
35 const char *email
= "devel@monitoring-plugins.org";
37 #define SLAVERESULTSIZE 96
41 #include "utils_base.h"
45 #include <mysqld_error.h>
50 char *db_socket
= NULL
;
59 char *opt_file
= NULL
;
60 char *opt_group
= NULL
;
61 unsigned int db_port
= MYSQL_PORT
;
62 bool check_slave
= false;
63 bool ignore_auth
= false;
66 static double warning_time
= 0;
67 static double critical_time
= 0;
69 #define LENGTH_METRIC_UNIT 6
70 static const char *metric_unit
[LENGTH_METRIC_UNIT
] = {
74 "Qcache_queries_in_cache",
79 #define LENGTH_METRIC_COUNTER 9
80 static const char *metric_counter
[LENGTH_METRIC_COUNTER
] = {
84 "Qcache_lowmem_prunes",
92 #define MYSQLDUMP_THREADS_QUERY "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'"
94 thresholds
*my_threshold
= NULL
;
96 int process_arguments (int, char **);
97 int validate_arguments (void);
98 void print_help (void);
99 void print_usage (void);
102 main (int argc
, char **argv
)
109 /* should be status */
113 char slaveresult
[SLAVERESULTSIZE
] = { 0 };
118 setlocale (LC_ALL
, "");
119 bindtextdomain (PACKAGE
, LOCALEDIR
);
120 textdomain (PACKAGE
);
122 /* Parse extra opts if any */
123 argv
=np_extra_opts (&argc
, argv
, progname
);
125 if (process_arguments (argc
, argv
) == ERROR
)
126 usage4 (_("Could not parse arguments"));
128 /* initialize mysql */
131 if (opt_file
!= NULL
)
132 mysql_options(&mysql
,MYSQL_READ_DEFAULT_FILE
,opt_file
);
134 if (opt_group
!= NULL
)
135 mysql_options(&mysql
,MYSQL_READ_DEFAULT_GROUP
,opt_group
);
137 mysql_options(&mysql
,MYSQL_READ_DEFAULT_GROUP
,"client");
140 mysql_ssl_set(&mysql
,key
,cert
,ca_cert
,ca_dir
,ciphers
);
141 /* establish a connection to the server and error checking */
142 if (!mysql_real_connect(&mysql
,db_host
,db_user
,db_pass
,db
,db_port
,db_socket
,0)) {
143 /* Depending on internally-selected auth plugin MySQL might return */
144 /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */
145 /* Semantically these errors are the same. */
146 if (ignore_auth
&& (mysql_errno (&mysql
) == ER_ACCESS_DENIED_ERROR
|| mysql_errno (&mysql
) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR
))
148 printf("MySQL OK - Version: %s (protocol %d)\n",
149 mysql_get_server_info(&mysql
),
150 mysql_get_proto_info(&mysql
)
152 mysql_close (&mysql
);
155 else if (mysql_errno (&mysql
) == CR_UNKNOWN_HOST
)
156 die (STATE_WARNING
, "%s\n", mysql_error (&mysql
));
157 else if (mysql_errno (&mysql
) == CR_VERSION_ERROR
)
158 die (STATE_WARNING
, "%s\n", mysql_error (&mysql
));
159 else if (mysql_errno (&mysql
) == CR_OUT_OF_MEMORY
)
160 die (STATE_WARNING
, "%s\n", mysql_error (&mysql
));
161 else if (mysql_errno (&mysql
) == CR_IPSOCK_ERROR
)
162 die (STATE_WARNING
, "%s\n", mysql_error (&mysql
));
163 else if (mysql_errno (&mysql
) == CR_SOCKET_CREATE_ERROR
)
164 die (STATE_WARNING
, "%s\n", mysql_error (&mysql
));
166 die (STATE_CRITICAL
, "%s\n", mysql_error (&mysql
));
169 /* get the server stats */
170 result
= strdup (mysql_stat (&mysql
));
172 /* error checking once more */
173 if (mysql_error (&mysql
)) {
174 if (mysql_errno (&mysql
) == CR_SERVER_GONE_ERROR
)
175 die (STATE_CRITICAL
, "%s\n", mysql_error (&mysql
));
176 else if (mysql_errno (&mysql
) == CR_SERVER_LOST
)
177 die (STATE_CRITICAL
, "%s\n", mysql_error (&mysql
));
178 else if (mysql_errno (&mysql
) == CR_UNKNOWN_ERROR
)
179 die (STATE_CRITICAL
, "%s\n", mysql_error (&mysql
));
182 /* try to fetch some perf data */
183 if (mysql_query (&mysql
, "show global status") == 0) {
184 if ( (res
= mysql_store_result (&mysql
)) == NULL
) {
185 error
= strdup(mysql_error(&mysql
));
186 mysql_close (&mysql
);
187 die (STATE_CRITICAL
, _("status store_result error: %s\n"), error
);
190 while ( (row
= mysql_fetch_row (res
)) != NULL
) {
193 for(i
= 0; i
< LENGTH_METRIC_UNIT
; i
++) {
194 if (strcmp(row
[0], metric_unit
[i
]) == 0) {
195 xasprintf(&perf
, "%s%s ", perf
, perfdata(metric_unit
[i
],
196 atol(row
[1]), "", false, 0, false, 0, false, 0, false, 0));
200 for(i
= 0; i
< LENGTH_METRIC_COUNTER
; i
++) {
201 if (strcmp(row
[0], metric_counter
[i
]) == 0) {
202 xasprintf(&perf
, "%s%s ", perf
, perfdata(metric_counter
[i
],
203 atol(row
[1]), "c", false, 0, false, 0, false, 0, false, 0));
208 /* remove trailing space */
209 if (strlen(perf
) > 0)
210 perf
[strlen(perf
) - 1] = '\0';
214 /* check the slave status */
215 if (mysql_query (&mysql
, "show slave status") != 0) {
216 error
= strdup(mysql_error(&mysql
));
217 mysql_close (&mysql
);
218 die (STATE_CRITICAL
, _("slave query error: %s\n"), error
);
221 /* store the result */
222 if ( (res
= mysql_store_result (&mysql
)) == NULL
) {
223 error
= strdup(mysql_error(&mysql
));
224 mysql_close (&mysql
);
225 die (STATE_CRITICAL
, _("slave store_result error: %s\n"), error
);
228 /* Check there is some data */
229 if (mysql_num_rows(res
) == 0) {
231 die (STATE_WARNING
, "%s\n", _("No slaves defined"));
234 /* fetch the first row */
235 if ( (row
= mysql_fetch_row (res
)) == NULL
) {
236 error
= strdup(mysql_error(&mysql
));
237 mysql_free_result (res
);
238 mysql_close (&mysql
);
239 die (STATE_CRITICAL
, _("slave fetch row error: %s\n"), error
);
242 if (mysql_field_count (&mysql
) == 12) {
244 snprintf (slaveresult
, SLAVERESULTSIZE
, _("Slave running: %s"), row
[6]);
245 if (strcmp (row
[6], "Yes") != 0) {
246 mysql_free_result (res
);
247 mysql_close (&mysql
);
248 die (STATE_CRITICAL
, "%s\n", slaveresult
);
252 /* mysql 4.x.x and mysql 5.x.x */
253 int slave_io_field
= -1 , slave_sql_field
= -1, seconds_behind_field
= -1, i
, num_fields
;
256 num_fields
= mysql_num_fields(res
);
257 fields
= mysql_fetch_fields(res
);
258 for(i
= 0; i
< num_fields
; i
++) {
259 if (strcmp(fields
[i
].name
, "Slave_IO_Running") == 0) {
263 if (strcmp(fields
[i
].name
, "Slave_SQL_Running") == 0) {
267 if (strcmp(fields
[i
].name
, "Seconds_Behind_Master") == 0) {
268 seconds_behind_field
= i
;
273 /* Check if slave status is available */
274 if ((slave_io_field
< 0) || (slave_sql_field
< 0) || (num_fields
== 0)) {
275 mysql_free_result (res
);
276 mysql_close (&mysql
);
277 die (STATE_CRITICAL
, "Slave status unavailable\n");
280 /* Save slave status in slaveresult */
281 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");
283 /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */
284 if (strcmp (row
[slave_io_field
], "Yes") != 0 || strcmp (row
[slave_sql_field
], "Yes") != 0) {
285 MYSQL_RES
*res_mysqldump
;
286 MYSQL_ROW row_mysqldump
;
287 unsigned int mysqldump_threads
= 0;
289 if (mysql_query (&mysql
, MYSQLDUMP_THREADS_QUERY
) == 0) {
290 /* store the result */
291 if ( (res_mysqldump
= mysql_store_result (&mysql
)) != NULL
) {
292 if (mysql_num_rows(res_mysqldump
) == 1) {
293 if ( (row_mysqldump
= mysql_fetch_row (res_mysqldump
)) != NULL
) {
294 mysqldump_threads
= atoi(row_mysqldump
[0]);
297 /* free the result */
298 mysql_free_result (res_mysqldump
);
300 mysql_close (&mysql
);
302 if (mysqldump_threads
== 0) {
303 die (STATE_CRITICAL
, "%s\n", slaveresult
);
305 strncat(slaveresult
, " Mysqldump: in progress", SLAVERESULTSIZE
-1);
310 if (seconds_behind_field
== -1) {
311 printf("seconds_behind_field not found\n");
313 printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field
, row
[seconds_behind_field
]);
317 /* Check Seconds Behind against threshold */
318 if ((seconds_behind_field
!= -1) && (row
[seconds_behind_field
] != NULL
&& strcmp (row
[seconds_behind_field
], "NULL") != 0)) {
319 double value
= atof(row
[seconds_behind_field
]);
322 status
= get_status(value
, my_threshold
);
324 xasprintf (&perf
, "%s %s", perf
, fperfdata ("seconds behind master", value
, "s",
325 true, (double) warning_time
,
326 true, (double) critical_time
,
330 if (status
== STATE_WARNING
) {
331 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult
, perf
);
333 } else if (status
== STATE_CRITICAL
) {
334 printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult
, perf
);
335 exit(STATE_CRITICAL
);
340 /* free the result */
341 mysql_free_result (res
);
344 /* close the connection */
345 mysql_close (&mysql
);
347 /* print out the result of stats */
349 printf ("%s %s|%s\n", result
, slaveresult
, perf
);
351 printf ("%s|%s\n", result
, perf
);
358 /* process command-line arguments */
360 process_arguments (int argc
, char **argv
)
363 char *warning
= NULL
;
364 char *critical
= NULL
;
367 static struct option longopts
[] = {
368 {"hostname", required_argument
, 0, 'H'},
369 {"socket", required_argument
, 0, 's'},
370 {"database", required_argument
, 0, 'd'},
371 {"username", required_argument
, 0, 'u'},
372 {"password", required_argument
, 0, 'p'},
373 {"file", required_argument
, 0, 'f'},
374 {"group", required_argument
, 0, 'g'},
375 {"port", required_argument
, 0, 'P'},
376 {"critical", required_argument
, 0, 'c'},
377 {"warning", required_argument
, 0, 'w'},
378 {"check-slave", no_argument
, 0, 'S'},
379 {"ignore-auth", no_argument
, 0, 'n'},
380 {"verbose", no_argument
, 0, 'v'},
381 {"version", no_argument
, 0, 'V'},
382 {"help", no_argument
, 0, 'h'},
383 {"ssl", no_argument
, 0, 'l'},
384 {"ca-cert", optional_argument
, 0, 'C'},
385 {"key", required_argument
,0,'k'},
386 {"cert", required_argument
,0,'a'},
387 {"ca-dir", required_argument
, 0, 'D'},
388 {"ciphers", required_argument
, 0, 'L'},
396 c
= getopt_long (argc
, argv
, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts
, &option
);
398 if (c
== -1 || c
== EOF
)
402 case 'H': /* hostname */
403 if (is_host (optarg
)) {
406 else if (*optarg
== '/') {
410 usage2 (_("Invalid hostname/address"), optarg
);
413 case 's': /* socket */
416 case 'd': /* database */
437 case 'u': /* username */
440 case 'p': /* authentication information: password */
441 db_pass
= strdup(optarg
);
443 /* Delete the password from process list */
444 while (*optarg
!= '\0') {
449 case 'f': /* client options file */
452 case 'g': /* client options group */
455 case 'P': /* critical time threshold */
456 db_port
= atoi (optarg
);
459 check_slave
= true; /* check-slave */
462 ignore_auth
= true; /* ignore-auth */
466 warning_time
= strtod (warning
, NULL
);
470 critical_time
= strtod (critical
, NULL
);
472 case 'V': /* version */
473 print_revision (progname
, NP_VERSION
);
474 exit (STATE_UNKNOWN
);
477 exit (STATE_UNKNOWN
);
488 set_thresholds(&my_threshold
, warning
, critical
);
493 if (is_host (argv
[c
])) {
497 usage2 (_("Invalid hostname/address"), argv
[c
]);
499 else if (db_user
== NULL
)
501 else if (db_pass
== NULL
)
505 else if (is_intnonneg (argv
[c
]))
506 db_port
= atoi (argv
[c
++]);
511 return validate_arguments ();
516 validate_arguments (void)
519 db_user
= strdup("");
522 db_host
= strdup("");
535 xasprintf (&myport
, "%d", MYSQL_PORT
);
537 print_revision (progname
, NP_VERSION
);
539 printf (_(COPYRIGHT
), copyright
, email
);
541 printf ("%s\n", _("This program tests connections to a MySQL server"));
547 printf (UT_HELP_VRSN
);
548 printf (UT_EXTRA_OPTS
);
550 printf (UT_HOST_PORT
, 'P', myport
);
551 printf (" %s\n", "-n, --ignore-auth");
552 printf (" %s\n", _("Ignore authentication failure and check for mysql connectivity only"));
554 printf (" %s\n", "-s, --socket=STRING");
555 printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
557 printf (" %s\n", "-d, --database=STRING");
558 printf (" %s\n", _("Check database with indicated name"));
559 printf (" %s\n", "-f, --file=STRING");
560 printf (" %s\n", _("Read from the specified client options file"));
561 printf (" %s\n", "-g, --group=STRING");
562 printf (" %s\n", _("Use a client options group"));
563 printf (" %s\n", "-u, --username=STRING");
564 printf (" %s\n", _("Connect using the indicated username"));
565 printf (" %s\n", "-p, --password=STRING");
566 printf (" %s\n", _("Use the indicated password to authenticate the connection"));
567 printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
568 printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
569 printf (" %s\n", "-S, --check-slave");
570 printf (" %s\n", _("Check if the slave thread is running properly."));
571 printf (" %s\n", "-w, --warning");
572 printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds"));
573 printf (" %s\n", _("behind master"));
574 printf (" %s\n", "-c, --critical");
575 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
576 printf (" %s\n", _("behind master"));
577 printf (" %s\n", "-l, --ssl");
578 printf (" %s\n", _("Use ssl encryption"));
579 printf (" %s\n", "-C, --ca-cert=STRING");
580 printf (" %s\n", _("Path to CA signing the cert"));
581 printf (" %s\n", "-a, --cert=STRING");
582 printf (" %s\n", _("Path to SSL certificate"));
583 printf (" %s\n", "-k, --key=STRING");
584 printf (" %s\n", _("Path to private SSL key"));
585 printf (" %s\n", "-D, --ca-dir=STRING");
586 printf (" %s\n", _("Path to CA directory"));
587 printf (" %s\n", "-L, --ciphers=STRING");
588 printf (" %s\n", _("List of valid SSL ciphers"));
592 printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
593 printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
594 printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
597 printf ("%s\n", _("Notes:"));
598 printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
599 printf (" %s\n", _("overriding any my.cnf settings."));
608 printf ("%s\n", _("Usage:"));
609 printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname
);
610 printf (" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
611 printf (" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n");