diff options
Diffstat (limited to 'plugins/check_mysql.c')
-rw-r--r-- | plugins/check_mysql.c | 88 |
1 files changed, 57 insertions, 31 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index ca3422b5..3d7ec4cd 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c | |||
@@ -50,15 +50,23 @@ static int verbose = 0; | |||
50 | 50 | ||
51 | #define LENGTH_METRIC_UNIT 6 | 51 | #define LENGTH_METRIC_UNIT 6 |
52 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { | 52 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { |
53 | "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache", "Threads_connected", "Threads_running"}; | 53 | "Open_files", "Open_tables", "Qcache_free_memory", "Qcache_queries_in_cache", |
54 | "Threads_connected", "Threads_running"}; | ||
54 | 55 | ||
55 | #define LENGTH_METRIC_COUNTER 9 | 56 | #define LENGTH_METRIC_COUNTER 9 |
56 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = { | 57 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = {"Connections", |
57 | "Connections", "Qcache_hits", "Qcache_inserts", "Qcache_lowmem_prunes", "Qcache_not_cached", "Queries", | 58 | "Qcache_hits", |
58 | "Questions", "Table_locks_waited", "Uptime"}; | 59 | "Qcache_inserts", |
59 | 60 | "Qcache_lowmem_prunes", | |
60 | #define MYSQLDUMP_THREADS_QUERY \ | 61 | "Qcache_not_cached", |
61 | "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE 'SELECT /*!40001 SQL_NO_CACHE */%'" | 62 | "Queries", |
63 | "Questions", | ||
64 | "Table_locks_waited", | ||
65 | "Uptime"}; | ||
66 | |||
67 | #define MYSQLDUMP_THREADS_QUERY \ | ||
68 | "SELECT COUNT(1) mysqldumpThreads FROM information_schema.processlist WHERE info LIKE " \ | ||
69 | "'SELECT /*!40001 SQL_NO_CACHE */%'" | ||
62 | 70 | ||
63 | typedef struct { | 71 | typedef struct { |
64 | int errorcode; | 72 | int errorcode; |
@@ -99,16 +107,19 @@ int main(int argc, char **argv) { | |||
99 | } | 107 | } |
100 | 108 | ||
101 | if (config.ssl) { | 109 | if (config.ssl) { |
102 | mysql_ssl_set(&mysql, config.key, config.cert, config.ca_cert, config.ca_dir, config.ciphers); | 110 | mysql_ssl_set(&mysql, config.key, config.cert, config.ca_cert, config.ca_dir, |
111 | config.ciphers); | ||
103 | } | 112 | } |
104 | /* establish a connection to the server and error checking */ | 113 | /* establish a connection to the server and error checking */ |
105 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, config.db_port, config.db_socket, 0)) { | 114 | if (!mysql_real_connect(&mysql, config.db_host, config.db_user, config.db_pass, config.db, |
115 | config.db_port, config.db_socket, 0)) { | ||
106 | /* Depending on internally-selected auth plugin MySQL might return */ | 116 | /* Depending on internally-selected auth plugin MySQL might return */ |
107 | /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ | 117 | /* ER_ACCESS_DENIED_NO_PASSWORD_ERROR or ER_ACCESS_DENIED_ERROR. */ |
108 | /* Semantically these errors are the same. */ | 118 | /* Semantically these errors are the same. */ |
109 | if (config.ignore_auth && | 119 | if (config.ignore_auth && (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR || |
110 | (mysql_errno(&mysql) == ER_ACCESS_DENIED_ERROR || mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) { | 120 | mysql_errno(&mysql) == ER_ACCESS_DENIED_NO_PASSWORD_ERROR)) { |
111 | printf("MySQL OK - Version: %s (protocol %d)\n", mysql_get_server_info(&mysql), mysql_get_proto_info(&mysql)); | 121 | printf("MySQL OK - Version: %s (protocol %d)\n", mysql_get_server_info(&mysql), |
122 | mysql_get_proto_info(&mysql)); | ||
112 | mysql_close(&mysql); | 123 | mysql_close(&mysql); |
113 | return STATE_OK; | 124 | return STATE_OK; |
114 | } | 125 | } |
@@ -157,13 +168,17 @@ int main(int argc, char **argv) { | |||
157 | while ((row = mysql_fetch_row(res)) != NULL) { | 168 | while ((row = mysql_fetch_row(res)) != NULL) { |
158 | for (int i = 0; i < LENGTH_METRIC_UNIT; i++) { | 169 | for (int i = 0; i < LENGTH_METRIC_UNIT; i++) { |
159 | if (strcmp(row[0], metric_unit[i]) == 0) { | 170 | if (strcmp(row[0], metric_unit[i]) == 0) { |
160 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], atol(row[1]), "", false, 0, false, 0, false, 0, false, 0)); | 171 | xasprintf(&perf, "%s%s ", perf, |
172 | perfdata(metric_unit[i], atol(row[1]), "", false, 0, false, 0, false, | ||
173 | 0, false, 0)); | ||
161 | continue; | 174 | continue; |
162 | } | 175 | } |
163 | } | 176 | } |
164 | for (int i = 0; i < LENGTH_METRIC_COUNTER; i++) { | 177 | for (int i = 0; i < LENGTH_METRIC_COUNTER; i++) { |
165 | if (strcmp(row[0], metric_counter[i]) == 0) { | 178 | if (strcmp(row[0], metric_counter[i]) == 0) { |
166 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], atol(row[1]), "c", false, 0, false, 0, false, 0, false, 0)); | 179 | xasprintf(&perf, "%s%s ", perf, |
180 | perfdata(metric_counter[i], atol(row[1]), "c", false, 0, false, 0, | ||
181 | false, 0, false, 0)); | ||
167 | continue; | 182 | continue; |
168 | } | 183 | } |
169 | } | 184 | } |
@@ -189,8 +204,8 @@ int main(int argc, char **argv) { | |||
189 | unsigned long minor_version = (server_verion_int % 10000) / 100; | 204 | unsigned long minor_version = (server_verion_int % 10000) / 100; |
190 | unsigned long patch_version = (server_verion_int % 100); | 205 | unsigned long patch_version = (server_verion_int % 100); |
191 | if (verbose) { | 206 | if (verbose) { |
192 | printf("Found MariaDB: %s, main version: %lu, minor version: %lu, patch version: %lu\n", server_version, major_version, | 207 | printf("Found MariaDB: %s, main version: %lu, minor version: %lu, patch version: %lu\n", |
193 | minor_version, patch_version); | 208 | server_version, major_version, minor_version, patch_version); |
194 | } | 209 | } |
195 | 210 | ||
196 | if (strstr(server_version, "MariaDB") != NULL) { | 211 | if (strstr(server_version, "MariaDB") != NULL) { |
@@ -292,11 +307,15 @@ int main(int argc, char **argv) { | |||
292 | } | 307 | } |
293 | 308 | ||
294 | /* Save replica status in replica_result */ | 309 | /* Save replica status in replica_result */ |
295 | snprintf(replica_result, REPLICA_RESULTSIZE, "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", row[replica_io_field], | 310 | snprintf(replica_result, REPLICA_RESULTSIZE, |
296 | row[replica_sql_field], seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown"); | 311 | "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", |
297 | 312 | row[replica_io_field], row[replica_sql_field], | |
298 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */ | 313 | seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown"); |
299 | if (strcmp(row[replica_io_field], "Yes") != 0 || strcmp(row[replica_sql_field], "Yes") != 0) { | 314 | |
315 | /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no | ||
316 | * mysqldump threads running */ | ||
317 | if (strcmp(row[replica_io_field], "Yes") != 0 || | ||
318 | strcmp(row[replica_sql_field], "Yes") != 0) { | ||
300 | MYSQL_RES *res_mysqldump; | 319 | MYSQL_RES *res_mysqldump; |
301 | MYSQL_ROW row_mysqldump; | 320 | MYSQL_ROW row_mysqldump; |
302 | unsigned int mysqldump_threads = 0; | 321 | unsigned int mysqldump_threads = 0; |
@@ -325,20 +344,23 @@ int main(int argc, char **argv) { | |||
325 | if (seconds_behind_field == -1) { | 344 | if (seconds_behind_field == -1) { |
326 | printf("seconds_behind_field not found\n"); | 345 | printf("seconds_behind_field not found\n"); |
327 | } else { | 346 | } else { |
328 | printf("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); | 347 | printf("seconds_behind_field(index %d)=%s\n", seconds_behind_field, |
348 | row[seconds_behind_field]); | ||
329 | } | 349 | } |
330 | } | 350 | } |
331 | 351 | ||
332 | /* Check Seconds Behind against threshold */ | 352 | /* Check Seconds Behind against threshold */ |
333 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && strcmp(row[seconds_behind_field], "NULL") != 0)) { | 353 | if ((seconds_behind_field != -1) && (row[seconds_behind_field] != NULL && |
354 | strcmp(row[seconds_behind_field], "NULL") != 0)) { | ||
334 | double value = atof(row[seconds_behind_field]); | 355 | double value = atof(row[seconds_behind_field]); |
335 | int status; | 356 | int status; |
336 | 357 | ||
337 | status = get_status(value, config.my_threshold); | 358 | status = get_status(value, config.my_threshold); |
338 | 359 | ||
339 | xasprintf(&perf, "%s %s", perf, | 360 | xasprintf(&perf, "%s %s", perf, |
340 | fperfdata("seconds behind master", value, "s", true, (double)config.warning_time, true, | 361 | fperfdata("seconds behind master", value, "s", true, |
341 | (double)config.critical_time, false, 0, false, 0)); | 362 | (double)config.warning_time, true, (double)config.critical_time, |
363 | false, 0, false, 0)); | ||
342 | 364 | ||
343 | if (status == STATE_WARNING) { | 365 | if (status == STATE_WARNING) { |
344 | printf("SLOW_REPLICA %s: %s|%s\n", _("WARNING"), replica_result, perf); | 366 | printf("SLOW_REPLICA %s: %s|%s\n", _("WARNING"), replica_result, perf); |
@@ -410,7 +432,8 @@ check_mysql_config_wrapper process_arguments(int argc, char **argv) { | |||
410 | 432 | ||
411 | int option = 0; | 433 | int option = 0; |
412 | while (true) { | 434 | while (true) { |
413 | int option_index = getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); | 435 | int option_index = |
436 | getopt_long(argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); | ||
414 | 437 | ||
415 | if (option_index == -1 || option_index == EOF) { | 438 | if (option_index == -1 || option_index == EOF) { |
416 | break; | 439 | break; |
@@ -580,15 +603,17 @@ void print_help(void) { | |||
580 | printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); | 603 | printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); |
581 | printf(" %s\n", _("Your clear-text password could be visible as a process table entry")); | 604 | printf(" %s\n", _("Your clear-text password could be visible as a process table entry")); |
582 | printf(" %s\n", "-S, --check-slave"); | 605 | printf(" %s\n", "-S, --check-slave"); |
583 | printf(" %s\n", | 606 | printf(" %s\n", _("Check if the slave thread is running properly. This option is deprecated " |
584 | _("Check if the slave thread is running properly. This option is deprecated in favour of check-replica, which does the same")); | 607 | "in favour of check-replica, which does the same")); |
585 | printf(" %s\n", "--check-replica"); | 608 | printf(" %s\n", "--check-replica"); |
586 | printf(" %s\n", _("Check if the replica thread is running properly.")); | 609 | printf(" %s\n", _("Check if the replica thread is running properly.")); |
587 | printf(" %s\n", "-w, --warning"); | 610 | printf(" %s\n", "-w, --warning"); |
588 | printf(" %s\n", _("Exit with WARNING status if replica server is more than INTEGER seconds")); | 611 | printf(" %s\n", |
612 | _("Exit with WARNING status if replica server is more than INTEGER seconds")); | ||
589 | printf(" %s\n", _("behind master")); | 613 | printf(" %s\n", _("behind master")); |
590 | printf(" %s\n", "-c, --critical"); | 614 | printf(" %s\n", "-c, --critical"); |
591 | printf(" %s\n", _("Exit with CRITICAL status if replica server is more then INTEGER seconds")); | 615 | printf(" %s\n", |
616 | _("Exit with CRITICAL status if replica server is more then INTEGER seconds")); | ||
592 | printf(" %s\n", _("behind master")); | 617 | printf(" %s\n", _("behind master")); |
593 | printf(" %s\n", "-l, --ssl"); | 618 | printf(" %s\n", "-l, --ssl"); |
594 | printf(" %s\n", _("Use ssl encryption")); | 619 | printf(" %s\n", _("Use ssl encryption")); |
@@ -604,7 +629,8 @@ void print_help(void) { | |||
604 | printf(" %s\n", _("List of valid SSL ciphers")); | 629 | printf(" %s\n", _("List of valid SSL ciphers")); |
605 | 630 | ||
606 | printf("\n"); | 631 | printf("\n"); |
607 | printf(" %s\n", _("There are no required arguments. By default, the local database is checked")); | 632 | printf(" %s\n", |
633 | _("There are no required arguments. By default, the local database is checked")); | ||
608 | printf(" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); | 634 | printf(" %s\n", _("using the default unix socket. You can force TCP on localhost by using an")); |
609 | printf(" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); | 635 | printf(" %s\n", _("IP address or FQDN ('localhost' will use the socket as well).")); |
610 | 636 | ||