diff options
Diffstat (limited to 'plugins/check_mysql.c')
| -rw-r--r-- | plugins/check_mysql.c | 76 |
1 files changed, 72 insertions, 4 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index ad3d86f3..11d4a2fd 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c | |||
| @@ -59,6 +59,32 @@ unsigned int db_port = MYSQL_PORT; | |||
| 59 | int check_slave = 0, warn_sec = 0, crit_sec = 0; | 59 | int check_slave = 0, warn_sec = 0, crit_sec = 0; |
| 60 | int verbose = 0; | 60 | int verbose = 0; |
| 61 | 61 | ||
| 62 | static double warning_time = 0; | ||
| 63 | static double critical_time = 0; | ||
| 64 | |||
| 65 | #define LENGTH_METRIC_UNIT 6 | ||
| 66 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { | ||
| 67 | "Open_files", | ||
| 68 | "Open_tables", | ||
| 69 | "Qcache_free_memory", | ||
| 70 | "Qcache_queries_in_cache", | ||
| 71 | "Threads_connected", | ||
| 72 | "Threads_running" | ||
| 73 | }; | ||
| 74 | |||
| 75 | #define LENGTH_METRIC_COUNTER 9 | ||
| 76 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = { | ||
| 77 | "Connections", | ||
| 78 | "Qcache_hits", | ||
| 79 | "Qcache_inserts", | ||
| 80 | "Qcache_lowmem_prunes", | ||
| 81 | "Qcache_not_cached", | ||
| 82 | "Queries", | ||
| 83 | "Questions", | ||
| 84 | "Table_locks_waited", | ||
| 85 | "Uptime" | ||
| 86 | }; | ||
| 87 | |||
| 62 | thresholds *my_threshold = NULL; | 88 | thresholds *my_threshold = NULL; |
| 63 | 89 | ||
| 64 | int process_arguments (int, char **); | 90 | int process_arguments (int, char **); |
| @@ -79,6 +105,9 @@ main (int argc, char **argv) | |||
| 79 | char *result = NULL; | 105 | char *result = NULL; |
| 80 | char *error = NULL; | 106 | char *error = NULL; |
| 81 | char slaveresult[SLAVERESULTSIZE]; | 107 | char slaveresult[SLAVERESULTSIZE]; |
| 108 | char* perf; | ||
| 109 | |||
| 110 | perf = strdup (""); | ||
| 82 | 111 | ||
| 83 | setlocale (LC_ALL, ""); | 112 | setlocale (LC_ALL, ""); |
| 84 | bindtextdomain (PACKAGE, LOCALEDIR); | 113 | bindtextdomain (PACKAGE, LOCALEDIR); |
| @@ -126,6 +155,37 @@ main (int argc, char **argv) | |||
| 126 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | 155 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); |
| 127 | } | 156 | } |
| 128 | 157 | ||
| 158 | /* try to fetch some perf data */ | ||
| 159 | if (mysql_query (&mysql, "show global status") == 0) { | ||
| 160 | if ( (res = mysql_store_result (&mysql)) == NULL) { | ||
| 161 | error = strdup(mysql_error(&mysql)); | ||
| 162 | mysql_close (&mysql); | ||
| 163 | die (STATE_CRITICAL, _("status store_result error: %s\n"), error); | ||
| 164 | } | ||
| 165 | |||
| 166 | while ( (row = mysql_fetch_row (res)) != NULL) { | ||
| 167 | int i; | ||
| 168 | |||
| 169 | for(i = 0; i < LENGTH_METRIC_UNIT; i++) { | ||
| 170 | if (strcmp(row[0], metric_unit[i]) == 0) { | ||
| 171 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], | ||
| 172 | atol(row[1]), "", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0)); | ||
| 173 | continue; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | for(i = 0; i < LENGTH_METRIC_COUNTER; i++) { | ||
| 177 | if (strcmp(row[0], metric_counter[i]) == 0) { | ||
| 178 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], | ||
| 179 | atol(row[1]), "c", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0)); | ||
| 180 | continue; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | /* remove trailing space */ | ||
| 185 | if (strlen(perf) > 0) | ||
| 186 | perf[strlen(perf) - 1] = '\0'; | ||
| 187 | } | ||
| 188 | |||
| 129 | if(check_slave) { | 189 | if(check_slave) { |
| 130 | /* check the slave status */ | 190 | /* check the slave status */ |
| 131 | if (mysql_query (&mysql, "show slave status") != 0) { | 191 | if (mysql_query (&mysql, "show slave status") != 0) { |
| @@ -218,11 +278,17 @@ main (int argc, char **argv) | |||
| 218 | 278 | ||
| 219 | status = get_status(value, my_threshold); | 279 | status = get_status(value, my_threshold); |
| 220 | 280 | ||
| 281 | xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s", | ||
| 282 | TRUE, (double) warning_time, | ||
| 283 | TRUE, (double) critical_time, | ||
| 284 | FALSE, 0, | ||
| 285 | FALSE, 0)); | ||
| 286 | |||
| 221 | if (status == STATE_WARNING) { | 287 | if (status == STATE_WARNING) { |
| 222 | printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult); | 288 | printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); |
| 223 | exit(STATE_WARNING); | 289 | exit(STATE_WARNING); |
| 224 | } else if (status == STATE_CRITICAL) { | 290 | } else if (status == STATE_CRITICAL) { |
| 225 | printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult); | 291 | printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf); |
| 226 | exit(STATE_CRITICAL); | 292 | exit(STATE_CRITICAL); |
| 227 | } | 293 | } |
| 228 | } | 294 | } |
| @@ -237,9 +303,9 @@ main (int argc, char **argv) | |||
| 237 | 303 | ||
| 238 | /* print out the result of stats */ | 304 | /* print out the result of stats */ |
| 239 | if (check_slave) { | 305 | if (check_slave) { |
| 240 | printf ("%s %s\n", result, slaveresult); | 306 | printf ("%s %s|%s\n", result, slaveresult, perf); |
| 241 | } else { | 307 | } else { |
| 242 | printf ("%s\n", result); | 308 | printf ("%s|%s\n", result, perf); |
| 243 | } | 309 | } |
| 244 | 310 | ||
| 245 | return STATE_OK; | 311 | return STATE_OK; |
| @@ -339,9 +405,11 @@ process_arguments (int argc, char **argv) | |||
| 339 | break; | 405 | break; |
| 340 | case 'w': | 406 | case 'w': |
| 341 | warning = optarg; | 407 | warning = optarg; |
| 408 | warning_time = strtod (warning, NULL); | ||
| 342 | break; | 409 | break; |
| 343 | case 'c': | 410 | case 'c': |
| 344 | critical = optarg; | 411 | critical = optarg; |
| 412 | critical_time = strtod (critical, NULL); | ||
| 345 | break; | 413 | break; |
| 346 | case 'V': /* version */ | 414 | case 'V': /* version */ |
| 347 | print_revision (progname, NP_VERSION); | 415 | print_revision (progname, NP_VERSION); |
