diff options
Diffstat (limited to 'plugins/check_dbi.c')
-rw-r--r-- | plugins/check_dbi.c | 108 |
1 files changed, 73 insertions, 35 deletions
diff --git a/plugins/check_dbi.c b/plugins/check_dbi.c index 9efcd1cb..468ded31 100644 --- a/plugins/check_dbi.c +++ b/plugins/check_dbi.c | |||
@@ -71,8 +71,9 @@ static double timediff(struct timeval /*start*/, struct timeval /*end*/); | |||
71 | 71 | ||
72 | static void np_dbi_print_error(dbi_conn /*conn*/, char * /*fmt*/, ...); | 72 | static void np_dbi_print_error(dbi_conn /*conn*/, char * /*fmt*/, ...); |
73 | 73 | ||
74 | static mp_state_enum do_query(dbi_conn /*conn*/, const char ** /*res_val_str*/, double * /*res_val*/, double * /*res_time*/, mp_dbi_metric /*metric*/, | 74 | static mp_state_enum do_query(dbi_conn /*conn*/, const char ** /*res_val_str*/, |
75 | mp_dbi_type /*type*/, char * /*np_dbi_query*/); | 75 | double * /*res_val*/, double * /*res_time*/, mp_dbi_metric /*metric*/, |
76 | mp_dbi_type /*type*/, char * /*np_dbi_query*/); | ||
76 | 77 | ||
77 | int main(int argc, char **argv) { | 78 | int main(int argc, char **argv) { |
78 | int status = STATE_UNKNOWN; | 79 | int status = STATE_UNKNOWN; |
@@ -118,7 +119,8 @@ int main(int argc, char **argv) { | |||
118 | dbi_inst *instance_p = {0}; | 119 | dbi_inst *instance_p = {0}; |
119 | 120 | ||
120 | if (dbi_initialize_r(NULL, instance_p) < 0) { | 121 | if (dbi_initialize_r(NULL, instance_p) < 0) { |
121 | printf("UNKNOWN - failed to initialize DBI; possibly you don't have any drivers installed.\n"); | 122 | printf( |
123 | "UNKNOWN - failed to initialize DBI; possibly you don't have any drivers installed.\n"); | ||
122 | return STATE_UNKNOWN; | 124 | return STATE_UNKNOWN; |
123 | } | 125 | } |
124 | 126 | ||
@@ -133,10 +135,12 @@ int main(int argc, char **argv) { | |||
133 | 135 | ||
134 | driver = dbi_driver_open_r(config.dbi_driver, instance_p); | 136 | driver = dbi_driver_open_r(config.dbi_driver, instance_p); |
135 | if (!driver) { | 137 | if (!driver) { |
136 | printf("UNKNOWN - failed to open DBI driver '%s'; possibly it's not installed.\n", config.dbi_driver); | 138 | printf("UNKNOWN - failed to open DBI driver '%s'; possibly it's not installed.\n", |
139 | config.dbi_driver); | ||
137 | 140 | ||
138 | printf("Known drivers:\n"); | 141 | printf("Known drivers:\n"); |
139 | for (driver = dbi_driver_list_r(NULL, instance_p); driver; driver = dbi_driver_list_r(driver, instance_p)) { | 142 | for (driver = dbi_driver_list_r(NULL, instance_p); driver; |
143 | driver = dbi_driver_list_r(driver, instance_p)) { | ||
140 | printf(" - %s\n", dbi_driver_get_name(driver)); | 144 | printf(" - %s\n", dbi_driver_get_name(driver)); |
141 | } | 145 | } |
142 | return STATE_UNKNOWN; | 146 | return STATE_UNKNOWN; |
@@ -156,7 +160,8 @@ int main(int argc, char **argv) { | |||
156 | const char *opt; | 160 | const char *opt; |
157 | 161 | ||
158 | if (verbose > 1) { | 162 | if (verbose > 1) { |
159 | printf("Setting DBI driver option '%s' to '%s'\n", config.dbi_options[i].key, config.dbi_options[i].value); | 163 | printf("Setting DBI driver option '%s' to '%s'\n", config.dbi_options[i].key, |
164 | config.dbi_options[i].value); | ||
160 | } | 165 | } |
161 | 166 | ||
162 | if (!dbi_conn_set_option(conn, config.dbi_options[i].key, config.dbi_options[i].value)) { | 167 | if (!dbi_conn_set_option(conn, config.dbi_options[i].key, config.dbi_options[i].value)) { |
@@ -164,10 +169,12 @@ int main(int argc, char **argv) { | |||
164 | } | 169 | } |
165 | /* else: status != 0 */ | 170 | /* else: status != 0 */ |
166 | 171 | ||
167 | np_dbi_print_error(conn, "UNKNOWN - failed to set option '%s' to '%s'", config.dbi_options[i].key, config.dbi_options[i].value); | 172 | np_dbi_print_error(conn, "UNKNOWN - failed to set option '%s' to '%s'", |
173 | config.dbi_options[i].key, config.dbi_options[i].value); | ||
168 | printf("Known driver options:\n"); | 174 | printf("Known driver options:\n"); |
169 | 175 | ||
170 | for (opt = dbi_conn_get_option_list(conn, NULL); opt; opt = dbi_conn_get_option_list(conn, opt)) { | 176 | for (opt = dbi_conn_get_option_list(conn, NULL); opt; |
177 | opt = dbi_conn_get_option_list(conn, opt)) { | ||
171 | printf(" - %s\n", opt); | 178 | printf(" - %s\n", opt); |
172 | } | 179 | } |
173 | dbi_conn_close(conn); | 180 | dbi_conn_close(conn); |
@@ -230,14 +237,16 @@ int main(int argc, char **argv) { | |||
230 | } | 237 | } |
231 | 238 | ||
232 | if (dbi_conn_select_db(conn, config.dbi_database)) { | 239 | if (dbi_conn_select_db(conn, config.dbi_database)) { |
233 | np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", config.dbi_database); | 240 | np_dbi_print_error(conn, "UNKNOWN - failed to select database '%s'", |
241 | config.dbi_database); | ||
234 | return STATE_UNKNOWN; | 242 | return STATE_UNKNOWN; |
235 | } | 243 | } |
236 | } | 244 | } |
237 | 245 | ||
238 | if (config.dbi_query) { | 246 | if (config.dbi_query) { |
239 | /* execute query */ | 247 | /* execute query */ |
240 | status = do_query(conn, &query_val_str, &query_val, &query_time, config.metric, config.type, config.dbi_query); | 248 | status = do_query(conn, &query_val_str, &query_val, &query_time, config.metric, config.type, |
249 | config.dbi_query); | ||
241 | if (status != STATE_OK) { | 250 | if (status != STATE_OK) { |
242 | /* do_query prints an error message in this case */ | 251 | /* do_query prints an error message in this case */ |
243 | return status; | 252 | return status; |
@@ -281,7 +290,8 @@ int main(int argc, char **argv) { | |||
281 | /* In case of METRIC_QUERY_RESULT, isnan(query_val) indicates an error | 290 | /* In case of METRIC_QUERY_RESULT, isnan(query_val) indicates an error |
282 | * which should have been reported and handled (abort) before | 291 | * which should have been reported and handled (abort) before |
283 | * ... unless we expected a string to be returned */ | 292 | * ... unless we expected a string to be returned */ |
284 | assert((config.metric != METRIC_QUERY_RESULT) || (!isnan(query_val)) || (config.type == TYPE_STRING)); | 293 | assert((config.metric != METRIC_QUERY_RESULT) || (!isnan(query_val)) || |
294 | (config.type == TYPE_STRING)); | ||
285 | 295 | ||
286 | assert((config.type != TYPE_STRING) || (config.expect || config.expect_re_str)); | 296 | assert((config.type != TYPE_STRING) || (config.expect || config.expect_re_str)); |
287 | 297 | ||
@@ -289,12 +299,14 @@ int main(int argc, char **argv) { | |||
289 | if (config.dbi_query) { | 299 | if (config.dbi_query) { |
290 | if (config.type == TYPE_STRING) { | 300 | if (config.type == TYPE_STRING) { |
291 | assert(config.expect || config.expect_re_str); | 301 | assert(config.expect || config.expect_re_str); |
292 | printf(", '%s' returned '%s' in %fs", config.dbi_query, query_val_str ? query_val_str : "<nothing>", query_time); | 302 | printf(", '%s' returned '%s' in %fs", config.dbi_query, |
303 | query_val_str ? query_val_str : "<nothing>", query_time); | ||
293 | if (status != STATE_OK) { | 304 | if (status != STATE_OK) { |
294 | if (config.expect) { | 305 | if (config.expect) { |
295 | printf(" (expected '%s')", config.expect); | 306 | printf(" (expected '%s')", config.expect); |
296 | } else if (config.expect_re_str) { | 307 | } else if (config.expect_re_str) { |
297 | printf(" (expected regex /%s/%s)", config.expect_re_str, ((config.expect_re_cflags & REG_ICASE) ? "i" : "")); | 308 | printf(" (expected regex /%s/%s)", config.expect_re_str, |
309 | ((config.expect_re_cflags & REG_ICASE) ? "i" : "")); | ||
298 | } | 310 | } |
299 | } | 311 | } |
300 | } else if (isnan(query_val)) { | 312 | } else if (isnan(query_val)) { |
@@ -304,18 +316,31 @@ int main(int argc, char **argv) { | |||
304 | } | 316 | } |
305 | } | 317 | } |
306 | 318 | ||
307 | printf(" | conntime=%fs;%s;%s;0; server_version=%u;%s;%s;0;", conn_time, | 319 | printf( |
308 | ((config.metric == METRIC_CONN_TIME) && config.warning_range) ? config.warning_range : "", | 320 | " | conntime=%fs;%s;%s;0; server_version=%u;%s;%s;0;", conn_time, |
309 | ((config.metric == METRIC_CONN_TIME) && config.critical_range) ? config.critical_range : "", server_version, | 321 | ((config.metric == METRIC_CONN_TIME) && config.warning_range) ? config.warning_range : "", |
310 | ((config.metric == METRIC_SERVER_VERSION) && config.warning_range) ? config.warning_range : "", | 322 | ((config.metric == METRIC_CONN_TIME) && config.critical_range) ? config.critical_range : "", |
311 | ((config.metric == METRIC_SERVER_VERSION) && config.critical_range) ? config.critical_range : ""); | 323 | server_version, |
324 | ((config.metric == METRIC_SERVER_VERSION) && config.warning_range) ? config.warning_range | ||
325 | : "", | ||
326 | ((config.metric == METRIC_SERVER_VERSION) && config.critical_range) ? config.critical_range | ||
327 | : ""); | ||
312 | if (config.dbi_query) { | 328 | if (config.dbi_query) { |
313 | if (!isnan(query_val)) { /* this is also true when -e is used */ | 329 | if (!isnan(query_val)) { /* this is also true when -e is used */ |
314 | printf(" query=%f;%s;%s;;", query_val, ((config.metric == METRIC_QUERY_RESULT) && config.warning_range) ? config.warning_range : "", | 330 | printf(" query=%f;%s;%s;;", query_val, |
315 | ((config.metric == METRIC_QUERY_RESULT) && config.critical_range) ? config.critical_range : ""); | 331 | ((config.metric == METRIC_QUERY_RESULT) && config.warning_range) |
332 | ? config.warning_range | ||
333 | : "", | ||
334 | ((config.metric == METRIC_QUERY_RESULT) && config.critical_range) | ||
335 | ? config.critical_range | ||
336 | : ""); | ||
316 | } | 337 | } |
317 | printf(" querytime=%fs;%s;%s;0;", query_time, ((config.metric == METRIC_QUERY_TIME) && config.warning_range) ? config.warning_range : "", | 338 | printf(" querytime=%fs;%s;%s;0;", query_time, |
318 | ((config.metric == METRIC_QUERY_TIME) && config.critical_range) ? config.critical_range : ""); | 339 | ((config.metric == METRIC_QUERY_TIME) && config.warning_range) ? config.warning_range |
340 | : "", | ||
341 | ((config.metric == METRIC_QUERY_TIME) && config.critical_range) | ||
342 | ? config.critical_range | ||
343 | : ""); | ||
319 | } | 344 | } |
320 | printf("\n"); | 345 | printf("\n"); |
321 | return status; | 346 | return status; |
@@ -442,7 +467,8 @@ check_dbi_config_wrapper process_arguments(int argc, char **argv) { | |||
442 | *value = '\0'; | 467 | *value = '\0'; |
443 | ++value; | 468 | ++value; |
444 | 469 | ||
445 | new = realloc(result.config.dbi_options, (result.config.dbi_options_num + 1) * sizeof(*new)); | 470 | new = realloc(result.config.dbi_options, |
471 | (result.config.dbi_options_num + 1) * sizeof(*new)); | ||
446 | if (!new) { | 472 | if (!new) { |
447 | printf("UNKNOWN - failed to reallocate memory\n"); | 473 | printf("UNKNOWN - failed to reallocate memory\n"); |
448 | exit(STATE_UNKNOWN); | 474 | exit(STATE_UNKNOWN); |
@@ -464,7 +490,8 @@ check_dbi_config_wrapper process_arguments(int argc, char **argv) { | |||
464 | } | 490 | } |
465 | } | 491 | } |
466 | 492 | ||
467 | set_thresholds(&result.config.dbi_thresholds, result.config.warning_range, result.config.critical_range); | 493 | set_thresholds(&result.config.dbi_thresholds, result.config.warning_range, |
494 | result.config.critical_range); | ||
468 | 495 | ||
469 | return validate_arguments(result); | 496 | return validate_arguments(result); |
470 | } | 497 | } |
@@ -474,21 +501,28 @@ check_dbi_config_wrapper validate_arguments(check_dbi_config_wrapper config_wrap | |||
474 | usage("Must specify a DBI driver"); | 501 | usage("Must specify a DBI driver"); |
475 | } | 502 | } |
476 | 503 | ||
477 | if (((config_wrapper.config.metric == METRIC_QUERY_RESULT) || (config_wrapper.config.metric == METRIC_QUERY_TIME)) && | 504 | if (((config_wrapper.config.metric == METRIC_QUERY_RESULT) || |
505 | (config_wrapper.config.metric == METRIC_QUERY_TIME)) && | ||
478 | (!config_wrapper.config.dbi_query)) { | 506 | (!config_wrapper.config.dbi_query)) { |
479 | usage("Must specify a query to execute (metric == QUERY_RESULT)"); | 507 | usage("Must specify a query to execute (metric == QUERY_RESULT)"); |
480 | } | 508 | } |
481 | 509 | ||
482 | if ((config_wrapper.config.metric != METRIC_CONN_TIME) && (config_wrapper.config.metric != METRIC_SERVER_VERSION) && | 510 | if ((config_wrapper.config.metric != METRIC_CONN_TIME) && |
483 | (config_wrapper.config.metric != METRIC_QUERY_RESULT) && (config_wrapper.config.metric != METRIC_QUERY_TIME)) { | 511 | (config_wrapper.config.metric != METRIC_SERVER_VERSION) && |
512 | (config_wrapper.config.metric != METRIC_QUERY_RESULT) && | ||
513 | (config_wrapper.config.metric != METRIC_QUERY_TIME)) { | ||
484 | usage("Invalid metric specified"); | 514 | usage("Invalid metric specified"); |
485 | } | 515 | } |
486 | 516 | ||
487 | if (config_wrapper.config.expect && (config_wrapper.config.warning_range || config_wrapper.config.critical_range || config_wrapper.config.expect_re_str)) { | 517 | if (config_wrapper.config.expect && |
518 | (config_wrapper.config.warning_range || config_wrapper.config.critical_range || | ||
519 | config_wrapper.config.expect_re_str)) { | ||
488 | usage("Do not mix -e and -w/-c/-r/-R"); | 520 | usage("Do not mix -e and -w/-c/-r/-R"); |
489 | } | 521 | } |
490 | 522 | ||
491 | if (config_wrapper.config.expect_re_str && (config_wrapper.config.warning_range || config_wrapper.config.critical_range || config_wrapper.config.expect)) { | 523 | if (config_wrapper.config.expect_re_str && |
524 | (config_wrapper.config.warning_range || config_wrapper.config.critical_range || | ||
525 | config_wrapper.config.expect)) { | ||
492 | usage("Do not mix -r/-R and -w/-c/-e"); | 526 | usage("Do not mix -r/-R and -w/-c/-e"); |
493 | } | 527 | } |
494 | 528 | ||
@@ -496,7 +530,8 @@ check_dbi_config_wrapper validate_arguments(check_dbi_config_wrapper config_wrap | |||
496 | usage("Option -e requires metric QUERY_RESULT"); | 530 | usage("Option -e requires metric QUERY_RESULT"); |
497 | } | 531 | } |
498 | 532 | ||
499 | if (config_wrapper.config.expect_re_str && (config_wrapper.config.metric != METRIC_QUERY_RESULT)) { | 533 | if (config_wrapper.config.expect_re_str && |
534 | (config_wrapper.config.metric != METRIC_QUERY_RESULT)) { | ||
500 | usage("Options -r/-R require metric QUERY_RESULT"); | 535 | usage("Options -r/-R require metric QUERY_RESULT"); |
501 | } | 536 | } |
502 | 537 | ||
@@ -607,7 +642,8 @@ void print_usage(void) { | |||
607 | printf(" [-e <string>] [-r|-R <regex>]\n"); | 642 | printf(" [-e <string>] [-r|-R <regex>]\n"); |
608 | } | 643 | } |
609 | 644 | ||
610 | const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_type, mp_dbi_metric metric, mp_dbi_type type) { | 645 | const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_type, |
646 | mp_dbi_metric metric, mp_dbi_type type) { | ||
611 | const char *str; | 647 | const char *str; |
612 | 648 | ||
613 | if (field_type != DBI_TYPE_STRING) { | 649 | if (field_type != DBI_TYPE_STRING) { |
@@ -630,7 +666,8 @@ const char *get_field_str(dbi_conn conn, dbi_result res, unsigned short field_ty | |||
630 | return str; | 666 | return str; |
631 | } | 667 | } |
632 | 668 | ||
633 | double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type, mp_dbi_metric metric, mp_dbi_type type) { | 669 | double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type, mp_dbi_metric metric, |
670 | mp_dbi_type type) { | ||
634 | double val = NAN; | 671 | double val = NAN; |
635 | 672 | ||
636 | if (*field_type == DBI_TYPE_INTEGER) { | 673 | if (*field_type == DBI_TYPE_INTEGER) { |
@@ -679,7 +716,8 @@ double get_field(dbi_conn conn, dbi_result res, unsigned short *field_type, mp_d | |||
679 | return val; | 716 | return val; |
680 | } | 717 | } |
681 | 718 | ||
682 | mp_state_enum get_query_result(dbi_conn conn, dbi_result res, const char **res_val_str, double *res_val, mp_dbi_metric metric, mp_dbi_type type) { | 719 | mp_state_enum get_query_result(dbi_conn conn, dbi_result res, const char **res_val_str, |
720 | double *res_val, mp_dbi_metric metric, mp_dbi_type type) { | ||
683 | unsigned short field_type; | 721 | unsigned short field_type; |
684 | double val = NAN; | 722 | double val = NAN; |
685 | 723 | ||
@@ -747,8 +785,8 @@ mp_state_enum get_query_result(dbi_conn conn, dbi_result res, const char **res_v | |||
747 | return STATE_OK; | 785 | return STATE_OK; |
748 | } | 786 | } |
749 | 787 | ||
750 | mp_state_enum do_query(dbi_conn conn, const char **res_val_str, double *res_val, double *res_time, mp_dbi_metric metric, mp_dbi_type type, | 788 | mp_state_enum do_query(dbi_conn conn, const char **res_val_str, double *res_val, double *res_time, |
751 | char *np_dbi_query) { | 789 | mp_dbi_metric metric, mp_dbi_type type, char *np_dbi_query) { |
752 | dbi_result res; | 790 | dbi_result res; |
753 | 791 | ||
754 | struct timeval timeval_start; | 792 | struct timeval timeval_start; |