From 03e9c598e5fe2274e0e171519c5ac52738c65b14 Mon Sep 17 00:00:00 2001 From: Matthew Jordan Date: Wed, 16 Jul 2014 13:55:36 +0000 Subject: cel_pgsql, cdr_pgsql, res_config_pgsql: Add PostgreSQL application_name support This patch adds support for the PostgreSQL application_name connection setting. When the appropriate PostgreSQL module's configuration is set with an application name, the name will be passed to PostgreSQL on connection and displayed in the database's pg_stat_activity view, as well as in CSV logs. This aids in managing which applications/servers are connected to a PostgreSQL database, as well as tracing the activity of those connections. Review: https://reviewboard.asterisk.org/r/3591 ASTERISK-23737 #close Reported by: Gergely Domodi patches: pgsql_application_name.patch uploaded by Gergely Domodi (License 6610) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@418755 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- res/res_config_pgsql.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'res/res_config_pgsql.c') diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c index b8ab994a7..3ee5e4e78 100644 --- a/res/res_config_pgsql.c +++ b/res/res_config_pgsql.c @@ -79,6 +79,7 @@ static char dbhost[MAX_DB_OPTION_SIZE] = ""; static char dbuser[MAX_DB_OPTION_SIZE] = ""; static char dbpass[MAX_DB_OPTION_SIZE] = ""; static char dbname[MAX_DB_OPTION_SIZE] = ""; +static char dbappname[MAX_DB_OPTION_SIZE] = ""; static char dbsock[MAX_DB_OPTION_SIZE] = ""; static int dbport = 5432; static time_t connect_time = 0; @@ -1436,6 +1437,12 @@ static int parse_config(int is_reload) dbport = atoi(s); } + if (!(s = ast_variable_retrieve(config, "general", "dbappname"))) { + dbappname[0] = '\0'; + } else { + ast_copy_string(dbappname, s, sizeof(dbappname)); + } + if (!ast_strlen_zero(dbhost)) { /* No socket needed */ } else if (!(s = ast_variable_retrieve(config, "general", "dbsock"))) { @@ -1499,18 +1506,27 @@ static int pgsql_reconnect(const char *database) /* DB password can legitimately be 0-length */ if ((!pgsqlConn) && (!ast_strlen_zero(dbhost) || !ast_strlen_zero(dbsock)) && !ast_strlen_zero(dbuser) && !ast_strlen_zero(my_database)) { - struct ast_str *connInfo = ast_str_create(128); + struct ast_str *conn_info = ast_str_create(128); + + if (!conn_info) { + ast_log(LOG_ERROR, "PostgreSQL RealTime: Failed to allocate memory for connection string.\n"); + return 0; + } - ast_str_set(&connInfo, 0, "host=%s port=%d dbname=%s user=%s", + ast_str_set(&conn_info, 0, "host=%s port=%d dbname=%s user=%s", S_OR(dbhost, dbsock), dbport, my_database, dbuser); - if (!ast_strlen_zero(dbpass)) - ast_str_append(&connInfo, 0, " password=%s", dbpass); - - ast_debug(1, "%u connInfo=%s\n", (unsigned int)ast_str_size(connInfo), ast_str_buffer(connInfo)); - pgsqlConn = PQconnectdb(ast_str_buffer(connInfo)); - ast_debug(1, "%u connInfo=%s\n", (unsigned int)ast_str_size(connInfo), ast_str_buffer(connInfo)); - ast_free(connInfo); - connInfo = NULL; + + if (!ast_strlen_zero(dbappname)) { + ast_str_append(&conn_info, 0, " application_name=%s", dbappname); + } + + if (!ast_strlen_zero(dbpass)) { + ast_str_append(&conn_info, 0, " password=%s", dbpass); + } + + pgsqlConn = PQconnectdb(ast_str_buffer(conn_info)); + ast_free(conn_info); + conn_info = NULL; ast_debug(1, "pgsqlConn=%p\n", pgsqlConn); if (pgsqlConn && PQstatus(pgsqlConn) == CONNECTION_OK) { -- cgit v1.2.3