summaryrefslogtreecommitdiffstats
path: root/plugins/check_dbi.c
diff options
context:
space:
mode:
authorSebastian Harl <sh@teamix.net>2011-04-07 16:09:33 (GMT)
committerSebastian Harl <sh@teamix.net>2012-06-06 12:10:55 (GMT)
commit0e02edec196a8f3cf866279c5609138db14ec931 (patch)
treef25b5031c2a901c6437a7a490149ddc657c9f413 /plugins/check_dbi.c
parente7dfcd4429b54bd98bac724665d7b5e6e30a7532 (diff)
downloadmonitoring-plugins-0e02edec196a8f3cf866279c5609138db14ec931.tar.gz
check_dbi: Check and report the time used by the query.
Diffstat (limited to 'plugins/check_dbi.c')
-rw-r--r--plugins/check_dbi.c54
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);
69void print_usage (void); 69void print_usage (void);
70void print_help (void); 70void print_help (void);
71 71
72double timediff (struct timeval, struct timeval);
73
72void np_dbi_print_error (dbi_conn, char *, ...); 74void np_dbi_print_error (dbi_conn, char *, ...);
73 75
74int do_query (dbi_conn, double *); 76int do_query (dbi_conn, double *, double *);
75 77
76int 78int
77main (int argc, char **argv) 79main (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
488int 487int
489do_query (dbi_conn conn, double *res_val) 488do_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
551double
552timediff (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
545void 565void
546np_dbi_print_error (dbi_conn conn, char *fmt, ...) 566np_dbi_print_error (dbi_conn conn, char *fmt, ...)
547{ 567{