@ -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 ;
}