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;
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));
97 /* Check for all alloca failures above at once */
98 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid)) {
99 ast_log(LOG_ERROR, "cdr_pgsql: Out of memory error (insert fails)\n");
100 ast_mutex_unlock(&pgsql_lock);
104 ast_log(LOG_DEBUG,"cdr_pgsql: inserting a CDR record.\n");
106 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);
107 ast_log(LOG_DEBUG,"cdr_pgsql: SQL command executed: %s\n",sqlcmd);
109 /* Test to be sure we're still connected... */
110 /* If we're connected, and connection is working, good. */
111 /* Otherwise, attempt reconnect. If it fails... sorry... */
112 if (PQstatus(conn) == CONNECTION_OK) {
115 ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
117 if (PQstatus(conn) == CONNECTION_OK) {
118 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
121 pgerror = PQerrorMessage(conn);
122 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
123 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
128 result = PQexec(conn, sqlcmd);
129 if ( PQresultStatus(result) != PGRES_COMMAND_OK) {
130 pgerror = PQresultErrorMessage(result);
131 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
132 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
133 ast_mutex_unlock(&pgsql_lock);
137 ast_mutex_unlock(&pgsql_lock);
141 char *description(void)
146 static int my_unload_module(void)
150 if (pghostname && hostname_alloc) {
155 if (pgdbname && dbname_alloc) {
160 if (pgdbuser && dbuser_alloc) {
165 if (pgdbsock && dbsock_alloc) {
170 if (pgpassword && password_alloc) {
175 if (pgdbport && dbport_alloc) {
180 ast_cdr_unregister(name);
184 static int my_load_module(void)
187 struct ast_config *cfg;
188 struct ast_variable *var;
192 cfg = ast_load(config);
194 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
198 var = ast_variable_browse(cfg, "global");
200 /* nothing configured */
204 tmp = ast_variable_retrieve(cfg,"global","hostname");
206 pghostname = malloc(strlen(tmp) + 1);
207 if (pghostname != NULL) {
209 strcpy(pghostname,tmp);
211 ast_log(LOG_ERROR,"Out of memory error.\n");
215 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming localhost\n");
216 pghostname = "localhost";
219 tmp = ast_variable_retrieve(cfg,"global","dbname");
221 pgdbname = malloc(strlen(tmp) + 1);
222 if (pgdbname != NULL) {
224 strcpy(pgdbname,tmp);
226 ast_log(LOG_ERROR,"Out of memory error.\n");
230 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
231 pgdbname = "asteriskcdrdb";
234 tmp = ast_variable_retrieve(cfg,"global","user");
236 pgdbuser = malloc(strlen(tmp) + 1);
237 if (pgdbuser != NULL) {
239 strcpy(pgdbuser,tmp);
241 ast_log(LOG_ERROR,"Out of memory error.\n");
245 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming root\n");
249 tmp = ast_variable_retrieve(cfg,"global","password");
251 pgpassword = malloc(strlen(tmp) + 1);
252 if (pgpassword != NULL) {
254 strcpy(pgpassword,tmp);
256 ast_log(LOG_ERROR,"Out of memory error.\n");
260 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
264 tmp = ast_variable_retrieve(cfg,"global","port");
266 pgdbport = malloc(strlen(tmp) + 1);
267 if (pgdbport != NULL) {
269 strcpy(pgdbport,tmp);
271 ast_log(LOG_ERROR,"Out of memory error.\n");
275 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
281 ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);
282 ast_log(LOG_DEBUG,"cdr_pgsql: got port of %s\n",pgdbport);
284 ast_log(LOG_DEBUG,"cdr_pgsql: got sock file of %s\n",pgdbsock);
285 ast_log(LOG_DEBUG,"cdr_pgsql: got user of %s\n",pgdbuser);
286 ast_log(LOG_DEBUG,"cdr_pgsql: got dbname of %s\n",pgdbname);
287 ast_log(LOG_DEBUG,"cdr_pgsql: got password of %s\n",pgpassword);
289 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
290 if (PQstatus(conn) != CONNECTION_BAD) {
291 ast_log(LOG_DEBUG,"Successfully connected to PostgreSQL database.\n");
294 pgerror = PQerrorMessage(conn);
295 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
296 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
300 res = ast_cdr_register(name, desc, pgsql_log);
302 ast_log(LOG_ERROR, "Unable to register PGSQL CDR handling\n");
307 int load_module(void)
309 return my_load_module();
312 int unload_module(void)
314 return my_unload_module();
320 return my_load_module();
330 return ASTERISK_GPL_KEY;