@ -44,9 +44,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
static char * res_config_pgsql_desc = " Postgresql RealTime Configuration Driver " ;
AST_MUTEX_DEFINE_STATIC ( pgsql_lock ) ;
# define RES_CONFIG_PGSQL_CONF "res_pgsql.conf"
PGconn * pgsqlConn = NULL ;
# define MAX_DB_OPTION_SIZE 64
static char dbhost [ MAX_DB_OPTION_SIZE ] = " " ;
static char dbuser [ MAX_DB_OPTION_SIZE ] = " " ;
static char dbpass [ MAX_DB_OPTION_SIZE ] = " " ;
@ -67,7 +71,9 @@ static char cli_realtime_pgsql_status_usage[] =
static struct ast_cli_entry cli_realtime_pgsql_status = {
{ " realtime " , " pgsql " , " status " , NULL } , realtime_pgsql_status ,
" Shows connection information for the Postgresql RealTime driver " , cli_realtime_pgsql_status_usage , NULL } ;
" Shows connection information for the Postgresql RealTime driver " ,
cli_realtime_pgsql_status_usage , NULL
} ;
static struct ast_variable * realtime_pgsql ( const char * database , const char * table , va_list ap )
{
@ -89,9 +95,9 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
newparam = va_arg ( ap , const char * ) ;
newval = va_arg ( ap , const char * ) ;
if ( ! newparam | | ! newval ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on. \n " ) ;
if ( pgsqlConn )
{
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on. \n " ) ;
if ( pgsqlConn ) {
PQfinish ( pgsqlConn ) ;
pgsqlConn = NULL ;
} ;
@ -102,11 +108,16 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
If there is only 1 set , then we have our query . Otherwise , loop thru the list and concat */
op = strchr ( newparam , ' ' ) ? " " : " = " ;
snprintf ( sql , sizeof ( sql ) , " SELECT * FROM %s WHERE %s%s '%s' " , table , newparam , op , newval ) ;
snprintf ( sql , sizeof ( sql ) , " SELECT * FROM %s WHERE %s%s '%s' " , table , newparam , op ,
newval ) ;
while ( ( newparam = va_arg ( ap , const char * ) ) ) {
newval = va_arg ( ap , const char * ) ;
if ( ! strchr ( newparam , ' ' ) ) op = " = " ; else op = " " ;
snprintf ( sql + strlen ( sql ) , sizeof ( sql ) - strlen ( sql ) , " AND %s%s '%s' " , newparam , op , newval ) ;
if ( ! strchr ( newparam , ' ' ) )
op = " = " ;
else
op = " " ;
snprintf ( sql + strlen ( sql ) , sizeof ( sql ) - strlen ( sql ) , " AND %s%s '%s' " , newparam ,
op , newval ) ;
}
va_end ( ap ) ;
@ -118,19 +129,20 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
}
if ( ! ( result = PQexec ( pgsqlConn , sql ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query: %s \n " , sql ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s \n " , PQerrorMessage ( pgsqlConn ) ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s \n " ,
PQerrorMessage ( pgsqlConn ) ) ;
ast_mutex_unlock ( & pgsql_lock ) ;
return NULL ;
}
else {
} else {
ExecStatusType result_status = PQresultStatus ( result ) ;
if ( result_status ! = PGRES_COMMAND_OK
& & result_status ! = PGRES_TUPLES_OK
& & result_status ! = PGRES_NONFATAL_ERROR )
{
ast_log ( LOG_WARNING , " Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
& & result_status ! = PGRES_NONFATAL_ERROR ) {
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query: %s \n " , sql ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s (%s) \n " ,
PQresultErrorMessage ( result ) , PQresStatus ( result_status ) ) ;
@ -179,7 +191,8 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
}
free ( fieldnames ) ;
} else {
ast_log ( LOG_WARNING , " Postgresql RealTime: Could not find any rows in table %s. \n " , table ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Could not find any rows in table %s. \n " , table ) ;
}
ast_mutex_unlock ( & pgsql_lock ) ;
@ -221,9 +234,9 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
newparam = va_arg ( ap , const char * ) ;
newval = va_arg ( ap , const char * ) ;
if ( ! newparam | | ! newval ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on. \n " ) ;
if ( pgsqlConn )
{
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on. \n " ) ;
if ( pgsqlConn ) {
PQfinish ( pgsqlConn ) ;
pgsqlConn = NULL ;
} ;
@ -238,13 +251,21 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
/* Create the first part of the query using the first parameter/value pairs we just extracted
If there is only 1 set , then we have our query . Otherwise , loop thru the list and concat */
if ( ! strchr ( newparam , ' ' ) ) op = " = " ; else op = " " ;
if ( ! strchr ( newparam , ' ' ) )
op = " = " ;
else
op = " " ;
snprintf ( sql , sizeof ( sql ) , " SELECT * FROM %s WHERE %s%s '%s' " , table , newparam , op , newval ) ;
snprintf ( sql , sizeof ( sql ) , " SELECT * FROM %s WHERE %s%s '%s' " , table , newparam , op ,
newval ) ;
while ( ( newparam = va_arg ( ap , const char * ) ) ) {
newval = va_arg ( ap , const char * ) ;
if ( ! strchr ( newparam , ' ' ) ) op = " = " ; else op = " " ;
snprintf ( sql + strlen ( sql ) , sizeof ( sql ) - strlen ( sql ) , " AND %s%s '%s' " , newparam , op , newval ) ;
if ( ! strchr ( newparam , ' ' ) )
op = " = " ;
else
op = " " ;
snprintf ( sql + strlen ( sql ) , sizeof ( sql ) - strlen ( sql ) , " AND %s%s '%s' " , newparam ,
op , newval ) ;
}
if ( initfield ) {
@ -261,19 +282,20 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
}
if ( ! ( result = PQexec ( pgsqlConn , sql ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query: %s \n " , sql ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s \n " , PQerrorMessage ( pgsqlConn ) ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s \n " ,
PQerrorMessage ( pgsqlConn ) ) ;
ast_mutex_unlock ( & pgsql_lock ) ;
return NULL ;
}
else {
} else {
ExecStatusType result_status = PQresultStatus ( result ) ;
if ( result_status ! = PGRES_COMMAND_OK
& & result_status ! = PGRES_TUPLES_OK
& & result_status ! = PGRES_NONFATAL_ERROR )
{
ast_log ( LOG_WARNING , " Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
& & result_status ! = PGRES_NONFATAL_ERROR ) {
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query: %s \n " , sql ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s (%s) \n " ,
PQresultErrorMessage ( result ) , PQresStatus ( result_status ) ) ;
@ -303,8 +325,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
for ( i = 0 ; i < numFields ; i + + )
fieldnames [ i ] = PQfname ( result , i ) ;
for ( rowIndex = 0 ; rowIndex < num_rows ; rowIndex + + )
{
for ( rowIndex = 0 ; rowIndex < num_rows ; rowIndex + + ) {
var = NULL ;
cat = ast_category_new ( " " ) ;
if ( ! cat ) {
@ -328,7 +349,8 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
}
free ( fieldnames ) ;
} else {
ast_log ( LOG_WARNING , " Postgresql RealTime: Could not find any rows in table %s. \n " , table ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Could not find any rows in table %s. \n " , table ) ;
}
ast_mutex_unlock ( & pgsql_lock ) ;
@ -337,7 +359,8 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
return cfg ;
}
static int update_pgsql ( const char * database , const char * table , const char * keyfield , const char * lookup , va_list ap )
static int update_pgsql ( const char * database , const char * table , const char * keyfield ,
const char * lookup , va_list ap )
{
PGresult * result = NULL ;
int numrows = 0 ;
@ -353,9 +376,9 @@ static int update_pgsql(const char *database, const char *table, const char *key
newparam = va_arg ( ap , const char * ) ;
newval = va_arg ( ap , const char * ) ;
if ( ! newparam | | ! newval ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on. \n " ) ;
if ( pgsqlConn )
{
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Realtime retrieval requires at least 1 parameter and 1 value to search on. \n " ) ;
if ( pgsqlConn ) {
PQfinish ( pgsqlConn ) ;
pgsqlConn = NULL ;
} ;
@ -368,10 +391,12 @@ static int update_pgsql(const char *database, const char *table, const char *key
snprintf ( sql , sizeof ( sql ) , " UPDATE %s SET %s = '%s' " , table , newparam , newval ) ;
while ( ( newparam = va_arg ( ap , const char * ) ) ) {
newval = va_arg ( ap , const char * ) ;
snprintf ( sql + strlen ( sql ) , sizeof ( sql ) - strlen ( sql ) , " , %s = '%s' " , newparam , newval ) ;
snprintf ( sql + strlen ( sql ) , sizeof ( sql ) - strlen ( sql ) , " , %s = '%s' " , newparam ,
newval ) ;
}
va_end ( ap ) ;
snprintf ( sql + strlen ( sql ) , sizeof ( sql ) - strlen ( sql ) , " WHERE %s = '%s' " , keyfield , lookup ) ;
snprintf ( sql + strlen ( sql ) , sizeof ( sql ) - strlen ( sql ) , " WHERE %s = '%s' " , keyfield ,
lookup ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Update SQL: %s \n " , sql ) ;
@ -383,19 +408,20 @@ static int update_pgsql(const char *database, const char *table, const char *key
}
if ( ! ( result = PQexec ( pgsqlConn , sql ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query: %s \n " , sql ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s \n " , PQerrorMessage ( pgsqlConn ) ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s \n " ,
PQerrorMessage ( pgsqlConn ) ) ;
ast_mutex_unlock ( & pgsql_lock ) ;
return - 1 ;
}
else {
} else {
ExecStatusType result_status = PQresultStatus ( result ) ;
if ( result_status ! = PGRES_COMMAND_OK
& & result_status ! = PGRES_TUPLES_OK
& & result_status ! = PGRES_NONFATAL_ERROR )
{
ast_log ( LOG_WARNING , " Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
& & result_status ! = PGRES_NONFATAL_ERROR ) {
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query: %s \n " , sql ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s (%s) \n " ,
PQresultErrorMessage ( result ) , PQresStatus ( result_status ) ) ;
@ -407,7 +433,8 @@ static int update_pgsql(const char *database, const char *table, const char *key
numrows = atoi ( PQcmdTuples ( result ) ) ;
ast_mutex_unlock ( & pgsql_lock ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Updated %d rows on table: %s \n " , numrows , table ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Updated %d rows on table: %s \n " , numrows ,
table ) ;
/* From http://dev.pgsql.com/doc/pgsql/en/pgsql-affected-rows.html
* An integer greater than zero indicates the number of rows affected
@ -421,7 +448,8 @@ static int update_pgsql(const char *database, const char *table, const char *key
return - 1 ;
}
static struct ast_config * config_pgsql ( const char * database , const char * table , const char * file , struct ast_config * cfg )
static struct ast_config * config_pgsql ( const char * database , const char * table ,
const char * file , struct ast_config * cfg )
{
PGresult * result = NULL ;
long num_rows ;
@ -438,7 +466,9 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
return NULL ;
}
snprintf ( sql , sizeof ( sql ) , " SELECT category, var_name, var_val, cat_metric FROM %s WHERE filename='%s' and commented=0 ORDER BY filename, cat_metric desc, var_metric asc, category, var_name, var_val, id " , table , file ) ;
snprintf ( sql , sizeof ( sql ) ,
" SELECT category, var_name, var_val, cat_metric FROM %s WHERE filename='%s' and commented=0 ORDER BY filename, cat_metric desc, var_metric asc, category, var_name, var_val, id " ,
table , file ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Static SQL: %s \n " , sql ) ;
@ -450,19 +480,20 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
}
if ( ! ( result = PQexec ( pgsqlConn , sql ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query: %s \n " , sql ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s \n " , PQerrorMessage ( pgsqlConn ) ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s \n " ,
PQerrorMessage ( pgsqlConn ) ) ;
ast_mutex_unlock ( & pgsql_lock ) ;
return NULL ;
}
else {
} else {
ExecStatusType result_status = PQresultStatus ( result ) ;
if ( result_status ! = PGRES_COMMAND_OK
& & result_status ! = PGRES_TUPLES_OK
& & result_status ! = PGRES_NONFATAL_ERROR )
{
ast_log ( LOG_WARNING , " Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
& & result_status ! = PGRES_NONFATAL_ERROR ) {
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Failed to query database. Check debug for more info. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query: %s \n " , sql ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Query Failed because: %s (%s) \n " ,
PQresultErrorMessage ( result ) , PQresStatus ( result_status ) ) ;
@ -490,8 +521,7 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
for ( i = 0 ; i < numFields ; i + + )
fieldnames [ i ] = PQfname ( result , i ) ;
for ( rowIndex = 0 ; rowIndex < num_rows ; rowIndex + + )
{
for ( rowIndex = 0 ; rowIndex < num_rows ; rowIndex + + ) {
char * field_category = PQgetvalue ( result , rowIndex , 0 ) ;
char * field_var_name = PQgetvalue ( result , rowIndex , 1 ) ;
char * field_var_val = PQgetvalue ( result , rowIndex , 2 ) ;
@ -519,7 +549,8 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
ast_variable_append ( cur_cat , new_v ) ;
}
} else {
ast_log ( LOG_WARNING , " Postgresql RealTime: Could not find config '%s' in database. \n " , file ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Could not find config '%s' in database. \n " , file ) ;
}
PQclear ( result ) ;
@ -543,8 +574,10 @@ int load_module (void)
ast_mutex_lock ( & pgsql_lock ) ;
if ( ! pgsql_reconnect ( NULL ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: Couldn't establish connection. Check debug. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Cannot Connect: %s \n " , PQerrorMessage ( pgsqlConn ) ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Couldn't establish connection. Check debug. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Cannot Connect: %s \n " ,
PQerrorMessage ( pgsqlConn ) ) ;
}
ast_config_engine_register ( & pgsql_engine ) ;
@ -563,8 +596,7 @@ int unload_module (void)
/* Aquire control before doing anything to the module itself. */
ast_mutex_lock ( & pgsql_lock ) ;
if ( pgsqlConn )
{
if ( pgsqlConn ) {
PQfinish ( pgsqlConn ) ;
pgsqlConn = NULL ;
} ;
@ -587,16 +619,17 @@ int reload (void)
/* Aquire control before doing anything to the module itself. */
ast_mutex_lock ( & pgsql_lock ) ;
if ( pgsqlConn )
{
if ( pgsqlConn ) {
PQfinish ( pgsqlConn ) ;
pgsqlConn = NULL ;
} ;
parse_config ( ) ;
if ( ! pgsql_reconnect ( NULL ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: Couldn't establish connection. Check debug. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Cannot Connect: %s \n " , PQerrorMessage ( pgsqlConn ) ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Couldn't establish connection. Check debug. \n " ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Cannot Connect: %s \n " ,
PQerrorMessage ( pgsqlConn ) ) ;
}
ast_verbose ( VERBOSE_PREFIX_2 " Postgresql RealTime reloaded. \n " ) ;
@ -616,42 +649,48 @@ int parse_config (void)
if ( config ) {
if ( ! ( s = ast_variable_retrieve ( config , " general " , " dbuser " ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: No database user found, using 'asterisk' as default. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: No database user found, using 'asterisk' as default. \n " ) ;
strncpy ( dbuser , " asterisk " , sizeof ( dbuser ) - 1 ) ;
} else {
strncpy ( dbuser , s , sizeof ( dbuser ) - 1 ) ;
}
if ( ! ( s = ast_variable_retrieve ( config , " general " , " dbpass " ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: No database password found, using 'asterisk' as default. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: No database password found, using 'asterisk' as default. \n " ) ;
strncpy ( dbpass , " asterisk " , sizeof ( dbpass ) - 1 ) ;
} else {
strncpy ( dbpass , s , sizeof ( dbpass ) - 1 ) ;
}
if ( ! ( s = ast_variable_retrieve ( config , " general " , " dbhost " ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: No database host found, using localhost via socket. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: No database host found, using localhost via socket. \n " ) ;
dbhost [ 0 ] = ' \0 ' ;
} else {
strncpy ( dbhost , s , sizeof ( dbhost ) - 1 ) ;
}
if ( ! ( s = ast_variable_retrieve ( config , " general " , " dbname " ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: No database name found, using 'asterisk' as default. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: No database name found, using 'asterisk' as default. \n " ) ;
strncpy ( dbname , " asterisk " , sizeof ( dbname ) - 1 ) ;
} else {
strncpy ( dbname , s , sizeof ( dbname ) - 1 ) ;
}
if ( ! ( s = ast_variable_retrieve ( config , " general " , " dbport " ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: No database port found, using 5432 as default. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: No database port found, using 5432 as default. \n " ) ;
dbport = 5432 ;
} else {
dbport = atoi ( s ) ;
}
if ( dbhost & & ! ( s = ast_variable_retrieve ( config , " general " , " dbsock " ) ) ) {
ast_log ( LOG_WARNING , " Postgresql RealTime: No database socket found, using '/tmp/pgsql.sock' as default. \n " ) ;
ast_log ( LOG_WARNING ,
" Postgresql RealTime: No database socket found, using '/tmp/pgsql.sock' as default. \n " ) ;
strncpy ( dbsock , " /tmp/pgsql.sock " , sizeof ( dbsock ) - 1 ) ;
} else {
strncpy ( dbsock , s , sizeof ( dbsock ) - 1 ) ;
@ -708,19 +747,16 @@ static int pgsql_reconnect(const char *database)
if ( ( ! pgsqlConn ) & & ( dbhost | | dbsock ) & & dbuser & & dbpass & & my_database ) {
char * connInfo = NULL ;
unsigned int size = 100
+ strlen ( dbhost )
unsigned int size = 100 + strlen ( dbhost )
+ strlen ( dbuser )
+ strlen ( dbpass )
+ strlen ( my_database ) ;
connInfo = malloc ( size ) ;
if ( ! connInfo )
{
ast_log ( LOG_WARNING , " Postgresql RealTime: Insufficient memory to allocate Pgsql resource. \n " ) ;
if ( ! connInfo ) {
ast_log ( LOG_WARNING ,
" Postgresql RealTime: Insufficient memory to allocate Pgsql resource. \n " ) ;
return 0 ;
}
else
{
} else {
sprintf ( connInfo , " host=%s port=%d dbname=%s user=%s password=%s " ,
dbhost , dbport , my_database , dbuser , dbpass ) ;
ast_log ( LOG_DEBUG , " %u connInfo=%s \n " , size , connInfo ) ;
@ -729,14 +765,17 @@ static int pgsql_reconnect(const char *database)
free ( connInfo ) ;
connInfo = NULL ;
ast_log ( LOG_DEBUG , " pgsqlConn=%p \n " , pgsqlConn ) ;
if ( pgsqlConn )
{
ast_log ( LOG_DEBUG , " Postgresql RealTime: Successfully connected to database. \n " ) ;
if ( pgsqlConn ) {
ast_log ( LOG_DEBUG ,
" Postgresql RealTime: Successfully connected to database. \n " ) ;
connect_time = time ( NULL ) ;
return 1 ;
} else {
ast_log ( LOG_ERROR , " Postgresql RealTime: Failed to connect database server %s on %s. Check debug for more info. \n " , dbname , dbhost ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Cannot Connect: %s \n " , PQresultErrorMessage ( NULL ) ) ;
ast_log ( LOG_ERROR ,
" Postgresql RealTime: Failed to connect database server %s on %s. Check debug for more info. \n " ,
dbname , dbhost ) ;
ast_log ( LOG_DEBUG , " Postgresql RealTime: Cannot Connect: %s \n " ,
PQresultErrorMessage ( NULL ) ) ;
return 0 ;
}
}
@ -766,13 +805,19 @@ static int realtime_pgsql_status(int fd, int argc, char **argv)
}
if ( ctime > 31536000 ) {
ast_cli ( fd , " %s%s for %d years, %d days, %d hours, %d minutes, %d seconds. \n " , status , status2 , ctime / 31536000 , ( ctime % 31536000 ) / 86400 , ( ctime % 86400 ) / 3600 , ( ctime % 3600 ) / 60 , ctime % 60 ) ;
ast_cli ( fd , " %s%s for %d years, %d days, %d hours, %d minutes, %d seconds. \n " ,
status , status2 , ctime / 31536000 , ( ctime % 31536000 ) / 86400 ,
( ctime % 86400 ) / 3600 , ( ctime % 3600 ) / 60 , ctime % 60 ) ;
} else if ( ctime > 86400 ) {
ast_cli ( fd , " %s%s for %d days, %d hours, %d minutes, %d seconds. \n " , status , status2 , ctime / 86400 , ( ctime % 86400 ) / 3600 , ( ctime % 3600 ) / 60 , ctime % 60 ) ;
ast_cli ( fd , " %s%s for %d days, %d hours, %d minutes, %d seconds. \n " , status ,
status2 , ctime / 86400 , ( ctime % 86400 ) / 3600 , ( ctime % 3600 ) / 60 ,
ctime % 60 ) ;
} else if ( ctime > 3600 ) {
ast_cli ( fd , " %s%s for %d hours, %d minutes, %d seconds. \n " , status , status2 , ctime / 3600 , ( ctime % 3600 ) / 60 , ctime % 60 ) ;
ast_cli ( fd , " %s%s for %d hours, %d minutes, %d seconds. \n " , status , status2 ,
ctime / 3600 , ( ctime % 3600 ) / 60 , ctime % 60 ) ;
} else if ( ctime > 60 ) {
ast_cli ( fd , " %s%s for %d minutes, %d seconds. \n " , status , status2 , ctime / 60 , ctime % 60 ) ;
ast_cli ( fd , " %s%s for %d minutes, %d seconds. \n " , status , status2 , ctime / 60 ,
ctime % 60 ) ;
} else {
ast_cli ( fd , " %s%s for %d seconds. \n " , status , status2 , ctime ) ;
}