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