connection logging dtrace probe

Started by Robert Treatalmost 17 years ago2 messages
#1Robert Treat
xzilla@users.sourceforge.net
1 attachment(s)

I whipped up a quick dtrace probe for one of our servers to monitor connection
attempts. My goal was to monitor for any connection attempts from a specific
role within the database. Unfortunatly you can't set logging of connections
for a specific user, and logging all connections on that machine would be
quite the logfile bloater... enter dtrace. With the probe, I can do something
like this:

-bash-3.00$ /opt/csw/bin/sudo dtrace -n 'postgresql*:::connection
{printf("connection attempt: %s@%s\n",copyinstr(arg0),copyinstr(arg1)) }' |
grep robert
dtrace: description 'postgresql*:::connection ' matched 5 probes
2 18984 ServerLoop:connection connection attempt: robert@robert
2 16222 ServerLoop:connection connection attempt: robert@robert
1 16876 ServerLoop:connection connection attempt: robert@pagila

which can be piped to logfile or whatever. I'm attaching a patch against 8.4
as an idea of what I've implemented (actual implementation was against a
custom build) but should be close to working (don't have a working pg repo on
any solaris machines atm). Any feedback appreciated (mostly wondering about
probe name or location). TIA

--
Robert Treat
Conjecture: http://www.xzilla.net
Consulting: http://www.omniti.com

Attachments:

connection_probe.difftext/x-diff; charset="iso 8859-15"; name=connection_probe.diffDownload
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 3380b80..ddf23d8 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -118,6 +118,7 @@
 #include "utils/datetime.h"
 #include "utils/memutils.h"
 #include "utils/ps_status.h"
+#include "pg_trace.h"
 
 #ifdef EXEC_BACKEND
 #include "storage/spin.h"
@@ -3142,6 +3143,8 @@ BackendInitialize(Port *port)
 		elog(FATAL, "could not disable timer for authorization timeout");
 	PG_SETMASK(&BlockSig);
 
+	TRACE_POSTGRESQL_CONNECTION_ATTEMPT(port->user_name, port->database_name);
+
 	if (Log_connections)
 		ereport(LOG,
 				(errmsg("connection authorized: user=%s database=%s",
diff --git a/src/backend/utils/probes.d b/src/backend/utils/probes.d
index f68a7d2..d8b418a 100644
--- a/src/backend/utils/probes.d
+++ b/src/backend/utils/probes.d
@@ -91,4 +91,6 @@ provider postgresql {
 	probe xlog__switch();
 	probe wal__buffer__write__dirty__start();
 	probe wal__buffer__write__dirty__done();
+
+	probe connection__attempt(char *, char *);
 };
#2ITAGAKI Takahiro
itagaki.takahiro@oss.ntt.co.jp
In reply to: Robert Treat (#1)
Re: connection logging dtrace probe

Hi,

Robert Treat <xzilla@users.sourceforge.net> wrote:

I whipped up a quick dtrace probe for one of our servers to monitor connection
attempts. My goal was to monitor for any connection attempts from a specific
role within the database.

How about adding probes not only for connection attempts but also for
connection performance? The name would be CONNECTION_[START|DONE] like
other existing probes.

I received a report from my client that Postgres didn't return responce
of heatbeat connection requests for *1 minute*, but there are few methods
to dig it now...

Regards,
---
ITAGAKI Takahiro
NTT Open Source Software Center