summaryrefslogtreecommitdiff
path: root/cdr
diff options
context:
space:
mode:
authorMatthew Jordan <mjordan@digium.com>2014-07-16 13:55:36 +0000
committerMatthew Jordan <mjordan@digium.com>2014-07-16 13:55:36 +0000
commit03e9c598e5fe2274e0e171519c5ac52738c65b14 (patch)
treee532af6b6244b867b0f1470bc7cee7790b758a42 /cdr
parentfee789dddbe0fe5791d42773e2293b693624e961 (diff)
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
Diffstat (limited to 'cdr')
-rw-r--r--cdr/cdr_pgsql.c68
1 files changed, 63 insertions, 5 deletions
diff --git a/cdr/cdr_pgsql.c b/cdr/cdr_pgsql.c
index 8322da9b5..815bb6a7d 100644
--- a/cdr/cdr_pgsql.c
+++ b/cdr/cdr_pgsql.c
@@ -63,7 +63,17 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
static const char name[] = "pgsql";
static const char config[] = "cdr_pgsql.conf";
-static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL, *encoding = NULL, *tz = NULL;
+
+static char *pghostname;
+static char *pgdbname;
+static char *pgdbuser;
+static char *pgpassword;
+static char *pgappname;
+static char *pgdbport;
+static char *table;
+static char *encoding;
+static char *tz;
+
static int connected = 0;
static int maxsize = 512, maxsize2 = 512;
static time_t connect_time = 0;
@@ -174,6 +184,34 @@ static char *handle_cdr_pgsql_status(struct ast_cli_entry *e, int cmd, struct as
return CLI_SUCCESS;
}
+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 int pgsql_log(struct ast_cdr *cdr)
{
struct ast_tm tm;
@@ -183,7 +221,8 @@ static int pgsql_log(struct ast_cdr *cdr)
ast_mutex_lock(&pgsql_lock);
if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
- conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
+ pgsql_reconnect();
+
if (PQstatus(conn) != CONNECTION_BAD) {
connected = 1;
connect_time = time(NULL);
@@ -441,12 +480,15 @@ static int unload_module(void)
ast_cli_unregister_multiple(cdr_pgsql_status_cli, ARRAY_LEN(cdr_pgsql_status_cli));
- PQfinish(conn);
-
+ if (conn) {
+ PQfinish(conn);
+ conn = NULL;
+ }
ast_free(pghostname);
ast_free(pgdbname);
ast_free(pgdbuser);
ast_free(pgpassword);
+ ast_free(pgappname);
ast_free(pgdbport);
ast_free(table);
ast_free(encoding);
@@ -519,6 +561,18 @@ static int config_module(int reload)
return -1;
}
+ if (!(tmp = ast_variable_retrieve(cfg, "global", "appname"))) {
+ tmp = "";
+ }
+
+ ast_free(pgappname);
+ if (!(pgappname = ast_strdup(tmp))) {
+ ast_config_destroy(cfg);
+ ast_mutex_unlock(&pgsql_lock);
+ return -1;
+ }
+
+
if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
tmp = "";
@@ -590,12 +644,14 @@ static int config_module(int reload)
ast_debug(1, "got user of %s\n", pgdbuser);
ast_debug(1, "got dbname of %s\n", pgdbname);
ast_debug(1, "got password of %s\n", pgpassword);
+ ast_debug(1, "got application name of %s\n", pgappname);
ast_debug(1, "got sql table name of %s\n", table);
ast_debug(1, "got encoding of %s\n", encoding);
ast_debug(1, "got timezone of %s\n", tz);
}
- conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
+ pgsql_reconnect();
+
if (PQstatus(conn) != CONNECTION_BAD) {
char sqlcmd[768];
char *fname, *ftype, *flen, *fnotnull, *fdef;
@@ -719,6 +775,8 @@ static int config_module(int reload)
ast_log(LOG_ERROR, "Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
connected = 0;
+ PQfinish(conn);
+ conn = NULL;
}
ast_config_destroy(cfg);