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 * \extref PostgreSQL http://www.postgresql.org/
33 * \ingroup cdr_drivers
37 <depend>pgsql</depend>
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
46 #include "asterisk/config.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/cdr.h"
49 #include "asterisk/module.h"
51 #define DATE_FORMAT "'%Y-%m-%d %T'"
53 static const char name[] = "pgsql";
54 static const char config[] = "cdr_pgsql.conf";
55 static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
56 static int connected = 0;
57 static int maxsize = 512, maxsize2 = 512;
59 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
61 static PGconn *conn = NULL;
67 unsigned int notnull:1;
68 unsigned int hasdefault:1;
69 AST_RWLIST_ENTRY(columns) list;
72 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
74 #define LENGTHEN_BUF1(size) \
76 /* Lengthen buffer, if necessary */ \
77 if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
78 if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
79 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
82 AST_RWLIST_UNLOCK(&psql_columns); \
88 #define LENGTHEN_BUF2(size) \
90 if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
91 if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
92 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
95 AST_RWLIST_UNLOCK(&psql_columns); \
101 static int pgsql_log(struct ast_cdr *cdr)
107 ast_mutex_lock(&pgsql_lock);
109 if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
110 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
111 if (PQstatus(conn) != CONNECTION_BAD) {
114 pgerror = PQerrorMessage(conn);
115 ast_log(LOG_ERROR, "Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
116 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
124 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
125 char buf[257], escapebuf[513], *value;
138 ast_str_set(&sql, 0, "INSERT INTO %s (", table);
139 ast_str_set(&sql2, 0, " VALUES (");
141 AST_RWLIST_RDLOCK(&psql_columns);
142 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
143 /* For fields not set, simply skip them */
144 ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
145 if (strcmp(cur->name, "calldate") == 0 && !value) {
146 ast_cdr_getvar(cdr, "start", &value, buf, sizeof(buf), 0, 0);
149 if (cur->notnull && !cur->hasdefault) {
150 /* Field is NOT NULL (but no default), must include it anyway */
151 LENGTHEN_BUF1(strlen(cur->name) + 2);
152 ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
154 ast_str_append(&sql2, 0, "%s''", first ? "" : ",");
160 LENGTHEN_BUF1(strlen(cur->name) + 2);
161 ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", 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, "%s%ld", first ? "" : ",", (long) cdr->start.tv_sec);
167 } else if (strncmp(cur->type, "float", 5) == 0) {
169 ast_str_append(&sql2, 0, "%s%f", first ? "" : ",", (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%s", first ? "" : ",", buf);
177 } else if (strcmp(cur->name, "answer") == 0) {
178 if (strncmp(cur->type, "int", 3) == 0) {
180 ast_str_append(&sql2, 0, "%s%ld", first ? "" : ",", (long) cdr->answer.tv_sec);
181 } else if (strncmp(cur->type, "float", 5) == 0) {
183 ast_str_append(&sql2, 0, "%s%f", first ? "" : ",", (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%s", first ? "" : ",", buf);
191 } else if (strcmp(cur->name, "end") == 0) {
192 if (strncmp(cur->type, "int", 3) == 0) {
194 ast_str_append(&sql2, 0, "%s%ld", first ? "" : ",", (long) cdr->end.tv_sec);
195 } else if (strncmp(cur->type, "float", 5) == 0) {
197 ast_str_append(&sql2, 0, "%s%f", first ? "" : ",", (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%s", first ? "" : ",", 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%s", first ? "" : ",", 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, "%s%f", first ? "" : ",", (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, "%s'%f'", first ? "" : ",", (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%s", first ? "" : ",", 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'%s'", first ? "" : ",", 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, "%30lld", &whatever) == 1) {
240 ast_str_append(&sql2, 0, "%s%lld", first ? "" : ",", whatever);
243 ast_str_append(&sql2, 0, "%s0", first ? "" : ",");
245 } else if (strncmp(cur->type, "float", 5) == 0) {
246 long double whatever;
247 if (value && sscanf(value, "%30Lf", &whatever) == 1) {
249 ast_str_append(&sql2, 0, "%s%30Lf", first ? "" : ",", whatever);
252 ast_str_append(&sql2, 0, "%s0", first ? "" : ",");
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) + 3);
261 ast_str_append(&sql2, 0, "%s'%s'", first ? "" : ",", escapebuf);
266 AST_RWLIST_UNLOCK(&psql_columns);
267 LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
268 ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
269 ast_verb(11, "[%s]\n", ast_str_buffer(sql));
271 ast_debug(2, "inserting a CDR record.\n");
273 /* Test to be sure we're still connected... */
274 /* If we're connected, and connection is working, good. */
275 /* Otherwise, attempt reconnect. If it fails... sorry... */
276 if (PQstatus(conn) == CONNECTION_OK) {
279 ast_log(LOG_ERROR, "Connection was lost... attempting to reconnect.\n");
281 if (PQstatus(conn) == CONNECTION_OK) {
282 ast_log(LOG_ERROR, "Connection reestablished.\n");
285 pgerror = PQerrorMessage(conn);
286 ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
287 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
291 ast_mutex_unlock(&pgsql_lock);
297 result = PQexec(conn, ast_str_buffer(sql));
298 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
299 pgerror = PQresultErrorMessage(result);
300 ast_log(LOG_ERROR, "Failed to insert call detail record into database!\n");
301 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
302 ast_log(LOG_ERROR, "Connection may have been lost... attempting to reconnect.\n");
304 if (PQstatus(conn) == CONNECTION_OK) {
305 ast_log(LOG_ERROR, "Connection reestablished.\n");
308 result = PQexec(conn, ast_str_buffer(sql));
309 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
310 pgerror = PQresultErrorMessage(result);
311 ast_log(LOG_ERROR, "HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
312 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
315 ast_mutex_unlock(&pgsql_lock);
325 ast_mutex_unlock(&pgsql_lock);
329 static int unload_module(void)
331 struct columns *current;
333 ast_cdr_unregister(name);
338 ast_free(pghostname);
344 ast_free(pgpassword);
350 AST_RWLIST_WRLOCK(&psql_columns);
351 while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
354 AST_RWLIST_UNLOCK(&psql_columns);
359 static int config_module(int reload)
361 struct ast_variable *var;
366 struct ast_config *cfg;
367 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
369 if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
370 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
372 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
375 if (!(var = ast_variable_browse(cfg, "global"))) {
376 ast_config_destroy(cfg);
380 if (!(tmp = ast_variable_retrieve(cfg, "global", "hostname"))) {
381 ast_log(LOG_WARNING, "PostgreSQL server hostname not specified. Assuming unix socket connection\n");
382 tmp = ""; /* connect via UNIX-socket by default */
386 ast_free(pghostname);
387 if (!(pghostname = ast_strdup(tmp))) {
388 ast_config_destroy(cfg);
392 if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
393 ast_log(LOG_WARNING, "PostgreSQL database not specified. Assuming asterisk\n");
394 tmp = "asteriskcdrdb";
399 if (!(pgdbname = ast_strdup(tmp))) {
400 ast_config_destroy(cfg);
404 if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
405 ast_log(LOG_WARNING, "PostgreSQL database user not specified. Assuming asterisk\n");
411 if (!(pgdbuser = ast_strdup(tmp))) {
412 ast_config_destroy(cfg);
416 if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
417 ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
422 ast_free(pgpassword);
423 if (!(pgpassword = ast_strdup(tmp))) {
424 ast_config_destroy(cfg);
428 if (!(tmp = ast_variable_retrieve(cfg, "global", "port"))) {
429 ast_log(LOG_WARNING, "PostgreSQL database port not specified. Using default 5432.\n");
435 if (!(pgdbport = ast_strdup(tmp))) {
436 ast_config_destroy(cfg);
440 if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
441 ast_log(LOG_WARNING, "CDR table not specified. Assuming cdr\n");
447 if (!(table = ast_strdup(tmp))) {
448 ast_config_destroy(cfg);
453 if (ast_strlen_zero(pghostname)) {
454 ast_debug(1, "using default unix socket\n");
456 ast_debug(1, "got hostname of %s\n", pghostname);
458 ast_debug(1, "got port of %s\n", pgdbport);
459 ast_debug(1, "got user of %s\n", pgdbuser);
460 ast_debug(1, "got dbname of %s\n", pgdbname);
461 ast_debug(1, "got password of %s\n", pgpassword);
462 ast_debug(1, "got sql table name of %s\n", table);
465 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
466 if (PQstatus(conn) != CONNECTION_BAD) {
468 char *fname, *ftype, *flen, *fnotnull, *fdef;
469 int i, rows, version;
470 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
472 version = PQserverVersion(conn);
474 if (version >= 70300) {
475 char *schemaname, *tablename;
476 if (strchr(table, '.')) {
477 schemaname = ast_strdupa(table);
478 tablename = strchr(schemaname, '.');
485 /* Escape special characters in schemaname */
486 if (strchr(schemaname, '\\') || strchr(schemaname, '\'')) {
487 char *tmp = schemaname, *ptr;
489 ptr = schemaname = alloca(strlen(tmp) * 2 + 1);
490 for (; *tmp; tmp++) {
491 if (strchr("\\'", *tmp)) {
498 /* Escape special characters in tablename */
499 if (strchr(tablename, '\\') || strchr(tablename, '\'')) {
500 char *tmp = tablename, *ptr;
502 ptr = tablename = alloca(strlen(tmp) * 2 + 1);
503 for (; *tmp; tmp++) {
504 if (strchr("\\'", *tmp)) {
512 snprintf(sqlcmd, sizeof(sqlcmd), "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod FROM (((pg_catalog.pg_class c INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace AND c.relname = '%s' AND n.nspname = %s%s%s) INNER JOIN pg_catalog.pg_attribute a ON (NOT a.attisdropped) AND a.attnum > 0 AND a.attrelid = c.oid) INNER JOIN pg_catalog.pg_type t ON t.oid = a.atttypid) LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum ORDER BY n.nspname, c.relname, attnum",
514 ast_strlen_zero(schemaname) ? "" : "'", ast_strlen_zero(schemaname) ? "current_schema()" : schemaname, ast_strlen_zero(schemaname) ? "" : "'");
516 snprintf(sqlcmd, sizeof(sqlcmd), "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod 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);
518 /* Query the columns */
519 result = PQexec(conn, sqlcmd);
520 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
521 pgerror = PQresultErrorMessage(result);
522 ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
525 return AST_MODULE_LOAD_DECLINE;
528 rows = PQntuples(result);
529 for (i = 0; i < rows; i++) {
530 fname = PQgetvalue(result, i, 0);
531 ftype = PQgetvalue(result, i, 1);
532 flen = PQgetvalue(result, i, 2);
533 fnotnull = PQgetvalue(result, i, 3);
534 fdef = PQgetvalue(result, i, 4);
535 if (atoi(flen) == -1) {
536 /* For varchar columns, the maximum length is encoded in a different field */
537 flen = PQgetvalue(result, i, 5);
539 ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
540 cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
542 sscanf(flen, "%30d", &cur->len);
543 cur->name = (char *)cur + sizeof(*cur);
544 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
545 strcpy(cur->name, fname);
546 strcpy(cur->type, ftype);
547 if (*fnotnull == 't') {
552 if (!ast_strlen_zero(fdef)) {
557 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
562 pgerror = PQerrorMessage(conn);
563 ast_log(LOG_ERROR, "Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
564 ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
568 ast_config_destroy(cfg);
570 return ast_cdr_register(name, ast_module_info->description, pgsql_log);
573 static int load_module(void)
575 return config_module(0) ? AST_MODULE_LOAD_DECLINE : 0;
578 static int reload(void)
580 return config_module(1);
583 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",
585 .unload = unload_module,