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
36 <depend>pgsql</depend>
39 #include <sys/types.h>
51 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
53 #include "asterisk/config.h"
54 #include "asterisk/options.h"
55 #include "asterisk/channel.h"
56 #include "asterisk/cdr.h"
57 #include "asterisk/module.h"
58 #include "asterisk/logger.h"
61 #define DATE_FORMAT "%Y-%m-%d %T"
63 static char *desc = "PostgreSQL CDR Backend";
64 static char *name = "pgsql";
65 static char *config = "cdr_pgsql.conf";
66 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbsock = NULL, *pgdbport = NULL, *table = NULL;
67 static int connected = 0;
69 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
72 static PGresult *result;
74 static int pgsql_log(struct ast_cdr *cdr)
77 char sqlcmd[2048] = "", timestr[128];
80 ast_mutex_lock(&pgsql_lock);
82 localtime_r(&cdr->start.tv_sec,&tm);
83 strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
85 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
86 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
87 if (PQstatus(conn) != CONNECTION_BAD) {
90 pgerror = PQerrorMessage(conn);
91 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
92 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
97 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
98 char *uniqueid=NULL, *userfield=NULL;
100 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
101 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
102 PQescapeString(clid, cdr->clid, strlen(cdr->clid));
103 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
104 PQescapeString(dcontext, cdr->dcontext, strlen(cdr->dcontext));
105 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
106 PQescapeString(channel, cdr->channel, strlen(cdr->channel));
107 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
108 PQescapeString(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
109 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
110 PQescapeString(lastapp, cdr->lastapp, strlen(cdr->lastapp));
111 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
112 PQescapeString(lastdata, cdr->lastdata, strlen(cdr->lastdata));
113 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
114 PQescapeString(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
115 if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)
116 PQescapeString(userfield, cdr->userfield, strlen(cdr->userfield));
118 /* Check for all alloca failures above at once */
119 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield)) {
120 ast_log(LOG_ERROR, "cdr_pgsql: Out of memory error (insert fails)\n");
121 ast_mutex_unlock(&pgsql_lock);
125 if (option_debug > 1)
126 ast_log(LOG_DEBUG, "cdr_pgsql: inserting a CDR record.\n");
128 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,"
129 "lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES"
130 " ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%ld,%ld,'%s',%ld,'%s','%s','%s')",
131 table,timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,
132 cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
134 if (option_debug > 2)
135 ast_log(LOG_DEBUG, "cdr_pgsql: SQL command executed: %s\n",sqlcmd);
137 /* Test to be sure we're still connected... */
138 /* If we're connected, and connection is working, good. */
139 /* Otherwise, attempt reconnect. If it fails... sorry... */
140 if (PQstatus(conn) == CONNECTION_OK) {
143 ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
145 if (PQstatus(conn) == CONNECTION_OK) {
146 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
149 pgerror = PQerrorMessage(conn);
150 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
151 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
153 ast_mutex_unlock(&pgsql_lock);
157 result = PQexec(conn, sqlcmd);
158 if ( PQresultStatus(result) != PGRES_COMMAND_OK) {
159 pgerror = PQresultErrorMessage(result);
160 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
161 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
162 ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
164 if (PQstatus(conn) == CONNECTION_OK) {
165 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
167 result = PQexec(conn, sqlcmd);
168 if ( PQresultStatus(result) != PGRES_COMMAND_OK)
170 pgerror = PQresultErrorMessage(result);
171 ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
172 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
175 ast_mutex_unlock(&pgsql_lock);
179 ast_mutex_unlock(&pgsql_lock);
183 static const char *description(void)
188 static int my_unload_module(void)
206 ast_cdr_unregister(name);
210 static int process_my_load_module(struct ast_config *cfg)
212 struct ast_variable *var;
216 if (!(var = ast_variable_browse(cfg, "global")))
219 if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
220 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
221 tmp = ""; /* connect via UNIX-socket by default */
224 if (!(pghostname = ast_strdup(tmp)))
227 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
228 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
229 tmp = "asteriskcdrdb";
232 if (!(pgdbname = ast_strdup(tmp)))
235 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
236 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming root\n");
240 if (!(pgdbuser = ast_strdup(tmp)))
243 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
244 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
248 if (!(pgpassword = ast_strdup(tmp)))
251 if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
252 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
256 if (!(pgdbport = ast_strdup(tmp)))
259 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
260 ast_log(LOG_WARNING,"CDR table not specified. Assuming cdr\n");
264 if (!(table = ast_strdup(tmp)))
268 if (ast_strlen_zero(pghostname))
269 ast_log(LOG_DEBUG, "cdr_pgsql: using default unix socket\n");
271 ast_log(LOG_DEBUG, "cdr_pgsql: got hostname of %s\n", pghostname);
272 ast_log(LOG_DEBUG, "cdr_pgsql: got port of %s\n", pgdbport);
273 ast_log(LOG_DEBUG, "cdr_pgsql: got user of %s\n", pgdbuser);
274 ast_log(LOG_DEBUG, "cdr_pgsql: got dbname of %s\n", pgdbname);
275 ast_log(LOG_DEBUG, "cdr_pgsql: got password of %s\n", pgpassword);
276 ast_log(LOG_DEBUG, "cdr_pgsql: got sql table name of %s\n", table);
279 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
280 if (PQstatus(conn) != CONNECTION_BAD) {
282 ast_log(LOG_DEBUG, "Successfully connected to PostgreSQL database.\n");
285 pgerror = PQerrorMessage(conn);
286 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
287 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
291 return ast_cdr_register(name, desc, pgsql_log);
294 static int my_load_module(void)
296 struct ast_config *cfg;
299 if (!(cfg = ast_config_load(config))) {
300 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
304 res = process_my_load_module(cfg);
305 ast_config_destroy(cfg);
310 static int load_module(void *mod)
312 return my_load_module();
315 static int unload_module(void *mod)
317 return my_unload_module();
320 static int reload(void *mod)
323 return my_load_module();
326 static const char *key(void)
328 return ASTERISK_GPL_KEY;
331 STD_MOD(MOD_0, reload, NULL, NULL);