From: Andrew Dunstan Date: Tue, 8 Apr 2014 15:28:43 +0000 (-0400) Subject: Add the client host IP address and port to application_name. X-Git-Tag: pgbouncer_1_6_rc1~14^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ac9842870517f8c4e4a915fe6b3ccadbcdbdea03;p=pgbouncer Add the client host IP address and port to application_name. This is enabled by a config parameter which defaults to 'on' If the client subsequently overwrites the application_name parameter during the session then this won't be sticky, but it does let you see (and log) where queries are actually coming from instead of just seeing pgbouncer as the source address and port. --- diff --git a/doc/config.txt b/doc/config.txt index 49e8480..b3af814 100644 --- a/doc/config.txt +++ b/doc/config.txt @@ -227,6 +227,15 @@ exclusively use Extended Query protocol will stay working. Default: 0 +==== application_name_add_host ==== + +Add the client host address and port to the application name setting set on connection start. +This helps in identifying the source of bad queries etc. The setting will be overwritten +without any detection if the application does SET APPLICATION_NAME after connecting. +Note that this is on by default. + +Default: 1 + === Log settings === ==== syslog ==== diff --git a/include/bouncer.h b/include/bouncer.h index efc608a..14e6087 100644 --- a/include/bouncer.h +++ b/include/bouncer.h @@ -427,6 +427,7 @@ extern int cf_tcp_defer_accept; extern int cf_log_connections; extern int cf_log_disconnections; extern int cf_log_pooler_errors; +extern int cf_application_name_add_host; extern const struct CfLookup pool_mode_map[]; diff --git a/src/client.c b/src/client.c index c8a2574..0fa8ad5 100644 --- a/src/client.c +++ b/src/client.c @@ -328,6 +328,7 @@ static bool decide_startup_pool(PgSocket *client, PktHdr *pkt) const char *username = NULL, *dbname = NULL; const char *key, *val; bool ok; + bool appname_found = false; while (1) { ok = mbuf_get_string(&pkt->data, &key); @@ -343,6 +344,17 @@ static bool decide_startup_pool(PgSocket *client, PktHdr *pkt) } else if (strcmp(key, "user") == 0) { slog_debug(client, "got var: %s=%s", key, val); username = val; + } else if (cf_application_name_add_host && + strcmp(key, "application_name") == 0) { + int port = pga_port(&client->remote_addr); + static char ipbuf[PGADDR_BUF], buf[1024]; + const char *ip; + + ip = pga_ntop(&client->remote_addr, ipbuf, sizeof(ipbuf)); + snprintf(buf, sizeof(buf), "%s (%s:%d)", val, ip, port); + slog_debug(client,"using application name %s",buf); + varcache_set(&client->vars, key, buf); + appname_found = true; } else if (varcache_set(&client->vars, key, val)) { slog_debug(client, "got var: %s=%s", key, val); } else if (strlist_contains(cf_ignore_startup_params, key)) { @@ -362,6 +374,18 @@ static bool decide_startup_pool(PgSocket *client, PktHdr *pkt) if (!dbname || !dbname[0]) dbname = username; + /* default application_name to "client at :" */ + if (!appname_found && cf_application_name_add_host) { + int port = pga_port(&client->remote_addr); + static char ipbuf[PGADDR_BUF], buf[1024]; + const char *ip; + + ip = pga_ntop(&client->remote_addr, ipbuf, sizeof(ipbuf)); + snprintf(buf, sizeof(buf), "client at %s:%d", ip, port); + slog_debug(client,"using default application name %s",buf); + varcache_set(&client->vars, key, buf); + } + /* check if limit allows, don't limit admin db nb: new incoming conn will be attached to PgSocket, thus get_active_client_count() counts it */ diff --git a/src/main.c b/src/main.c index 1cd5f3e..691d4af 100644 --- a/src/main.c +++ b/src/main.c @@ -134,6 +134,7 @@ int cf_stats_period; int cf_log_connections; int cf_log_disconnections; int cf_log_pooler_errors; +int cf_application_name_add_host; /* * config file description @@ -231,6 +232,7 @@ CF_ABS("stats_period", CF_INT, cf_stats_period, 0, "60"), CF_ABS("log_connections", CF_INT, cf_log_connections, 0, "1"), CF_ABS("log_disconnections", CF_INT, cf_log_disconnections, 0, "1"), CF_ABS("log_pooler_errors", CF_INT, cf_log_pooler_errors, 0, "1"), +CF_ABS("application_name_add_host", CF_INT, cf_application_name_add_host, 0, "1"), {NULL} };