diff options
-rw-r--r-- | plugins/check_dbi.c | 54 |
1 files changed, 37 insertions, 17 deletions
diff --git a/plugins/check_dbi.c b/plugins/check_dbi.c index d4ac4e3..f59d7c4 100644 --- a/plugins/check_dbi.c +++ b/plugins/check_dbi.c | |||
@@ -69,9 +69,11 @@ int validate_arguments (void); | |||
69 | void print_usage (void); | 69 | void print_usage (void); |
70 | void print_help (void); | 70 | void print_help (void); |
71 | 71 | ||
72 | double timediff (struct timeval, struct timeval); | ||
73 | |||
72 | void np_dbi_print_error (dbi_conn, char *, ...); | 74 | void np_dbi_print_error (dbi_conn, char *, ...); |
73 | 75 | ||
74 | int do_query (dbi_conn, double *); | 76 | int do_query (dbi_conn, double *, double *); |
75 | 77 | ||
76 | int | 78 | int |
77 | main (int argc, char **argv) | 79 | main (int argc, char **argv) |
@@ -85,7 +87,8 @@ main (int argc, char **argv) | |||
85 | dbi_conn conn; | 87 | dbi_conn conn; |
86 | 88 | ||
87 | struct timeval start_timeval, end_timeval; | 89 | struct timeval start_timeval, end_timeval; |
88 | double elapsed_time; | 90 | double conn_time = 0.0; |
91 | double query_time = 0.0; | ||
89 | 92 | ||
90 | double query_val = 0.0; | 93 | double query_val = 0.0; |
91 | 94 | ||
@@ -190,17 +193,12 @@ main (int argc, char **argv) | |||
190 | } | 193 | } |
191 | 194 | ||
192 | gettimeofday (&end_timeval, NULL); | 195 | gettimeofday (&end_timeval, NULL); |
193 | while (start_timeval.tv_usec > end_timeval.tv_usec) { | 196 | conn_time = timediff (start_timeval, end_timeval); |
194 | --end_timeval.tv_sec; | ||
195 | end_timeval.tv_usec += 1000000; | ||
196 | } | ||
197 | elapsed_time = (double)(end_timeval.tv_sec - start_timeval.tv_sec) | ||
198 | + (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0; | ||
199 | 197 | ||
200 | if (verbose) | 198 | if (verbose) |
201 | printf("Time elapsed: %f\n", elapsed_time); | 199 | printf("Time elapsed: %f\n", conn_time); |
202 | 200 | ||
203 | conntime_status = get_status (elapsed_time, conntime_thresholds); | 201 | conntime_status = get_status (conn_time, conntime_thresholds); |
204 | 202 | ||
205 | /* select a database */ | 203 | /* select a database */ |
206 | if (np_dbi_database) { | 204 | if (np_dbi_database) { |
@@ -215,7 +213,7 @@ main (int argc, char **argv) | |||
215 | } | 213 | } |
216 | 214 | ||
217 | /* execute query */ | 215 | /* execute query */ |
218 | status = do_query (conn, &query_val); | 216 | status = do_query (conn, &query_val, &query_time); |
219 | if (status != STATE_OK) | 217 | if (status != STATE_OK) |
220 | /* do_query prints an error message in this case */ | 218 | /* do_query prints an error message in this case */ |
221 | return status; | 219 | return status; |
@@ -234,14 +232,15 @@ main (int argc, char **argv) | |||
234 | else | 232 | else |
235 | exit_status = status; | 233 | exit_status = status; |
236 | 234 | ||
237 | printf ("%s - %s: connection time: %fs, %s: '%s' returned %f", | 235 | printf ("%s - %s: connection time: %fs, %s: '%s' returned %f in %fs", |
238 | state_text (exit_status), | 236 | state_text (exit_status), |
239 | state_text (conntime_status), elapsed_time, | 237 | state_text (conntime_status), conn_time, |
240 | state_text (status), np_dbi_query, query_val); | 238 | state_text (status), np_dbi_query, query_val, query_time); |
241 | printf (" | conntime=%fs;%s;%s;0 query=%f;%s;%s;0\n", elapsed_time, | 239 | printf (" | conntime=%fs;%s;%s;0 query=%f;%s;%s;0 querytime=%fs;;;0\n", conn_time, |
242 | conntime_warning_range ? conntime_warning_range : "", | 240 | conntime_warning_range ? conntime_warning_range : "", |
243 | conntime_critical_range ? conntime_critical_range : "", | 241 | conntime_critical_range ? conntime_critical_range : "", |
244 | query_val, warning_range ? warning_range : "", critical_range ? critical_range : ""); | 242 | query_val, warning_range ? warning_range : "", critical_range ? critical_range : "", |
243 | query_time); | ||
245 | return exit_status; | 244 | return exit_status; |
246 | } | 245 | } |
247 | 246 | ||
@@ -486,16 +485,20 @@ get_field (dbi_conn conn, dbi_result res, unsigned short *field_type) | |||
486 | } | 485 | } |
487 | 486 | ||
488 | int | 487 | int |
489 | do_query (dbi_conn conn, double *res_val) | 488 | do_query (dbi_conn conn, double *res_val, double *res_time) |
490 | { | 489 | { |
491 | dbi_result res; | 490 | dbi_result res; |
492 | 491 | ||
493 | unsigned short field_type; | 492 | unsigned short field_type; |
494 | double val = 0.0; | 493 | double val = 0.0; |
495 | 494 | ||
495 | struct timeval timeval_start, timeval_end; | ||
496 | |||
496 | if (verbose) | 497 | if (verbose) |
497 | printf ("Executing query '%s'\n", np_dbi_query); | 498 | printf ("Executing query '%s'\n", np_dbi_query); |
498 | 499 | ||
500 | gettimeofday (&timeval_start, NULL); | ||
501 | |||
499 | res = dbi_conn_query (conn, np_dbi_query); | 502 | res = dbi_conn_query (conn, np_dbi_query); |
500 | if (! res) { | 503 | if (! res) { |
501 | np_dbi_print_error (conn, "CRITICAL - failed to execute query '%s'", np_dbi_query); | 504 | np_dbi_print_error (conn, "CRITICAL - failed to execute query '%s'", np_dbi_query); |
@@ -531,6 +534,9 @@ do_query (dbi_conn conn, double *res_val) | |||
531 | if (field_type != DBI_TYPE_ERROR) | 534 | if (field_type != DBI_TYPE_ERROR) |
532 | val = get_field (conn, res, &field_type); | 535 | val = get_field (conn, res, &field_type); |
533 | 536 | ||
537 | gettimeofday (&timeval_end, NULL); | ||
538 | *res_time = timediff (timeval_start, timeval_end); | ||
539 | |||
534 | if (field_type == DBI_TYPE_ERROR) { | 540 | if (field_type == DBI_TYPE_ERROR) { |
535 | np_dbi_print_error (conn, "CRITICAL - failed to fetch data"); | 541 | np_dbi_print_error (conn, "CRITICAL - failed to fetch data"); |
536 | return STATE_CRITICAL; | 542 | return STATE_CRITICAL; |
@@ -542,6 +548,20 @@ do_query (dbi_conn conn, double *res_val) | |||
542 | return STATE_OK; | 548 | return STATE_OK; |
543 | } | 549 | } |
544 | 550 | ||
551 | double | ||
552 | timediff (struct timeval start, struct timeval end) | ||
553 | { | ||
554 | double diff; | ||
555 | |||
556 | while (start.tv_usec > end.tv_usec) { | ||
557 | --end.tv_sec; | ||
558 | end.tv_usec += 1000000; | ||
559 | } | ||
560 | diff = (double)(end.tv_sec - start.tv_sec) | ||
561 | + (double)(end.tv_usec - start.tv_usec) / 1000000.0; | ||
562 | return diff; | ||
563 | } | ||
564 | |||
545 | void | 565 | void |
546 | np_dbi_print_error (dbi_conn conn, char *fmt, ...) | 566 | np_dbi_print_error (dbi_conn conn, char *fmt, ...) |
547 | { | 567 | { |