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);
127 ast_mutex_unlock(&pgsql_lock);
131 result = PQexec(conn, sqlcmd);
132 if ( PQresultStatus(result) != PGRES_COMMAND_OK) {
133 pgerror = PQresultErrorMessage(result);
134 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
135 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
136 ast_mutex_unlock(&pgsql_lock);
140 ast_mutex_unlock(&pgsql_lock);
144 char *description(void)
149 static int my_unload_module(void)
153 if (pghostname && hostname_alloc) {
158 if (pgdbname && dbname_alloc) {
163 if (pgdbuser && dbuser_alloc) {
168 if (pgdbsock && dbsock_alloc) {
173 if (pgpassword && password_alloc) {
178 if (pgdbport && dbport_alloc) {
183 ast_cdr_unregister(name);
187 static int my_load_module(void)
190 struct ast_config *cfg;
191 struct ast_variable *var;
195 cfg = ast_load(config);
197 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
201 var = ast_variable_browse(cfg, "global");
203 /* nothing configured */
207 tmp = ast_variable_retrieve(cfg,"global","hostname");
209 pghostname = malloc(strlen(tmp) + 1);
210 if (pghostname != NULL) {
212 strcpy(pghostname,tmp);
214 ast_log(LOG_ERROR,"Out of memory error.\n");
218 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming localhost\n");
219 pghostname = "localhost";
222 tmp = ast_variable_retrieve(cfg,"global","dbname");
224 pgdbname = malloc(strlen(tmp) + 1);
225 if (pgdbname != NULL) {
227 strcpy(pgdbname,tmp);
229 ast_log(LOG_ERROR,"Out of memory error.\n");
233 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
234 pgdbname = "asteriskcdrdb";
237 tmp = ast_variable_retrieve(cfg,"global","user");
239 pgdbuser = malloc(strlen(tmp) + 1);
240 if (pgdbuser != NULL) {
242 strcpy(pgdbuser,tmp);
244 ast_log(LOG_ERROR,"Out of memory error.\n");
248 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming root\n");
252 tmp = ast_variable_retrieve(cfg,"global","password");
254 pgpassword = malloc(strlen(tmp) + 1);
255 if (pgpassword != NULL) {
257 strcpy(pgpassword,tmp);
259 ast_log(LOG_ERROR,"Out of memory error.\n");
263 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
267 tmp = ast_variable_retrieve(cfg,"global","port");
269 pgdbport = malloc(strlen(tmp) + 1);
270 if (pgdbport != NULL) {
272 strcpy(pgdbport,tmp);
274 ast_log(LOG_ERROR,"Out of memory error.\n");
278 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
284 ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);
285 ast_log(LOG_DEBUG,"cdr_pgsql: got port of %s\n",pgdbport);
287 ast_log(LOG_DEBUG,"cdr_pgsql: got sock file of %s\n",pgdbsock);
288 ast_log(LOG_DEBUG,"cdr_pgsql: got user of %s\n",pgdbuser);
289 ast_log(LOG_DEBUG,"cdr_pgsql: got dbname of %s\n",pgdbname);
290 ast_log(LOG_DEBUG,"cdr_pgsql: got password of %s\n",pgpassword);
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 int load_module(void)
312 return my_load_module();
315 int unload_module(void)
317 return my_unload_module();
323 return my_load_module();
333 return ASTERISK_GPL_KEY;