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;
59 static int maxsize = 512, maxsize2 = 512;
61 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
63 static PGconn *conn = NULL;
69 unsigned int notnull:1;
70 unsigned int hasdefault:1;
71 AST_RWLIST_ENTRY(columns) list;
74 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
76 #define LENGTHEN_BUF1(size) \
78 /* Lengthen buffer, if necessary */ \
79 if (sql->used + size + 1 > sql->len) { \
80 if (ast_str_make_space(&sql, ((sql->len + size + 1) / 512 + 1) * 512) != 0) { \
81 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
84 AST_RWLIST_UNLOCK(&psql_columns); \
90 #define LENGTHEN_BUF2(size) \
92 if (sql2->used + size + 1 > sql2->len) { \
93 if (ast_str_make_space(&sql2, ((sql2->len + size + 3) / 512 + 1) * 512) != 0) { \
94 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
97 AST_RWLIST_UNLOCK(&psql_columns); \
103 static int pgsql_log(struct ast_cdr *cdr)
109 ast_mutex_lock(&pgsql_lock);
111 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
112 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
113 if (PQstatus(conn) != CONNECTION_BAD) {
116 pgerror = PQerrorMessage(conn);
117 ast_log(LOG_ERROR, "Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
118 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
126 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
127 char buf[257], escapebuf[513], *value;
139 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
140 ast_str_set(&sql2, 0, " VALUES (");
142 AST_RWLIST_RDLOCK(&psql_columns);
143 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
144 /* For fields not set, simply skip them */
145 ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
146 if (strcmp(cur->name, "calldate") == 0 && !value) {
147 ast_cdr_getvar(cdr, "start", &value, buf, sizeof(buf), 0, 0);
150 if (cur->notnull && !cur->hasdefault) {
151 /* Field is NOT NULL (but no default), must include it anyway */
152 LENGTHEN_BUF1(strlen(cur->name) + 2);
153 ast_str_append(&sql, 0, "\"%s\",", cur->name);
155 ast_str_append(&sql2, 0, "'',");
160 LENGTHEN_BUF1(strlen(cur->name) + 2);
161 ast_str_append(&sql, 0, "\"%s\",", cur->name);
163 if (strcmp(cur->name, "start") == 0 || strcmp(cur->name, "calldate") == 0) {
164 if (strncmp(cur->type, "int", 3) == 0) {
166 ast_str_append(&sql2, 0, "%ld", cdr->start.tv_sec);
167 } else if (strncmp(cur->type, "float", 5) == 0) {
169 ast_str_append(&sql2, 0, "%f", (double)cdr->start.tv_sec + (double)cdr->start.tv_usec / 1000000.0);
171 /* char, hopefully */
173 ast_localtime(&cdr->start, &tm, NULL);
174 ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
175 ast_str_append(&sql2, 0, "%s", buf);
177 } else if (strcmp(cur->name, "answer") == 0) {
178 if (strncmp(cur->type, "int", 3) == 0) {
180 ast_str_append(&sql2, 0, "%ld", cdr->answer.tv_sec);
181 } else if (strncmp(cur->type, "float", 5) == 0) {
183 ast_str_append(&sql2, 0, "%f", (double)cdr->answer.tv_sec + (double)cdr->answer.tv_usec / 1000000.0);
185 /* char, hopefully */
187 ast_localtime(&cdr->start, &tm, NULL);
188 ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
189 ast_str_append(&sql2, 0, "%s", buf);
191 } else if (strcmp(cur->name, "end") == 0) {
192 if (strncmp(cur->type, "int", 3) == 0) {
194 ast_str_append(&sql2, 0, "%ld", cdr->end.tv_sec);
195 } else if (strncmp(cur->type, "float", 5) == 0) {
197 ast_str_append(&sql2, 0, "%f", (double)cdr->end.tv_sec + (double)cdr->end.tv_usec / 1000000.0);
199 /* char, hopefully */
201 ast_localtime(&cdr->end, &tm, NULL);
202 ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
203 ast_str_append(&sql2, 0, "%s", buf);
205 } else if (strcmp(cur->name, "duration") == 0 || strcmp(cur->name, "billsec") == 0) {
206 if (cur->type[0] == 'i') {
207 /* Get integer, no need to escape anything */
208 ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
210 ast_str_append(&sql2, 0, "%s", value);
211 } else if (strncmp(cur->type, "float", 5) == 0) {
212 struct timeval *when = cur->name[0] == 'd' ? &cdr->start : &cdr->answer;
214 ast_str_append(&sql2, 0, "%f", (double)cdr->end.tv_sec - when->tv_sec + cdr->end.tv_usec / 1000000.0 - when->tv_usec / 1000000.0);
216 /* Char field, probably */
217 struct timeval *when = cur->name[0] == 'd' ? &cdr->start : &cdr->answer;
219 ast_str_append(&sql2, 0, "'%f'", (double)cdr->end.tv_sec - when->tv_sec + cdr->end.tv_usec / 1000000.0 - when->tv_usec / 1000000.0);
221 } else if (strcmp(cur->name, "disposition") == 0 || strcmp(cur->name, "amaflags") == 0) {
222 if (strncmp(cur->type, "int", 3) == 0) {
223 /* Integer, no need to escape anything */
224 ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 1);
226 ast_str_append(&sql2, 0, "%s", value);
228 /* Although this is a char field, there are no special characters in the values for these fields */
229 ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
231 ast_str_append(&sql2, 0, "'%s'", value);
234 /* Arbitrary field, could be anything */
235 ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
236 if (strncmp(cur->type, "int", 3) == 0) {
238 if (value && sscanf(value, "%lld", &whatever) == 1) {
240 ast_str_append(&sql2, 0, "%lld", whatever);
243 ast_str_append(&sql2, 0, "0");
245 } else if (strncmp(cur->type, "float", 5) == 0) {
246 long double whatever;
247 if (value && sscanf(value, "%Lf", &whatever) == 1) {
249 ast_str_append(&sql2, 0, "%30Lf", whatever);
252 ast_str_append(&sql2, 0, "0");
254 /* XXX Might want to handle dates, times, and other misc fields here XXX */
257 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
260 LENGTHEN_BUF2(strlen(escapebuf) + 2);
261 ast_str_append(&sql2, 0, "'%s'", escapebuf);
265 ast_str_append(&sql2, 0, ",");
267 AST_RWLIST_UNLOCK(&psql_columns);
268 LENGTHEN_BUF1(sql2->len);
269 sql->str[sql->used - 1] = ')';
270 sql2->str[sql2->used - 1] = ')';
271 ast_str_append(&sql, 0, "%s", sql2->str);
272 ast_verb(11, "[%s]\n", sql->str);
274 ast_debug(2, "inserting a CDR record.\n");
276 /* Test to be sure we're still connected... */
277 /* If we're connected, and connection is working, good. */
278 /* Otherwise, attempt reconnect. If it fails... sorry... */
279 if (PQstatus(conn) == CONNECTION_OK) {
282 ast_log(LOG_ERROR, "Connection was lost... attempting to reconnect.\n");
284 if (PQstatus(conn) == CONNECTION_OK) {
285 ast_log(LOG_ERROR, "Connection reestablished.\n");
288 pgerror = PQerrorMessage(conn);
289 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
290 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
294 ast_mutex_unlock(&pgsql_lock);
300 result = PQexec(conn, sql->str);
301 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
302 pgerror = PQresultErrorMessage(result);
303 ast_log(LOG_ERROR, "Failed to insert call detail record into database!\n");
304 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
305 ast_log(LOG_ERROR, "Connection may have been lost... attempting to reconnect.\n");
307 if (PQstatus(conn) == CONNECTION_OK) {
308 ast_log(LOG_ERROR, "Connection reestablished.\n");
311 result = PQexec(conn, sql->str);
312 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
313 pgerror = PQresultErrorMessage(result);
314 ast_log(LOG_ERROR, "HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
315 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
318 ast_mutex_unlock(&pgsql_lock);
328 ast_mutex_unlock(&pgsql_lock);
332 static int unload_module(void)
334 struct columns *current;
335 ast_cdr_unregister(name);
337 /* Give all threads time to finish */
342 ast_free(pghostname);
348 ast_free(pgpassword);
354 AST_RWLIST_WRLOCK(&psql_columns);
355 while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
358 AST_RWLIST_UNLOCK(&psql_columns);
363 static int config_module(int reload)
365 struct ast_variable *var;
370 struct ast_config *cfg;
371 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
373 if ((cfg = ast_config_load(config, config_flags)) == NULL) {
374 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
376 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
379 if (!(var = ast_variable_browse(cfg, "global"))) {
380 ast_config_destroy(cfg);
384 if (!(tmp = ast_variable_retrieve(cfg, "global", "hostname"))) {
385 ast_log(LOG_WARNING, "PostgreSQL server hostname not specified. Assuming unix socket connection\n");
386 tmp = ""; /* connect via UNIX-socket by default */
390 ast_free(pghostname);
391 if (!(pghostname = ast_strdup(tmp))) {
392 ast_config_destroy(cfg);
396 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
397 ast_log(LOG_WARNING, "PostgreSQL database not specified. Assuming asterisk\n");
398 tmp = "asteriskcdrdb";
403 if (!(pgdbname = ast_strdup(tmp))) {
404 ast_config_destroy(cfg);
408 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
409 ast_log(LOG_WARNING, "PostgreSQL database user not specified. Assuming asterisk\n");
415 if (!(pgdbuser = ast_strdup(tmp))) {
416 ast_config_destroy(cfg);
420 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
421 ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
426 ast_free(pgpassword);
427 if (!(pgpassword = ast_strdup(tmp))) {
428 ast_config_destroy(cfg);
432 if (!(tmp = ast_variable_retrieve(cfg, "global", "port"))) {
433 ast_log(LOG_WARNING, "PostgreSQL database port not specified. Using default 5432.\n");
439 if (!(pgdbport = ast_strdup(tmp))) {
440 ast_config_destroy(cfg);
444 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
445 ast_log(LOG_WARNING, "CDR table not specified. Assuming cdr\n");
451 if (!(table = ast_strdup(tmp))) {
452 ast_config_destroy(cfg);
457 if (ast_strlen_zero(pghostname)) {
458 ast_debug(1, "using default unix socket\n");
460 ast_debug(1, "got hostname of %s\n", pghostname);
462 ast_debug(1, "got port of %s\n", pgdbport);
463 ast_debug(1, "got user of %s\n", pgdbuser);
464 ast_debug(1, "got dbname of %s\n", pgdbname);
465 ast_debug(1, "got password of %s\n", pgpassword);
466 ast_debug(1, "got sql table name of %s\n", table);
469 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
470 if (PQstatus(conn) != CONNECTION_BAD) {
472 char *fname, *ftype, *flen, *fnotnull, *fdef;
474 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
477 /* Query the columns */
478 snprintf(sqlcmd, sizeof(sqlcmd), "select a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc from pg_class c, pg_type t, pg_attribute a left outer join pg_attrdef d on a.atthasdef and d.adrelid = a.attrelid and d.adnum = a.attnum where c.oid = a.attrelid and a.atttypid = t.oid and (a.attnum > 0) and c.relname = '%s' order by c.relname, attnum", table);
479 result = PQexec(conn, sqlcmd);
480 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
481 pgerror = PQresultErrorMessage(result);
482 ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
485 return AST_MODULE_LOAD_DECLINE;
488 rows = PQntuples(result);
489 for (i = 0; i < rows; i++) {
490 fname = PQgetvalue(result, i, 0);
491 ftype = PQgetvalue(result, i, 1);
492 flen = PQgetvalue(result, i, 2);
493 fnotnull = PQgetvalue(result, i, 3);
494 fdef = PQgetvalue(result, i, 4);
495 ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
496 cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
498 sscanf(flen, "%d", &cur->len);
499 cur->name = (char *)cur + sizeof(*cur);
500 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
501 strcpy(cur->name, fname);
502 strcpy(cur->type, ftype);
503 if (*fnotnull == 't') {
508 if (!ast_strlen_zero(fdef)) {
513 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
518 pgerror = PQerrorMessage(conn);
519 ast_log(LOG_ERROR, "Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
520 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
524 ast_config_destroy(cfg);
526 return ast_cdr_register(name, ast_module_info->description, pgsql_log);
529 static int load_module(void)
531 return config_module(0) ? AST_MODULE_LOAD_DECLINE : 0;
534 static int reload(void)
536 return config_module(1);
539 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",
541 .unload = unload_module,