avoid the weird usage of assert() in the ALSA header files that gcc 4.2 wants to...
[asterisk-bristuff.git] / cdr / cdr_pgsql.c
blobee115ac488d1ca9ce3cc8db1506c8ba6a64c8f5e
1 /*
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.
23 /*! \file
25 * \brief PostgreSQL CDR logger
27 * \author Matthew D. Hardeman <mhardemn@papersoft.com>
29 * See also
30 * \arg \ref Config_cdr
31 * \arg http://www.postgresql.org/
32 * \ingroup cdr_drivers
35 /*** MODULEINFO
36 <depend>pgsql</depend>
37 ***/
39 #include "asterisk.h"
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43 #include <sys/types.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <time.h>
50 #include <libpq-fe.h>
52 #include "asterisk/config.h"
53 #include "asterisk/options.h"
54 #include "asterisk/channel.h"
55 #include "asterisk/cdr.h"
56 #include "asterisk/module.h"
57 #include "asterisk/logger.h"
58 #include "asterisk.h"
60 #define DATE_FORMAT "%Y-%m-%d %T"
62 static char *name = "pgsql";
63 static char *config = "cdr_pgsql.conf";
64 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
65 static int connected = 0;
67 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
69 static PGconn *conn = NULL;
71 static int pgsql_log(struct ast_cdr *cdr)
73 struct tm tm;
74 time_t t = cdr->start.tv_sec;
75 char sqlcmd[2048] = "", timestr[128];
76 char *pgerror;
77 PGresult *result;
79 ast_mutex_lock(&pgsql_lock);
81 ast_localtime(&t, &tm, NULL);
82 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) {
87 connected = 1;
88 } else {
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);
92 PQfinish(conn);
93 conn = NULL;
97 if (connected) {
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);
123 return -1;
126 if (option_debug > 1)
127 ast_log(LOG_DEBUG, "cdr_pgsql: inserting a CDR record.\n");
129 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,"
130 "lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES"
131 " ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%ld,%ld,'%s',%ld,'%s','%s','%s')",
132 table,timestr,clid,cdr->src, cdr->dst, dcontext,channel, dstchannel, lastapp, lastdata,
133 cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
135 if (option_debug > 2)
136 ast_log(LOG_DEBUG, "cdr_pgsql: SQL command executed: %s\n",sqlcmd);
138 /* Test to be sure we're still connected... */
139 /* If we're connected, and connection is working, good. */
140 /* Otherwise, attempt reconnect. If it fails... sorry... */
141 if (PQstatus(conn) == CONNECTION_OK) {
142 connected = 1;
143 } else {
144 ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
145 PQreset(conn);
146 if (PQstatus(conn) == CONNECTION_OK) {
147 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
148 connected = 1;
149 } else {
150 pgerror = PQerrorMessage(conn);
151 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
152 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
153 PQfinish(conn);
154 conn = NULL;
155 connected = 0;
156 ast_mutex_unlock(&pgsql_lock);
157 return -1;
160 result = PQexec(conn, sqlcmd);
161 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
162 pgerror = PQresultErrorMessage(result);
163 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
164 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
165 ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
166 PQreset(conn);
167 if (PQstatus(conn) == CONNECTION_OK) {
168 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
169 connected = 1;
170 PQclear(result);
171 result = PQexec(conn, sqlcmd);
172 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
173 pgerror = PQresultErrorMessage(result);
174 ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
175 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
178 ast_mutex_unlock(&pgsql_lock);
179 PQclear(result);
180 return -1;
182 PQclear(result);
184 ast_mutex_unlock(&pgsql_lock);
185 return 0;
188 static int my_unload_module(void)
190 PQfinish(conn);
191 if (pghostname)
192 free(pghostname);
193 if (pgdbname)
194 free(pgdbname);
195 if (pgdbuser)
196 free(pgdbuser);
197 if (pgpassword)
198 free(pgpassword);
199 if (pgdbport)
200 free(pgdbport);
201 if (table)
202 free(table);
203 ast_cdr_unregister(name);
204 return 0;
207 static int process_my_load_module(struct ast_config *cfg)
209 struct ast_variable *var;
210 char *pgerror;
211 const char *tmp;
213 if (!(var = ast_variable_browse(cfg, "global")))
214 return 0;
216 if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
217 ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
218 tmp = ""; /* connect via UNIX-socket by default */
221 if (!(pghostname = ast_strdup(tmp)))
222 return -1;
224 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
225 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
226 tmp = "asteriskcdrdb";
229 if (!(pgdbname = ast_strdup(tmp)))
230 return -1;
232 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
233 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
234 tmp = "asterisk";
237 if (!(pgdbuser = ast_strdup(tmp)))
238 return -1;
240 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
241 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
242 tmp = "";
245 if (!(pgpassword = ast_strdup(tmp)))
246 return -1;
248 if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
249 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
250 tmp = "5432";
253 if (!(pgdbport = ast_strdup(tmp)))
254 return -1;
256 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
257 ast_log(LOG_WARNING,"CDR table not specified. Assuming cdr\n");
258 tmp = "cdr";
261 if (!(table = ast_strdup(tmp)))
262 return -1;
264 if (option_debug) {
265 if (ast_strlen_zero(pghostname))
266 ast_log(LOG_DEBUG, "cdr_pgsql: using default unix socket\n");
267 else
268 ast_log(LOG_DEBUG, "cdr_pgsql: got hostname of %s\n", pghostname);
269 ast_log(LOG_DEBUG, "cdr_pgsql: got port of %s\n", pgdbport);
270 ast_log(LOG_DEBUG, "cdr_pgsql: got user of %s\n", pgdbuser);
271 ast_log(LOG_DEBUG, "cdr_pgsql: got dbname of %s\n", pgdbname);
272 ast_log(LOG_DEBUG, "cdr_pgsql: got password of %s\n", pgpassword);
273 ast_log(LOG_DEBUG, "cdr_pgsql: got sql table name of %s\n", table);
276 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
277 if (PQstatus(conn) != CONNECTION_BAD) {
278 if (option_debug)
279 ast_log(LOG_DEBUG, "Successfully connected to PostgreSQL database.\n");
280 connected = 1;
281 } else {
282 pgerror = PQerrorMessage(conn);
283 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
284 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
285 connected = 0;
288 return ast_cdr_register(name, ast_module_info->description, pgsql_log);
291 static int my_load_module(void)
293 struct ast_config *cfg;
294 int res;
296 if (!(cfg = ast_config_load(config))) {
297 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
298 return AST_MODULE_LOAD_DECLINE;
301 res = process_my_load_module(cfg);
302 ast_config_destroy(cfg);
304 return res;
307 static int load_module(void)
309 return my_load_module();
312 static int unload_module(void)
314 return my_unload_module();
317 static int reload(void)
319 int res;
320 ast_mutex_lock(&pgsql_lock);
321 my_unload_module();
322 res = my_load_module();
323 ast_mutex_unlock(&pgsql_lock);
324 return res;
327 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",
328 .load = load_module,
329 .unload = unload_module,
330 .reload = reload,