2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2003 - 2006
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 * See http://www.asterisk.org for more information about
13 * the Asterisk project. Please do not directly contact
14 * any of the maintainers of this project for assistance;
15 * the project provides a web site, mailing lists and IRC
16 * channels for your use.
18 * This program is free software, distributed under the terms of
19 * the GNU General Public License Version 2. See the LICENSE file
20 * at the top of the source tree.
25 * \brief PostgreSQL CDR logger
27 * \author Matthew D. Hardeman <mhardemn@papersoft.com>
30 * \arg \ref Config_cdr
31 * \arg http://www.postgresql.org/
32 * \ingroup cdr_drivers
35 #include <sys/types.h>
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49 #include "asterisk/config.h"
50 #include "asterisk/options.h"
51 #include "asterisk/channel.h"
52 #include "asterisk/cdr.h"
53 #include "asterisk/module.h"
54 #include "asterisk/logger.h"
57 #define DATE_FORMAT "%Y-%m-%d %T"
59 static char *desc = "PostgreSQL CDR Backend";
60 static char *name = "pgsql";
61 static char *config = "cdr_pgsql.conf";
62 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbsock = NULL, *pgdbport = NULL, *table = NULL;
63 static int connected = 0;
65 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
68 static PGresult *result;
70 static int pgsql_log(struct ast_cdr *cdr)
73 char sqlcmd[2048] = "", timestr[128];
76 ast_mutex_lock(&pgsql_lock);
78 localtime_r(&cdr->start.tv_sec,&tm);
79 strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
81 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
82 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
83 if (PQstatus(conn) != CONNECTION_BAD) {
86 pgerror = PQerrorMessage(conn);
87 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
88 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
93 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
94 char *uniqueid=NULL, *userfield=NULL;
96 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
97 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
98 PQescapeString(clid, cdr->clid, strlen(cdr->clid));
99 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
100 PQescapeString(dcontext, cdr->dcontext, strlen(cdr->dcontext));
101 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
102 PQescapeString(channel, cdr->channel, strlen(cdr->channel));
103 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
104 PQescapeString(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
105 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
106 PQescapeString(lastapp, cdr->lastapp, strlen(cdr->lastapp));
107 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
108 PQescapeString(lastdata, cdr->lastdata, strlen(cdr->lastdata));
109 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
110 PQescapeString(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
111 if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)
112 PQescapeString(userfield, cdr->userfield, strlen(cdr->userfield));
114 /* Check for all alloca failures above at once */
115 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield)) {
116 ast_log(LOG_ERROR, "cdr_pgsql: Out of memory error (insert fails)\n");
117 ast_mutex_unlock(&pgsql_lock);
121 ast_log(LOG_DEBUG,"cdr_pgsql: inserting a CDR record.\n");
123 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,"
124 "lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES"
125 " ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%d,%d,'%s',%d,'%s','%s','%s')",
126 table,timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,
127 cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
129 ast_log(LOG_DEBUG,"cdr_pgsql: SQL command executed: %s\n",sqlcmd);
131 /* Test to be sure we're still connected... */
132 /* If we're connected, and connection is working, good. */
133 /* Otherwise, attempt reconnect. If it fails... sorry... */
134 if (PQstatus(conn) == CONNECTION_OK) {
137 ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
139 if (PQstatus(conn) == CONNECTION_OK) {
140 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
143 pgerror = PQerrorMessage(conn);
144 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
145 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
147 ast_mutex_unlock(&pgsql_lock);
151 result = PQexec(conn, sqlcmd);
152 if ( PQresultStatus(result) != PGRES_COMMAND_OK) {
153 pgerror = PQresultErrorMessage(result);
154 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
155 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
156 ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
158 if (PQstatus(conn) == CONNECTION_OK) {
159 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
161 result = PQexec(conn, sqlcmd);
162 if ( PQresultStatus(result) != PGRES_COMMAND_OK)
164 pgerror = PQresultErrorMessage(result);
165 ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
166 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
169 ast_mutex_unlock(&pgsql_lock);
173 ast_mutex_unlock(&pgsql_lock);
177 char *description(void)
182 static int my_unload_module(void)
200 ast_cdr_unregister(name);
204 static int process_my_load_module(struct ast_config *cfg)
207 struct ast_variable *var;
211 var = ast_variable_browse(cfg, "global");
213 /* nothing configured */
217 tmp = ast_variable_retrieve(cfg,"global","hostname");
219 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming localhost\n");
222 pghostname = strdup(tmp);
223 if (pghostname == NULL) {
224 ast_log(LOG_ERROR,"Out of memory error.\n");
228 tmp = ast_variable_retrieve(cfg,"global","dbname");
230 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
231 tmp = "asteriskcdrdb";
233 pgdbname = strdup(tmp);
234 if (pgdbname == NULL) {
235 ast_log(LOG_ERROR,"Out of memory error.\n");
239 tmp = ast_variable_retrieve(cfg,"global","user");
241 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming root\n");
244 pgdbuser = strdup(tmp);
245 if (pgdbuser == NULL) {
246 ast_log(LOG_ERROR,"Out of memory error.\n");
250 tmp = ast_variable_retrieve(cfg,"global","password");
252 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
255 pgpassword = strdup(tmp);
256 if (pgpassword == NULL) {
257 ast_log(LOG_ERROR,"Out of memory error.\n");
261 tmp = ast_variable_retrieve(cfg,"global","port");
263 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
266 pgdbport = strdup(tmp);
267 if (pgdbport == NULL) {
268 ast_log(LOG_ERROR,"Out of memory error.\n");
272 tmp = ast_variable_retrieve(cfg,"global","table");
274 ast_log(LOG_WARNING,"CDR table not specified. Assuming cdr\n");
279 ast_log(LOG_ERROR,"Out of memory error.\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);
290 ast_log(LOG_DEBUG,"cdr_pgsql: got sql table name of %s\n",table);
292 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
293 if (PQstatus(conn) != CONNECTION_BAD) {
294 ast_log(LOG_DEBUG,"Successfully connected to PostgreSQL database.\n");
297 pgerror = PQerrorMessage(conn);
298 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
299 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
303 res = ast_cdr_register(name, desc, pgsql_log);
305 ast_log(LOG_ERROR, "Unable to register PGSQL CDR handling\n");
310 static int my_load_module(void)
312 struct ast_config *cfg;
314 cfg = ast_config_load(config);
316 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
319 res = process_my_load_module(cfg);
320 ast_config_destroy(cfg);
324 int load_module(void)
326 return my_load_module();
329 int unload_module(void)
331 return my_unload_module();
337 return my_load_module();
342 /* To be able to unload the module */
343 if ( ast_mutex_trylock(&pgsql_lock) ) {
346 ast_mutex_unlock(&pgsql_lock);
353 return ASTERISK_GPL_KEY;