[nagiosplug] check_pgsql: Determine connection time in ...

"Nagios Plugin Development" <[email protected]> Sat, 17 Aug 2013 20:50:51 +0000
Newsgroups gmane.network.nagios.plugins.cvs
Message-ID <[email protected]>
 Module: nagiosplug
 Branch: master
 Commit: c0bef3da51bd8b6be658f619a4659c95ad83d4bd
 Author: Sebastian Harl <[email protected]>
   Date: Fri Apr  8 11:17:33 2011 +0200
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=c0bef3d

check_pgsql: Determine connection time in µs-resolution.

… thus, treat "elapsed time" and the thresholds as floating point values.

---

 plugins/check_pgsql.c |   22 +++++++++++++++-------
 1 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c
index 54d2d58..d3116b0 100644
--- a/plugins/check_pgsql.c
+++ b/plugins/check_pgsql.c
@@ -147,7 +147,9 @@ main (int argc, char **argv)
 	PGconn *conn;
 	char *conninfo = NULL;
 
-	int elapsed_time;
+	struct timeval start_timeval;
+	struct timeval end_timeval;
+	double elapsed_time;
 	int status = STATE_UNKNOWN;
 	int query_status = STATE_UNKNOWN;
 
@@ -199,13 +201,19 @@ main (int argc, char **argv)
 		asprintf (&conninfo, "%s password = '%s'", conninfo, pgpasswd);
 
 	/* make a connection to the database */
-	time (&start_time);
+	gettimeofday (&start_timeval, NULL);
 	conn = PQconnectdb (conninfo);
-	time (&end_time);
-	elapsed_time = (int) (end_time - start_time);
+	gettimeofday (&end_timeval, NULL);
+
+	while (start_timeval.tv_usec > end_timeval.tv_usec) {
+		--end_timeval.tv_sec;
+		end_timeval.tv_usec += 1000000;
+	}
+	elapsed_time = (double)(end_timeval.tv_sec - start_timeval.tv_sec)
+		+ (double)(end_timeval.tv_usec - start_timeval.tv_usec) / 1000000.0;
 
 	if (verbose)
-		printf("Time elapsed: %d\n", elapsed_time);
+		printf("Time elapsed: %f\n", elapsed_time);
 
 	/* check to see that the backend connection was successfully made */
 	if (verbose)
@@ -239,10 +247,10 @@ main (int argc, char **argv)
 				PQprotocolVersion (conn), PQbackendPID (conn));
 	}
 
-	printf (_(" %s - database %s (%d sec.)|%s\n"),
+	printf (_(" %s - database %s (%f sec.)|%s\n"),
 	        state_text(status), dbName, elapsed_time,
 	        fperfdata("time", elapsed_time, "s",
-	                 (int)twarn, twarn, (int)tcrit, tcrit, TRUE, 0, FALSE,0));
+	                 !!(twarn > 0.0), twarn, !!(tcrit > 0.0), tcrit, TRUE, 0, FALSE,0));
 
 	if (pgquery)
 		query_status = do_query (conn, pgquery);


------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead. 
Download for free and get started troubleshooting in minutes. 
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
Nagiosplug-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nagiosplug-checkins