diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-11-07 14:15:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-07 14:15:26 +0100 |
| commit | b5e7f4e6cc8c586fdb20949369e05105157a61a7 (patch) | |
| tree | fa0690f4f78a828f5bc8fb2bec07222bb5a6ab0e /plugins | |
| parent | ca3acbe274bf44578c5ec5d0e5414379cbddb21f (diff) | |
| parent | 0fb65a3a90e778f942a1f61f210a2dd969dd9597 (diff) | |
| download | monitoring-plugins-b5e7f4e6cc8c586fdb20949369e05105157a61a7.tar.gz | |
Merge pull request #2174 from RincewindsHat/modern_output/check_mysql
Modern output/check mysql
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/check_mysql.c | 277 | ||||
| -rw-r--r-- | plugins/check_mysql.d/config.h | 8 | ||||
| -rw-r--r-- | plugins/t/check_mysql.t | 7 |
3 files changed, 180 insertions, 112 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index 6134d6c6..9d8094c0 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c | |||
| @@ -30,13 +30,11 @@ | |||
| 30 | * | 30 | * |
| 31 | *****************************************************************************/ | 31 | *****************************************************************************/ |
| 32 | 32 | ||
| 33 | const char *progname = "check_mysql"; | ||
| 34 | const char *copyright = "1999-2024"; | ||
| 35 | const char *email = "devel@monitoring-plugins.org"; | ||
| 36 | |||
| 37 | #define REPLICA_RESULTSIZE 96 | ||
| 38 | |||
| 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" |
| @@ -46,8 +44,14 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 46 | #include <mysqld_error.h> | 44 | #include <mysqld_error.h> |
| 47 | #include <errmsg.h> | 45 | #include <errmsg.h> |
| 48 | 46 | ||
| 47 | const char *progname = "check_mysql"; | ||
| 48 | const char *copyright = "1999-2024"; | ||
| 49 | const char *email = "devel@monitoring-plugins.org"; | ||
| 50 | |||
| 49 | static int verbose = 0; | 51 | static int verbose = 0; |
| 50 | 52 | ||
| 53 | #define REPLICA_RESULTSIZE 96 | ||
| 54 | |||
| 51 | #define LENGTH_METRIC_UNIT 6 | 55 | #define LENGTH_METRIC_UNIT 6 |
| 52 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { | 56 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { |
| 53 | "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache", | 57 | "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache", |
| @@ -110,7 +114,11 @@ int main(int argc, char **argv) { | |||
| 110 | mysql_ssl_set(&mysql, config.key, config.cert, config.ca_cert, config.ca_dir, | 114 | mysql_ssl_set(&mysql, config.key, config.cert, config.ca_cert, config.ca_dir, |
| 111 | config.ciphers); | 115 | config.ciphers); |
| 112 | } | 116 | } |
| 113 | /* establish a connection to the server and error checking */ | 117 | |
| 118 | mp_check overall = mp_check_init(); | ||
| 119 | |||
| 120 | mp_subcheck sc_connection = mp_subcheck_init(); | ||
| 121 | /* establish a connection to the server and check for errors */ | ||
| 114 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, | 122 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, |
| 115 | config.db_port, config.db_socket, 0)) { | 123 | config.db_port, config.db_socket, 0)) { |
| 116 | /* Depending on internally-selected auth plugin MySQL might return */ | 124 | /* Depending on internally-selected auth plugin MySQL might return */ |
| @@ -118,78 +126,115 @@ int main(int argc, char **argv) { | |||
| 118 | /* Semantically these errors are the same. */ | 126 | /* Semantically these errors are the same. */ |
| 119 | if (config.ignore_auth && (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR || | 127 | if (config.ignore_auth && (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR || |
| 120 | mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) { | 128 | mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) { |
| 121 | printf("MySQL OK - Version: %s (protocol %d)\n", mysql_get_server_info(&mysql), | 129 | xasprintf(&sc_connection.output, "Version: %s (protocol %d)", |
| 122 | mysql_get_proto_info(&mysql)); | 130 | mysql_get_server_info(&mysql), mysql_get_proto_info(&mysql)); |
| 123 | mysql_close(&mysql); | 131 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_OK); |
| 124 | return STATE_OK; | ||
| 125 | } | ||
| 126 | 132 | ||
| 127 | if (mysql_errno(&mysql) == CR_UNKNOWN_HOST) { | 133 | mysql_close(&mysql); |
| 128 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 129 | } else if (mysql_errno(&mysql) == CR_VERSION_ERROR) { | ||
| 130 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 131 | } else if (mysql_errno(&mysql) == CR_OUT_OF_MEMORY) { | ||
| 132 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 133 | } else if (mysql_errno(&mysql) == CR_IPSOCK_ERROR) { | ||
| 134 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 135 | } else if (mysql_errno(&mysql) == CR_SOCKET_CREATE_ERROR) { | ||
| 136 | die(STATE_WARNING, "%s\n", mysql_error(&mysql)); | ||
| 137 | } else { | 134 | } else { |
| 138 | die(STATE_CRITICAL, "%s\n", mysql_error(&mysql)); | 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 | } | ||
| 139 | } | 154 | } |
| 155 | |||
| 156 | mp_add_subcheck_to_check(&overall, sc_connection); | ||
| 157 | mp_exit(overall); | ||
| 158 | } else { | ||
| 159 | // successful connection | ||
| 160 | sc_connection = mp_set_subcheck_state(sc_connection, STATE_OK); | ||
| 161 | xasprintf(&sc_connection.output, "Version: %s (protocol %d)", mysql_get_server_info(&mysql), | ||
| 162 | mysql_get_proto_info(&mysql)); | ||
| 163 | mp_add_subcheck_to_check(&overall, sc_connection); | ||
| 140 | } | 164 | } |
| 141 | 165 | ||
| 142 | /* get the server stats */ | 166 | /* get the server stats */ |
| 143 | char *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); | ||
| 144 | 171 | ||
| 145 | /* error checking once more */ | 172 | /* error checking once more */ |
| 146 | if (mysql_error(&mysql)) { | 173 | if (mysql_errno(&mysql) != 0) { |
| 147 | if (mysql_errno(&mysql) == CR_SERVER_GONE_ERROR) { | 174 | if ((mysql_errno(&mysql) == CR_SERVER_GONE_ERROR) || |
| 148 | die(STATE_CRITICAL, "%s\n", mysql_error(&mysql)); | 175 | (mysql_errno(&mysql) == CR_SERVER_LOST) || (mysql_errno(&mysql) == CR_UNKNOWN_ERROR)) { |
| 149 | } else if (mysql_errno(&mysql) == CR_SERVER_LOST) { | 176 | sc_stats = mp_set_subcheck_state(sc_stats, STATE_CRITICAL); |
| 150 | die(STATE_CRITICAL, "%s\n", mysql_error(&mysql)); | 177 | xasprintf(&sc_stats.output, "Retrieving stats failed: %s", mysql_error(&mysql)); |
| 151 | } else if (mysql_errno(&mysql) == CR_UNKNOWN_ERROR) { | 178 | } else { |
| 152 | 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)); | ||
| 153 | } | 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); | ||
| 154 | } | 191 | } |
| 155 | 192 | ||
| 156 | char *perf = strdup(""); | ||
| 157 | char *error = NULL; | ||
| 158 | MYSQL_RES *res; | 193 | MYSQL_RES *res; |
| 159 | MYSQL_ROW row; | 194 | MYSQL_ROW row; |
| 195 | mp_subcheck sc_query = mp_subcheck_init(); | ||
| 160 | /* try to fetch some perf data */ | 196 | /* try to fetch some perf data */ |
| 161 | if (mysql_query(&mysql, "show global status") == 0) { | 197 | if (mysql_query(&mysql, "show global status") == 0) { |
| 162 | if ((res = mysql_store_result(&mysql)) == NULL) { | 198 | if ((res = mysql_store_result(&mysql)) == NULL) { |
| 163 | error = strdup(mysql_error(&mysql)); | 199 | xasprintf(&sc_connection.output, "query failed - status store_result error: %s", |
| 200 | mysql_error(&mysql)); | ||
| 164 | mysql_close(&mysql); | 201 | mysql_close(&mysql); |
| 165 | die(STATE_CRITICAL, _("status store_result error: %s\n"), error); | 202 | |
| 203 | sc_query = mp_set_subcheck_state(sc_query, STATE_CRITICAL); | ||
| 204 | mp_add_subcheck_to_check(&overall, sc_query); | ||
| 205 | mp_exit(overall); | ||
| 166 | } | 206 | } |
| 167 | 207 | ||
| 168 | while ((row = mysql_fetch_row(res)) != NULL) { | 208 | while ((row = mysql_fetch_row(res)) != NULL) { |
| 169 | for (int i = 0; i < LENGTH_METRIC_UNIT; i++) { | 209 | for (int i = 0; i < LENGTH_METRIC_UNIT; i++) { |
| 170 | if (strcmp(row[0], metric_unit[i]) == 0) { | 210 | if (strcmp(row[0], metric_unit[i]) == 0) { |
| 171 | xasprintf(&perf, "%s%s ", perf, | 211 | mp_perfdata pd_mysql_stat = perfdata_init(); |
| 172 | perfdata(metric_unit[i], atol(row[1]), "", false, 0, false, 0, false, | 212 | pd_mysql_stat.label = (char *)metric_unit[i]; |
| 173 | 0, false, 0)); | 213 | pd_mysql_stat.value = mp_create_pd_value(atol(row[1])); |
| 214 | mp_add_perfdata_to_subcheck(&sc_stats, pd_mysql_stat); | ||
| 174 | continue; | 215 | continue; |
| 175 | } | 216 | } |
| 176 | } | 217 | } |
| 218 | |||
| 177 | for (int i = 0; i < LENGTH_METRIC_COUNTER; i++) { | 219 | for (int i = 0; i < LENGTH_METRIC_COUNTER; i++) { |
| 178 | if (strcmp(row[0], metric_counter[i]) == 0) { | 220 | if (strcmp(row[0], metric_counter[i]) == 0) { |
| 179 | xasprintf(&perf, "%s%s ", perf, | 221 | mp_perfdata pd_mysql_stat = perfdata_init(); |
| 180 | perfdata(metric_counter[i], atol(row[1]), "c", false, 0, false, 0, | 222 | pd_mysql_stat.label = (char *)metric_counter[i]; |
| 181 | false, 0, false, 0)); | 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); | ||
| 182 | continue; | 226 | continue; |
| 183 | } | 227 | } |
| 184 | } | 228 | } |
| 185 | } | 229 | } |
| 186 | /* remove trailing space */ | 230 | } else { |
| 187 | if (strlen(perf) > 0) { | 231 | // Query failed! |
| 188 | perf[strlen(perf) - 1] = '\0'; | 232 | xasprintf(&sc_connection.output, "query failed"); |
| 189 | } | 233 | sc_query = mp_set_subcheck_state(sc_query, STATE_CRITICAL); |
| 234 | mp_add_subcheck_to_check(&overall, sc_query); | ||
| 235 | mp_exit(overall); | ||
| 190 | } | 236 | } |
| 191 | 237 | ||
| 192 | char replica_result[REPLICA_RESULTSIZE] = {0}; | ||
| 193 | if (config.check_replica) { | 238 | if (config.check_replica) { |
| 194 | // Detect which version we are, on older version | 239 | // Detect which version we are, on older version |
| 195 | // "show slave status" should work, on newer ones | 240 | // "show slave status" should work, on newer ones |
| @@ -203,8 +248,10 @@ int main(int argc, char **argv) { | |||
| 203 | unsigned long major_version = server_verion_int / 10000; | 248 | unsigned long major_version = server_verion_int / 10000; |
| 204 | unsigned long minor_version = (server_verion_int % 10000) / 100; | 249 | unsigned long minor_version = (server_verion_int % 10000) / 100; |
| 205 | unsigned long patch_version = (server_verion_int % 100); | 250 | unsigned long patch_version = (server_verion_int % 100); |
| 251 | |||
| 206 | if (verbose) { | 252 | if (verbose) { |
| 207 | printf("Found MariaDB: %s, main version: %lu, minor version: %lu, patch version: %lu\n", | 253 | printf("Found MariaDB/MySQL: %s, main version: %lu, minor version: %lu, patch version: " |
| 254 | "%lu\n", | ||
| 208 | server_version, major_version, minor_version, patch_version); | 255 | server_version, major_version, minor_version, patch_version); |
| 209 | } | 256 | } |
| 210 | 257 | ||
| @@ -235,43 +282,60 @@ int main(int argc, char **argv) { | |||
| 235 | replica_query = "show replica status"; | 282 | replica_query = "show replica status"; |
| 236 | } | 283 | } |
| 237 | 284 | ||
| 285 | mp_subcheck sc_replica = mp_subcheck_init(); | ||
| 286 | |||
| 238 | /* check the replica status */ | 287 | /* check the replica status */ |
| 239 | if (mysql_query(&mysql, replica_query) != 0) { | 288 | if (mysql_query(&mysql, replica_query) != 0) { |
| 240 | error = strdup(mysql_error(&mysql)); | 289 | xasprintf(&sc_replica.output, "replica query error: %s", mysql_error(&mysql)); |
| 241 | mysql_close(&mysql); | 290 | mysql_close(&mysql); |
| 242 | die(STATE_CRITICAL, _("replica query error: %s\n"), error); | 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); | ||
| 243 | } | 295 | } |
| 244 | 296 | ||
| 245 | /* store the result */ | 297 | /* store the result */ |
| 246 | if ((res = mysql_store_result(&mysql)) == NULL) { | 298 | if ((res = mysql_store_result(&mysql)) == NULL) { |
| 247 | error = strdup(mysql_error(&mysql)); | 299 | xasprintf(&sc_replica.output, "replica store_result error: %s", mysql_error(&mysql)); |
| 248 | mysql_close(&mysql); | 300 | mysql_close(&mysql); |
| 249 | die(STATE_CRITICAL, _("replica 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); | ||
| 250 | } | 305 | } |
| 251 | 306 | ||
| 252 | /* Check there is some data */ | 307 | /* Check there is some data */ |
| 253 | if (mysql_num_rows(res) == 0) { | 308 | if (mysql_num_rows(res) == 0) { |
| 254 | mysql_close(&mysql); | 309 | mysql_close(&mysql); |
| 255 | die(STATE_WARNING, "%s\n", _("No replicas 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); | ||
| 256 | } | 315 | } |
| 257 | 316 | ||
| 258 | /* fetch the first row */ | 317 | /* fetch the first row */ |
| 259 | if ((row = mysql_fetch_row(res)) == NULL) { | 318 | if ((row = mysql_fetch_row(res)) == NULL) { |
| 260 | error = strdup(mysql_error(&mysql)); | 319 | xasprintf(&sc_replica.output, "replica fetch row error: %s", mysql_error(&mysql)); |
| 261 | mysql_free_result(res); | 320 | mysql_free_result(res); |
| 262 | mysql_close(&mysql); | 321 | mysql_close(&mysql); |
| 263 | die(STATE_CRITICAL, _("replica 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); | ||
| 264 | } | 326 | } |
| 265 | 327 | ||
| 266 | if (mysql_field_count(&mysql) == 12) { | 328 | if (mysql_field_count(&mysql) == 12) { |
| 267 | /* mysql 3.23.x */ | 329 | /* mysql 3.23.x */ |
| 268 | snprintf(replica_result, REPLICA_RESULTSIZE, _("Replica running: %s"), row[6]); | 330 | xasprintf(&sc_replica.output, "Replica running: %s", row[6]); |
| 269 | if (strcmp(row[6], "Yes") != 0) { | 331 | if (strcmp(row[6], "Yes") != 0) { |
| 270 | mysql_free_result(res); | 332 | mysql_free_result(res); |
| 271 | mysql_close(&mysql); | 333 | mysql_close(&mysql); |
| 272 | die(STATE_CRITICAL, "%s\n", replica_result); | ||
| 273 | } | ||
| 274 | 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); | ||
| 338 | } | ||
| 275 | } else { | 339 | } else { |
| 276 | /* mysql 4.x.x and mysql 5.x.x */ | 340 | /* mysql 4.x.x and mysql 5.x.x */ |
| 277 | int replica_io_field = -1; | 341 | int replica_io_field = -1; |
| @@ -315,14 +379,18 @@ int main(int argc, char **argv) { | |||
| 315 | if ((replica_io_field < 0) || (replica_sql_field < 0) || (num_fields == 0)) { | 379 | if ((replica_io_field < 0) || (replica_sql_field < 0) || (num_fields == 0)) { |
| 316 | mysql_free_result(res); | 380 | mysql_free_result(res); |
| 317 | mysql_close(&mysql); | 381 | mysql_close(&mysql); |
| 318 | die(STATE_CRITICAL, "Replica 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); | ||
| 319 | } | 387 | } |
| 320 | 388 | ||
| 321 | /* Save replica status in replica_result */ | 389 | /* Save replica status in replica_result */ |
| 322 | snprintf(replica_result, REPLICA_RESULTSIZE, | 390 | xasprintf(&sc_replica.output, |
| 323 | "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", | 391 | "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", |
| 324 | row[replica_io_field], row[replica_sql_field], | 392 | row[replica_io_field], row[replica_sql_field], |
| 325 | seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown"); | 393 | seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown"); |
| 326 | 394 | ||
| 327 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no | 395 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no |
| 328 | * mysqldump threads running */ | 396 | * mysqldump threads running */ |
| @@ -345,10 +413,14 @@ int main(int argc, char **argv) { | |||
| 345 | } | 413 | } |
| 346 | mysql_close(&mysql); | 414 | mysql_close(&mysql); |
| 347 | } | 415 | } |
| 416 | |||
| 348 | if (mysqldump_threads == 0) { | 417 | if (mysqldump_threads == 0) { |
| 349 | die(STATE_CRITICAL, "%s\n", replica_result); | 418 | sc_replica = mp_set_subcheck_state(sc_replica, STATE_CRITICAL); |
| 419 | mp_add_subcheck_to_check(&overall, sc_replica); | ||
| 420 | mp_exit(overall); | ||
| 350 | } else { | 421 | } else { |
| 351 | strncat(replica_result, " Mysqldump: in progress", REPLICA_RESULTSIZE - 1); | 422 | xasprintf(&sc_replica.output, "%s %s", sc_replica.output, |
| 423 | " Mysqldump: in progress"); | ||
| 352 | } | 424 | } |
| 353 | } | 425 | } |
| 354 | 426 | ||
| @@ -364,22 +436,22 @@ int main(int argc, char **argv) { | |||
| 364 | /* Check Seconds Behind against threshold */ | 436 | /* Check Seconds Behind against threshold */ |
| 365 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && | 437 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && |
| 366 | strcmp(row[seconds_behind_field], "NULL") != 0)) { | 438 | strcmp(row[seconds_behind_field], "NULL") != 0)) { |
| 367 | double value = atof(row[seconds_behind_field]); | 439 | mp_perfdata pd_seconds_behind = perfdata_init(); |
| 368 | int status; | 440 | pd_seconds_behind.label = "seconds behind master"; |
| 369 | 441 | pd_seconds_behind.value = mp_create_pd_value(atof(row[seconds_behind_field])); | |
| 370 | status = get_status(value, config.my_threshold); | 442 | pd_seconds_behind = |
| 371 | 443 | mp_pd_set_thresholds(pd_seconds_behind, config.replica_thresholds); | |
| 372 | xasprintf(&perf, "%s %s", perf, | 444 | pd_seconds_behind.uom = "s"; |
| 373 | fperfdata("seconds behind master", value, "s", true, | 445 | mp_add_perfdata_to_subcheck(&sc_replica, pd_seconds_behind); |
| 374 | (double)config.warning_time, true, (double)config.critical_time, | 446 | |
| 375 | false, 0, false, 0)); | 447 | mp_state_enum status = mp_get_pd_status(pd_seconds_behind); |
| 376 | 448 | ||
| 377 | if (status == STATE_WARNING) { | 449 | sc_replica = mp_set_subcheck_state(sc_replica, status); |
| 378 | printf("SLOW_REPLICA %s: %s|%s\n", _("WARNING"), replica_result, perf); | 450 | |
| 379 | exit(STATE_WARNING); | 451 | if (status != STATE_OK) { |
| 380 | } else if (status == STATE_CRITICAL) { | 452 | xasprintf(&sc_replica.output, "slow replica - %s", sc_replica.output); |
| 381 | printf("SLOW_REPLICA %s: %s|%s\n", _("CRITICAL"), replica_result, perf); | 453 | mp_add_subcheck_to_check(&overall, sc_replica); |
| 382 | exit(STATE_CRITICAL); | 454 | mp_exit(overall); |
| 383 | } | 455 | } |
| 384 | } | 456 | } |
| 385 | } | 457 | } |
| @@ -391,20 +463,16 @@ int main(int argc, char **argv) { | |||
| 391 | /* close the connection */ | 463 | /* close the connection */ |
| 392 | mysql_close(&mysql); | 464 | mysql_close(&mysql); |
| 393 | 465 | ||
| 394 | /* print out the result of stats */ | 466 | mp_exit(overall); |
| 395 | if (config.check_replica) { | ||
| 396 | printf("%s %s|%s\n", result, replica_result, perf); | ||
| 397 | } else { | ||
| 398 | printf("%s|%s\n", result, perf); | ||
| 399 | } | ||
| 400 | |||
| 401 | return STATE_OK; | ||
| 402 | } | 467 | } |
| 403 | 468 | ||
| 404 | #define CHECK_REPLICA_OPT CHAR_MAX + 1 | ||
| 405 | |||
| 406 | /* process command-line arguments */ | 469 | /* process command-line arguments */ |
| 407 | check_mysql_config_wrapper process_arguments(int argc, char **argv) { | 470 | check_mysql_config_wrapper process_arguments(int argc, char **argv) { |
| 471 | |||
| 472 | enum { | ||
| 473 | CHECK_REPLICA_OPT = CHAR_MAX + 1, | ||
| 474 | }; | ||
| 475 | |||
| 408 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 476 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 409 | {"socket", required_argument, 0, 's'}, | 477 | {"socket", required_argument, 0, 's'}, |
| 410 | {"database", required_argument, 0, 'd'}, | 478 | {"database", required_argument, 0, 'd'}, |
| @@ -439,9 +507,6 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) { | |||
| 439 | return result; | 507 | return result; |
| 440 | } | 508 | } |
| 441 | 509 | ||
| 442 | char *warning = NULL; | ||
| 443 | char *critical = NULL; | ||
| 444 | |||
| 445 | int option = 0; | 510 | int option = 0; |
| 446 | while (true) { | 511 | while (true) { |
| 447 | int option_index = | 512 | int option_index = |
| @@ -513,14 +578,22 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) { | |||
| 513 | case 'n': | 578 | case 'n': |
| 514 | result.config.ignore_auth = true; /* ignore-auth */ | 579 | result.config.ignore_auth = true; /* ignore-auth */ |
| 515 | break; | 580 | break; |
| 516 | case 'w': | 581 | case 'w': { |
| 517 | warning = optarg; | 582 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 518 | result.config.warning_time = strtod(warning, NULL); | 583 | if (tmp.error != MP_PARSING_SUCCES) { |
| 519 | break; | 584 | die(STATE_UNKNOWN, "failed to parse warning time threshold"); |
| 520 | case 'c': | 585 | } |
| 521 | critical = optarg; | 586 | result.config.replica_thresholds = |
| 522 | result.config.critical_time = strtod(critical, NULL); | 587 | mp_thresholds_set_warn(result.config.replica_thresholds, tmp.range); |
| 523 | break; | 588 | } break; |
| 589 | case 'c': { | ||
| 590 | mp_range_parsed tmp = mp_parse_range_string(optarg); | ||
| 591 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 592 | die(STATE_UNKNOWN, "failed to parse critical time threshold"); | ||
| 593 | } | ||
| 594 | result.config.replica_thresholds = | ||
| 595 | mp_thresholds_set_crit(result.config.replica_thresholds, tmp.range); | ||
| 596 | } break; | ||
| 524 | case 'V': /* version */ | 597 | case 'V': /* version */ |
| 525 | print_revision(progname, NP_VERSION); | 598 | print_revision(progname, NP_VERSION); |
| 526 | exit(STATE_UNKNOWN); | 599 | exit(STATE_UNKNOWN); |
| @@ -537,8 +610,6 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) { | |||
| 537 | 610 | ||
| 538 | int index = optind; | 611 | int index = optind; |
| 539 | 612 | ||
| 540 | set_thresholds(&result.config.my_threshold, warning, critical); | ||
| 541 | |||
| 542 | while (argc > index) { | 613 | while (argc > index) { |
| 543 | if (result.config.db_host == NULL) { | 614 | if (result.config.db_host == NULL) { |
| 544 | if (is_host(argv[index])) { | 615 | if (is_host(argv[index])) { |
diff --git a/plugins/check_mysql.d/config.h b/plugins/check_mysql.d/config.h index 71ddbe8d..ef086cfc 100644 --- a/plugins/check_mysql.d/config.h +++ b/plugins/check_mysql.d/config.h | |||
| @@ -24,9 +24,7 @@ typedef struct { | |||
| 24 | bool check_replica; | 24 | bool check_replica; |
| 25 | bool ignore_auth; | 25 | bool ignore_auth; |
| 26 | 26 | ||
| 27 | double warning_time; | 27 | mp_thresholds replica_thresholds; |
| 28 | double critical_time; | ||
| 29 | thresholds *my_threshold; | ||
| 30 | 28 | ||
| 31 | } check_mysql_config; | 29 | } check_mysql_config; |
| 32 | 30 | ||
| @@ -50,9 +48,7 @@ check_mysql_config check_mysql_config_init() { | |||
| 50 | .check_replica = false, | 48 | .check_replica = false, |
| 51 | .ignore_auth = false, | 49 | .ignore_auth = false, |
| 52 | 50 | ||
| 53 | .warning_time = 0, | 51 | .replica_thresholds = mp_thresholds_init(), |
| 54 | .critical_time = 0, | ||
| 55 | .my_threshold = NULL, | ||
| 56 | }; | 52 | }; |
| 57 | return tmp; | 53 | return tmp; |
| 58 | } | 54 | } |
diff --git a/plugins/t/check_mysql.t b/plugins/t/check_mysql.t index a383bc99..9114cccc 100644 --- a/plugins/t/check_mysql.t +++ b/plugins/t/check_mysql.t | |||
| @@ -11,6 +11,7 @@ | |||
| 11 | # mysql -u$user -p$password -h$host $db | 11 | # mysql -u$user -p$password -h$host $db |
| 12 | 12 | ||
| 13 | use strict; | 13 | use strict; |
| 14 | use warnings; | ||
| 14 | use Test::More; | 15 | use Test::More; |
| 15 | use NPTest; | 16 | use NPTest; |
| 16 | 17 | ||
| @@ -40,7 +41,7 @@ SKIP: { | |||
| 40 | 41 | ||
| 41 | $result = NPTest->testCmd("./check_mysql -S -H $mysqlserver $mysql_login_details"); | 42 | $result = NPTest->testCmd("./check_mysql -S -H $mysqlserver $mysql_login_details"); |
| 42 | cmp_ok( $result->return_code, "==", 1, "No replicas defined" ); | 43 | cmp_ok( $result->return_code, "==", 1, "No replicas defined" ); |
| 43 | like( $result->output, "/No replicas defined/", "Correct error message"); | 44 | like( $result->output, "/no replicas defined/", "Correct error message"); |
| 44 | } | 45 | } |
| 45 | 46 | ||
| 46 | SKIP: { | 47 | SKIP: { |
| @@ -54,7 +55,7 @@ SKIP: { | |||
| 54 | 55 | ||
| 55 | $result = NPTest->testCmd("./check_mysql -S -s $mysqlsocket $mysql_login_details"); | 56 | $result = NPTest->testCmd("./check_mysql -S -s $mysqlsocket $mysql_login_details"); |
| 56 | cmp_ok( $result->return_code, "==", 1, "No replicas defined" ); | 57 | cmp_ok( $result->return_code, "==", 1, "No replicas defined" ); |
| 57 | like( $result->output, "/No replicas defined/", "Correct error message"); | 58 | like( $result->output, "/no replicas defined/", "Correct error message"); |
| 58 | } | 59 | } |
| 59 | 60 | ||
| 60 | SKIP: { | 61 | SKIP: { |
| @@ -70,5 +71,5 @@ SKIP: { | |||
| 70 | 71 | ||
| 71 | $result = NPTest->testCmd("./check_mysql -S -H $with_replica $with_replica_login -w 60:"); | 72 | $result = NPTest->testCmd("./check_mysql -S -H $with_replica $with_replica_login -w 60:"); |
| 72 | cmp_ok( $result->return_code, '==', 1, 'Alert warning if < 60 seconds behind'); | 73 | cmp_ok( $result->return_code, '==', 1, 'Alert warning if < 60 seconds behind'); |
| 73 | like( $result->output, "/^SLOW_REPLICA WARNING:/", "Output okay"); | 74 | like( $result->output, "/^slow_replica/", "Output okay"); |
| 74 | } | 75 | } |
