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"
36 #define DATE_FORMAT "%Y-%m-%d %T"
38 static char *desc = "PostgreSQL CDR Backend";
39 static char *name = "pgsql";
40 static char *config = "cdr_pgsql.conf";
41 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbsock = NULL, *pgdbport = NULL;
42 static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbport_alloc = 0;
43 static int connected = 0;
45 static ast_mutex_t pgsql_lock = AST_MUTEX_INITIALIZER;
50 static int pgsql_log(struct ast_cdr *cdr)
54 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 ast_log(LOG_ERROR, "cdr_pgsql: cannot connect to database server %s. Call will not be logged\n", pghostname);
74 /* Test to be sure we're still connected... */
75 /* If we're connected, and connection is working, good. */
76 /* Otherwise, attempt reconnect. If it fails... sorry... */
78 if (PQstatus(conn) == CONNECTION_OK) {
81 ast_log(LOG_ERROR, "cdr_pgsql: connection was lost... reattempting connection.");
83 if (PQstatus(conn) == CONNECTION_OK) {
84 ast_log(LOG_ERROR, "cdr_pgsql: connection reestablished.");
87 ast_log(LOG_ERROR, "cdr_pgsql: unable to reconnect to database.");
95 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
98 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
99 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
100 PQescapeString(clid, cdr->clid, strlen(cdr->clid));
101 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
102 PQescapeString(dcontext, cdr->dcontext, strlen(cdr->dcontext));
103 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
104 PQescapeString(channel, cdr->channel, strlen(cdr->channel));
105 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
106 PQescapeString(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
107 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
108 PQescapeString(lastapp, cdr->lastapp, strlen(cdr->lastapp));
109 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
110 PQescapeString(lastdata, cdr->lastdata, strlen(cdr->lastdata));
111 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
112 PQescapeString(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
114 /* Check for all alloca failures above at once */
115 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
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 sprintf(sqlcmd,"INSERT INTO cdr (calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid) VALUES ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%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);
124 ast_log(LOG_DEBUG,"cdr_pgsql: SQL command as follows: %s\n",sqlcmd);
126 result = PQexec(conn, sqlcmd);
127 if ( PQresultStatus(result) != PGRES_COMMAND_OK) {
128 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database.");
129 ast_mutex_unlock(&pgsql_lock);
133 ast_mutex_unlock(&pgsql_lock);
137 char *description(void)
142 static int my_unload_module(void)
146 if (pghostname && hostname_alloc) {
151 if (pgdbname && dbname_alloc) {
156 if (pgdbuser && dbuser_alloc) {
161 if (pgdbsock && dbsock_alloc) {
166 if (pgpassword && password_alloc) {
171 if (pgdbport && dbport_alloc) {
176 ast_cdr_unregister(name);
180 static int my_load_module(void)
183 struct ast_config *cfg;
184 struct ast_variable *var;
187 cfg = ast_load(config);
189 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
193 var = ast_variable_browse(cfg, "global");
195 /* nothing configured */
199 tmp = ast_variable_retrieve(cfg,"global","hostname");
201 pghostname = malloc(strlen(tmp) + 1);
202 if (pghostname != NULL) {
204 strcpy(pghostname,tmp);
206 ast_log(LOG_ERROR,"Out of memory error.\n");
210 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming localhost\n");
211 pghostname = "localhost";
214 tmp = ast_variable_retrieve(cfg,"global","dbname");
216 pgdbname = malloc(strlen(tmp) + 1);
217 if (pgdbname != NULL) {
219 strcpy(pgdbname,tmp);
221 ast_log(LOG_ERROR,"Out of memory error.\n");
225 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asteriskcdrdb\n");
226 pgdbname = "asteriskcdrdb";
229 tmp = ast_variable_retrieve(cfg,"global","user");
231 pgdbuser = malloc(strlen(tmp) + 1);
232 if (pgdbuser != NULL) {
234 strcpy(pgdbuser,tmp);
236 ast_log(LOG_ERROR,"Out of memory error.\n");
240 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming root\n");
244 tmp = ast_variable_retrieve(cfg,"global","password");
246 pgpassword = malloc(strlen(tmp) + 1);
247 if (pgpassword != NULL) {
249 strcpy(pgpassword,tmp);
251 ast_log(LOG_ERROR,"Out of memory error.\n");
255 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
259 tmp = ast_variable_retrieve(cfg,"global","port");
261 pgdbport = malloc(strlen(tmp) + 1);
262 if (pgdbport != NULL) {
264 strcpy(pgdbport,tmp);
266 ast_log(LOG_ERROR,"Out of memory error.\n");
270 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default.\n");
276 ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);
277 ast_log(LOG_DEBUG,"cdr_pgsql: got port of %s\n",pgdbport);
279 ast_log(LOG_DEBUG,"cdr_pgsql: got sock file of %s\n",pgdbsock);
280 ast_log(LOG_DEBUG,"cdr_pgsql: got user of %s\n",pgdbuser);
281 ast_log(LOG_DEBUG,"cdr_pgsql: got dbname of %s\n",pgdbname);
282 ast_log(LOG_DEBUG,"cdr_pgsql: got password of %s\n",pgpassword);
284 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
285 if (PQstatus(conn) != CONNECTION_BAD) {
286 ast_log(LOG_DEBUG,"Successfully connected to PostgreSQL database.\n");
289 ast_log(LOG_ERROR, "cdr_pgsql: cannot connect to database server %s. Call will not be logged\n", pghostname);
293 res = ast_cdr_register(name, desc, pgsql_log);
295 ast_log(LOG_ERROR, "Unable to register PGSQL CDR handling\n");
300 int load_module(void)
302 return my_load_module();
305 int unload_module(void)
307 return my_unload_module();
313 return my_load_module();
323 return ASTERISK_GPL_KEY;