(closes issue #11594)
[asterisk-bristuff.git] / cdr / cdr_pgsql.c
bloba2144a7129999d5e74996e87634a0ea227e53ecf
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 *src=NULL, *dst=NULL, *uniqueid=NULL, *userfield=NULL;
100 int pgerr;
102 /* Maximum space needed would be if all characters needed to be escaped, plus a trailing NULL */
103 if ((clid = alloca(strlen(cdr->clid) * 2 + 1)) != NULL)
104 PQescapeStringConn(conn, clid, cdr->clid, strlen(cdr->clid), &pgerr);
105 if ((dcontext = alloca(strlen(cdr->dcontext) * 2 + 1)) != NULL)
106 PQescapeStringConn(conn, dcontext, cdr->dcontext, strlen(cdr->dcontext), &pgerr);
107 if ((channel = alloca(strlen(cdr->channel) * 2 + 1)) != NULL)
108 PQescapeStringConn(conn, channel, cdr->channel, strlen(cdr->channel), &pgerr);
109 if ((dstchannel = alloca(strlen(cdr->dstchannel) * 2 + 1)) != NULL)
110 PQescapeStringConn(conn, dstchannel, cdr->dstchannel, strlen(cdr->dstchannel), &pgerr);
111 if ((lastapp = alloca(strlen(cdr->lastapp) * 2 + 1)) != NULL)
112 PQescapeStringConn(conn, lastapp, cdr->lastapp, strlen(cdr->lastapp), &pgerr);
113 if ((lastdata = alloca(strlen(cdr->lastdata) * 2 + 1)) != NULL)
114 PQescapeStringConn(conn, lastdata, cdr->lastdata, strlen(cdr->lastdata), &pgerr);
115 if ((uniqueid = alloca(strlen(cdr->uniqueid) * 2 + 1)) != NULL)
116 PQescapeStringConn(conn, uniqueid, cdr->uniqueid, strlen(cdr->uniqueid), &pgerr);
117 if ((userfield = alloca(strlen(cdr->userfield) * 2 + 1)) != NULL)
118 PQescapeStringConn(conn, userfield, cdr->userfield, strlen(cdr->userfield), &pgerr);
119 if ((src = alloca(strlen(cdr->src) * 2 + 1)) != NULL)
120 PQescapeStringConn(conn, src, cdr->src, strlen(cdr->src), &pgerr);
121 if ((dst = alloca(strlen(cdr->dst) * 2 + 1)) != NULL)
122 PQescapeStringConn(conn, dst, cdr->dst, strlen(cdr->dst), &pgerr);
124 /* Check for all alloca failures above at once */
125 if ((!clid) || (!dcontext) || (!channel) || (!dstchannel) || (!lastapp) || (!lastdata) || (!uniqueid) || (!userfield) || (!src) || (!dst)) {
126 ast_log(LOG_ERROR, "cdr_pgsql: Out of memory error (insert fails)\n");
127 ast_mutex_unlock(&pgsql_lock);
128 return -1;
131 if (option_debug > 1)
132 ast_log(LOG_DEBUG, "cdr_pgsql: inserting a CDR record.\n");
134 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s (calldate,clid,src,dst,dcontext,channel,dstchannel,"
135 "lastapp,lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) VALUES"
136 " ('%s','%s','%s','%s','%s', '%s','%s','%s','%s',%ld,%ld,'%s',%ld,'%s','%s','%s')",
137 table, timestr, clid, src, dst, dcontext, channel, dstchannel, lastapp, lastdata,
138 cdr->duration,cdr->billsec,ast_cdr_disp2str(cdr->disposition),cdr->amaflags, cdr->accountcode, uniqueid, userfield);
140 if (option_debug > 2)
141 ast_log(LOG_DEBUG, "cdr_pgsql: SQL command executed: %s\n",sqlcmd);
143 /* Test to be sure we're still connected... */
144 /* If we're connected, and connection is working, good. */
145 /* Otherwise, attempt reconnect. If it fails... sorry... */
146 if (PQstatus(conn) == CONNECTION_OK) {
147 connected = 1;
148 } else {
149 ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
150 PQreset(conn);
151 if (PQstatus(conn) == CONNECTION_OK) {
152 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
153 connected = 1;
154 } else {
155 pgerror = PQerrorMessage(conn);
156 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
157 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
158 PQfinish(conn);
159 conn = NULL;
160 connected = 0;
161 ast_mutex_unlock(&pgsql_lock);
162 return -1;
165 result = PQexec(conn, sqlcmd);
166 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
167 pgerror = PQresultErrorMessage(result);
168 ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
169 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
170 ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
171 PQreset(conn);
172 if (PQstatus(conn) == CONNECTION_OK) {
173 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
174 connected = 1;
175 PQclear(result);
176 result = PQexec(conn, sqlcmd);
177 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
178 pgerror = PQresultErrorMessage(result);
179 ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
180 ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
183 ast_mutex_unlock(&pgsql_lock);
184 PQclear(result);
185 return -1;
187 PQclear(result);
189 ast_mutex_unlock(&pgsql_lock);
190 return 0;
193 static int my_unload_module(void)
195 PQfinish(conn);
196 if (pghostname)
197 free(pghostname);
198 if (pgdbname)
199 free(pgdbname);
200 if (pgdbuser)
201 free(pgdbuser);
202 if (pgpassword)
203 free(pgpassword);
204 if (pgdbport)
205 free(pgdbport);
206 if (table)
207 free(table);
208 ast_cdr_unregister(name);
209 return 0;
212 static int process_my_load_module(struct ast_config *cfg)
214 struct ast_variable *var;
215 char *pgerror;
216 const char *tmp;
218 if (!(var = ast_variable_browse(cfg, "global")))
219 return 0;
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 */
226 if (!(pghostname = ast_strdup(tmp)))
227 return -1;
229 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
230 ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
231 tmp = "asteriskcdrdb";
234 if (!(pgdbname = ast_strdup(tmp)))
235 return -1;
237 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
238 ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
239 tmp = "asterisk";
242 if (!(pgdbuser = ast_strdup(tmp)))
243 return -1;
245 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
246 ast_log(LOG_WARNING,"PostgreSQL database password not specified. Assuming blank\n");
247 tmp = "";
250 if (!(pgpassword = ast_strdup(tmp)))
251 return -1;
253 if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
254 ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
255 tmp = "5432";
258 if (!(pgdbport = ast_strdup(tmp)))
259 return -1;
261 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
262 ast_log(LOG_WARNING,"CDR table not specified. Assuming cdr\n");
263 tmp = "cdr";
266 if (!(table = ast_strdup(tmp)))
267 return -1;
269 if (option_debug) {
270 if (ast_strlen_zero(pghostname))
271 ast_log(LOG_DEBUG, "cdr_pgsql: using default unix socket\n");
272 else
273 ast_log(LOG_DEBUG, "cdr_pgsql: got hostname of %s\n", pghostname);
274 ast_log(LOG_DEBUG, "cdr_pgsql: got port of %s\n", pgdbport);
275 ast_log(LOG_DEBUG, "cdr_pgsql: got user of %s\n", pgdbuser);
276 ast_log(LOG_DEBUG, "cdr_pgsql: got dbname of %s\n", pgdbname);
277 ast_log(LOG_DEBUG, "cdr_pgsql: got password of %s\n", pgpassword);
278 ast_log(LOG_DEBUG, "cdr_pgsql: got sql table name of %s\n", table);
281 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
282 if (PQstatus(conn) != CONNECTION_BAD) {
283 if (option_debug)
284 ast_log(LOG_DEBUG, "Successfully connected to PostgreSQL database.\n");
285 connected = 1;
286 } else {
287 pgerror = PQerrorMessage(conn);
288 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
289 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
290 connected = 0;
293 return ast_cdr_register(name, ast_module_info->description, pgsql_log);
296 static int my_load_module(void)
298 struct ast_config *cfg;
299 int res;
301 if (!(cfg = ast_config_load(config))) {
302 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
303 return AST_MODULE_LOAD_DECLINE;
306 res = process_my_load_module(cfg);
307 ast_config_destroy(cfg);
309 return res;
312 static int load_module(void)
314 return my_load_module();
317 static int unload_module(void)
319 return my_unload_module();
322 static int reload(void)
324 int res;
325 ast_mutex_lock(&pgsql_lock);
326 my_unload_module();
327 res = my_load_module();
328 ast_mutex_unlock(&pgsql_lock);
329 return res;
332 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",
333 .load = load_module,
334 .unload = unload_module,
335 .reload = reload,