[monitoring-plugins] Merge pull request #1716 from ...

GitHub git at monitoring-plugins.org
Wed Sep 16 13:30:14 CEST 2026


    Module: monitoring-plugins
    Branch: master
    Commit: ec1047a7fccfd26fe9d5f549f42e15b197326cbc
    Author: Gerardo Malazdrewicz <36243997+GerMalaz at users.noreply.github.com>
 Committer: GitHub <noreply at github.com>
      Date: Wed Sep 16 08:24:55 2026 -0300
       URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=ec1047a7

Merge pull request #1716 from GerMalaz/mysql_named_slaves

check_mysql: Add a -N slave_name option

---

 plugins/check_mysql.c          | 56 ++++++++++++++++++++++++++++++++----------
 plugins/check_mysql.d/config.h |  4 +++
 2 files changed, 47 insertions(+), 13 deletions(-)

diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index b09fa4bb..61c5278b 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -93,11 +93,21 @@ int main(int argc, char **argv) {
 	}
 
 	const check_mysql_config config = tmp_config.config;
+	char *replica_query = NULL;
+	char *replica_status_query = NULL;
+	int num_replicas = 0;
+	char *no_such_named_replica = NULL;
 
 	if (config.output_format_is_set) {
 		mp_set_format(config.output_format);
 	}
 
+	if (config.replica_name == NULL) {
+		replica_status_query = strdup("status");
+	} else {
+		mopl_utils_xasprintf(&no_such_named_replica, _("no such replica named '%s'"), config.replica_name);
+	}
+
 	MYSQL mysql;
 	/* initialize mysql  */
 	mysql_init(&mysql);
@@ -270,6 +280,9 @@ int main(int argc, char **argv) {
 					use_deprecated_slave_status = true;
 				}
 			}
+			if (config.replica_name != NULL) {
+				mopl_utils_xasprintf(&replica_status_query, "'%s' status", config.replica_name);
+			}
 		} else {
 			// Looks like MySQL or at least not like MariaDB
 			if (major_version < 8) {
@@ -277,18 +290,22 @@ int main(int argc, char **argv) {
 			} else if (major_version == 10 && minor_version < 4) {
 				use_deprecated_slave_status = true;
 			}
-		}
-
-		char *replica_query = NULL;
-		if (use_deprecated_slave_status) {
-			replica_query = "show slave status";
-		} else {
-			replica_query = "show replica status";
+			if (config.replica_name != NULL) {
+				mopl_utils_xasprintf(&replica_status_query, "status for channel '%s'", config.replica_name);
+			}
 		}
 
 		mp_subcheck sc_replica = mp_subcheck_init();
 
 		/* check the replica status */
+		mopl_utils_xasprintf(&replica_query, "show %s %s",
+			use_deprecated_slave_status ? "slave" : "replica",
+			replica_status_query);
+
+		if (verbose) {
+			printf("Replica query: %s\n", replica_query);
+		}
+
 		if (mysql_query(&mysql, replica_query) != 0) {
 			mopl_utils_xasprintf(&sc_replica.output, "replica query error: %s", mysql_error(&mysql));
 			mysql_close(&mysql);
@@ -309,10 +326,18 @@ int main(int argc, char **argv) {
 		}
 
 		/* Check there is some data */
-		if (mysql_num_rows(res) == 0) {
+		num_replicas = mysql_num_rows(res);
+		if (num_replicas != 1) {
 			mysql_close(&mysql);
 
-			mopl_utils_xasprintf(&sc_replica.output, "no replicas defined");
+			mopl_utils_xasprintf(&sc_replica.output,
+				num_replicas == 0
+					? (config.replica_name != NULL)
+						? no_such_named_replica
+						:  _("no replicas defined")
+					:  _("too many replicas defined, please provide a replica name as an argument")
+			);
+
 			sc_replica = mp_set_subcheck_state(sc_replica, STATE_WARNING);
 			mp_add_subcheck_to_check(&overall, sc_replica);
 			mp_exit(overall);
@@ -376,8 +401,7 @@ int main(int argc, char **argv) {
 			}
 
 			/* Check if replica status is available */
-			if ((replica_io_field < 0) || (replica_sql_field < 0) || (num_fields == 0) ||
-				(last_errno_field != -1)) {
+			if ((replica_io_field < 0) || (replica_sql_field < 0) || (num_fields == 0)) {
 				mysql_free_result(res);
 				mysql_close(&mysql);
 
@@ -489,6 +513,7 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) {
 									   {"warning", required_argument, 0, 'w'},
 									   {"check-slave", no_argument, 0, 'S'},
 									   {"check-replica", no_argument, 0, CHECK_REPLICA_OPT},
+									   {"replica-name", required_argument, 0, 'N'},
 									   {"ignore-auth", no_argument, 0, 'n'},
 									   {"verbose", no_argument, 0, 'v'},
 									   {"version", no_argument, 0, 'V'},
@@ -515,7 +540,7 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) {
 	int option = 0;
 	while (true) {
 		int option_index =
-			getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option);
+			getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:N:", longopts, &option);
 
 		if (CHECK_EOF(option_index)) {
 			break;
@@ -580,6 +605,9 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) {
 		case CHECK_REPLICA_OPT:
 			result.config.check_replica = true; /* check-slave */
 			break;
+		case 'N': /* replica-name */
+			result.config.replica_name = optarg;
+			break;
 		case 'n':
 			result.config.ignore_auth = true; /* ignore-auth */
 			break;
@@ -706,6 +734,8 @@ void print_help(void) {
 						 "in favour of check-replica, which does the same"));
 	printf(" %s\n", "--check-replica");
 	printf("    %s\n", _("Check if the replica thread is running properly."));
+	printf (" %s\n", "-N, --replica-name");
+	printf ("    %s\n", _("Use a named replica"));
 	printf(" %s\n", "-w, --warning");
 	printf("    %s\n",
 		   _("Exit with WARNING status if replica server is more than INTEGER seconds"));
@@ -746,6 +776,6 @@ void print_help(void) {
 void print_usage(void) {
 	printf("%s\n", _("Usage:"));
 	printf(" %s [-d database] [-H host] [-P port] [-s socket]\n", progname);
-	printf("       [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
+	printf("       [-u user] [-p password] [-S] [-N replica] [-l] [-a cert] [-k key]\n");
 	printf("       [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n");
 }
diff --git a/plugins/check_mysql.d/config.h b/plugins/check_mysql.d/config.h
index 1d8c82bb..7fa66e79 100644
--- a/plugins/check_mysql.d/config.h
+++ b/plugins/check_mysql.d/config.h
@@ -23,6 +23,8 @@ typedef struct {
 	char *opt_group;
 
 	bool check_replica;
+        char *replica_name;
+
 	bool ignore_auth;
 
 	mp_thresholds replica_thresholds;
@@ -49,6 +51,8 @@ check_mysql_config check_mysql_config_init() {
 		.opt_group = NULL,
 
 		.check_replica = false,
+                .replica_name = NULL,
+
 		.ignore_auth = false,
 
 		.replica_thresholds = mp_thresholds_init(),



More information about the Commits mailing list