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
This commit is contained in:
Matthew Jordan
2014-07-16 13:55:36 +00:00
parent fee789dddb
commit 03e9c598e5
7 changed files with 179 additions and 26 deletions

View File

@@ -4,8 +4,8 @@
* Copyright (C) 2008
*
* Steve Murphy - adapted to CEL, from:
* Matthew D. Hardeman <mhardemn@papersoft.com>
* Adapted from the MySQL CDR logger originally by James Sharp
* Matthew D. Hardeman <mhardemn@papersoft.com>
* Adapted from the MySQL CDR logger originally by James Sharp
*
* Modified April, 2007; Dec, 2008
* Steve Murphy <murf@digium.com>
@@ -26,8 +26,8 @@
/*! \file
*
* \brief PostgreSQL CEL logger
*
* \brief PostgreSQL CEL logger
*
* \author Steve Murphy <murf@digium.com>
* PostgreSQL http://www.postgresql.org/
*
@@ -61,7 +61,15 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#define PGSQL_BACKEND_NAME "CEL PGSQL backend"
static char *config = "cel_pgsql.conf";
static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
static char *pghostname;
static char *pgdbname;
static char *pgdbuser;
static char *pgpassword;
static char *pgappname;
static char *pgdbport;
static char *table;
static int connected = 0;
static int maxsize = 512, maxsize2 = 512;
@@ -114,6 +122,35 @@ static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
} \
} while (0)
static void pgsql_reconnect(void)
{
struct ast_str *conn_info = ast_str_create(128);
if (!conn_info) {
ast_log(LOG_ERROR, "Failed to allocate memory for connection string.\n");
return;
}
if (conn) {
PQfinish(conn);
conn = NULL;
}
ast_str_set(&conn_info, 0, "host=%s port=%s dbname=%s user=%s",
pghostname, pgdbport, pgdbname, pgdbuser);
if (!ast_strlen_zero(pgappname)) {
ast_str_append(&conn_info, 0, " application_name=%s", pgappname);
}
if (!ast_strlen_zero(pgpassword)) {
ast_str_append(&conn_info, 0, " password=%s", pgpassword);
}
conn = PQconnectdb(ast_str_buffer(conn_info));
ast_free(conn_info);
}
static void pgsql_log(struct ast_event *event)
{
struct ast_tm tm;
@@ -133,7 +170,7 @@ static void pgsql_log(struct ast_event *event)
ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
pgsql_reconnect();
if (PQstatus(conn) != CONNECTION_BAD) {
connected = 1;
} else {
@@ -368,6 +405,10 @@ static int my_unload_module(void)
ast_free(pgpassword);
pgpassword = NULL;
}
if (pgappname) {
ast_free(pgappname);
pgappname = NULL;
}
if (pgdbport) {
ast_free(pgdbport);
pgdbport = NULL;
@@ -440,6 +481,17 @@ static int process_my_load_module(struct ast_config *cfg)
ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
return AST_MODULE_LOAD_DECLINE;
}
if (!(tmp = ast_variable_retrieve(cfg, "global", "appname"))) {
tmp = "";
}
if (pgappname) {
ast_free(pgappname);
}
if (!(pgappname = ast_strdup(tmp))) {
ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying appname info\n");
return AST_MODULE_LOAD_DECLINE;
}
if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
tmp = "5432";
@@ -478,7 +530,7 @@ static int process_my_load_module(struct ast_config *cfg)
cel_show_user_def ? "Yes" : "No");
}
conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
pgsql_reconnect();
if (PQstatus(conn) != CONNECTION_BAD) {
char sqlcmd[512];
char *fname, *ftype, *flen, *fnotnull, *fdef;
@@ -540,6 +592,8 @@ static int process_my_load_module(struct ast_config *cfg)
ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
connected = 0;
PQfinish(conn);
conn = NULL;
}
return AST_MODULE_LOAD_SUCCESS;
}