2 * Asterisk -- A telephony toolkit for Linux.
4 * PostgreSQL CDR logger
6 * Matthew D. Hardeman <mhardemn@papersoft.com>
7 * Adapted from the MySQL CDR logger originally by James Sharp
9 * Modified September 2003
10 * Matthew D. Hardeman <mhardemn@papersoft.com>
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License.
17 #include <sys/types.h>
18 #include <asterisk/config.h>
19 #include <asterisk/options.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/cdr.h>
22 #include <asterisk/module.h>
23 #include <asterisk/logger.h>
24 #include "../asterisk.h"
35 #define DATE_FORMAT "%Y-%m-%d %T"
37 static char *desc = "PostgreSQL CDR Backend";
38 static char *name = "pgsql";
39 static char *config = "cdr_pgsql.conf";
40 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbsock = NULL, *pgdbport = NULL;
41 static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbport_alloc = 0;
42 static int connected = 0;
44 static ast_mutex_t pgsql_lock = AST_MUTEX_INITIALIZER;
49 static int pgsql_log(struct ast_cdr *cdr)
53 char sqlcmd[2048], timestr[128];
57 ast_mutex_lock(&pgsql_lock);
59 memset(sqlcmd,0,2048);
61 gettimeofday(&tv,NULL);
64 strftime(timestr,128,DATE_FORMAT,&tm);
66 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
67 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
68 if (PQstatus(conn) != CONNECTION_BAD) {
71 pgerror = PQerrorMessage(conn);
72 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
73 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
78 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
79 char *uniqueid=NULL, *userfield=NULL;
81 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
82 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
83 PQescapeString(clid, cdr->clid, strlen(cdr->clid));
84 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
85 PQescapeString(dcontext, cdr->dcontext, strlen(cdr->dcontext));
86 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
87 PQescapeString(channel, cdr->channel, strlen(cdr->channel));
88 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
89 PQescapeString(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
90 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
91 PQescapeString(lastapp, cdr->lastapp, strlen(cdr->lastapp));
92 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
93 PQescapeString(lastdata, cdr->lastdata, strlen(cdr->lastdata));
94 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
95 PQescapeString(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
96 if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) !+ NULL)
97 PQescapeString(userfield, cdr->userfield, strlen(cdr->userfield));
99 /* Check for all alloca failures above at once */
100 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield)) {
101 ast_log(LOG_ERROR, "cdr_pgsql: Out of memory error (insert fails)\n");
102 ast_mutex_unlock(&pgsql_lock);
106 ast_log(LOG_DEBUG,"cdr_pgsql: inserting a CDR record.\n");
108 sprintf(sqlcmd,"INSERT INTO cdr (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')",timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
109 ast_log(LOG_DEBUG,"cdr_pgsql: SQL command executed: %s\n",sqlcmd);
111 /* Test to be sure we're still connected... */
112 /* If we're connected, and connection is working, good. */
113 /* Otherwise, attempt reconnect. If it fails... sorry... */
114 if (PQstatus(conn) == CONNECTION_OK) {
117 ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
119 if (PQstatus(conn) == CONNECTION_OK) {
120 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
123 pgerror = PQerrorMessage(conn);
124 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
125 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
130 result = PQexec(conn, sqlcmd);
131 if ( PQresultStatus(result) != PGRES_COMMAND_OK) {
132 pgerror = PQresultErrorMessage(result);
133 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
134 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
135 ast_mutex_unlock(&pgsql_lock);
139 ast_mutex_unlock(&pgsql_lock);
143 char *description(void)
148 static int my_unload_module(void)
152 if (pghostname && hostname_alloc) {
157 if (pgdbname && dbname_alloc) {
162 if (pgdbuser && dbuser_alloc) {
167 if (pgdbsock && dbsock_alloc) {
172 if (pgpassword && password_alloc) {
177 if (pgdbport && dbport_alloc) {
182 ast_cdr_unregister(name);
186 static int my_load_module(void)
189 struct ast_config *cfg;
190 struct ast_variable *var;
194 cfg = ast_load(config);
196 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
200 var = ast_variable_browse(cfg, "global");
202 /* nothing configured */
206 tmp = ast_variable_retrieve(cfg,"global","hostname");
208 pghostname = malloc(strlen(tmp) + 1);
209 if (pghostname != NULL) {
211 strcpy(pghostname,tmp);
213 ast_log(LOG_ERROR,"Out of memory error.\n");
217 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming localhost\n");
218 pghostname = "localhost";
221 tmp = ast_variable_retrieve(cfg,"global","dbname");
223 pgdbname = malloc(strlen(tmp) + 1);
224 if (pgdbname != NULL) {
226 strcpy(pgdbname,tmp);
228 ast_log(LOG_ERROR,"Out of memory error.\n");
232 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
233 pgdbname = "asteriskcdrdb";
236 tmp = ast_variable_retrieve(cfg,"global","user");
238 pgdbuser = malloc(strlen(tmp) + 1);
239 if (pgdbuser != NULL) {
241 strcpy(pgdbuser,tmp);
243 ast_log(LOG_ERROR,"Out of memory error.\n");
247 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming root\n");
251 tmp = ast_variable_retrieve(cfg,"global","password");
253 pgpassword = malloc(strlen(tmp) + 1);
254 if (pgpassword != NULL) {
256 strcpy(pgpassword,tmp);
258 ast_log(LOG_ERROR,"Out of memory error.\n");
262 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
266 tmp = ast_variable_retrieve(cfg,"global","port");
268 pgdbport = malloc(strlen(tmp) + 1);
269 if (pgdbport != NULL) {
271 strcpy(pgdbport,tmp);
273 ast_log(LOG_ERROR,"Out of memory error.\n");
277 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
283 ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);
284 ast_log(LOG_DEBUG,"cdr_pgsql: got port of %s\n",pgdbport);
286 ast_log(LOG_DEBUG,"cdr_pgsql: got sock file of %s\n",pgdbsock);
287 ast_log(LOG_DEBUG,"cdr_pgsql: got user of %s\n",pgdbuser);
288 ast_log(LOG_DEBUG,"cdr_pgsql: got dbname of %s\n",pgdbname);
289 ast_log(LOG_DEBUG,"cdr_pgsql: got password of %s\n",pgpassword);
291 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
292 if (PQstatus(conn) != CONNECTION_BAD) {
293 ast_log(LOG_DEBUG,"Successfully connected to PostgreSQL database.\n");
296 pgerror = PQerrorMessage(conn);
297 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
298 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
302 res = ast_cdr_register(name, desc, pgsql_log);
304 ast_log(LOG_ERROR, "Unable to register PGSQL CDR handling\n");
309 int load_module(void)
311 return my_load_module();
314 int unload_module(void)
316 return my_unload_module();
322 return my_load_module();
332 return ASTERISK_GPL_KEY;