--- NEW FILE: cdr_pgsql.c ---
/*
* Asterisk -- A telephony toolkit for Linux.
*
* PostgreSQL CDR logger
*
* Matthew D. Hardeman <mhardemn@papersoft.com>
* Adapted from the MySQL CDR logger originally by James Sharp
*
* Modified September 2003
* Matthew D. Hardeman <mhardemn@papersoft.com>
*
* This program is free software, distributed under the terms of
* the GNU General Public License.
*
*/
gettimeofday(&tv,NULL);
t = tv.tv_sec;
localtime_r(&t,&tm);
strftime(timestr,128,DATE_FORMAT,&tm);
if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
if (PQstatus(conn) != CONNECTION_BAD) {
connected = 1;
} else {
ast_log(LOG_ERROR, "cdr_pgsql: cannot connect to database server %s. Call will not be logged\n", pghostname);
}
} else {
/* Test to be sure we're still connected... */
/* If we're connected, and connection is working, good. */
/* Otherwise, attempt reconnect. If it fails... sorry... */
if (PQstatus(conn) == CONNECTION_OK) {
connected = 1;
} else {
ast_log(LOG_ERROR, "cdr_pgsql: connection was lost... reattempting connection.");
PQreset(conn);
if (PQstatus(conn) == CONNECTION_OK) {
ast_log(LOG_ERROR, "cdr_pgsql: connection reestablished.");
connected = 1;
} else {
ast_log(LOG_ERROR, "cdr_pgsql: unable to reconnect to database.");
connected = 0;
}
}
tmp = ast_variable_retrieve(cfg,"global","port");
if (tmp) {
pgdbport = malloc(strlen(tmp) + 1);
if (pgdbport != NULL) {
dbport_alloc = 1;
strcpy(pgdbport,tmp);
} else {
ast_log(LOG_ERROR,"Out of memory error.\n");
return -1;
}
} else {
ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default.\n");
pgdbport = "5432";
}
ast_destroy(cfg);
ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);
ast_log(LOG_DEBUG,"cdr_pgsql: got port of %s\n",pgdbport);
if (pgdbsock)
ast_log(LOG_DEBUG,"cdr_pgsql: got sock file of %s\n",pgdbsock);
ast_log(LOG_DEBUG,"cdr_pgsql: got user of %s\n",pgdbuser);
ast_log(LOG_DEBUG,"cdr_pgsql: got dbname of %s\n",pgdbname);
ast_log(LOG_DEBUG,"cdr_pgsql: got password of %s\n",pgpassword);
conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
if (PQstatus(conn) != CONNECTION_BAD) {
ast_log(LOG_DEBUG,"Successfully connected to PostgreSQL database.\n");
connected = 1;
} else {
ast_log(LOG_ERROR, "cdr_pgsql: cannot connect to database server %s. Call will not be logged\n", pghostname);
connected = 0;
}
res = ast_cdr_register(name, desc, pgsql_log);
if (res) {
ast_log(LOG_ERROR, "Unable to register PGSQL CDR handling\n");
}
return res;
}
int load_module(void)
{
return my_load_module();
}
int unload_module(void)
{
return my_unload_module();
}
int reload(void)
{
my_unload_module();
return my_load_module();
}
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum