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>
28 * \extref PostgreSQL http://www.postgresql.org/
31 * \arg \ref Config_cdr
32 * \arg http://www.postgresql.org/
33 * \ingroup cdr_drivers
37 <depend>pgsql</depend>
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include <sys/types.h>
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 *name = "pgsql";
64 static char *config = "cdr_pgsql.conf";
65 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
66 static int connected = 0;
68 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
70 static PGconn *conn = NULL;
72 static int pgsql_log(struct ast_cdr *cdr)
75 char sqlcmd[2048] = "", timestr[128];
79 ast_mutex_lock(&pgsql_lock);
81 ast_localtime(&cdr->start, &tm, NULL);
82 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
84 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
85 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
86 if (PQstatus(conn) != CONNECTION_BAD) {
89 pgerror = PQerrorMessage(conn);
90 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
91 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
98 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
99 char *uniqueid=NULL, *userfield=NULL;
101 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
102 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
103 PQescapeString(clid, cdr->clid, strlen(cdr->clid));
104 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
105 PQescapeString(dcontext, cdr->dcontext, strlen(cdr->dcontext));
106 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
107 PQescapeString(channel, cdr->channel, strlen(cdr->channel));
108 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
109 PQescapeString(dstchannel, cdr->dstchannel, strlen(cdr->dstchannel));
110 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
111 PQescapeString(lastapp, cdr->lastapp, strlen(cdr->lastapp));
112 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
113 PQescapeString(lastdata, cdr->lastdata, strlen(cdr->lastdata));
114 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
115 PQescapeString(uniqueid, cdr->uniqueid, strlen(cdr->uniqueid));
116 if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)
117 PQescapeString(userfield, cdr->userfield, strlen(cdr->userfield));
119 /* Check for all alloca failures above at once */
120 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield)) {
121 ast_log(LOG_ERROR, "cdr_pgsql: Out of memory error (insert fails)\n");
122 ast_mutex_unlock(&pgsql_lock);
126 ast_debug(2, "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 ast_debug(3, "cdr_pgsql: SQL command executed: %s\n",sqlcmd);
136 /* Test to be sure we're still connected... */
137 /* If we're connected, and connection is working, good. */
138 /* Otherwise, attempt reconnect. If it fails... sorry... */
139 if (PQstatus(conn) == CONNECTION_OK) {
142 ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
144 if (PQstatus(conn) == CONNECTION_OK) {
145 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
148 pgerror = PQerrorMessage(conn);
149 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
150 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
154 ast_mutex_unlock(&pgsql_lock);
158 result = PQexec(conn, sqlcmd);
159 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
160 pgerror = PQresultErrorMessage(result);
161 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
162 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
163 ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
165 if (PQstatus(conn) == CONNECTION_OK) {
166 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
169 result = PQexec(conn, sqlcmd);
170 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
171 pgerror = PQresultErrorMessage(result);
172 ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
173 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
176 ast_mutex_unlock(&pgsql_lock);
182 ast_mutex_unlock(&pgsql_lock);
186 static int unload_module(void)
190 ast_free(pghostname);
196 ast_free(pgpassword);
201 ast_cdr_unregister(name);
205 static int config_module(int reload)
207 struct ast_variable *var;
210 struct ast_config *cfg;
211 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
213 if ((cfg = ast_config_load(config, config_flags)) == NULL) {
214 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
216 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
219 if (!(var = ast_variable_browse(cfg, "global")))
222 if (!(tmp = ast_variable_retrieve(cfg, "global", "hostname"))) {
223 ast_log(LOG_WARNING, "PostgreSQL server hostname not specified. Assuming unix socket connection\n");
224 tmp = ""; /* connect via UNIX-socket by default */
228 ast_free(pghostname);
229 if (!(pghostname = ast_strdup(tmp)))
232 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
233 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
234 tmp = "asteriskcdrdb";
239 if (!(pgdbname = ast_strdup(tmp)))
242 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
243 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
249 if (!(pgdbuser = ast_strdup(tmp)))
252 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
253 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
258 ast_free(pgpassword);
259 if (!(pgpassword = ast_strdup(tmp)))
262 if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
263 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
269 if (!(pgdbport = ast_strdup(tmp)))
272 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
273 ast_log(LOG_WARNING,"CDR table not specified. Assuming cdr\n");
279 if (!(table = ast_strdup(tmp)))
283 if (ast_strlen_zero(pghostname))
284 ast_debug(1, "cdr_pgsql: using default unix socket\n");
286 ast_debug(1, "cdr_pgsql: got hostname of %s\n", pghostname);
287 ast_debug(1, "cdr_pgsql: got port of %s\n", pgdbport);
288 ast_debug(1, "cdr_pgsql: got user of %s\n", pgdbuser);
289 ast_debug(1, "cdr_pgsql: got dbname of %s\n", pgdbname);
290 ast_debug(1, "cdr_pgsql: got password of %s\n", pgpassword);
291 ast_debug(1, "cdr_pgsql: got sql table name of %s\n", table);
294 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
295 if (PQstatus(conn) != CONNECTION_BAD) {
296 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
299 pgerror = PQerrorMessage(conn);
300 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
301 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
305 return ast_cdr_register(name, ast_module_info->description, pgsql_log);
308 static int load_module(void)
310 return config_module(0) ? AST_MODULE_LOAD_DECLINE : 0;
313 static int reload(void)
315 return config_module(1);
318 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",
320 .unload = unload_module,