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, *table = NULL;
41 static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbport_alloc = 0, table_alloc = 0;
42 static int connected = 0;
44 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
49 static int pgsql_log(struct ast_cdr *cdr)
52 char sqlcmd[2048] = "", timestr[128];
55 ast_mutex_lock(&pgsql_lock);
57 localtime_r(&cdr->start.tv_sec,&tm);
58 strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
60 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
61 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
62 if (PQstatus(conn) != CONNECTION_BAD) {
65 pgerror = PQerrorMessage(conn);
66 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
67 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
72 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
73 char *uniqueid=NULL, *userfield=NULL;
75 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
76 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
77 PQescapeString(clid, cdr->clid, strlen(cdr->clid));
78 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
79 PQescapeString(dcontext, cdr->dcontext, strlen(cdr->dcontext));
80 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
81 PQescapeString(channel, cdr->channel, strlen(cdr->channel));
82 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
83 PQescapeString(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
84 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
85 PQescapeString(lastapp, cdr->lastapp, strlen(cdr->lastapp));
86 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
87 PQescapeString(lastdata, cdr->lastdata, strlen(cdr->lastdata));
88 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
89 PQescapeString(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
90 if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)
91 PQescapeString(userfield, cdr->userfield, strlen(cdr->userfield));
93 /* Check for all alloca failures above at once */
94 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield)) {
95 ast_log(LOG_ERROR, "cdr_pgsql: Out of memory error (insert fails)\n");
96 ast_mutex_unlock(&pgsql_lock);
100 ast_log(LOG_DEBUG,"cdr_pgsql: inserting a CDR record.\n");
102 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,"
103 "lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES"
104 " ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%i,%i,'%s',%i,'%s','%s','%s')",
105 table,timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,
106 cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
108 ast_log(LOG_DEBUG,"cdr_pgsql: SQL command executed: %s\n",sqlcmd);
110 /* Test to be sure we're still connected... */
111 /* If we're connected, and connection is working, good. */
112 /* Otherwise, attempt reconnect. If it fails... sorry... */
113 if (PQstatus(conn) == CONNECTION_OK) {
116 ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
118 if (PQstatus(conn) == CONNECTION_OK) {
119 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
122 pgerror = PQerrorMessage(conn);
123 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
124 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
126 ast_mutex_unlock(&pgsql_lock);
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)
154 if (pghostname && hostname_alloc) {
159 if (pgdbname && dbname_alloc) {
164 if (pgdbuser && dbuser_alloc) {
169 if (pgdbsock && dbsock_alloc) {
174 if (pgpassword && password_alloc) {
179 if (pgdbport && dbport_alloc) {
184 if (table && table_alloc) {
189 ast_cdr_unregister(name);
193 static int process_my_load_module(struct ast_config *cfg)
196 struct ast_variable *var;
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) {
210 memset(pghostname, 0, strlen(tmp) + 1);
212 strncpy(pghostname, tmp, strlen(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) {
226 memset(pgdbname, 0, strlen(tmp) + 1);
228 strncpy(pgdbname, tmp, strlen(tmp));
230 ast_log(LOG_ERROR,"Out of memory error.\n");
234 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
235 pgdbname = "asteriskcdrdb";
238 tmp = ast_variable_retrieve(cfg,"global","user");
240 pgdbuser = malloc(strlen(tmp) + 1);
241 if (pgdbuser != NULL) {
242 memset(pgdbuser, 0, strlen(tmp) + 1);
244 strncpy(pgdbuser, tmp, strlen(tmp));
246 ast_log(LOG_ERROR,"Out of memory error.\n");
250 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming root\n");
254 tmp = ast_variable_retrieve(cfg,"global","password");
256 pgpassword = malloc(strlen(tmp) + 1);
257 if (pgpassword != NULL) {
258 memset(pgpassword, 0, strlen(tmp) + 1);
260 strncpy(pgpassword, tmp, strlen(tmp));
262 ast_log(LOG_ERROR,"Out of memory error.\n");
266 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
270 tmp = ast_variable_retrieve(cfg,"global","port");
272 pgdbport = malloc(strlen(tmp) + 1);
273 if (pgdbport != NULL) {
274 memset(pgdbport, 0, strlen(tmp) + 1);
276 strncpy(pgdbport, tmp, strlen(tmp));
278 ast_log(LOG_ERROR,"Out of memory error.\n");
282 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
285 /* Loading stuff for table name */
286 tmp = ast_variable_retrieve(cfg,"global","table");
288 table = malloc(strlen(tmp) + 1);
290 memset(table, 0, strlen(tmp) + 1);
292 strncpy(table, tmp, strlen(tmp));
294 ast_log(LOG_ERROR,"Out of memory error.\n");
298 ast_log(LOG_WARNING,"CDR table not specified. Assuming cdr\n");
302 ast_log(LOG_DEBUG,"cdr_pgsql: got hostname of %s\n",pghostname);
303 ast_log(LOG_DEBUG,"cdr_pgsql: got port of %s\n",pgdbport);
305 ast_log(LOG_DEBUG,"cdr_pgsql: got sock file of %s\n",pgdbsock);
306 ast_log(LOG_DEBUG,"cdr_pgsql: got user of %s\n",pgdbuser);
307 ast_log(LOG_DEBUG,"cdr_pgsql: got dbname of %s\n",pgdbname);
308 ast_log(LOG_DEBUG,"cdr_pgsql: got password of %s\n",pgpassword);
309 ast_log(LOG_DEBUG,"cdr_pgsql: got sql table name of %s\n",table);
311 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
312 if (PQstatus(conn) != CONNECTION_BAD) {
313 ast_log(LOG_DEBUG,"Successfully connected to PostgreSQL database.\n");
316 pgerror = PQerrorMessage(conn);
317 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
318 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
322 res = ast_cdr_register(name, desc, pgsql_log);
324 ast_log(LOG_ERROR, "Unable to register PGSQL CDR handling\n");
329 static int my_load_module(void)
331 struct ast_config *cfg;
333 cfg = ast_load(config);
335 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
338 res = process_my_load_module(cfg);
343 int load_module(void)
345 return my_load_module();
348 int unload_module(void)
350 return my_unload_module();
356 return my_load_module();
361 /* To be able to unload the module */
362 if ( ast_mutex_trylock(&pgsql_lock) ) {
365 ast_mutex_unlock(&pgsql_lock);
372 return ASTERISK_GPL_KEY;