diff options
Diffstat (limited to 'plugins/check_mysql.c')
| -rw-r--r-- | plugins/check_mysql.c | 154 |
1 files changed, 147 insertions, 7 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index 51579c2a..db670e2d 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c | |||
| @@ -49,10 +49,44 @@ char *db_host = NULL; | |||
| 49 | char *db_socket = NULL; | 49 | char *db_socket = NULL; |
| 50 | char *db_pass = NULL; | 50 | char *db_pass = NULL; |
| 51 | char *db = NULL; | 51 | char *db = NULL; |
| 52 | char *ca_cert = NULL; | ||
| 53 | char *ca_dir = NULL; | ||
| 54 | char *cert = NULL; | ||
| 55 | char *key = NULL; | ||
| 56 | char *ciphers = NULL; | ||
| 57 | bool ssl = false; | ||
| 58 | char *opt_file = NULL; | ||
| 59 | char *opt_group = NULL; | ||
| 52 | unsigned int db_port = MYSQL_PORT; | 60 | unsigned int db_port = MYSQL_PORT; |
| 53 | int check_slave = 0, warn_sec = 0, crit_sec = 0; | 61 | int check_slave = 0, warn_sec = 0, crit_sec = 0; |
| 54 | int verbose = 0; | 62 | int verbose = 0; |
| 55 | 63 | ||
| 64 | static double warning_time = 0; | ||
| 65 | static double critical_time = 0; | ||
| 66 | |||
| 67 | #define LENGTH_METRIC_UNIT 6 | ||
| 68 | static const char *metric_unit[LENGTH_METRIC_UNIT] = { | ||
| 69 | "Open_files", | ||
| 70 | "Open_tables", | ||
| 71 | "Qcache_free_memory", | ||
| 72 | "Qcache_queries_in_cache", | ||
| 73 | "Threads_connected", | ||
| 74 | "Threads_running" | ||
| 75 | }; | ||
| 76 | |||
| 77 | #define LENGTH_METRIC_COUNTER 9 | ||
| 78 | static const char *metric_counter[LENGTH_METRIC_COUNTER] = { | ||
| 79 | "Connections", | ||
| 80 | "Qcache_hits", | ||
| 81 | "Qcache_inserts", | ||
| 82 | "Qcache_lowmem_prunes", | ||
| 83 | "Qcache_not_cached", | ||
| 84 | "Queries", | ||
| 85 | "Questions", | ||
| 86 | "Table_locks_waited", | ||
| 87 | "Uptime" | ||
| 88 | }; | ||
| 89 | |||
| 56 | thresholds *my_threshold = NULL; | 90 | thresholds *my_threshold = NULL; |
| 57 | 91 | ||
| 58 | int process_arguments (int, char **); | 92 | int process_arguments (int, char **); |
| @@ -73,6 +107,9 @@ main (int argc, char **argv) | |||
| 73 | char *result = NULL; | 107 | char *result = NULL; |
| 74 | char *error = NULL; | 108 | char *error = NULL; |
| 75 | char slaveresult[SLAVERESULTSIZE]; | 109 | char slaveresult[SLAVERESULTSIZE]; |
| 110 | char* perf; | ||
| 111 | |||
| 112 | perf = strdup (""); | ||
| 76 | 113 | ||
| 77 | setlocale (LC_ALL, ""); | 114 | setlocale (LC_ALL, ""); |
| 78 | bindtextdomain (PACKAGE, LOCALEDIR); | 115 | bindtextdomain (PACKAGE, LOCALEDIR); |
| @@ -86,9 +123,17 @@ main (int argc, char **argv) | |||
| 86 | 123 | ||
| 87 | /* initialize mysql */ | 124 | /* initialize mysql */ |
| 88 | mysql_init (&mysql); | 125 | mysql_init (&mysql); |
| 126 | |||
| 127 | if (opt_file != NULL) | ||
| 128 | mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file); | ||
| 89 | 129 | ||
| 90 | mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); | 130 | if (opt_group != NULL) |
| 131 | mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group); | ||
| 132 | else | ||
| 133 | mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); | ||
| 91 | 134 | ||
| 135 | if (ssl) | ||
| 136 | mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers); | ||
| 92 | /* establish a connection to the server and error checking */ | 137 | /* establish a connection to the server and error checking */ |
| 93 | if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) { | 138 | if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) { |
| 94 | if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) | 139 | if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) |
| @@ -118,6 +163,37 @@ main (int argc, char **argv) | |||
| 118 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); | 163 | die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); |
| 119 | } | 164 | } |
| 120 | 165 | ||
| 166 | /* try to fetch some perf data */ | ||
| 167 | if (mysql_query (&mysql, "show global status") == 0) { | ||
| 168 | if ( (res = mysql_store_result (&mysql)) == NULL) { | ||
| 169 | error = strdup(mysql_error(&mysql)); | ||
| 170 | mysql_close (&mysql); | ||
| 171 | die (STATE_CRITICAL, _("status store_result error: %s\n"), error); | ||
| 172 | } | ||
| 173 | |||
| 174 | while ( (row = mysql_fetch_row (res)) != NULL) { | ||
| 175 | int i; | ||
| 176 | |||
| 177 | for(i = 0; i < LENGTH_METRIC_UNIT; i++) { | ||
| 178 | if (strcmp(row[0], metric_unit[i]) == 0) { | ||
| 179 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i], | ||
| 180 | atol(row[1]), "", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0)); | ||
| 181 | continue; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | for(i = 0; i < LENGTH_METRIC_COUNTER; i++) { | ||
| 185 | if (strcmp(row[0], metric_counter[i]) == 0) { | ||
| 186 | xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i], | ||
| 187 | atol(row[1]), "c", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0)); | ||
| 188 | continue; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | /* remove trailing space */ | ||
| 193 | if (strlen(perf) > 0) | ||
| 194 | perf[strlen(perf) - 1] = '\0'; | ||
| 195 | } | ||
| 196 | |||
| 121 | if(check_slave) { | 197 | if(check_slave) { |
| 122 | /* check the slave status */ | 198 | /* check the slave status */ |
| 123 | if (mysql_query (&mysql, "show slave status") != 0) { | 199 | if (mysql_query (&mysql, "show slave status") != 0) { |
| @@ -210,11 +286,17 @@ main (int argc, char **argv) | |||
| 210 | 286 | ||
| 211 | status = get_status(value, my_threshold); | 287 | status = get_status(value, my_threshold); |
| 212 | 288 | ||
| 289 | xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s", | ||
| 290 | TRUE, (double) warning_time, | ||
| 291 | TRUE, (double) critical_time, | ||
| 292 | FALSE, 0, | ||
| 293 | FALSE, 0)); | ||
| 294 | |||
| 213 | if (status == STATE_WARNING) { | 295 | if (status == STATE_WARNING) { |
| 214 | printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult); | 296 | printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); |
| 215 | exit(STATE_WARNING); | 297 | exit(STATE_WARNING); |
| 216 | } else if (status == STATE_CRITICAL) { | 298 | } else if (status == STATE_CRITICAL) { |
| 217 | printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult); | 299 | printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf); |
| 218 | exit(STATE_CRITICAL); | 300 | exit(STATE_CRITICAL); |
| 219 | } | 301 | } |
| 220 | } | 302 | } |
| @@ -229,9 +311,9 @@ main (int argc, char **argv) | |||
| 229 | 311 | ||
| 230 | /* print out the result of stats */ | 312 | /* print out the result of stats */ |
| 231 | if (check_slave) { | 313 | if (check_slave) { |
| 232 | printf ("%s %s\n", result, slaveresult); | 314 | printf ("%s %s|%s\n", result, slaveresult, perf); |
| 233 | } else { | 315 | } else { |
| 234 | printf ("%s\n", result); | 316 | printf ("%s|%s\n", result, perf); |
| 235 | } | 317 | } |
| 236 | 318 | ||
| 237 | return STATE_OK; | 319 | return STATE_OK; |
| @@ -253,6 +335,8 @@ process_arguments (int argc, char **argv) | |||
| 253 | {"database", required_argument, 0, 'd'}, | 335 | {"database", required_argument, 0, 'd'}, |
| 254 | {"username", required_argument, 0, 'u'}, | 336 | {"username", required_argument, 0, 'u'}, |
| 255 | {"password", required_argument, 0, 'p'}, | 337 | {"password", required_argument, 0, 'p'}, |
| 338 | {"file", required_argument, 0, 'f'}, | ||
| 339 | {"group", required_argument, 0, 'g'}, | ||
| 256 | {"port", required_argument, 0, 'P'}, | 340 | {"port", required_argument, 0, 'P'}, |
| 257 | {"critical", required_argument, 0, 'c'}, | 341 | {"critical", required_argument, 0, 'c'}, |
| 258 | {"warning", required_argument, 0, 'w'}, | 342 | {"warning", required_argument, 0, 'w'}, |
| @@ -260,6 +344,12 @@ process_arguments (int argc, char **argv) | |||
| 260 | {"verbose", no_argument, 0, 'v'}, | 344 | {"verbose", no_argument, 0, 'v'}, |
| 261 | {"version", no_argument, 0, 'V'}, | 345 | {"version", no_argument, 0, 'V'}, |
| 262 | {"help", no_argument, 0, 'h'}, | 346 | {"help", no_argument, 0, 'h'}, |
| 347 | {"ssl", no_argument, 0, 'l'}, | ||
| 348 | {"ca-cert", optional_argument, 0, 'C'}, | ||
| 349 | {"key", required_argument,0,'k'}, | ||
| 350 | {"cert", required_argument,0,'a'}, | ||
| 351 | {"ca-dir", required_argument, 0, 'D'}, | ||
| 352 | {"ciphers", required_argument, 0, 'L'}, | ||
| 263 | {0, 0, 0, 0} | 353 | {0, 0, 0, 0} |
| 264 | }; | 354 | }; |
| 265 | 355 | ||
| @@ -267,7 +357,7 @@ process_arguments (int argc, char **argv) | |||
| 267 | return ERROR; | 357 | return ERROR; |
| 268 | 358 | ||
| 269 | while (1) { | 359 | while (1) { |
| 270 | c = getopt_long (argc, argv, "hvVSP:p:u:d:H:s:c:w:", longopts, &option); | 360 | c = getopt_long (argc, argv, "hlvVSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option); |
| 271 | 361 | ||
| 272 | if (c == -1 || c == EOF) | 362 | if (c == -1 || c == EOF) |
| 273 | break; | 363 | break; |
| @@ -287,6 +377,24 @@ process_arguments (int argc, char **argv) | |||
| 287 | case 'd': /* database */ | 377 | case 'd': /* database */ |
| 288 | db = optarg; | 378 | db = optarg; |
| 289 | break; | 379 | break; |
| 380 | case 'l': | ||
| 381 | ssl = true; | ||
| 382 | break; | ||
| 383 | case 'C': | ||
| 384 | ca_cert = optarg; | ||
| 385 | break; | ||
| 386 | case 'a': | ||
| 387 | cert = optarg; | ||
| 388 | break; | ||
| 389 | case 'k': | ||
| 390 | key = optarg; | ||
| 391 | break; | ||
| 392 | case 'D': | ||
| 393 | ca_dir = optarg; | ||
| 394 | break; | ||
| 395 | case 'L': | ||
| 396 | ciphers = optarg; | ||
| 397 | break; | ||
| 290 | case 'u': /* username */ | 398 | case 'u': /* username */ |
| 291 | db_user = optarg; | 399 | db_user = optarg; |
| 292 | break; | 400 | break; |
| @@ -299,6 +407,12 @@ process_arguments (int argc, char **argv) | |||
| 299 | optarg++; | 407 | optarg++; |
| 300 | } | 408 | } |
| 301 | break; | 409 | break; |
| 410 | case 'f': /* client options file */ | ||
| 411 | opt_file = optarg; | ||
| 412 | break; | ||
| 413 | case 'g': /* client options group */ | ||
| 414 | opt_group = optarg; | ||
| 415 | break; | ||
| 302 | case 'P': /* critical time threshold */ | 416 | case 'P': /* critical time threshold */ |
| 303 | db_port = atoi (optarg); | 417 | db_port = atoi (optarg); |
| 304 | break; | 418 | break; |
| @@ -307,9 +421,11 @@ process_arguments (int argc, char **argv) | |||
| 307 | break; | 421 | break; |
| 308 | case 'w': | 422 | case 'w': |
| 309 | warning = optarg; | 423 | warning = optarg; |
| 424 | warning_time = strtod (warning, NULL); | ||
| 310 | break; | 425 | break; |
| 311 | case 'c': | 426 | case 'c': |
| 312 | critical = optarg; | 427 | critical = optarg; |
| 428 | critical_time = strtod (critical, NULL); | ||
| 313 | break; | 429 | break; |
| 314 | case 'V': /* version */ | 430 | case 'V': /* version */ |
| 315 | print_revision (progname, NP_VERSION); | 431 | print_revision (progname, NP_VERSION); |
| @@ -360,6 +476,12 @@ validate_arguments (void) | |||
| 360 | if (db_user == NULL) | 476 | if (db_user == NULL) |
| 361 | db_user = strdup(""); | 477 | db_user = strdup(""); |
| 362 | 478 | ||
| 479 | if (opt_file == NULL) | ||
| 480 | opt_file = strdup(""); | ||
| 481 | |||
| 482 | if (opt_group == NULL) | ||
| 483 | opt_group = strdup(""); | ||
| 484 | |||
| 363 | if (db_host == NULL) | 485 | if (db_host == NULL) |
| 364 | db_host = strdup(""); | 486 | db_host = strdup(""); |
| 365 | 487 | ||
| @@ -395,6 +517,10 @@ print_help (void) | |||
| 395 | 517 | ||
| 396 | printf (" %s\n", "-d, --database=STRING"); | 518 | printf (" %s\n", "-d, --database=STRING"); |
| 397 | printf (" %s\n", _("Check database with indicated name")); | 519 | printf (" %s\n", _("Check database with indicated name")); |
| 520 | printf (" %s\n", "-f, --file=STRING"); | ||
| 521 | printf (" %s\n", _("Read from the specified client options file")); | ||
| 522 | printf (" %s\n", "-g, --group=STRING"); | ||
| 523 | printf (" %s\n", _("Use a client options group")); | ||
| 398 | printf (" %s\n", "-u, --username=STRING"); | 524 | printf (" %s\n", "-u, --username=STRING"); |
| 399 | printf (" %s\n", _("Connect using the indicated username")); | 525 | printf (" %s\n", _("Connect using the indicated username")); |
| 400 | printf (" %s\n", "-p, --password=STRING"); | 526 | printf (" %s\n", "-p, --password=STRING"); |
| @@ -409,6 +535,19 @@ print_help (void) | |||
| 409 | printf (" %s\n", "-c, --critical"); | 535 | printf (" %s\n", "-c, --critical"); |
| 410 | printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); | 536 | printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); |
| 411 | printf (" %s\n", _("behind master")); | 537 | printf (" %s\n", _("behind master")); |
| 538 | printf (" %s\n", "-l, --ssl"); | ||
| 539 | printf (" %s\n", _("Use ssl encryptation")); | ||
| 540 | printf (" %s\n", "-C, --ca-cert=STRING"); | ||
| 541 | printf (" %s\n", _("Path to CA signing the cert")); | ||
| 542 | printf (" %s\n", "-a, --cert=STRING"); | ||
| 543 | printf (" %s\n", _("Path to SSL certificate")); | ||
| 544 | printf (" %s\n", "-k, --key=STRING"); | ||
| 545 | printf (" %s\n", _("Path to private SSL key")); | ||
| 546 | printf (" %s\n", "-D, --ca-dir=STRING"); | ||
| 547 | printf (" %s\n", _("Path to CA directory")); | ||
| 548 | printf (" %s\n", "-L, --ciphers=STRING"); | ||
| 549 | printf (" %s\n", _("List of valid SSL ciphers")); | ||
| 550 | |||
| 412 | 551 | ||
| 413 | printf ("\n"); | 552 | printf ("\n"); |
| 414 | printf (" %s\n", _("There are no required arguments. By default, the local database is checked")); | 553 | printf (" %s\n", _("There are no required arguments. By default, the local database is checked")); |
| @@ -429,5 +568,6 @@ print_usage (void) | |||
| 429 | { | 568 | { |
| 430 | printf ("%s\n", _("Usage:")); | 569 | printf ("%s\n", _("Usage:")); |
| 431 | printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname); | 570 | printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname); |
| 432 | printf (" [-u user] [-p password] [-S]\n"); | 571 | printf (" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n"); |
| 572 | printf (" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n"); | ||
| 433 | } | 573 | } |
