check_mysql.c: Detect running mysqldump

When checking a slave, if the IO Thread or the SQL Thread are stopped, check for running mysqldump threads, return STATE_OK if there is any.
Requires PROCESS privilege to work (else the mysqldump thread(s) would not be detected).

Enlarged SLAVERESULTSIZE to fit "Mysqldump: in progress" at the end of the string.
Got a NULL pointer in row[seconds_behind_field] instead of the "NULL" string when a mysqldump is running [mysql 5.7.34 + libmariadb3 10.3.31], so added a check for that.
This commit is contained in:
Gerardo Malazdrewicz 2021-10-31 09:37:55 -03:00 committed by GitHub
parent 7415eb2f06
commit 3929c5ac37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -34,7 +34,7 @@ const char *progname = "check_mysql";
const char *copyright = "1999-2011";
const char *email = "devel@monitoring-plugins.org";
#define SLAVERESULTSIZE 70
#define SLAVERESULTSIZE 96
#include "common.h"
#include "utils.h"
@ -89,6 +89,8 @@ static const char *metric_counter[LENGTH_METRIC_COUNTER] = {
"Uptime"
};
#define MYSQLDUMP_THREADS_QUERY "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'"
thresholds *my_threshold = NULL;
int process_arguments (int, char **);
@ -275,11 +277,29 @@ main (int argc, char **argv)
/* Save slave status in slaveresult */
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");
/* Raise critical error if SQL THREAD or IO THREAD are stopped */
/* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */
if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
mysql_free_result (res);
mysql_close (&mysql);
die (STATE_CRITICAL, "%s\n", slaveresult);
MYSQL_RES *res_mysqldump;
MYSQL_ROW row_mysqldump;
unsigned int mysqldump_threads = 0;
if (mysql_query (&mysql, MYSQLDUMP_THREADS_QUERY) == 0) {
/* store the result */
if ( (res_mysqldump = mysql_store_result (&mysql)) != NULL) {
if (mysql_num_rows(res_mysqldump) == 1) {
if ( (row_mysqldump = mysql_fetch_row (res_mysqldump)) != NULL) {
mysqldump_threads = atoi(row_mysqldump[0]);
}
}
/* free the result */
mysql_free_result (res_mysqldump);
}
}
if (mysqldump_threads == 0) {
die (STATE_CRITICAL, "%s\n", slaveresult);
} else {
strlcat (slaveresult, " Mysqldump: in progress", SLAVERESULTSIZE);
}
}
if (verbose >=3) {
@ -291,7 +311,7 @@ main (int argc, char **argv)
}
/* Check Seconds Behind against threshold */
if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp (row[seconds_behind_field], "NULL") != 0)) {
double value = atof(row[seconds_behind_field]);
int status;