Fri Aug 24 02:23:34 2007

Asterisk developer's documentation


cdr_pgsql.c File Reference

PostgreSQL CDR logger. More...

#include "asterisk.h"
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <libpq-fe.h>
#include "asterisk/config.h"
#include "asterisk/options.h"
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"

Include dependency graph for cdr_pgsql.c:

Go to the source code of this file.

Defines

#define DATE_FORMAT   "%Y-%m-%d %T"

Functions

 AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT,"PostgreSQL CDR Backend",.load=load_module,.unload=unload_module,.reload=reload,)
 AST_MUTEX_DEFINE_STATIC (pgsql_lock)
static int load_module (void)
static int my_load_module (void)
static int my_unload_module (void)
static int pgsql_log (struct ast_cdr *cdr)
static int process_my_load_module (struct ast_config *cfg)
static int reload (void)
static int unload_module (void)

Variables

static char * config = "cdr_pgsql.conf"
static PGconn * conn = NULL
static int connected = 0
static char * name = "pgsql"
static char * pgdbname = NULL
static char * pgdbport = NULL
static char * pgdbuser = NULL
static char * pghostname = NULL
static char * pgpassword = NULL
static char * table = NULL


Detailed Description

PostgreSQL CDR logger.

Author:
Matthew D. Hardeman <mhardemn@papersoft.com>
See also

Definition in file cdr_pgsql.c.


Define Documentation

#define DATE_FORMAT   "%Y-%m-%d %T"

Definition at line 60 of file cdr_pgsql.c.


Function Documentation

AST_MODULE_INFO ( ASTERISK_GPL_KEY  ,
AST_MODFLAG_DEFAULT  ,
"PostgreSQL CDR Backend"  ,
load = load_module,
unload = unload_module,
reload = reload 
)

AST_MUTEX_DEFINE_STATIC ( pgsql_lock   ) 

static int load_module ( void   )  [static]

Definition at line 308 of file cdr_pgsql.c.

References my_load_module().

00309 {
00310    return my_load_module();
00311 }

static int my_load_module ( void   )  [static]

Definition at line 292 of file cdr_pgsql.c.

References ast_config_destroy(), ast_config_load(), ast_log(), AST_MODULE_LOAD_DECLINE, LOG_WARNING, and process_my_load_module().

00293 {
00294    struct ast_config *cfg;
00295    int res;
00296 
00297    if (!(cfg = ast_config_load(config))) {
00298       ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
00299       return AST_MODULE_LOAD_DECLINE;
00300    }
00301 
00302    res = process_my_load_module(cfg);
00303    ast_config_destroy(cfg);
00304 
00305    return res;
00306 }

static int my_unload_module ( void   )  [static]

Definition at line 189 of file cdr_pgsql.c.

References ast_cdr_unregister(), and free.

00190 { 
00191    PQfinish(conn);
00192    if (pghostname)
00193       free(pghostname);
00194    if (pgdbname)
00195       free(pgdbname);
00196    if (pgdbuser)
00197       free(pgdbuser);
00198    if (pgpassword)
00199       free(pgpassword);
00200    if (pgdbport)
00201       free(pgdbport);
00202    if (table)
00203       free(table);
00204    ast_cdr_unregister(name);
00205    return 0;
00206 }

static int pgsql_log ( struct ast_cdr cdr  )  [static]

Definition at line 71 of file cdr_pgsql.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr_disp2str(), ast_localtime(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_cdr::billsec, ast_cdr::channel, ast_cdr::clid, DATE_FORMAT, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, ast_cdr::duration, ast_cdr::lastapp, ast_cdr::lastdata, LOG_DEBUG, LOG_ERROR, option_debug, result, ast_cdr::src, ast_cdr::start, t, ast_cdr::uniqueid, ast_cdr::userfield, and userfield.

Referenced by load_module(), and process_my_load_module().

00072 {
00073    struct tm tm;
00074    time_t t = cdr->start.tv_sec;
00075    static char sqlcmd[4096] = "", timestr[128];
00076    char *pgerror;
00077    PGresult *result;
00078 
00079    ast_mutex_lock(&pgsql_lock);
00080 
00081    ast_localtime(&t, &tm, NULL);
00082    strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
00083 
00084    if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
00085       conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
00086       if (PQstatus(conn) != CONNECTION_BAD) {
00087          connected = 1;
00088       } else {
00089          pgerror = PQerrorMessage(conn);
00090          ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
00091          ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
00092          PQfinish(conn);
00093          conn = NULL;
00094       }
00095    }
00096 
00097    if (connected) {
00098       char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
00099       char *uniqueid=NULL, *userfield=NULL;
00100 
00101       /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
00102       if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
00103          PQescapeString(clid, cdr->clid, strlen(cdr->clid));
00104       if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
00105          PQescapeString(dcontext, cdr->dcontext, strlen(cdr->dcontext));
00106       if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
00107          PQescapeString(channel, cdr->channel, strlen(cdr->channel));
00108       if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
00109          PQescapeString(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
00110       if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
00111          PQescapeString(lastapp, cdr->lastapp, strlen(cdr->lastapp));
00112       if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
00113          PQescapeString(lastdata, cdr->lastdata, strlen(cdr->lastdata));
00114       if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
00115          PQescapeString(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
00116       if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)
00117          PQescapeString(userfield, cdr->userfield, strlen(cdr->userfield));
00118 
00119       /* Check for all alloca failures above at once */
00120       if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield)) {
00121          ast_log(LOG_ERROR, "cdr_pgsql:  Out of memory error (insert fails)\n");
00122          ast_mutex_unlock(&pgsql_lock);
00123          return -1;
00124       }
00125 
00126       if (option_debug > 1)
00127          ast_log(LOG_DEBUG, "cdr_pgsql: inserting a CDR record.\n");
00128 
00129       /* XXX: need to check string length, and, may be, realloc buffer */
00130       snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,"
00131              "lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES"
00132              " ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%ld,%ld,'%s',%ld,'%s','%s','%s')",
00133              table,timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,
00134              cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
00135       
00136       if (option_debug > 2)
00137          ast_log(LOG_DEBUG, "cdr_pgsql: SQL command executed:  %s\n",sqlcmd);
00138       
00139       /* Test to be sure we're still connected... */
00140       /* If we're connected, and connection is working, good. */
00141       /* Otherwise, attempt reconnect.  If it fails... sorry... */
00142       if (PQstatus(conn) == CONNECTION_OK) {
00143          connected = 1;
00144       } else {
00145          ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
00146          PQreset(conn);
00147          if (PQstatus(conn) == CONNECTION_OK) {
00148             ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
00149             connected = 1;
00150          } else {
00151             pgerror = PQerrorMessage(conn);
00152             ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
00153             ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
00154             PQfinish(conn);
00155             conn = NULL;
00156             connected = 0;
00157             ast_mutex_unlock(&pgsql_lock);
00158             return -1;
00159          }
00160       }
00161       result = PQexec(conn, sqlcmd);
00162       if (PQresultStatus(result) != PGRES_COMMAND_OK) {
00163          pgerror = PQresultErrorMessage(result);
00164          ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
00165          ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
00166          ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
00167          PQreset(conn);
00168          if (PQstatus(conn) == CONNECTION_OK) {
00169             ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
00170             connected = 1;
00171             PQclear(result);
00172             result = PQexec(conn, sqlcmd);
00173             if (PQresultStatus(result) != PGRES_COMMAND_OK) {
00174                pgerror = PQresultErrorMessage(result);
00175                ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
00176                ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
00177             }
00178          }
00179          ast_mutex_unlock(&pgsql_lock);
00180          PQclear(result);
00181          return -1;
00182       }
00183       PQclear(result);
00184    }
00185    ast_mutex_unlock(&pgsql_lock);
00186    return 0;
00187 }

static int process_my_load_module ( struct ast_config cfg  )  [static]

Definition at line 208 of file cdr_pgsql.c.

References ast_cdr_register(), ast_log(), ast_strdup, ast_strlen_zero(), ast_variable_browse(), ast_variable_retrieve(), LOG_DEBUG, LOG_ERROR, LOG_WARNING, option_debug, pgsql_log(), and var.

Referenced by my_load_module().

00209 {
00210    struct ast_variable *var;
00211         char *pgerror;
00212    const char *tmp;
00213 
00214    if (!(var = ast_variable_browse(cfg, "global")))
00215       return 0;
00216 
00217    if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
00218       ast_log(LOG_WARNING,"PostgreSQL server hostname not specified.  Assuming unix socket connection\n");
00219       tmp = "";   /* connect via UNIX-socket by default */
00220    }
00221    
00222    if (!(pghostname = ast_strdup(tmp)))
00223       return -1;
00224 
00225    if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
00226       ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
00227       tmp = "asterisk";
00228    }
00229 
00230    if (!(pgdbname = ast_strdup(tmp)))
00231       return -1;
00232 
00233    if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
00234       ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming asterisk\n");
00235       tmp = "asterisk";
00236    }
00237 
00238    if (!(pgdbuser = ast_strdup(tmp)))
00239       return -1;
00240 
00241    if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
00242       ast_log(LOG_WARNING,"PostgreSQL database password not specified.  Assuming blank\n");
00243       tmp = "";
00244    }
00245 
00246    if (!(pgpassword = ast_strdup(tmp)))
00247       return -1;
00248 
00249    if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
00250       ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
00251       tmp = "5432";
00252    }
00253 
00254    if (!(pgdbport = ast_strdup(tmp)))
00255       return -1;
00256 
00257    if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
00258       ast_log(LOG_WARNING,"CDR table not specified.  Assuming cdr\n");
00259       tmp = "cdr";
00260    }
00261 
00262    if (!(table = ast_strdup(tmp)))
00263       return -1;
00264 
00265    if (option_debug) {
00266          if (ast_strlen_zero(pghostname))
00267          ast_log(LOG_DEBUG, "cdr_pgsql: using default unix socket\n");
00268       else
00269          ast_log(LOG_DEBUG, "cdr_pgsql: got hostname of %s\n", pghostname);
00270       ast_log(LOG_DEBUG, "cdr_pgsql: got port of %s\n", pgdbport);
00271       ast_log(LOG_DEBUG, "cdr_pgsql: got user of %s\n", pgdbuser);
00272       ast_log(LOG_DEBUG, "cdr_pgsql: got dbname of %s\n", pgdbname);
00273       ast_log(LOG_DEBUG, "cdr_pgsql: got password of %s\n", pgpassword);
00274       ast_log(LOG_DEBUG, "cdr_pgsql: got sql table name of %s\n", table);
00275    }
00276    
00277    conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
00278    if (PQstatus(conn) != CONNECTION_BAD) {
00279       if (option_debug)
00280          ast_log(LOG_DEBUG, "Successfully connected to PostgreSQL database.\n");
00281       connected = 1;
00282    } else {
00283                 pgerror = PQerrorMessage(conn);
00284       ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
00285                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
00286       connected = 0;
00287    }
00288 
00289    return ast_cdr_register(name, ast_module_info->description, pgsql_log);
00290 }

static int reload ( void   )  [static]

Definition at line 318 of file cdr_pgsql.c.

References ast_mutex_lock(), ast_mutex_unlock(), my_load_module(), and my_unload_module().

00319 {
00320    int res;
00321    ast_mutex_lock(&pgsql_lock);
00322    my_unload_module();
00323    res = my_load_module();
00324    ast_mutex_unlock(&pgsql_lock);
00325    return res;
00326 }

static int unload_module ( void   )  [static]

Definition at line 313 of file cdr_pgsql.c.

References my_unload_module().

00314 {
00315    return my_unload_module();
00316 }


Variable Documentation

char* config = "cdr_pgsql.conf" [static]

Definition at line 63 of file cdr_pgsql.c.

PGconn* conn = NULL [static]

Definition at line 69 of file cdr_pgsql.c.

Referenced by cdr_pgsql_connect(), pgsql_log(), pgsql_test_connection(), reload(), and unload_module().

int connected = 0 [static]

Definition at line 65 of file cdr_pgsql.c.

char* name = "pgsql" [static]

Definition at line 62 of file cdr_pgsql.c.

char * pgdbname = NULL [static]

Definition at line 64 of file cdr_pgsql.c.

char * pgdbport = NULL [static]

Definition at line 64 of file cdr_pgsql.c.

char * pgdbuser = NULL [static]

Definition at line 64 of file cdr_pgsql.c.

char* pghostname = NULL [static]

Definition at line 64 of file cdr_pgsql.c.

char * pgpassword = NULL [static]

Definition at line 64 of file cdr_pgsql.c.

char * table = NULL [static]

Definition at line 64 of file cdr_pgsql.c.


Generated on Fri Aug 24 02:23:34 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1