diff options
Diffstat (limited to 'plugins/check_dbi.c')
| -rw-r--r-- | plugins/check_dbi.c | 121 |
1 files changed, 62 insertions, 59 deletions
diff --git a/plugins/check_dbi.c b/plugins/check_dbi.c index 286635cc..9bc68eb3 100644 --- a/plugins/check_dbi.c +++ b/plugins/check_dbi.c | |||
| @@ -34,6 +34,7 @@ const char *copyright = "2011-2024"; | |||
| 34 | const char *email = "devel@monitoring-plugins.org"; | 34 | const char *email = "devel@monitoring-plugins.org"; |
| 35 | 35 | ||
| 36 | #include "../lib/monitoringplug.h" | 36 | #include "../lib/monitoringplug.h" |
| 37 | #include "thresholds.h" | ||
| 37 | #include "perfdata.h" | 38 | #include "perfdata.h" |
| 38 | #include "output.h" | 39 | #include "output.h" |
| 39 | #include "states.h" | 40 | #include "states.h" |
| @@ -66,7 +67,6 @@ typedef struct { | |||
| 66 | } check_dbi_config_wrapper; | 67 | } check_dbi_config_wrapper; |
| 67 | 68 | ||
| 68 | static check_dbi_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | 69 | static check_dbi_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); |
| 69 | static check_dbi_config_wrapper validate_arguments(check_dbi_config_wrapper /*config_wrapper*/); | ||
| 70 | void print_usage(void); | 70 | void print_usage(void); |
| 71 | static void print_help(void); | 71 | static void print_help(void); |
| 72 | 72 | ||
| @@ -82,7 +82,8 @@ typedef struct { | |||
| 82 | const char *error_string; | 82 | const char *error_string; |
| 83 | mp_state_enum query_processing_status; | 83 | mp_state_enum query_processing_status; |
| 84 | } do_query_result; | 84 | } do_query_result; |
| 85 | static do_query_result do_query(dbi_conn conn, mp_dbi_metric metric, mp_dbi_type type, char *query); | 85 | static do_query_result do_query(dbi_conn conn, check_dbi_metric metric, check_dbi_type type, |
| 86 | char *query); | ||
| 86 | 87 | ||
| 87 | int main(int argc, char **argv) { | 88 | int main(int argc, char **argv) { |
| 88 | setlocale(LC_ALL, ""); | 89 | setlocale(LC_ALL, ""); |
| @@ -228,8 +229,8 @@ int main(int argc, char **argv) { | |||
| 228 | pd_conn_duration = mp_set_pd_value(pd_conn_duration, conn_time); | 229 | pd_conn_duration = mp_set_pd_value(pd_conn_duration, conn_time); |
| 229 | 230 | ||
| 230 | if (config.metric == METRIC_CONN_TIME) { | 231 | if (config.metric == METRIC_CONN_TIME) { |
| 231 | // TODO set pd thresholds | 232 | pd_conn_duration = mp_pd_set_thresholds(pd_conn_duration, config.thresholds); |
| 232 | mp_state_enum status = get_status(conn_time, config.dbi_thresholds); | 233 | mp_state_enum status = mp_get_pd_status(pd_conn_duration); |
| 233 | sc_connection_time = mp_set_subcheck_state(sc_connection_time, status); | 234 | sc_connection_time = mp_set_subcheck_state(sc_connection_time, status); |
| 234 | if (status != STATE_OK) { | 235 | if (status != STATE_OK) { |
| 235 | xasprintf(&sc_connection_time.output, "%s violates thresholds", | 236 | xasprintf(&sc_connection_time.output, "%s violates thresholds", |
| @@ -250,7 +251,11 @@ int main(int argc, char **argv) { | |||
| 250 | xasprintf(&sc_server_version.output, "Connected to server version %u", server_version); | 251 | xasprintf(&sc_server_version.output, "Connected to server version %u", server_version); |
| 251 | 252 | ||
| 252 | if (config.metric == METRIC_SERVER_VERSION) { | 253 | if (config.metric == METRIC_SERVER_VERSION) { |
| 253 | mp_state_enum status = get_status(server_version, config.dbi_thresholds); | 254 | mp_perfdata pd_server_version = perfdata_init(); |
| 255 | pd_server_version = mp_set_pd_value(pd_server_version, server_version); | ||
| 256 | pd_server_version = mp_pd_set_thresholds(pd_server_version, config.thresholds); | ||
| 257 | mp_state_enum status = mp_get_pd_status(pd_server_version); | ||
| 258 | mp_add_perfdata_to_subcheck(&sc_server_version, pd_server_version); | ||
| 254 | 259 | ||
| 255 | sc_server_version = mp_set_subcheck_state(sc_server_version, status); | 260 | sc_server_version = mp_set_subcheck_state(sc_server_version, status); |
| 256 | 261 | ||
| @@ -262,17 +267,16 @@ int main(int argc, char **argv) { | |||
| 262 | mp_add_subcheck_to_check(&overall, sc_server_version); | 267 | mp_add_subcheck_to_check(&overall, sc_server_version); |
| 263 | 268 | ||
| 264 | /* select a database */ | 269 | /* select a database */ |
| 265 | if (config.dbi_database) { | 270 | if (config.database) { |
| 266 | if (verbose > 1) { | 271 | if (verbose > 1) { |
| 267 | printf("Selecting database '%s'\n", config.dbi_database); | 272 | printf("Selecting database '%s'\n", config.database); |
| 268 | } | 273 | } |
| 269 | 274 | ||
| 270 | mp_subcheck sc_select_db = mp_subcheck_init(); | 275 | mp_subcheck sc_select_db = mp_subcheck_init(); |
| 271 | sc_select_db = mp_set_subcheck_default_state(sc_select_db, STATE_OK); | 276 | sc_select_db = mp_set_subcheck_default_state(sc_select_db, STATE_OK); |
| 272 | 277 | ||
| 273 | if (dbi_conn_select_db(conn, config.dbi_database)) { | 278 | if (dbi_conn_select_db(conn, config.database)) { |
| 274 | np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", | 279 | np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", config.database); |
| 275 | config.dbi_database); | ||
| 276 | exit(STATE_UNKNOWN); | 280 | exit(STATE_UNKNOWN); |
| 277 | } else { | 281 | } else { |
| 278 | mp_add_subcheck_to_check(&overall, sc_select_db); | 282 | mp_add_subcheck_to_check(&overall, sc_select_db); |
| @@ -280,12 +284,12 @@ int main(int argc, char **argv) { | |||
| 280 | } | 284 | } |
| 281 | 285 | ||
| 282 | // Do a query (if configured) | 286 | // Do a query (if configured) |
| 283 | if (config.dbi_query) { | 287 | if (config.query) { |
| 284 | mp_subcheck sc_query = mp_subcheck_init(); | 288 | mp_subcheck sc_query = mp_subcheck_init(); |
| 285 | sc_query = mp_set_subcheck_default_state(sc_query, STATE_UNKNOWN); | 289 | sc_query = mp_set_subcheck_default_state(sc_query, STATE_UNKNOWN); |
| 286 | 290 | ||
| 287 | /* execute query */ | 291 | /* execute query */ |
| 288 | do_query_result query_res = do_query(conn, config.metric, config.type, config.dbi_query); | 292 | do_query_result query_res = do_query(conn, config.metric, config.type, config.query); |
| 289 | 293 | ||
| 290 | if (query_res.error_code != 0) { | 294 | if (query_res.error_code != 0) { |
| 291 | xasprintf(&sc_query.output, "Query failed: %s", query_res.error_string); | 295 | xasprintf(&sc_query.output, "Query failed: %s", query_res.error_string); |
| @@ -299,7 +303,7 @@ int main(int argc, char **argv) { | |||
| 299 | sc_query = mp_set_subcheck_state(sc_query, query_res.query_processing_status); | 303 | sc_query = mp_set_subcheck_state(sc_query, query_res.query_processing_status); |
| 300 | } else { | 304 | } else { |
| 301 | // query succeeded in general | 305 | // query succeeded in general |
| 302 | xasprintf(&sc_query.output, "Query '%s' succeeded", config.dbi_query); | 306 | xasprintf(&sc_query.output, "Query '%s' succeeded", config.query); |
| 303 | 307 | ||
| 304 | // that's a OK by default now | 308 | // that's a OK by default now |
| 305 | sc_query = mp_set_subcheck_default_state(sc_query, STATE_OK); | 309 | sc_query = mp_set_subcheck_default_state(sc_query, STATE_OK); |
| @@ -309,7 +313,7 @@ int main(int argc, char **argv) { | |||
| 309 | pd_query_duration = mp_set_pd_value(pd_query_duration, query_res.query_duration); | 313 | pd_query_duration = mp_set_pd_value(pd_query_duration, query_res.query_duration); |
| 310 | pd_query_duration.label = "querytime"; | 314 | pd_query_duration.label = "querytime"; |
| 311 | if (config.metric == METRIC_QUERY_TIME) { | 315 | if (config.metric == METRIC_QUERY_TIME) { |
| 312 | // TODO set thresholds | 316 | pd_query_duration = mp_pd_set_thresholds(pd_query_duration, config.thresholds); |
| 313 | } | 317 | } |
| 314 | 318 | ||
| 315 | mp_add_perfdata_to_subcheck(&sc_query, pd_query_duration); | 319 | mp_add_perfdata_to_subcheck(&sc_query, pd_query_duration); |
| @@ -362,19 +366,20 @@ int main(int argc, char **argv) { | |||
| 362 | // so we expected a number | 366 | // so we expected a number |
| 363 | // this is a CRITICAL | 367 | // this is a CRITICAL |
| 364 | xasprintf(&sc_query.output, "Query '%s' result is not numeric", | 368 | xasprintf(&sc_query.output, "Query '%s' result is not numeric", |
| 365 | config.dbi_query); | 369 | config.query); |
| 366 | sc_query = mp_set_subcheck_state(sc_query, STATE_CRITICAL); | 370 | sc_query = mp_set_subcheck_state(sc_query, STATE_CRITICAL); |
| 367 | 371 | ||
| 368 | } else { | 372 | } else { |
| 369 | mp_state_enum query_numerical_result = | ||
| 370 | get_status(query_res.result_number, config.dbi_thresholds); | ||
| 371 | sc_query = mp_set_subcheck_state(sc_query, query_numerical_result); | ||
| 372 | 373 | ||
| 373 | mp_perfdata pd_query_val = perfdata_init(); | 374 | mp_perfdata pd_query_val = perfdata_init(); |
| 374 | pd_query_val = mp_set_pd_value(pd_query_val, query_res.result_number); | 375 | pd_query_val = mp_set_pd_value(pd_query_val, query_res.result_number); |
| 375 | pd_query_val.label = "query"; | 376 | pd_query_val.label = "query"; |
| 377 | pd_query_val = mp_pd_set_thresholds(pd_query_val, config.thresholds); | ||
| 378 | |||
| 376 | mp_add_perfdata_to_subcheck(&sc_query, pd_query_val); | 379 | mp_add_perfdata_to_subcheck(&sc_query, pd_query_val); |
| 380 | mp_state_enum query_numerical_result = mp_get_pd_status(pd_query_val); | ||
| 377 | 381 | ||
| 382 | sc_query = mp_set_subcheck_state(sc_query, query_numerical_result); | ||
| 378 | // TODO set pd thresholds | 383 | // TODO set pd thresholds |
| 379 | // if (config.dbi_thresholds->warning) { | 384 | // if (config.dbi_thresholds->warning) { |
| 380 | // pd_query_val.warn= config.dbi_thresholds->warning | 385 | // pd_query_val.warn= config.dbi_thresholds->warning |
| @@ -393,8 +398,7 @@ int main(int argc, char **argv) { | |||
| 393 | } | 398 | } |
| 394 | } | 399 | } |
| 395 | } else if (config.metric == METRIC_QUERY_TIME) { | 400 | } else if (config.metric == METRIC_QUERY_TIME) { |
| 396 | mp_state_enum query_time_status = | 401 | mp_state_enum query_time_status = mp_get_pd_status(pd_query_duration); |
| 397 | get_status(query_res.query_duration, config.dbi_thresholds); | ||
| 398 | mp_set_subcheck_state(sc_query, query_time_status); | 402 | mp_set_subcheck_state(sc_query, query_time_status); |
| 399 | 403 | ||
| 400 | if (query_time_status == STATE_OK) { | 404 | if (query_time_status == STATE_OK) { |
| @@ -464,14 +468,22 @@ check_dbi_config_wrapper process_arguments(int argc, char **argv) { | |||
| 464 | print_revision(progname, NP_VERSION); | 468 | print_revision(progname, NP_VERSION); |
| 465 | exit(STATE_UNKNOWN); | 469 | exit(STATE_UNKNOWN); |
| 466 | 470 | ||
| 467 | case 'c': /* critical range */ | 471 | case 'c': /* critical range */ { |
| 468 | result.config.critical_range = optarg; | 472 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 473 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 474 | die(STATE_UNKNOWN, "failed to parse critical threshold"); | ||
| 475 | } | ||
| 476 | result.config.thresholds = mp_thresholds_set_crit(result.config.thresholds, tmp.range); | ||
| 469 | result.config.type = TYPE_NUMERIC; | 477 | result.config.type = TYPE_NUMERIC; |
| 470 | break; | 478 | } break; |
| 471 | case 'w': /* warning range */ | 479 | case 'w': /* warning range */ { |
| 472 | result.config.warning_range = optarg; | 480 | mp_range_parsed tmp = mp_parse_range_string(optarg); |
| 481 | if (tmp.error != MP_PARSING_SUCCES) { | ||
| 482 | die(STATE_UNKNOWN, "failed to parse warning threshold"); | ||
| 483 | } | ||
| 484 | result.config.thresholds = mp_thresholds_set_warn(result.config.thresholds, tmp.range); | ||
| 473 | result.config.type = TYPE_NUMERIC; | 485 | result.config.type = TYPE_NUMERIC; |
| 474 | break; | 486 | } break; |
| 475 | case 'e': | 487 | case 'e': |
| 476 | result.config.expect = optarg; | 488 | result.config.expect = optarg; |
| 477 | result.config.type = TYPE_STRING; | 489 | result.config.type = TYPE_STRING; |
| @@ -559,10 +571,10 @@ check_dbi_config_wrapper process_arguments(int argc, char **argv) { | |||
| 559 | new->value = value; | 571 | new->value = value; |
| 560 | } break; | 572 | } break; |
| 561 | case 'q': | 573 | case 'q': |
| 562 | result.config.dbi_query = optarg; | 574 | result.config.query = optarg; |
| 563 | break; | 575 | break; |
| 564 | case 'D': | 576 | case 'D': |
| 565 | result.config.dbi_database = optarg; | 577 | result.config.database = optarg; |
| 566 | break; | 578 | break; |
| 567 | case output_format_index: { | 579 | case output_format_index: { |
| 568 | parsed_output_format parser = mp_parse_output_format(optarg); | 580 | parsed_output_format parser = mp_parse_output_format(optarg); |
| @@ -579,57 +591,48 @@ check_dbi_config_wrapper process_arguments(int argc, char **argv) { | |||
| 579 | } | 591 | } |
| 580 | } | 592 | } |
| 581 | 593 | ||
| 582 | set_thresholds(&result.config.dbi_thresholds, result.config.warning_range, | 594 | if (!result.config.dbi_driver) { |
| 583 | result.config.critical_range); | ||
| 584 | |||
| 585 | return validate_arguments(result); | ||
| 586 | } | ||
| 587 | |||
| 588 | check_dbi_config_wrapper validate_arguments(check_dbi_config_wrapper config_wrapper) { | ||
| 589 | if (!config_wrapper.config.dbi_driver) { | ||
| 590 | usage("Must specify a DBI driver"); | 595 | usage("Must specify a DBI driver"); |
| 591 | } | 596 | } |
| 592 | 597 | ||
| 593 | if (((config_wrapper.config.metric == METRIC_QUERY_RESULT) || | 598 | if (((result.config.metric == METRIC_QUERY_RESULT) || |
| 594 | (config_wrapper.config.metric == METRIC_QUERY_TIME)) && | 599 | (result.config.metric == METRIC_QUERY_TIME)) && |
| 595 | (!config_wrapper.config.dbi_query)) { | 600 | (!result.config.query)) { |
| 596 | usage("Must specify a query to execute (metric == QUERY_RESULT)"); | 601 | usage("Must specify a query to execute (metric == QUERY_RESULT)"); |
| 597 | } | 602 | } |
| 598 | 603 | ||
| 599 | if ((config_wrapper.config.metric != METRIC_CONN_TIME) && | 604 | if ((result.config.metric != METRIC_CONN_TIME) && |
| 600 | (config_wrapper.config.metric != METRIC_SERVER_VERSION) && | 605 | (result.config.metric != METRIC_SERVER_VERSION) && |
| 601 | (config_wrapper.config.metric != METRIC_QUERY_RESULT) && | 606 | (result.config.metric != METRIC_QUERY_RESULT) && |
| 602 | (config_wrapper.config.metric != METRIC_QUERY_TIME)) { | 607 | (result.config.metric != METRIC_QUERY_TIME)) { |
| 603 | usage("Invalid metric specified"); | 608 | usage("Invalid metric specified"); |
| 604 | } | 609 | } |
| 605 | 610 | ||
| 606 | if (config_wrapper.config.expect && | 611 | if (result.config.expect && |
| 607 | (config_wrapper.config.warning_range || config_wrapper.config.critical_range || | 612 | (result.config.thresholds.warning_is_set || result.config.thresholds.critical_is_set || |
| 608 | config_wrapper.config.expect_re_str)) { | 613 | result.config.expect_re_str)) { |
| 609 | usage("Do not mix -e and -w/-c/-r/-R"); | 614 | usage("Do not mix -e and -w/-c/-r/-R"); |
| 610 | } | 615 | } |
| 611 | 616 | ||
| 612 | if (config_wrapper.config.expect_re_str && | 617 | if (result.config.expect_re_str && |
| 613 | (config_wrapper.config.warning_range || config_wrapper.config.critical_range || | 618 | (result.config.thresholds.warning_is_set || result.config.thresholds.critical_is_set || |
| 614 | config_wrapper.config.expect)) { | 619 | result.config.expect)) { |
| 615 | usage("Do not mix -r/-R and -w/-c/-e"); | 620 | usage("Do not mix -r/-R and -w/-c/-e"); |
| 616 | } | 621 | } |
| 617 | 622 | ||
| 618 | if (config_wrapper.config.expect && (config_wrapper.config.metric != METRIC_QUERY_RESULT)) { | 623 | if (result.config.expect && (result.config.metric != METRIC_QUERY_RESULT)) { |
| 619 | usage("Option -e requires metric QUERY_RESULT"); | 624 | usage("Option -e requires metric QUERY_RESULT"); |
| 620 | } | 625 | } |
| 621 | 626 | ||
| 622 | if (config_wrapper.config.expect_re_str && | 627 | if (result.config.expect_re_str && (result.config.metric != METRIC_QUERY_RESULT)) { |
| 623 | (config_wrapper.config.metric != METRIC_QUERY_RESULT)) { | ||
| 624 | usage("Options -r/-R require metric QUERY_RESULT"); | 628 | usage("Options -r/-R require metric QUERY_RESULT"); |
| 625 | } | 629 | } |
| 626 | 630 | ||
| 627 | if (config_wrapper.config.type == TYPE_STRING) { | 631 | if (result.config.type == TYPE_STRING) { |
| 628 | assert(config_wrapper.config.expect || config_wrapper.config.expect_re_str); | 632 | assert(result.config.expect || result.config.expect_re_str); |
| 629 | } | 633 | } |
| 630 | 634 | ||
| 631 | config_wrapper.errorcode = OK; | 635 | return result; |
| 632 | return config_wrapper; | ||
| 633 | } | 636 | } |
| 634 | 637 | ||
| 635 | void print_help(void) { | 638 | void print_help(void) { |
| @@ -735,7 +738,7 @@ void print_usage(void) { | |||
| 735 | printf(" [-e <string>] [-r|-R <regex>]\n"); | 738 | printf(" [-e <string>] [-r|-R <regex>]\n"); |
| 736 | } | 739 | } |
| 737 | 740 | ||
| 738 | const char *get_field_str(dbi_result res, mp_dbi_metric metric, mp_dbi_type type) { | 741 | const char *get_field_str(dbi_result res, check_dbi_metric metric, check_dbi_type type) { |
| 739 | const char *str = dbi_result_get_string_idx(res, 1); | 742 | const char *str = dbi_result_get_string_idx(res, 1); |
| 740 | if ((!str) || (strcmp(str, "ERROR") == 0)) { | 743 | if ((!str) || (strcmp(str, "ERROR") == 0)) { |
| 741 | if (metric != METRIC_QUERY_RESULT) { | 744 | if (metric != METRIC_QUERY_RESULT) { |
| @@ -755,7 +758,7 @@ typedef struct { | |||
| 755 | int error_code; | 758 | int error_code; |
| 756 | int dbi_error_code; // not sure if useful | 759 | int dbi_error_code; // not sure if useful |
| 757 | } get_field_wrapper; | 760 | } get_field_wrapper; |
| 758 | get_field_wrapper get_field(dbi_result res, mp_dbi_metric metric, mp_dbi_type type) { | 761 | get_field_wrapper get_field(dbi_result res, check_dbi_metric metric, check_dbi_type type) { |
| 759 | 762 | ||
| 760 | unsigned short field_type = dbi_result_get_field_type_idx(res, 1); | 763 | unsigned short field_type = dbi_result_get_field_type_idx(res, 1); |
| 761 | get_field_wrapper result = { | 764 | get_field_wrapper result = { |
| @@ -815,7 +818,7 @@ get_field_wrapper get_field(dbi_result res, mp_dbi_metric metric, mp_dbi_type ty | |||
| 815 | return result; | 818 | return result; |
| 816 | } | 819 | } |
| 817 | 820 | ||
| 818 | static do_query_result do_query(dbi_conn conn, mp_dbi_metric metric, mp_dbi_type type, | 821 | static do_query_result do_query(dbi_conn conn, check_dbi_metric metric, check_dbi_type type, |
| 819 | char *query) { | 822 | char *query) { |
| 820 | assert(query); | 823 | assert(query); |
| 821 | 824 | ||
