Module: monitoring-plugins
    Branch: maintenanc-2.4.0-check_mysql
    Commit: 4886fa3debf79aa2faac9d0180e8ddb4bb1d4aaa
    Author: Lorenz Kästle <[email protected]>
 Committer: Lorenz Kästle <[email protected]>
      Date: Mon Feb 24 19:52:08 2025 +0100
       URL: 
https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=4886fa3d

Add MySQL server version dectection and adaptive replica query

---

 plugins/check_mysql.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 49 insertions(+), 2 deletions(-)

diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index 6a7daf11..1b7403f7 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -211,8 +211,55 @@ main (int argc, char **argv)
        }
 
        if(check_slave) {
-               /* check the slave status */
-               if (mysql_query (&mysql, "show slave status") != 0) {
+               // Detect which version we are, on older version
+               // "show slave status" should work, on newer ones
+               // "show replica status"
+               // But first we have to find out whether this is
+               // MySQL or MariaDB since the version numbering scheme
+               // is different
+               bool use_deprecated_slave_status = false;
+               const char *server_version = mysql_get_server_info(&mysql);
+               unsigned long server_verion_int = 
mysql_get_server_version(&mysql);
+               unsigned long major_version = server_verion_int / 10000;
+               unsigned long minor_version = (server_verion_int % 10000) / 100;
+               unsigned long patch_version = (server_verion_int % 100);
+               if (verbose) {
+                       printf("Found MariaDB: %s, main version: %lu, minor 
version: %lu, patch version: %lu\n", server_version, major_version,
+                                  minor_version, patch_version);
+               }
+
+               if (strstr(server_version, "MariaDB") != NULL) {
+                       // Looks like MariaDB, new commands should be available 
after 10.5.1
+                       if (major_version < 10) {
+                               use_deprecated_slave_status = true;
+                       } else if (major_version == 10) {
+                               if (minor_version < 5) {
+                                       use_deprecated_slave_status = true;
+                               } else if (minor_version == 5 && patch_version 
< 1) {
+                                       use_deprecated_slave_status = true;
+                               }
+                       }
+               } else if (strstr(server_version, "MySQL") != NULL) {
+                       // Looks like MySQL
+                       if (major_version < 8) {
+                               use_deprecated_slave_status = true;
+                       } else if (major_version == 10 && minor_version < 4) {
+                               use_deprecated_slave_status = true;
+                       }
+               } else {
+                       printf("Not a known sever implementation: %s\n", 
server_version);
+                       exit(STATE_UNKNOWN);
+               }
+
+               char *replica_query = NULL;
+               if (use_deprecated_slave_status) {
+                       replica_query = "show slave status";
+               } else {
+                       replica_query = "show replica status";
+               }
+
+               /* check the replica status */
+               if (mysql_query(&mysql, replica_query) != 0) {
                        error = strdup(mysql_error(&mysql));
                        mysql_close (&mysql);
                        die (STATE_CRITICAL, _("slave query error: %s\n"), 
error);

Reply via email to