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$")
48 #include "asterisk/config.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/cdr.h"
51 #include "asterisk/module.h"
53 #define DATE_FORMAT "%Y-%m-%d %T"
55 static char *name = "pgsql";
56 static char *config = "cdr_pgsql.conf";
57 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
58 static int connected = 0;
60 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
62 static PGconn *conn = NULL;
64 static int pgsql_log(struct ast_cdr *cdr)
67 char sqlcmd[2048] = "", timestr[128];
71 ast_mutex_lock(&pgsql_lock);
73 ast_localtime(&cdr->start, &tm, NULL);
74 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
76 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
77 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
78 if (PQstatus(conn) != CONNECTION_BAD) {
81 pgerror = PQerrorMessage(conn);
82 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
83 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
90 char *clid=NULL, *dcontext=NULL, *channel=NULL, *dstchannel=NULL, *lastapp=NULL, *lastdata=NULL;
91 char *src=NULL, *dst=NULL, *uniqueid=NULL, *userfield=NULL;
94 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
95 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
96 PQescapeStringConn(conn, clid, cdr->clid, strlen(cdr->clid), &pgerr);
97 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
98 PQescapeStringConn(conn, dcontext, cdr->dcontext, strlen(cdr->dcontext), &pgerr);
99 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
100 PQescapeStringConn(conn, channel, cdr->channel, strlen(cdr->channel), &pgerr);
101 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
102 PQescapeStringConn(conn, dstchannel, cdr->dstchannel, strlen(cdr->dstchannel), &pgerr);
103 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
104 PQescapeStringConn(conn, lastapp, cdr->lastapp, strlen(cdr->lastapp), &pgerr);
105 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
106 PQescapeStringConn(conn, lastdata, cdr->lastdata, strlen(cdr->lastdata), &pgerr);
107 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
108 PQescapeStringConn(conn, uniqueid, cdr->uniqueid, strlen(cdr->uniqueid), &pgerr);
109 if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)
110 PQescapeStringConn(conn, userfield, cdr->userfield, strlen(cdr->userfield), &pgerr);
111 if ((src = alloca(strlen(cdr->src) * 2 + 1)) != NULL)
112 PQescapeStringConn(conn, src, cdr->src, strlen(cdr->src), &pgerr);
113 if ((dst = alloca(strlen(cdr->dst) * 2 + 1)) != NULL)
114 PQescapeStringConn(conn, dst, cdr->dst, strlen(cdr->dst), &pgerr);
116 /* Check for all alloca failures above at once */
117 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield) || (!src) || (!dst)) {
118 ast_log(LOG_ERROR, "cdr_pgsql: Out of memory error (insert fails)\n");
119 ast_mutex_unlock(&pgsql_lock);
123 ast_debug(2, "cdr_pgsql: inserting a CDR record.\n");
125 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,"
126 "lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES"
127 " ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%ld,%ld,'%s',%ld,'%s','%s','%s')",
128 table, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata,
129 cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
131 ast_debug(3, "cdr_pgsql: SQL command executed: %s\n",sqlcmd);
133 /* Test to be sure we're still connected... */
134 /* If we're connected, and connection is working, good. */
135 /* Otherwise, attempt reconnect. If it fails... sorry... */
136 if (PQstatus(conn) == CONNECTION_OK) {
139 ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
141 if (PQstatus(conn) == CONNECTION_OK) {
142 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
145 pgerror = PQerrorMessage(conn);
146 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
147 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
151 ast_mutex_unlock(&pgsql_lock);
155 result = PQexec(conn, sqlcmd);
156 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
157 pgerror = PQresultErrorMessage(result);
158 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
159 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
160 ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
162 if (PQstatus(conn) == CONNECTION_OK) {
163 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
166 result = PQexec(conn, sqlcmd);
167 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
168 pgerror = PQresultErrorMessage(result);
169 ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
170 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
173 ast_mutex_unlock(&pgsql_lock);
179 ast_mutex_unlock(&pgsql_lock);
183 static int unload_module(void)
187 ast_free(pghostname);
193 ast_free(pgpassword);
198 ast_cdr_unregister(name);
202 static int config_module(int reload)
204 struct ast_variable *var;
207 struct ast_config *cfg;
208 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
210 if ((cfg = ast_config_load(config, config_flags)) == NULL) {
211 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
213 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
216 if (!(var = ast_variable_browse(cfg, "global"))) {
217 ast_config_destroy(cfg);
221 if (!(tmp = ast_variable_retrieve(cfg, "global", "hostname"))) {
222 ast_log(LOG_WARNING, "PostgreSQL server hostname not specified. Assuming unix socket connection\n");
223 tmp = ""; /* connect via UNIX-socket by default */
227 ast_free(pghostname);
228 if (!(pghostname = ast_strdup(tmp))) {
229 ast_config_destroy(cfg);
233 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
234 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
235 tmp = "asteriskcdrdb";
240 if (!(pgdbname = ast_strdup(tmp))) {
241 ast_config_destroy(cfg);
245 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
246 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
252 if (!(pgdbuser = ast_strdup(tmp))) {
253 ast_config_destroy(cfg);
257 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
258 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
263 ast_free(pgpassword);
264 if (!(pgpassword = ast_strdup(tmp))) {
265 ast_config_destroy(cfg);
269 if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
270 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
276 if (!(pgdbport = ast_strdup(tmp))) {
277 ast_config_destroy(cfg);
281 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
282 ast_log(LOG_WARNING,"CDR table not specified. Assuming cdr\n");
288 if (!(table = ast_strdup(tmp))) {
289 ast_config_destroy(cfg);
294 if (ast_strlen_zero(pghostname))
295 ast_debug(1, "cdr_pgsql: using default unix socket\n");
297 ast_debug(1, "cdr_pgsql: got hostname of %s\n", pghostname);
298 ast_debug(1, "cdr_pgsql: got port of %s\n", pgdbport);
299 ast_debug(1, "cdr_pgsql: got user of %s\n", pgdbuser);
300 ast_debug(1, "cdr_pgsql: got dbname of %s\n", pgdbname);
301 ast_debug(1, "cdr_pgsql: got password of %s\n", pgpassword);
302 ast_debug(1, "cdr_pgsql: got sql table name of %s\n", table);
305 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
306 if (PQstatus(conn) != CONNECTION_BAD) {
307 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
310 pgerror = PQerrorMessage(conn);
311 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
312 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
316 ast_config_destroy(cfg);
318 return ast_cdr_register(name, ast_module_info->description, pgsql_log);
321 static int load_module(void)
323 return config_module(0) ? AST_MODULE_LOAD_DECLINE : 0;
326 static int reload(void)
328 return config_module(1);
331 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",
333 .unload = unload_module,