diff options
Diffstat (limited to 'plugins/check_mysql.c')
| -rw-r--r-- | plugins/check_mysql.c | 1005 |
1 files changed, 563 insertions, 442 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index 6a7daf11..9d8094c0 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c | |||
| @@ -1,444 +1,560 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** |
| 2 | * | 2 | * |
| 3 | * Monitoring check_mysql plugin | 3 | * Monitoring check_mysql plugin |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 6 | * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) | 6 | * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) |
| 7 | * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) | 7 | * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) |
| 8 | * Copyright (c) 1999-2011 Monitoring Plugins Development Team | 8 | * Copyright (c) 1999-2024 Monitoring Plugins Development Team |
| 9 | * | 9 | * |
| 10 | * Description: | 10 | * Description: |
| 11 | * | 11 | * |
| 12 | * This file contains the check_mysql plugin | 12 | * This file contains the check_mysql plugin |
| 13 | * | 13 | * |
| 14 | * This program tests connections to a mysql server | 14 | * This program tests connections to a mysql server |
| 15 | * | 15 | * |
| 16 | * | 16 | * |
| 17 | * This program is free software: you can redistribute it and/or modify | 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 | 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 | 19 | * the Free Software Foundation, either version 3 of the License, or |
| 20 | * (at your option) any later version. | 20 | * (at your option) any later version. |
| 21 | * | 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, | 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. | 25 | * GNU General Public License for more details. |
| 26 | * | 26 | * |
| 27 | * You should have received a copy of the GNU General Public License | 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/>. | 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 29 | * | 29 | * |
| 30 | * | 30 | * |
| 31 | *****************************************************************************/ | 31 | *****************************************************************************/ |
| 32 | |||
| 33 | const char *progname = "check_mysql"; | ||
| 34 | const char *copyright = "1999-2011"; | ||
| 35 | const char *email = "devel@monitoring-plugins.org"; | ||
| 36 | |||
| 37 | #define SLAVERESULTSIZE 96 | ||
| 38 | 32 | ||
| 39 | #include "common.h" | 33 | #include "common.h" |
| 34 | #include "output.h" | ||
| 35 | #include "perfdata.h" | ||
| 36 | #include "states.h" | ||
| 37 | #include "thresholds.h" | ||
| 40 | #include "utils.h" | 38 | #include "utils.h" |
| 41 | #include "utils_base.h" | 39 | #include "utils_base.h" |
| 42 | #include "netutils.h" | 40 | #include "netutils.h" |
| 41 | #include "check_mysql.d/config.h" | ||
| 43 | 42 | ||
| 44 | #include <mysql.h> | 43 | #include <mysql.h> |
| 45 | #include <mysqld_error.h> | 44 | #include <mysqld_error.h> |
| 46 | #include <errmsg.h> | 45 | #include <errmsg.h> |
| 47 | 46 | ||
| 48 | char *db_user = NULL; | 47 | const char *progname = "check_mysql"; |
| 49 | char *db_host = NULL; | 48 | const char *copyright = "1999-2024"; |
| 50 | char *db_socket = NULL; | 49 | const char *email = "devel@monitoring-plugins.org"; |
| 51 | char *db_pass = NULL; | 50 | |
| 52 | char *db = NULL; | 51 | static int verbose = 0; |
| 53 | char *ca_cert = NULL; | 52 | |
| 54 | char *ca_dir = NULL; | 53 | #define REPLICA_RESULTSIZE 96 |
| 55 | char *cert = NULL; | ||
| 56 | char *key = NULL; | ||
| 57 | char *ciphers = NULL; | ||
| 58 | bool ssl = false; | ||
| 59 | char *opt_file = NULL; | ||
| 60 | char *opt_group = NULL; | ||
| 61 | unsigned int db_port = MYSQL_PORT; | ||
| 62 | int check_slave = 0, warn_sec = 0, crit_sec = 0; | ||
| 63 | int ignore_auth = 0; | ||
| 64 | int verbose = 0; | ||
| 65 | |||
| 66 | static double warning_time = 0; | ||
| 67 | static double critical_time = 0; | ||
| 68 | 54 | ||
| 69 | #define LENGTH_METRIC_UNIT 6 | 55 | #define LENGTH_METRIC_UNIT 6 |
| 70 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { | 56 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { |
| 71 | "Open_files", | 57 | "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache", |
| 72 | "Open_tables", | 58 | "Threads_connected", "Threads_running"}; |
| 73 | "Qcache_free_memory", | ||
| 74 | "Qcache_queries_in_cache", | ||
| 75 | "Threads_connected", | ||
| 76 | "Threads_running" | ||
| 77 | }; | ||
| 78 | 59 | ||
| 79 | #define LENGTH_METRIC_COUNTER 9 | 60 | #define LENGTH_METRIC_COUNTER 9 |
| 80 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = { | 61 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = {"Connections", |
| 81 | "Connections", | 62 | "Qcache_hits", |
| 82 | "Qcache_hits", | 63 | "Qcache_inserts", |
| 83 | "Qcache_inserts", | 64 | "Qcache_lowmem_prunes", |
| 84 | "Qcache_lowmem_prunes", | 65 | "Qcache_not_cached", |
| 85 | "Qcache_not_cached", | 66 | "Queries", |
| 86 | "Queries", | 67 | "Questions", |
| 87 | "Questions", | 68 | "Table_locks_waited", |
| 88 | "Table_locks_waited", | 69 | "Uptime"}; |
| 89 | "Uptime" | 70 | |
| 90 | }; | 71 | #define MYSQLDUMP_THREADS_QUERY \ |
| 91 | 72 | "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE " \ | |
| 92 | #define MYSQLDUMP_THREADS_QUERY "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'" | 73 | "'SELECT /*!40001 SQL_NO_CACHE */%'" |
| 93 | 74 | ||
| 94 | thresholds *my_threshold = NULL; | 75 | typedef struct { |
| 95 | 76 | int errorcode; | |
| 96 | int process_arguments (int, char **); | 77 | check_mysql_config config; |
| 97 | int validate_arguments (void); | 78 | } check_mysql_config_wrapper; |
| 98 | void print_help (void); | 79 | static check_mysql_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
| 99 | void print_usage (void); | 80 | static check_mysql_config_wrapper validate_arguments(check_mysql_config_wrapper /*config_wrapper*/); |
| 100 | 81 | static void print_help(void); | |
| 101 | int | 82 | void print_usage(void); |
| 102 | main (int argc, char **argv) | 83 | |
| 103 | { | 84 | int main(int argc, char **argv) { |
| 85 | setlocale(LC_ALL, ""); | ||
| 86 | bindtextdomain(PACKAGE, LOCALEDIR); | ||
| 87 | textdomain(PACKAGE); | ||
| 104 | 88 | ||
| 105 | MYSQL mysql; | 89 | /* Parse extra opts if any */ |
| 106 | MYSQL_RES *res; | 90 | argv = np_extra_opts(&argc, argv, progname); |
| 107 | MYSQL_ROW row; | 91 | |
| 92 | check_mysql_config_wrapper tmp_config = process_arguments(argc, argv); | ||
| 93 | if (tmp_config.errorcode == ERROR) { | ||
| 94 | usage4(_("Could not parse arguments")); | ||
| 95 | } | ||
| 108 | 96 | ||
| 109 | /* should be status */ | 97 | const check_mysql_config config = tmp_config.config; |
| 110 | 98 | ||
| 111 | char *result = NULL; | 99 | MYSQL mysql; |
| 112 | char *error = NULL; | 100 | /* initialize mysql */ |
| 113 | char slaveresult[SLAVERESULTSIZE] = { 0 }; | 101 | mysql_init(&mysql); |
| 114 | char* perf; | ||
| 115 | 102 | ||
| 116 | perf = strdup (""); | 103 | if (config.opt_file != NULL) { |
| 104 | mysql_options(&mysql, MYSQL_READ_DEFAULT_FILE, config.opt_file); | ||
| 105 | } | ||
| 117 | 106 | ||
| 118 | setlocale (LC_ALL, ""); | 107 | if (config.opt_group != NULL) { |
| 119 | bindtextdomain (PACKAGE, LOCALEDIR); | 108 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, config.opt_group); |
| 120 | textdomain (PACKAGE); | 109 | } else { |
| 110 | mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "client"); | ||
| 111 | } | ||
| 121 | 112 | ||
| 122 | /* Parse extra opts if any */ | 113 | if (config.ssl) { |
| 123 | argv=np_extra_opts (&argc, argv, progname); | 114 | mysql_ssl_set(&mysql, config.key, config.cert, config.ca_cert, config.ca_dir, |
| 115 | config.ciphers); | ||
| 116 | } | ||
| 124 | 117 | ||
| 125 | if (process_arguments (argc, argv) == ERROR) | 118 | mp_check overall = mp_check_init(); |
| 126 | usage4 (_("Could not parse arguments")); | ||
| 127 | 119 | ||
| 128 | /* initialize mysql */ | 120 | mp_subcheck sc_connection = mp_subcheck_init(); |
| 129 | mysql_init (&mysql); | 121 | /* establish a connection to the server and check for errors */ |
| 130 | 122 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, | |
| 131 | if (opt_file != NULL) | 123 | config.db_port, config.db_socket, 0)) { |
| 132 | mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file); | ||
| 133 | |||
| 134 | if (opt_group != NULL) | ||
| 135 | mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group); | ||
| 136 | else | ||
| 137 | mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); | ||
| 138 | |||
| 139 | if (ssl) | ||
| 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 */ | 124 | /* Depending on internally-selected auth plugin MySQL might return */ |
| 144 | /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ | 125 | /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ |
| 145 | /* Semantically these errors are the same. */ | 126 | /* 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)) | 127 | if (config.ignore_auth && (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR || |
| 147 | { | 128 | mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) { |
| 148 | printf("MySQL OK - Version: %s (protocol %d)\n", | 129 | xasprintf(&sc_connection.output, "Version: %s (protocol %d)", |
| 149 | mysql_get_server_info(&mysql), | 130 | mysql_get_server_info(&mysql), mysql_get_proto_info(&mysql)); |
| 150 | mysql_get_proto_info(&mysql) | 131 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_OK); |
| 151 | ); | 132 | |
| 152 | mysql_close (&mysql); | 133 | mysql_close(&mysql); |
| 153 | return STATE_OK; | 134 | } else { |
| 135 | if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { | ||
| 136 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_WARNING); | ||
| 137 | xasprintf(&sc_connection.output, "%s", mysql_error(&mysql)); | ||
| 138 | } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { | ||
| 139 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_WARNING); | ||
| 140 | xasprintf(&sc_connection.output, "%s", mysql_error(&mysql)); | ||
| 141 | } else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY) { | ||
| 142 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_WARNING); | ||
| 143 | xasprintf(&sc_connection.output, "%s", mysql_error(&mysql)); | ||
| 144 | } else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR) { | ||
| 145 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_WARNING); | ||
| 146 | xasprintf(&sc_connection.output, "%s", mysql_error(&mysql)); | ||
| 147 | } else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR) { | ||
| 148 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_WARNING); | ||
| 149 | xasprintf(&sc_connection.output, "%s", mysql_error(&mysql)); | ||
| 150 | } else { | ||
| 151 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_CRITICAL); | ||
| 152 | xasprintf(&sc_connection.output, "%s", mysql_error(&mysql)); | ||
| 153 | } | ||
| 154 | } | 154 | } |
| 155 | else if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) | 155 | |
| 156 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | 156 | mp_add_subcheck_to_check(&overall, sc_connection); |
| 157 | else if (mysql_errno (&mysql) == CR_VERSION_ERROR) | 157 | mp_exit(overall); |
| 158 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | 158 | } else { |
| 159 | else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) | 159 | // successful connection |
| 160 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | 160 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_OK); |
| 161 | else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) | 161 | xasprintf(&sc_connection.output, "Version: %s (protocol %d)", mysql_get_server_info(&mysql), |
| 162 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | 162 | mysql_get_proto_info(&mysql)); |
| 163 | else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) | 163 | mp_add_subcheck_to_check(&overall, sc_connection); |
| 164 | die (STATE_WARNING, "%s\n", mysql_error (&mysql)); | ||
| 165 | else | ||
| 166 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | ||
| 167 | } | 164 | } |
| 168 | 165 | ||
| 169 | /* get the server stats */ | 166 | /* get the server stats */ |
| 170 | result = strdup (mysql_stat (&mysql)); | 167 | char *mysql_stats = strdup(mysql_stat(&mysql)); |
| 168 | |||
| 169 | mp_subcheck sc_stats = mp_subcheck_init(); | ||
| 170 | sc_stats = mp_set_subcheck_default_state(sc_stats, STATE_OK); | ||
| 171 | 171 | ||
| 172 | /* error checking once more */ | 172 | /* error checking once more */ |
| 173 | if (mysql_error (&mysql)) { | 173 | if (mysql_errno(&mysql) != 0) { |
| 174 | if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) | 174 | if ((mysql_errno(&mysql) == CR_SERVER_GONE_ERROR) || |
| 175 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | 175 | (mysql_errno(&mysql) == CR_SERVER_LOST) || (mysql_errno(&mysql) == CR_UNKNOWN_ERROR)) { |
| 176 | else if (mysql_errno (&mysql) == CR_SERVER_LOST) | 176 | sc_stats = mp_set_subcheck_state(sc_stats, STATE_CRITICAL); |
| 177 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | 177 | xasprintf(&sc_stats.output, "Retrieving stats failed: %s", mysql_error(&mysql)); |
| 178 | else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) | 178 | } else { |
| 179 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | 179 | // not sure which error modes occur here, but mysql_error indicates an error |
| 180 | sc_stats = mp_set_subcheck_state(sc_stats, STATE_WARNING); | ||
| 181 | xasprintf(&sc_stats.output, "retrieving stats caused an error: %s", | ||
| 182 | mysql_error(&mysql)); | ||
| 183 | } | ||
| 184 | |||
| 185 | mp_add_subcheck_to_check(&overall, sc_stats); | ||
| 186 | mp_exit(overall); | ||
| 187 | } else { | ||
| 188 | xasprintf(&sc_stats.output, "retrieved stats: %s", mysql_stats); | ||
| 189 | sc_stats = mp_set_subcheck_state(sc_stats, STATE_OK); | ||
| 190 | mp_add_subcheck_to_check(&overall, sc_stats); | ||
| 180 | } | 191 | } |
| 181 | 192 | ||
| 193 | MYSQL_RES *res; | ||
| 194 | MYSQL_ROW row; | ||
| 195 | mp_subcheck sc_query = mp_subcheck_init(); | ||
| 182 | /* try to fetch some perf data */ | 196 | /* try to fetch some perf data */ |
| 183 | if (mysql_query (&mysql, "show global status") == 0) { | 197 | if (mysql_query(&mysql, "show global status") == 0) { |
| 184 | if ( (res = mysql_store_result (&mysql)) == NULL) { | 198 | if ((res = mysql_store_result(&mysql)) == NULL) { |
| 185 | error = strdup(mysql_error(&mysql)); | 199 | xasprintf(&sc_connection.output, "query failed - status store_result error: %s", |
| 186 | mysql_close (&mysql); | 200 | mysql_error(&mysql)); |
| 187 | die (STATE_CRITICAL, _("status store_result error: %s\n"), error); | 201 | mysql_close(&mysql); |
| 188 | } | ||
| 189 | 202 | ||
| 190 | while ( (row = mysql_fetch_row (res)) != NULL) { | 203 | sc_query = mp_set_subcheck_state(sc_query, STATE_CRITICAL); |
| 191 | int i; | 204 | mp_add_subcheck_to_check(&overall, sc_query); |
| 205 | mp_exit(overall); | ||
| 206 | } | ||
| 192 | 207 | ||
| 193 | for(i = 0; i < LENGTH_METRIC_UNIT; i++) { | 208 | while ((row = mysql_fetch_row(res)) != NULL) { |
| 209 | for (int i = 0; i < LENGTH_METRIC_UNIT; i++) { | ||
| 194 | if (strcmp(row[0], metric_unit[i]) == 0) { | 210 | if (strcmp(row[0], metric_unit[i]) == 0) { |
| 195 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], | 211 | mp_perfdata pd_mysql_stat = perfdata_init(); |
| 196 | atol(row[1]), "", false, 0, false, 0, false, 0, false, 0)); | 212 | pd_mysql_stat.label = (char *)metric_unit[i]; |
| 213 | pd_mysql_stat.value = mp_create_pd_value(atol(row[1])); | ||
| 214 | mp_add_perfdata_to_subcheck(&sc_stats, pd_mysql_stat); | ||
| 197 | continue; | 215 | continue; |
| 198 | } | 216 | } |
| 199 | } | 217 | } |
| 200 | for(i = 0; i < LENGTH_METRIC_COUNTER; i++) { | 218 | |
| 219 | for (int i = 0; i < LENGTH_METRIC_COUNTER; i++) { | ||
| 201 | if (strcmp(row[0], metric_counter[i]) == 0) { | 220 | if (strcmp(row[0], metric_counter[i]) == 0) { |
| 202 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], | 221 | mp_perfdata pd_mysql_stat = perfdata_init(); |
| 203 | atol(row[1]), "c", false, 0, false, 0, false, 0, false, 0)); | 222 | pd_mysql_stat.label = (char *)metric_counter[i]; |
| 223 | pd_mysql_stat.value = mp_create_pd_value(atol(row[1])); | ||
| 224 | pd_mysql_stat.uom = "c"; | ||
| 225 | mp_add_perfdata_to_subcheck(&sc_stats, pd_mysql_stat); | ||
| 204 | continue; | 226 | continue; |
| 205 | } | 227 | } |
| 206 | } | 228 | } |
| 207 | } | 229 | } |
| 208 | /* remove trailing space */ | 230 | } else { |
| 209 | if (strlen(perf) > 0) | 231 | // Query failed! |
| 210 | perf[strlen(perf) - 1] = '\0'; | 232 | xasprintf(&sc_connection.output, "query failed"); |
| 233 | sc_query = mp_set_subcheck_state(sc_query, STATE_CRITICAL); | ||
| 234 | mp_add_subcheck_to_check(&overall, sc_query); | ||
| 235 | mp_exit(overall); | ||
| 211 | } | 236 | } |
| 212 | 237 | ||
| 213 | if(check_slave) { | 238 | if (config.check_replica) { |
| 214 | /* check the slave status */ | 239 | // Detect which version we are, on older version |
| 215 | if (mysql_query (&mysql, "show slave status") != 0) { | 240 | // "show slave status" should work, on newer ones |
| 216 | error = strdup(mysql_error(&mysql)); | 241 | // "show replica status" |
| 217 | mysql_close (&mysql); | 242 | // But first we have to find out whether this is |
| 218 | die (STATE_CRITICAL, _("slave query error: %s\n"), error); | 243 | // MySQL or MariaDB since the version numbering scheme |
| 244 | // is different | ||
| 245 | bool use_deprecated_slave_status = false; | ||
| 246 | const char *server_version = mysql_get_server_info(&mysql); | ||
| 247 | unsigned long server_verion_int = mysql_get_server_version(&mysql); | ||
| 248 | unsigned long major_version = server_verion_int / 10000; | ||
| 249 | unsigned long minor_version = (server_verion_int % 10000) / 100; | ||
| 250 | unsigned long patch_version = (server_verion_int % 100); | ||
| 251 | |||
| 252 | if (verbose) { | ||
| 253 | printf("Found MariaDB/MySQL: %s, main version: %lu, minor version: %lu, patch version: " | ||
| 254 | "%lu\n", | ||
| 255 | server_version, major_version, minor_version, patch_version); | ||
| 256 | } | ||
| 257 | |||
| 258 | if (strstr(server_version, "MariaDB") != NULL) { | ||
| 259 | // Looks like MariaDB, new commands should be available after 10.5.1 | ||
| 260 | if (major_version < 10) { | ||
| 261 | use_deprecated_slave_status = true; | ||
| 262 | } else if (major_version == 10) { | ||
| 263 | if (minor_version < 5) { | ||
| 264 | use_deprecated_slave_status = true; | ||
| 265 | } else if (minor_version == 5 && patch_version < 1) { | ||
| 266 | use_deprecated_slave_status = true; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | } else { | ||
| 270 | // Looks like MySQL or at least not like MariaDB | ||
| 271 | if (major_version < 8) { | ||
| 272 | use_deprecated_slave_status = true; | ||
| 273 | } else if (major_version == 10 && minor_version < 4) { | ||
| 274 | use_deprecated_slave_status = true; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | char *replica_query = NULL; | ||
| 279 | if (use_deprecated_slave_status) { | ||
| 280 | replica_query = "show slave status"; | ||
| 281 | } else { | ||
| 282 | replica_query = "show replica status"; | ||
| 283 | } | ||
| 284 | |||
| 285 | mp_subcheck sc_replica = mp_subcheck_init(); | ||
| 286 | |||
| 287 | /* check the replica status */ | ||
| 288 | if (mysql_query(&mysql, replica_query) != 0) { | ||
| 289 | xasprintf(&sc_replica.output, "replica query error: %s", mysql_error(&mysql)); | ||
| 290 | mysql_close(&mysql); | ||
| 291 | |||
| 292 | sc_replica = mp_set_subcheck_state(sc_replica, STATE_CRITICAL); | ||
| 293 | mp_add_subcheck_to_check(&overall, sc_replica); | ||
| 294 | mp_exit(overall); | ||
| 219 | } | 295 | } |
| 220 | 296 | ||
| 221 | /* store the result */ | 297 | /* store the result */ |
| 222 | if ( (res = mysql_store_result (&mysql)) == NULL) { | 298 | if ((res = mysql_store_result(&mysql)) == NULL) { |
| 223 | error = strdup(mysql_error(&mysql)); | 299 | xasprintf(&sc_replica.output, "replica store_result error: %s", mysql_error(&mysql)); |
| 224 | mysql_close (&mysql); | 300 | mysql_close(&mysql); |
| 225 | die (STATE_CRITICAL, _("slave store_result error: %s\n"), error); | 301 | |
| 302 | sc_replica = mp_set_subcheck_state(sc_replica, STATE_CRITICAL); | ||
| 303 | mp_add_subcheck_to_check(&overall, sc_replica); | ||
| 304 | mp_exit(overall); | ||
| 226 | } | 305 | } |
| 227 | 306 | ||
| 228 | /* Check there is some data */ | 307 | /* Check there is some data */ |
| 229 | if (mysql_num_rows(res) == 0) { | 308 | if (mysql_num_rows(res) == 0) { |
| 230 | mysql_close(&mysql); | 309 | mysql_close(&mysql); |
| 231 | die (STATE_WARNING, "%s\n", _("No slaves defined")); | 310 | |
| 311 | xasprintf(&sc_replica.output, "no replicas defined"); | ||
| 312 | sc_replica = mp_set_subcheck_state(sc_replica, STATE_WARNING); | ||
| 313 | mp_add_subcheck_to_check(&overall, sc_replica); | ||
| 314 | mp_exit(overall); | ||
| 232 | } | 315 | } |
| 233 | 316 | ||
| 234 | /* fetch the first row */ | 317 | /* fetch the first row */ |
| 235 | if ( (row = mysql_fetch_row (res)) == NULL) { | 318 | if ((row = mysql_fetch_row(res)) == NULL) { |
| 236 | error = strdup(mysql_error(&mysql)); | 319 | xasprintf(&sc_replica.output, "replica fetch row error: %s", mysql_error(&mysql)); |
| 237 | mysql_free_result (res); | 320 | mysql_free_result(res); |
| 238 | mysql_close (&mysql); | 321 | mysql_close(&mysql); |
| 239 | die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error); | 322 | |
| 323 | sc_replica = mp_set_subcheck_state(sc_replica, STATE_CRITICAL); | ||
| 324 | mp_add_subcheck_to_check(&overall, sc_replica); | ||
| 325 | mp_exit(overall); | ||
| 240 | } | 326 | } |
| 241 | 327 | ||
| 242 | if (mysql_field_count (&mysql) == 12) { | 328 | if (mysql_field_count(&mysql) == 12) { |
| 243 | /* mysql 3.23.x */ | 329 | /* mysql 3.23.x */ |
| 244 | snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]); | 330 | xasprintf(&sc_replica.output, "Replica running: %s", row[6]); |
| 245 | if (strcmp (row[6], "Yes") != 0) { | 331 | if (strcmp(row[6], "Yes") != 0) { |
| 246 | mysql_free_result (res); | 332 | mysql_free_result(res); |
| 247 | mysql_close (&mysql); | 333 | mysql_close(&mysql); |
| 248 | die (STATE_CRITICAL, "%s\n", slaveresult); | 334 | |
| 335 | sc_replica = mp_set_subcheck_state(sc_replica, STATE_CRITICAL); | ||
| 336 | mp_add_subcheck_to_check(&overall, sc_replica); | ||
| 337 | mp_exit(overall); | ||
| 249 | } | 338 | } |
| 250 | |||
| 251 | } else { | 339 | } else { |
| 252 | /* mysql 4.x.x and mysql 5.x.x */ | 340 | /* 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; | 341 | int replica_io_field = -1; |
| 254 | MYSQL_FIELD* fields; | 342 | int replica_sql_field = -1; |
| 255 | 343 | int seconds_behind_field = -1; | |
| 344 | int num_fields; | ||
| 345 | MYSQL_FIELD *fields; | ||
| 256 | num_fields = mysql_num_fields(res); | 346 | num_fields = mysql_num_fields(res); |
| 257 | fields = mysql_fetch_fields(res); | 347 | fields = mysql_fetch_fields(res); |
| 258 | for(i = 0; i < num_fields; i++) { | 348 | for (int i = 0; i < num_fields; i++) { |
| 259 | if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { | 349 | if (use_deprecated_slave_status) { |
| 260 | slave_io_field = i; | 350 | if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { |
| 261 | continue; | 351 | replica_io_field = i; |
| 262 | } | 352 | continue; |
| 263 | if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { | 353 | } |
| 264 | slave_sql_field = i; | 354 | if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { |
| 265 | continue; | 355 | replica_sql_field = i; |
| 266 | } | 356 | continue; |
| 267 | if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { | 357 | } |
| 268 | seconds_behind_field = i; | 358 | if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { |
| 269 | continue; | 359 | seconds_behind_field = i; |
| 360 | continue; | ||
| 361 | } | ||
| 362 | } else { | ||
| 363 | if (strcmp(fields[i].name, "Replica_IO_Running") == 0) { | ||
| 364 | replica_io_field = i; | ||
| 365 | continue; | ||
| 366 | } | ||
| 367 | if (strcmp(fields[i].name, "Replica_SQL_Running") == 0) { | ||
| 368 | replica_sql_field = i; | ||
| 369 | continue; | ||
| 370 | } | ||
| 371 | if (strcmp(fields[i].name, "Seconds_Behind_Source") == 0) { | ||
| 372 | seconds_behind_field = i; | ||
| 373 | continue; | ||
| 374 | } | ||
| 270 | } | 375 | } |
| 271 | } | 376 | } |
| 272 | 377 | ||
| 273 | /* Check if slave status is available */ | 378 | /* Check if replica status is available */ |
| 274 | if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { | 379 | if ((replica_io_field < 0) || (replica_sql_field < 0) || (num_fields == 0)) { |
| 275 | mysql_free_result (res); | 380 | mysql_free_result(res); |
| 276 | mysql_close (&mysql); | 381 | mysql_close(&mysql); |
| 277 | die (STATE_CRITICAL, "Slave status unavailable\n"); | 382 | |
| 383 | xasprintf(&sc_replica.output, "Replica status unavailable"); | ||
| 384 | sc_replica = mp_set_subcheck_state(sc_replica, STATE_CRITICAL); | ||
| 385 | mp_add_subcheck_to_check(&overall, sc_replica); | ||
| 386 | mp_exit(overall); | ||
| 278 | } | 387 | } |
| 279 | 388 | ||
| 280 | /* Save slave status in slaveresult */ | 389 | /* Save replica status in replica_result */ |
| 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"); | 390 | xasprintf(&sc_replica.output, |
| 391 | "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", | ||
| 392 | row[replica_io_field], row[replica_sql_field], | ||
| 393 | seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown"); | ||
| 282 | 394 | ||
| 283 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */ | 395 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no |
| 284 | if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { | 396 | * mysqldump threads running */ |
| 397 | if (strcmp(row[replica_io_field], "Yes") != 0 || | ||
| 398 | strcmp(row[replica_sql_field], "Yes") != 0) { | ||
| 285 | MYSQL_RES *res_mysqldump; | 399 | MYSQL_RES *res_mysqldump; |
| 286 | MYSQL_ROW row_mysqldump; | 400 | MYSQL_ROW row_mysqldump; |
| 287 | unsigned int mysqldump_threads = 0; | 401 | unsigned int mysqldump_threads = 0; |
| 288 | 402 | ||
| 289 | if (mysql_query (&mysql, MYSQLDUMP_THREADS_QUERY) == 0) { | 403 | if (mysql_query(&mysql, MYSQLDUMP_THREADS_QUERY) == 0) { |
| 290 | /* store the result */ | 404 | /* store the result */ |
| 291 | if ( (res_mysqldump = mysql_store_result (&mysql)) != NULL) { | 405 | if ((res_mysqldump = mysql_store_result(&mysql)) != NULL) { |
| 292 | if (mysql_num_rows(res_mysqldump) == 1) { | 406 | if (mysql_num_rows(res_mysqldump) == 1) { |
| 293 | if ( (row_mysqldump = mysql_fetch_row (res_mysqldump)) != NULL) { | 407 | if ((row_mysqldump = mysql_fetch_row(res_mysqldump)) != NULL) { |
| 294 | mysqldump_threads = atoi(row_mysqldump[0]); | 408 | mysqldump_threads = atoi(row_mysqldump[0]); |
| 295 | } | 409 | } |
| 296 | } | 410 | } |
| 297 | /* free the result */ | 411 | /* free the result */ |
| 298 | mysql_free_result (res_mysqldump); | 412 | mysql_free_result(res_mysqldump); |
| 299 | } | 413 | } |
| 300 | mysql_close (&mysql); | 414 | mysql_close(&mysql); |
| 301 | } | 415 | } |
| 416 | |||
| 302 | if (mysqldump_threads == 0) { | 417 | if (mysqldump_threads == 0) { |
| 303 | die (STATE_CRITICAL, "%s\n", slaveresult); | 418 | sc_replica = mp_set_subcheck_state(sc_replica, STATE_CRITICAL); |
| 419 | mp_add_subcheck_to_check(&overall, sc_replica); | ||
| 420 | mp_exit(overall); | ||
| 304 | } else { | 421 | } else { |
| 305 | strncat(slaveresult, " Mysqldump: in progress", SLAVERESULTSIZE-1); | 422 | xasprintf(&sc_replica.output, "%s %s", sc_replica.output, |
| 423 | " Mysqldump: in progress"); | ||
| 306 | } | 424 | } |
| 307 | } | 425 | } |
| 308 | 426 | ||
| 309 | if (verbose >=3) { | 427 | if (verbose >= 3) { |
| 310 | if (seconds_behind_field == -1) { | 428 | if (seconds_behind_field == -1) { |
| 311 | printf("seconds_behind_field not found\n"); | 429 | printf("seconds_behind_field not found\n"); |
| 312 | } else { | 430 | } else { |
| 313 | printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); | 431 | printf("seconds_behind_field(index %d)=%s\n", seconds_behind_field, |
| 432 | row[seconds_behind_field]); | ||
| 314 | } | 433 | } |
| 315 | } | 434 | } |
| 316 | 435 | ||
| 317 | /* Check Seconds Behind against threshold */ | 436 | /* Check Seconds Behind against threshold */ |
| 318 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp (row[seconds_behind_field], "NULL") != 0)) { | 437 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && |
| 319 | double value = atof(row[seconds_behind_field]); | 438 | strcmp(row[seconds_behind_field], "NULL") != 0)) { |
| 320 | int status; | 439 | mp_perfdata pd_seconds_behind = perfdata_init(); |
| 321 | 440 | pd_seconds_behind.label = "seconds behind master"; | |
| 322 | status = get_status(value, my_threshold); | 441 | pd_seconds_behind.value = mp_create_pd_value(atof(row[seconds_behind_field])); |
| 323 | 442 | pd_seconds_behind = | |
| 324 | xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s", | 443 | mp_pd_set_thresholds(pd_seconds_behind, config.replica_thresholds); |
| 325 | true, (double) warning_time, | 444 | pd_seconds_behind.uom = "s"; |
| 326 | true, (double) critical_time, | 445 | mp_add_perfdata_to_subcheck(&sc_replica, pd_seconds_behind); |
| 327 | false, 0, | 446 | |
| 328 | false, 0)); | 447 | mp_state_enum status = mp_get_pd_status(pd_seconds_behind); |
| 329 | 448 | ||
| 330 | if (status == STATE_WARNING) { | 449 | sc_replica = mp_set_subcheck_state(sc_replica, status); |
| 331 | printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); | 450 | |
| 332 | exit(STATE_WARNING); | 451 | if (status != STATE_OK) { |
| 333 | } else if (status == STATE_CRITICAL) { | 452 | xasprintf(&sc_replica.output, "slow replica - %s", sc_replica.output); |
| 334 | printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf); | 453 | mp_add_subcheck_to_check(&overall, sc_replica); |
| 335 | exit(STATE_CRITICAL); | 454 | mp_exit(overall); |
| 336 | } | 455 | } |
| 337 | } | 456 | } |
| 338 | } | 457 | } |
| 339 | 458 | ||
| 340 | /* free the result */ | 459 | /* free the result */ |
| 341 | mysql_free_result (res); | 460 | mysql_free_result(res); |
| 342 | } | 461 | } |
| 343 | 462 | ||
| 344 | /* close the connection */ | 463 | /* close the connection */ |
| 345 | mysql_close (&mysql); | 464 | mysql_close(&mysql); |
| 346 | 465 | ||
| 347 | /* print out the result of stats */ | 466 | mp_exit(overall); |
| 348 | if (check_slave) { | ||
| 349 | printf ("%s %s|%s\n", result, slaveresult, perf); | ||
| 350 | } else { | ||
| 351 | printf ("%s|%s\n", result, perf); | ||
| 352 | } | ||
| 353 | |||
| 354 | return STATE_OK; | ||
| 355 | } | 467 | } |
| 356 | 468 | ||
| 357 | |||
| 358 | /* process command-line arguments */ | 469 | /* process command-line arguments */ |
| 359 | int | 470 | check_mysql_config_wrapper process_arguments(int argc, char **argv) { |
| 360 | process_arguments (int argc, char **argv) | ||
| 361 | { | ||
| 362 | int c; | ||
| 363 | char *warning = NULL; | ||
| 364 | char *critical = NULL; | ||
| 365 | 471 | ||
| 366 | int option = 0; | 472 | enum { |
| 367 | static struct option longopts[] = { | 473 | CHECK_REPLICA_OPT = CHAR_MAX + 1, |
| 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'}, | ||
| 389 | {0, 0, 0, 0} | ||
| 390 | }; | 474 | }; |
| 391 | 475 | ||
| 392 | if (argc < 1) | 476 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 393 | return ERROR; | 477 | {"socket", required_argument, 0, 's'}, |
| 478 | {"database", required_argument, 0, 'd'}, | ||
| 479 | {"username", required_argument, 0, 'u'}, | ||
| 480 | {"password", required_argument, 0, 'p'}, | ||
| 481 | {"file", required_argument, 0, 'f'}, | ||
| 482 | {"group", required_argument, 0, 'g'}, | ||
| 483 | {"port", required_argument, 0, 'P'}, | ||
| 484 | {"critical", required_argument, 0, 'c'}, | ||
| 485 | {"warning", required_argument, 0, 'w'}, | ||
| 486 | {"check-slave", no_argument, 0, 'S'}, | ||
| 487 | {"check-replica", no_argument, 0, CHECK_REPLICA_OPT}, | ||
| 488 | {"ignore-auth", no_argument, 0, 'n'}, | ||
| 489 | {"verbose", no_argument, 0, 'v'}, | ||
| 490 | {"version", no_argument, 0, 'V'}, | ||
| 491 | {"help", no_argument, 0, 'h'}, | ||
| 492 | {"ssl", no_argument, 0, 'l'}, | ||
| 493 | {"ca-cert", optional_argument, 0, 'C'}, | ||
| 494 | {"key", required_argument, 0, 'k'}, | ||
| 495 | {"cert", required_argument, 0, 'a'}, | ||
| 496 | {"ca-dir", required_argument, 0, 'D'}, | ||
| 497 | {"ciphers", required_argument, 0, 'L'}, | ||
| 498 | {0, 0, 0, 0}}; | ||
| 499 | |||
| 500 | check_mysql_config_wrapper result = { | ||
| 501 | .errorcode = OK, | ||
| 502 | .config = check_mysql_config_init(), | ||
| 503 | }; | ||
| 394 | 504 | ||
| 395 | while (1) { | 505 | if (argc < 1) { |
| 396 | c = getopt_long (argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); | 506 | result.errorcode = ERROR; |
| 507 | return result; | ||
| 508 | } | ||
| 509 | |||
| 510 | int option = 0; | ||
| 511 | while (true) { | ||
| 512 | int option_index = | ||
| 513 | getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); | ||
| 397 | 514 | ||
| 398 | if (c == -1 || c == EOF) | 515 | if (option_index == -1 || option_index == EOF) { |
| 399 | break; | 516 | break; |
| 517 | } | ||
| 400 | 518 | ||
| 401 | switch (c) { | 519 | switch (option_index) { |
| 402 | case 'H': /* hostname */ | 520 | case 'H': /* hostname */ |
| 403 | if (is_host (optarg)) { | 521 | if (is_host(optarg)) { |
| 404 | db_host = optarg; | 522 | result.config.db_host = optarg; |
| 405 | } | 523 | } else if (*optarg == '/') { |
| 406 | else if (*optarg == '/') { | 524 | result.config.db_socket = optarg; |
| 407 | db_socket = optarg; | 525 | } else { |
| 408 | } | 526 | usage2(_("Invalid hostname/address"), optarg); |
| 409 | else { | ||
| 410 | usage2 (_("Invalid hostname/address"), optarg); | ||
| 411 | } | 527 | } |
| 412 | break; | 528 | break; |
| 413 | case 's': /* socket */ | 529 | case 's': /* socket */ |
| 414 | db_socket = optarg; | 530 | result.config.db_socket = optarg; |
| 415 | break; | 531 | break; |
| 416 | case 'd': /* database */ | 532 | case 'd': /* database */ |
| 417 | db = optarg; | 533 | result.config.db = optarg; |
| 418 | break; | 534 | break; |
| 419 | case 'l': | 535 | case 'l': |
| 420 | ssl = true; | 536 | result.config.ssl = true; |
| 421 | break; | 537 | break; |
| 422 | case 'C': | 538 | case 'C': |
| 423 | ca_cert = optarg; | 539 | result.config.ca_cert = optarg; |
| 424 | break; | 540 | break; |
| 425 | case 'a': | 541 | case 'a': |
| 426 | cert = optarg; | 542 | result.config.cert = optarg; |
| 427 | break; | 543 | break; |
| 428 | case 'k': | 544 | case 'k': |
| 429 | key = optarg; | 545 | result.config.key = optarg; |
| 430 | break; | 546 | break; |
| 431 | case 'D': | 547 | case 'D': |
| 432 | ca_dir = optarg; | 548 | result.config.ca_dir = optarg; |
| 433 | break; | 549 | break; |
| 434 | case 'L': | 550 | case 'L': |
| 435 | ciphers = optarg; | 551 | result.config.ciphers = optarg; |
| 436 | break; | 552 | break; |
| 437 | case 'u': /* username */ | 553 | case 'u': /* username */ |
| 438 | db_user = optarg; | 554 | result.config.db_user = optarg; |
| 439 | break; | 555 | break; |
| 440 | case 'p': /* authentication information: password */ | 556 | case 'p': /* authentication information: password */ |
| 441 | db_pass = strdup(optarg); | 557 | result.config.db_pass = strdup(optarg); |
| 442 | 558 | ||
| 443 | /* Delete the password from process list */ | 559 | /* Delete the password from process list */ |
| 444 | while (*optarg != '\0') { | 560 | while (*optarg != '\0') { |
| @@ -446,167 +562,172 @@ process_arguments (int argc, char **argv) | |||
| 446 | optarg++; | 562 | optarg++; |
| 447 | } | 563 | } |
| 448 | break; | 564 | break; |
| 449 | case 'f': /* client options file */ | 565 | case 'f': /* client options file */ |
| 450 | opt_file = optarg; | 566 | result.config.opt_file = optarg; |
| 451 | break; | 567 | break; |
| 452 | case 'g': /* client options group */ | 568 | case 'g': /* client options group */ |
| 453 | opt_group = optarg; | 569 | result.config.opt_group = optarg; |
| 454 | break; | 570 | break; |
| 455 | case 'P': /* critical time threshold */ | 571 | case 'P': /* critical time threshold */ |
| 456 | db_port = atoi (optarg); | 572 | result.config.db_port = atoi(optarg); |
| 457 | break; | 573 | break; |
| 458 | case 'S': | 574 | case 'S': |
| 459 | check_slave = 1; /* check-slave */ | 575 | case CHECK_REPLICA_OPT: |
| 576 | result.config.check_replica = true; /* check-slave */ | ||
| 460 | break; | 577 | break; |
| 461 | case 'n': | 578 | case 'n': |
| 462 | ignore_auth = 1; /* ignore-auth */ | 579 | result.config.ignore_auth = true; /* ignore-auth */ |
| 463 | break; | 580 | break; |
| 464 | case 'w': | 581 | case 'w': { |
| 465 | warning = optarg; | 582 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 466 | warning_time = strtod (warning, NULL); | 583 | if (tmp.error != MP_PARSING_SUCCES) { |
| 467 | break; | 584 | die(STATE_UNKNOWN, "failed to parse warning time threshold"); |
| 468 | case 'c': | 585 | } |
| 469 | critical = optarg; | 586 | result.config.replica_thresholds = |
| 470 | critical_time = strtod (critical, NULL); | 587 | mp_thresholds_set_warn(result.config.replica_thresholds, tmp.range); |
| 471 | break; | 588 | } break; |
| 472 | case 'V': /* version */ | 589 | case 'c': { |
| 473 | print_revision (progname, NP_VERSION); | 590 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 474 | exit (STATE_UNKNOWN); | 591 | if (tmp.error != MP_PARSING_SUCCES) { |
| 475 | case 'h': /* help */ | 592 | die(STATE_UNKNOWN, "failed to parse critical time threshold"); |
| 476 | print_help (); | 593 | } |
| 477 | exit (STATE_UNKNOWN); | 594 | result.config.replica_thresholds = |
| 595 | mp_thresholds_set_crit(result.config.replica_thresholds, tmp.range); | ||
| 596 | } break; | ||
| 597 | case 'V': /* version */ | ||
| 598 | print_revision(progname, NP_VERSION); | ||
| 599 | exit(STATE_UNKNOWN); | ||
| 600 | case 'h': /* help */ | ||
| 601 | print_help(); | ||
| 602 | exit(STATE_UNKNOWN); | ||
| 478 | case 'v': | 603 | case 'v': |
| 479 | verbose++; | 604 | verbose++; |
| 480 | break; | 605 | break; |
| 481 | case '?': /* help */ | 606 | case '?': /* help */ |
| 482 | usage5 (); | 607 | usage5(); |
| 483 | } | 608 | } |
| 484 | } | 609 | } |
| 485 | 610 | ||
| 486 | c = optind; | 611 | int index = optind; |
| 487 | |||
| 488 | set_thresholds(&my_threshold, warning, critical); | ||
| 489 | |||
| 490 | while ( argc > c ) { | ||
| 491 | 612 | ||
| 492 | if (db_host == NULL) | 613 | while (argc > index) { |
| 493 | if (is_host (argv[c])) { | 614 | if (result.config.db_host == NULL) { |
| 494 | db_host = argv[c++]; | 615 | if (is_host(argv[index])) { |
| 616 | result.config.db_host = argv[index++]; | ||
| 617 | } else { | ||
| 618 | usage2(_("Invalid hostname/address"), argv[index]); | ||
| 495 | } | 619 | } |
| 496 | else { | 620 | } else if (result.config.db_user == NULL) { |
| 497 | usage2 (_("Invalid hostname/address"), argv[c]); | 621 | result.config.db_user = argv[index++]; |
| 498 | } | 622 | } else if (result.config.db_pass == NULL) { |
| 499 | else if (db_user == NULL) | 623 | result.config.db_pass = argv[index++]; |
| 500 | db_user = argv[c++]; | 624 | } else if (result.config.db == NULL) { |
| 501 | else if (db_pass == NULL) | 625 | result.config.db = argv[index++]; |
| 502 | db_pass = argv[c++]; | 626 | } else if (is_intnonneg(argv[index])) { |
| 503 | else if (db == NULL) | 627 | result.config.db_port = atoi(argv[index++]); |
| 504 | db = argv[c++]; | 628 | } else { |
| 505 | else if (is_intnonneg (argv[c])) | ||
| 506 | db_port = atoi (argv[c++]); | ||
| 507 | else | ||
| 508 | break; | 629 | break; |
| 630 | } | ||
| 509 | } | 631 | } |
| 510 | 632 | ||
| 511 | return validate_arguments (); | 633 | return validate_arguments(result); |
| 512 | } | 634 | } |
| 513 | 635 | ||
| 636 | check_mysql_config_wrapper validate_arguments(check_mysql_config_wrapper config_wrapper) { | ||
| 637 | if (config_wrapper.config.db_user == NULL) { | ||
| 638 | config_wrapper.config.db_user = strdup(""); | ||
| 639 | } | ||
| 514 | 640 | ||
| 515 | int | 641 | if (config_wrapper.config.db_host == NULL) { |
| 516 | validate_arguments (void) | 642 | config_wrapper.config.db_host = strdup(""); |
| 517 | { | 643 | } |
| 518 | if (db_user == NULL) | ||
| 519 | db_user = strdup(""); | ||
| 520 | |||
| 521 | if (db_host == NULL) | ||
| 522 | db_host = strdup(""); | ||
| 523 | 644 | ||
| 524 | if (db == NULL) | 645 | if (config_wrapper.config.db == NULL) { |
| 525 | db = strdup(""); | 646 | config_wrapper.config.db = strdup(""); |
| 647 | } | ||
| 526 | 648 | ||
| 527 | return OK; | 649 | return config_wrapper; |
| 528 | } | 650 | } |
| 529 | 651 | ||
| 530 | 652 | void print_help(void) { | |
| 531 | void | ||
| 532 | print_help (void) | ||
| 533 | { | ||
| 534 | char *myport; | 653 | char *myport; |
| 535 | xasprintf (&myport, "%d", MYSQL_PORT); | 654 | xasprintf(&myport, "%d", MYSQL_PORT); |
| 536 | 655 | ||
| 537 | print_revision (progname, NP_VERSION); | 656 | print_revision(progname, NP_VERSION); |
| 538 | 657 | ||
| 539 | printf (_(COPYRIGHT), copyright, email); | 658 | printf(_(COPYRIGHT), copyright, email); |
| 540 | 659 | ||
| 541 | printf ("%s\n", _("This program tests connections to a MySQL server")); | 660 | printf("%s\n", _("This program tests connections to a MySQL server")); |
| 542 | 661 | ||
| 543 | printf ("\n\n"); | 662 | printf("\n\n"); |
| 544 | 663 | ||
| 545 | print_usage (); | 664 | print_usage(); |
| 546 | 665 | ||
| 547 | printf (UT_HELP_VRSN); | 666 | printf(UT_HELP_VRSN); |
| 548 | printf (UT_EXTRA_OPTS); | 667 | printf(UT_EXTRA_OPTS); |
| 549 | 668 | ||
| 550 | printf (UT_HOST_PORT, 'P', myport); | 669 | printf(UT_HOST_PORT, 'P', myport); |
| 551 | printf (" %s\n", "-n, --ignore-auth"); | 670 | printf(" %s\n", "-n, --ignore-auth"); |
| 552 | printf (" %s\n", _("Ignore authentication failure and check for mysql connectivity only")); | 671 | printf(" %s\n", _("Ignore authentication failure and check for mysql connectivity only")); |
| 553 | 672 | ||
| 554 | printf (" %s\n", "-s, --socket=STRING"); | 673 | printf(" %s\n", "-s, --socket=STRING"); |
| 555 | printf (" %s\n", _("Use the specified socket (has no effect if -H is used)")); | 674 | printf(" %s\n", _("Use the specified socket (has no effect if -H is used)")); |
| 556 | 675 | ||
| 557 | printf (" %s\n", "-d, --database=STRING"); | 676 | printf(" %s\n", "-d, --database=STRING"); |
| 558 | printf (" %s\n", _("Check database with indicated name")); | 677 | printf(" %s\n", _("Check database with indicated name")); |
| 559 | printf (" %s\n", "-f, --file=STRING"); | 678 | printf(" %s\n", "-f, --file=STRING"); |
| 560 | printf (" %s\n", _("Read from the specified client options file")); | 679 | printf(" %s\n", _("Read from the specified client options file")); |
| 561 | printf (" %s\n", "-g, --group=STRING"); | 680 | printf(" %s\n", "-g, --group=STRING"); |
| 562 | printf (" %s\n", _("Use a client options group")); | 681 | printf(" %s\n", _("Use a client options group")); |
| 563 | printf (" %s\n", "-u, --username=STRING"); | 682 | printf(" %s\n", "-u, --username=STRING"); |
| 564 | printf (" %s\n", _("Connect using the indicated username")); | 683 | printf(" %s\n", _("Connect using the indicated username")); |
| 565 | printf (" %s\n", "-p, --password=STRING"); | 684 | printf(" %s\n", "-p, --password=STRING"); |
| 566 | printf (" %s\n", _("Use the indicated password to authenticate the connection")); | 685 | printf(" %s\n", _("Use the indicated password to authenticate the connection")); |
| 567 | printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); | 686 | 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")); | 687 | printf(" %s\n", _("Your clear-text password could be visible as a process table entry")); |
| 569 | printf (" %s\n", "-S, --check-slave"); | 688 | printf(" %s\n", "-S, --check-slave"); |
| 570 | printf (" %s\n", _("Check if the slave thread is running properly.")); | 689 | printf(" %s\n", _("Check if the slave thread is running properly. This option is deprecated " |
| 571 | printf (" %s\n", "-w, --warning"); | 690 | "in favour of check-replica, which does the same")); |
| 572 | printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds")); | 691 | printf(" %s\n", "--check-replica"); |
| 573 | printf (" %s\n", _("behind master")); | 692 | printf(" %s\n", _("Check if the replica thread is running properly.")); |
| 574 | printf (" %s\n", "-c, --critical"); | 693 | printf(" %s\n", "-w, --warning"); |
| 575 | printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); | 694 | printf(" %s\n", |
| 576 | printf (" %s\n", _("behind master")); | 695 | _("Exit with WARNING status if replica server is more than INTEGER seconds")); |
| 577 | printf (" %s\n", "-l, --ssl"); | 696 | printf(" %s\n", _("behind master")); |
| 578 | printf (" %s\n", _("Use ssl encryption")); | 697 | printf(" %s\n", "-c, --critical"); |
| 579 | printf (" %s\n", "-C, --ca-cert=STRING"); | 698 | printf(" %s\n", |
| 580 | printf (" %s\n", _("Path to CA signing the cert")); | 699 | _("Exit with CRITICAL status if replica server is more then INTEGER seconds")); |
| 581 | printf (" %s\n", "-a, --cert=STRING"); | 700 | printf(" %s\n", _("behind master")); |
| 582 | printf (" %s\n", _("Path to SSL certificate")); | 701 | printf(" %s\n", "-l, --ssl"); |
| 583 | printf (" %s\n", "-k, --key=STRING"); | 702 | printf(" %s\n", _("Use ssl encryption")); |
| 584 | printf (" %s\n", _("Path to private SSL key")); | 703 | printf(" %s\n", "-C, --ca-cert=STRING"); |
| 585 | printf (" %s\n", "-D, --ca-dir=STRING"); | 704 | printf(" %s\n", _("Path to CA signing the cert")); |
| 586 | printf (" %s\n", _("Path to CA directory")); | 705 | printf(" %s\n", "-a, --cert=STRING"); |
| 587 | printf (" %s\n", "-L, --ciphers=STRING"); | 706 | printf(" %s\n", _("Path to SSL certificate")); |
| 588 | printf (" %s\n", _("List of valid SSL ciphers")); | 707 | printf(" %s\n", "-k, --key=STRING"); |
| 589 | 708 | printf(" %s\n", _("Path to private SSL key")); | |
| 590 | 709 | printf(" %s\n", "-D, --ca-dir=STRING"); | |
| 591 | printf ("\n"); | 710 | printf(" %s\n", _("Path to CA directory")); |
| 592 | printf (" %s\n", _("There are no required arguments. By default, the local database is checked")); | 711 | printf(" %s\n", "-L, --ciphers=STRING"); |
| 593 | printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); | 712 | printf(" %s\n", _("List of valid SSL ciphers")); |
| 594 | printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); | 713 | |
| 595 | 714 | printf("\n"); | |
| 596 | printf ("\n"); | 715 | printf(" %s\n", |
| 597 | printf ("%s\n", _("Notes:")); | 716 | _("There are no required arguments. By default, the local database is checked")); |
| 598 | printf (" %s\n", _("You must specify -p with an empty string to force an empty password,")); | 717 | printf(" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); |
| 599 | printf (" %s\n", _("overriding any my.cnf settings.")); | 718 | printf(" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); |
| 600 | 719 | ||
| 601 | printf (UT_SUPPORT); | 720 | printf("\n"); |
| 721 | printf("%s\n", _("Notes:")); | ||
| 722 | printf(" %s\n", _("You must specify -p with an empty string to force an empty password,")); | ||
| 723 | printf(" %s\n", _("overriding any my.cnf settings.")); | ||
| 724 | |||
| 725 | printf(UT_SUPPORT); | ||
| 602 | } | 726 | } |
| 603 | 727 | ||
| 604 | 728 | void print_usage(void) { | |
| 605 | void | 729 | printf("%s\n", _("Usage:")); |
| 606 | print_usage (void) | 730 | printf(" %s [-d database] [-H host] [-P port] [-s socket]\n", progname); |
| 607 | { | 731 | printf(" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n"); |
| 608 | printf ("%s\n", _("Usage:")); | 732 | printf(" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n"); |
| 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"); | ||
| 612 | } | 733 | } |
