Permit additional CDR columns to be saved in Postgres. Note that these
[asterisk/asterisk.git] / cdr / cdr_pgsql.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2003 - 2006
5  *
6  * Matthew D. Hardeman <mhardemn@papersoft.com> 
7  * Adapted from the MySQL CDR logger originally by James Sharp 
8  *
9  * Modified September 2003
10  * Matthew D. Hardeman <mhardemn@papersoft.com>
11  *
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.
17  *
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.
21  */
22
23 /*! \file
24  *
25  * \brief PostgreSQL CDR logger 
26  * 
27  * \author Matthew D. Hardeman <mhardemn@papersoft.com> 
28  * \extref PostgreSQL http://www.postgresql.org/
29  *
30  * See also
31  * \arg \ref Config_cdr
32  * \arg http://www.postgresql.org/
33  * \ingroup cdr_drivers
34  */
35
36 /*** MODULEINFO
37         <depend>pgsql</depend>
38  ***/
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43
44 #include <time.h>
45
46 #include <libpq-fe.h>
47
48 #include "asterisk/config.h"
49 #include "asterisk/channel.h"
50 #include "asterisk/cdr.h"
51 #include "asterisk/module.h"
52
53 #define DATE_FORMAT "'%Y-%m-%d %T'"
54
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;
60
61 AST_MUTEX_DEFINE_STATIC(pgsql_lock);
62
63 static PGconn   *conn = NULL;
64
65 struct columns {
66         char *name;
67         char *type;
68         int len;
69         AST_RWLIST_ENTRY(columns) list;
70 };
71
72 static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
73
74 #define LENGTHEN_BUF1(size)                                                                                                             \
75                         do {                                                                                                                            \
76                                 /* Lengthen buffer, if necessary */                                                             \
77                                 if ((newsize = lensql + (size) + 3) > sizesql) {        \
78                                         if ((tmp = ast_realloc(sql, (newsize / 512 + 1) * 512))) {      \
79                                                 sql = tmp;                                                                                              \
80                                                 sizesql = (newsize / 512 + 1) * 512;                                    \
81                                         } else {                                                                                                        \
82                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n"); \
83                                                 ast_free(sql);                                                                                  \
84                                                 ast_free(sql2);                                                                                 \
85                                                 AST_RWLIST_UNLOCK(&psql_columns);                                               \
86                                                 return -1;                                                                                              \
87                                         }                                                                                                                       \
88                                 }                                                                                                                               \
89                         } while (0)
90
91 #define LENGTHEN_BUF2(size)                                                                                                             \
92                         do {                                                                                                                            \
93                                 if ((newsize = lensql2 + (size) + 3) > sizesql2) {                              \
94                                         if ((tmp = ast_realloc(sql2, (newsize / 512 + 1) * 512))) {     \
95                                                 sql2 = tmp;                                                                                             \
96                                                 sizesql2 = (newsize / 512 + 1) * 512;                                   \
97                                         } else {                                                                                                        \
98                                                 ast_log(LOG_ERROR, "Unable to allocate sufficient memory.  Insert CDR failed.\n");      \
99                                                 ast_free(sql);                                                                                  \
100                                                 ast_free(sql2);                                                                                 \
101                                                 AST_RWLIST_UNLOCK(&psql_columns);                                               \
102                                                 return -1;                                                                                              \
103                                         }                                                                                                                       \
104                                 }                                                                                                                               \
105                         } while (0)
106
107 static int pgsql_log(struct ast_cdr *cdr)
108 {
109         struct ast_tm tm;
110         char *pgerror;
111         PGresult *result;
112
113         ast_mutex_lock(&pgsql_lock);
114
115         if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
116                 conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
117                 if (PQstatus(conn) != CONNECTION_BAD) {
118                         connected = 1;
119                 } else {
120                         pgerror = PQerrorMessage(conn);
121                         ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
122                         ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
123                         PQfinish(conn);
124                         conn = NULL;
125                 }
126         }
127
128         if (connected) {
129                 struct columns *cur;
130                 int lensql, lensql2, sizesql = maxsize, sizesql2 = maxsize2, newsize;
131                 char *sql = ast_calloc(sizeof(char), sizesql), *sql2 = ast_calloc(sizeof(char), sizesql2), *tmp, *value;
132                 char buf[257], escapebuf[513];
133   
134                 lensql = snprintf(sql, sizesql, "INSERT INTO %s (", table);
135                 lensql2 = snprintf(sql2, sizesql2, " VALUES (");
136   
137                 AST_RWLIST_RDLOCK(&psql_columns);
138                 AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
139                         /* For fields not set, simply skip them */
140                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
141                         if (!value)
142                                 continue;
143                         
144                         LENGTHEN_BUF1(strlen(cur->name));
145                         lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", cur->name);
146
147                         if (strcmp(cur->name, "start") == 0 || strcmp(cur->name, "calldate") == 0) {
148                                 if (strncmp(cur->type, "int", 3) == 0) {
149                                         LENGTHEN_BUF2(12);
150                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%ld", cdr->start.tv_sec);
151                                 } else if (strncmp(cur->type, "float", 5) == 0) {
152                                         LENGTHEN_BUF2(30);
153                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%f", (double)cdr->start.tv_sec + (double)cdr->start.tv_usec / 1000000.0);
154                                 } else {
155                                         /* char, hopefully */
156                                         LENGTHEN_BUF2(30);
157                                         ast_localtime(&cdr->start, &tm, NULL);
158                                         lensql2 += ast_strftime(sql2 + lensql2, sizesql2 - lensql2, DATE_FORMAT, &tm);
159                                 }
160                         } else if (strcmp(cur->name, "answer") == 0) {
161                                 if (strncmp(cur->type, "int", 3) == 0) {
162                                         LENGTHEN_BUF2(12);
163                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%ld", cdr->answer.tv_sec);
164                                 } else if (strncmp(cur->type, "float", 5) == 0) {
165                                         LENGTHEN_BUF2(30);
166                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%f", (double)cdr->answer.tv_sec + (double)cdr->answer.tv_usec / 1000000.0);
167                                 } else {
168                                         /* char, hopefully */
169                                         LENGTHEN_BUF2(30);
170                                         ast_localtime(&cdr->start, &tm, NULL);
171                                         lensql2 += ast_strftime(sql2 + lensql2, sizesql2 - lensql2, DATE_FORMAT, &tm);
172                                 }
173                         } else if (strcmp(cur->name, "end") == 0) {
174                                 if (strncmp(cur->type, "int", 3) == 0) {
175                                         LENGTHEN_BUF2(12);
176                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%ld", cdr->end.tv_sec);
177                                 } else if (strncmp(cur->type, "float", 5) == 0) {
178                                         LENGTHEN_BUF2(30);
179                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%f", (double)cdr->end.tv_sec + (double)cdr->end.tv_usec / 1000000.0);
180                                 } else {
181                                         /* char, hopefully */
182                                         LENGTHEN_BUF2(30);
183                                         ast_localtime(&cdr->end, &tm, NULL);
184                                         lensql2 += ast_strftime(sql2 + lensql2, sizesql2 - lensql2, DATE_FORMAT, &tm);
185                                 }
186                         } else if (strcmp(cur->name, "duration") == 0 || strcmp(cur->name, "billsec") == 0) {
187                                 if (cur->type[0] == 'i') {
188                                         /* Get integer, no need to escape anything */
189                                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
190                                         LENGTHEN_BUF2(12);
191                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%s", value);
192                                 } else if (strncmp(cur->type, "float", 5) == 0) {
193                                         struct timeval *tv = cur->name[0] == 'd' ? &cdr->start : &cdr->answer;
194                                         LENGTHEN_BUF2(30);
195                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%f", (double)cdr->end.tv_sec - tv->tv_sec + cdr->end.tv_usec / 1000000.0 - tv->tv_usec / 1000000.0);
196                                 } else {
197                                         /* Char field, probably */
198                                         struct timeval *tv = cur->name[0] == 'd' ? &cdr->start : &cdr->answer;
199                                         LENGTHEN_BUF2(30);
200                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%f'", (double)cdr->end.tv_sec - tv->tv_sec + cdr->end.tv_usec / 1000000.0 - tv->tv_usec / 1000000.0);
201                                 }
202                         } else if (strcmp(cur->name, "disposition") == 0 || strcmp(cur->name, "amaflags") == 0) {
203                                 if (strncmp(cur->type, "int", 3) == 0) {
204                                         /* Integer, no need to escape anything */
205                                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 1);
206                                         LENGTHEN_BUF2(12);
207                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%s", value);
208                                 } else {
209                                         /* Although this is a char field, there are no special characters in the values for these fields */
210                                         ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
211                                         LENGTHEN_BUF2(30);
212                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%s'", value);
213                                 }
214                         } else {
215                                 /* Arbitrary field, could be anything */
216                                 ast_cdr_getvar(cdr, cur->name, &value, buf, sizeof(buf), 0, 0);
217                                 if (strncmp(cur->type, "int", 3) == 0) {
218                                         long long whatever;
219                                         if (value && sscanf(value, "%lld", &whatever) == 1) {
220                                                 LENGTHEN_BUF2(25);
221                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lld", whatever);
222                                         } else {
223                                                 LENGTHEN_BUF2(1);
224                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "0");
225                                         }
226                                 } else if (strncmp(cur->type, "float", 5) == 0) {
227                                         long double whatever;
228                                         if (value && sscanf(value, "%Lf", &whatever) == 1) {
229                                                 LENGTHEN_BUF2(50);
230                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%30Lf", whatever);
231                                         } else {
232                                                 LENGTHEN_BUF2(1);
233                                                 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "0");
234                                         }
235                                 /* XXX Might want to handle dates, times, and other misc fields here XXX */
236                                 } else {
237                                         if (value)
238                                                 PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
239                                         else
240                                                 escapebuf[0] = '\0';
241                                         LENGTHEN_BUF2(strlen(escapebuf) + 2);
242                                         lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%s'", escapebuf);
243                                 }
244                         }
245                         LENGTHEN_BUF2(1);
246                         strcat(sql2 + lensql2, ",");
247                         lensql2++;
248                 }
249                 AST_RWLIST_UNLOCK(&psql_columns);
250                 LENGTHEN_BUF1(lensql2);
251                 sql[lensql - 1] = ')';
252                 sql2[lensql2 - 1] = ')';
253                 strcat(sql + lensql, sql2);
254                 ast_verb(11, "[%s]\n", sql);
255
256                 ast_debug(2, "cdr_pgsql: inserting a CDR record.\n");
257
258                 /* Test to be sure we're still connected... */
259                 /* If we're connected, and connection is working, good. */
260                 /* Otherwise, attempt reconnect.  If it fails... sorry... */
261                 if (PQstatus(conn) == CONNECTION_OK) {
262                         connected = 1;
263                 } else {
264                         ast_log(LOG_ERROR, "cdr_pgsql: Connection was lost... attempting to reconnect.\n");
265                         PQreset(conn);
266                         if (PQstatus(conn) == CONNECTION_OK) {
267                                 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
268                                 connected = 1;
269                         } else {
270                                 pgerror = PQerrorMessage(conn);
271                                 ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
272                                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
273                                 PQfinish(conn);
274                                 conn = NULL;
275                                 connected = 0;
276                                 ast_mutex_unlock(&pgsql_lock);
277                                 return -1;
278                         }
279                 }
280                 result = PQexec(conn, sql);
281                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
282                         pgerror = PQresultErrorMessage(result);
283                         ast_log(LOG_ERROR,"cdr_pgsql: Failed to insert call detail record into database!\n");
284                         ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
285                         ast_log(LOG_ERROR,"cdr_pgsql: Connection may have been lost... attempting to reconnect.\n");
286                         PQreset(conn);
287                         if (PQstatus(conn) == CONNECTION_OK) {
288                                 ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
289                                 connected = 1;
290                                 PQclear(result);
291                                 result = PQexec(conn, sql);
292                                 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
293                                         pgerror = PQresultErrorMessage(result);
294                                         ast_log(LOG_ERROR,"cdr_pgsql: HARD ERROR!  Attempted reconnection failed.  DROPPING CALL RECORD!\n");
295                                         ast_log(LOG_ERROR,"cdr_pgsql: Reason: %s\n", pgerror);
296                                 }
297                         }
298                         ast_mutex_unlock(&pgsql_lock);
299                         PQclear(result);
300                         return -1;
301                 }
302                 PQclear(result);
303         }
304         ast_mutex_unlock(&pgsql_lock);
305         return 0;
306 }
307
308 static int unload_module(void)
309 {
310         struct columns *cur;
311         ast_cdr_unregister(name);
312
313         /* Give all threads time to finish */
314         usleep(1);
315         PQfinish(conn);
316
317         if (pghostname)
318                 ast_free(pghostname);
319         if (pgdbname)
320                 ast_free(pgdbname);
321         if (pgdbuser)
322                 ast_free(pgdbuser);
323         if (pgpassword)
324                 ast_free(pgpassword);
325         if (pgdbport)
326                 ast_free(pgdbport);
327         if (table)
328                 ast_free(table);
329
330         AST_RWLIST_WRLOCK(&psql_columns);
331         while ((cur = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
332                 ast_free(cur);
333         }
334         AST_RWLIST_UNLOCK(&psql_columns);
335
336         return 0;
337 }
338
339 static int config_module(int reload)
340 {
341         struct ast_variable *var;
342         char *pgerror;
343         struct columns *cur;
344         PGresult *result;
345         const char *tmp;
346         struct ast_config *cfg;
347         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
348
349         if ((cfg = ast_config_load(config, config_flags)) == NULL) {
350                 ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CDR's: %s\n", config);
351                 return -1;
352         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
353                 return 0;
354
355         if (!(var = ast_variable_browse(cfg, "global"))) {
356                 ast_config_destroy(cfg);
357                 return 0;
358         }
359
360         if (!(tmp = ast_variable_retrieve(cfg, "global", "hostname"))) {
361                 ast_log(LOG_WARNING, "PostgreSQL server hostname not specified.  Assuming unix socket connection\n");
362                 tmp = "";       /* connect via UNIX-socket by default */
363         }
364
365         if (pghostname)
366                 ast_free(pghostname);
367         if (!(pghostname = ast_strdup(tmp))) {
368                 ast_config_destroy(cfg);
369                 return -1;
370         }
371
372         if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
373                 ast_log(LOG_WARNING,"PostgreSQL database not specified.  Assuming asterisk\n");
374                 tmp = "asteriskcdrdb";
375         }
376
377         if (pgdbname)
378                 ast_free(pgdbname);
379         if (!(pgdbname = ast_strdup(tmp))) {
380                 ast_config_destroy(cfg);
381                 return -1;
382         }
383
384         if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
385                 ast_log(LOG_WARNING,"PostgreSQL database user not specified.  Assuming asterisk\n");
386                 tmp = "asterisk";
387         }
388
389         if (pgdbuser)
390                 ast_free(pgdbuser);
391         if (!(pgdbuser = ast_strdup(tmp))) {
392                 ast_config_destroy(cfg);
393                 return -1;
394         }
395
396         if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
397                 ast_log(LOG_WARNING,"PostgreSQL database password not specified.  Assuming blank\n");
398                 tmp = "";
399         }
400
401         if (pgpassword)
402                 ast_free(pgpassword);
403         if (!(pgpassword = ast_strdup(tmp))) {
404                 ast_config_destroy(cfg);
405                 return -1;
406         }
407
408         if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
409                 ast_log(LOG_WARNING,"PostgreSQL database port not specified.  Using default 5432.\n");
410                 tmp = "5432";
411         }
412
413         if (pgdbport)
414                 ast_free(pgdbport);
415         if (!(pgdbport = ast_strdup(tmp))) {
416                 ast_config_destroy(cfg);
417                 return -1;
418         }
419
420         if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
421                 ast_log(LOG_WARNING,"CDR table not specified.  Assuming cdr\n");
422                 tmp = "cdr";
423         }
424
425         if (table)
426                 ast_free(table);
427         if (!(table = ast_strdup(tmp))) {
428                 ast_config_destroy(cfg);
429                 return -1;
430         }
431
432         if (option_debug) {
433                 if (ast_strlen_zero(pghostname))
434                         ast_debug(1, "cdr_pgsql: using default unix socket\n");
435                 else
436                         ast_debug(1, "cdr_pgsql: got hostname of %s\n", pghostname);
437                         ast_debug(1, "cdr_pgsql: got port of %s\n", pgdbport);
438                         ast_debug(1, "cdr_pgsql: got user of %s\n", pgdbuser);
439                         ast_debug(1, "cdr_pgsql: got dbname of %s\n", pgdbname);
440                         ast_debug(1, "cdr_pgsql: got password of %s\n", pgpassword);
441                         ast_debug(1, "cdr_pgsql: got sql table name of %s\n", table);
442         }
443         
444         conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
445         if (PQstatus(conn) != CONNECTION_BAD) {
446                 char sqlcmd[256];
447                 char *fname, *ftype, *flen;
448                 int i, rows;
449                 ast_debug(1, "Successfully connected to PostgreSQL database.\n");
450                 connected = 1;
451
452                 /* Query the columns */
453                 snprintf(sqlcmd, sizeof(sqlcmd), "select a.attname, t.typname, a.attlen from pg_class c, pg_attribute a, pg_type t where c.oid = a.attrelid and a.atttypid = t.oid and (a.attnum > 0) and c.relname = '%s' order by c.relname, attnum", table);
454                 result = PQexec(conn, sqlcmd);
455                 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
456                         pgerror = PQresultErrorMessage(result);
457                         ast_log(LOG_ERROR, "cdr_pgsql: Failed to query database columns: %s\n", pgerror);
458                         PQclear(result);
459                         unload_module();
460                         return AST_MODULE_LOAD_DECLINE;
461                 }
462
463                 rows = PQntuples(result);
464                 for (i = 0; i < rows; i++) {
465                         fname = PQgetvalue(result, i, 0);
466                         ftype = PQgetvalue(result, i, 1);
467                         flen = PQgetvalue(result, i, 2);
468                         ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
469                         cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
470                         if (cur) {
471                                 sscanf(flen, "%d", &cur->len);
472                                 cur->name = (char *)cur + sizeof(*cur);
473                                 cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
474                                 strcpy(cur->name, fname);
475                                 strcpy(cur->type, ftype);
476                                 AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
477                         }
478                 }
479                 PQclear(result);
480         } else {
481                 pgerror = PQerrorMessage(conn);
482                 ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  CALLS WILL NOT BE LOGGED!!\n", pghostname);
483                 ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
484                 connected = 0;
485         }
486
487         ast_config_destroy(cfg);
488
489         return ast_cdr_register(name, ast_module_info->description, pgsql_log);
490 }
491
492 static int load_module(void)
493 {
494         return config_module(0) ? AST_MODULE_LOAD_DECLINE : 0;
495 }
496
497 static int reload(void)
498 {
499         return config_module(1);
500 }
501
502 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",
503                 .load = load_module,
504                 .unload = unload_module,
505                 .reload = reload,
506                );