2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Tilghman Lesher
6 * Tilghman Lesher <cdr_adaptive_odbc__v1@the-tilghman.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Adaptive ODBC CDR backend
23 * \author Tilghman Lesher <cdr_adaptive_odbc__v1@the-tilghman.com>
24 * \ingroup cdr_drivers
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <sys/types.h>
44 #include "asterisk/config.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/lock.h"
47 #include "asterisk/linkedlists.h"
48 #include "asterisk/res_odbc.h"
49 #include "asterisk/cdr.h"
50 #include "asterisk/module.h"
52 #define CONFIG "cdr_adaptive_odbc.conf"
54 static char *name = "Adaptive ODBC";
55 /* Optimization to reduce number of memory allocations */
56 static int maxsize = 512, maxsize2 = 512;
69 AST_LIST_ENTRY(columns) list;
75 unsigned int usegmtime:1;
76 AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
77 AST_RWLIST_ENTRY(tables) list;
80 static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
82 static int load_config(void)
84 struct ast_config *cfg;
85 struct ast_variable *var;
86 const char *tmp, *catg;
87 struct tables *tableptr;
88 struct columns *entry;
93 int lenconnection, lentable, usegmtime = 0;
97 struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
99 cfg = ast_config_load(CONFIG, config_flags);
100 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
101 ast_log(LOG_WARNING, "Unable to load " CONFIG ". No adaptive ODBC CDRs.\n");
105 for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
106 var = ast_variable_browse(cfg, catg);
110 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
111 ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg);
114 ast_copy_string(connection, tmp, sizeof(connection));
115 lenconnection = strlen(connection);
117 if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "usegmtime"))) {
118 usegmtime = ast_true(tmp);
121 /* When loading, we want to be sure we can connect. */
122 obj = ast_odbc_request_obj(connection, 1);
124 ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg);
128 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
129 ast_log(LOG_NOTICE, "No table name found. Assuming 'cdr'.\n");
132 ast_copy_string(table, tmp, sizeof(table));
133 lentable = strlen(table);
135 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
136 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
137 ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
138 ast_odbc_release_obj(obj);
142 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
143 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
144 ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection);
145 ast_odbc_release_obj(obj);
149 tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
151 ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
152 ast_odbc_release_obj(obj);
157 tableptr->usegmtime = usegmtime;
158 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
159 tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
160 ast_copy_string(tableptr->connection, connection, lenconnection + 1);
161 ast_copy_string(tableptr->table, table, lentable + 1);
163 ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
165 /* Check for filters first */
166 for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
167 if (strncmp(var->name, "filter", 6) == 0) {
168 char *cdrvar = ast_strdupa(var->name + 6);
169 cdrvar = ast_strip(cdrvar);
170 ast_verb(3, "Found filter %s for cdr variable %s in %s@%s\n", var->value, cdrvar, tableptr->table, tableptr->connection);
172 entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(cdrvar) + 1 + strlen(var->value) + 1);
174 ast_log(LOG_ERROR, "Out of memory creating filter entry for CDR variable '%s' in table '%s' on connection '%s'\n", cdrvar, table, connection);
179 /* NULL column entry means this isn't a column in the database */
181 entry->cdrname = (char *)entry + sizeof(*entry);
182 entry->filtervalue = (char *)entry + sizeof(*entry) + strlen(cdrvar) + 1;
183 strcpy(entry->cdrname, cdrvar);
184 strcpy(entry->filtervalue, var->value);
186 AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
190 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
191 char *cdrvar = "", *staticvalue = "";
193 SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
195 /* Is there an alias for this column? */
197 /* NOTE: This seems like a non-optimal parse method, but I'm going
198 * for user configuration readability, rather than fast parsing. We
199 * really don't parse this file all that often, anyway.
201 for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
202 if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, columnname) == 0) {
203 char *alias = ast_strdupa(var->name + 5);
204 cdrvar = ast_strip(alias);
205 ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
207 } else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, columnname) == 0) {
208 char *item = ast_strdupa(var->name + 6);
209 item = ast_strip(item);
210 if (item[0] == '"' && item[strlen(item) - 1] == '"') {
211 /* Remove surrounding quotes */
212 item[strlen(item) - 1] = '\0';
219 entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1 + strlen(staticvalue) + 1);
221 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
225 entry->name = (char *)entry + sizeof(*entry);
226 strcpy(entry->name, columnname);
228 if (!ast_strlen_zero(cdrvar)) {
229 entry->cdrname = entry->name + strlen(columnname) + 1;
230 strcpy(entry->cdrname, cdrvar);
231 } else { /* Point to same place as the column name */
232 entry->cdrname = (char *)entry + sizeof(*entry);
235 if (!ast_strlen_zero(staticvalue)) {
236 entry->staticvalue = entry->cdrname + strlen(entry->cdrname) + 1;
237 strcpy(entry->staticvalue, staticvalue);
240 SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
241 SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
242 SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
243 SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
244 SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
245 SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
247 /* Specification states that the octenlen should be the maximum number of bytes
248 * returned in a char or binary column, but it seems that some drivers just set
249 * it to NULL. (Bad Postgres! No biscuit!) */
250 if (entry->octetlen == 0)
251 entry->octetlen = entry->size;
253 ast_verb(10, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, (long) entry->size, (long) entry->octetlen, entry->decimals, entry->radix);
254 /* Insert column info into column list */
255 AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
259 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
260 ast_odbc_release_obj(obj);
262 if (AST_LIST_FIRST(&(tableptr->columns)))
263 AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
270 static int free_config(void)
272 struct tables *table;
273 struct columns *entry;
274 while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
275 while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
283 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
288 SQLINTEGER nativeerror = 0, numfields = 0;
289 SQLSMALLINT diagbytes = 0;
290 unsigned char state[10], diagnostic[256];
292 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
293 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
294 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
298 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
299 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
300 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
301 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
302 for (i = 0; i < numfields; i++) {
303 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
304 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
306 ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
310 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
317 #define LENGTHEN_BUF1(size) \
319 /* Lengthen buffer, if necessary */ \
320 if (sql->used + size + 1 > sql->len) { \
321 if (ast_str_make_space(&sql, ((sql->len + size + 1) / 512 + 1) * 512) != 0) { \
322 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
325 AST_RWLIST_UNLOCK(&odbc_tables); \
331 #define LENGTHEN_BUF2(size) \
333 if (sql2->used + size + 1 > sql2->len) { \
334 if (ast_str_make_space(&sql2, ((sql2->len + size + 3) / 512 + 1) * 512) != 0) { \
335 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
338 AST_RWLIST_UNLOCK(&odbc_tables); \
344 static int odbc_log(struct ast_cdr *cdr)
346 struct tables *tableptr;
347 struct columns *entry;
348 struct odbc_obj *obj;
349 struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
351 char colbuf[1024], *colptr;
352 SQLHSTMT stmt = NULL;
363 if (AST_RWLIST_RDLOCK(&odbc_tables)) {
364 ast_log(LOG_ERROR, "Unable to lock table list. Insert CDR(s) failed.\n");
370 AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
371 ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table);
372 ast_str_set(&sql2, 0, " VALUES (");
374 /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
375 if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) {
376 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, sql->str);
380 AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
382 if (strcasecmp(entry->cdrname, "start") == 0) {
384 } else if (strcasecmp(entry->cdrname, "answer") == 0) {
386 } else if (strcasecmp(entry->cdrname, "end") == 0) {
390 /* Check if we have a similarly named variable */
391 if (entry->staticvalue) {
392 colptr = ast_strdupa(entry->staticvalue);
393 } else if (datefield && tableptr->usegmtime) {
394 struct timeval date_tv = (datefield == 1) ? cdr->start : (datefield == 2) ? cdr->answer : cdr->end;
395 struct ast_tm tm = { 0, };
396 ast_localtime(&date_tv, &tm, "UTC");
397 ast_strftime(colbuf, sizeof(colbuf), "%Y-%m-%d %H:%M:%S", &tm);
400 ast_cdr_getvar(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), 0, datefield ? 0 : 1);
404 /* Check first if the column filters this entry. Note that this
405 * is very specifically NOT ast_strlen_zero(), because the filter
406 * could legitimately specify that the field is blank, which is
407 * different from the field being unspecified (NULL). */
408 if (entry->filtervalue && strcasecmp(colptr, entry->filtervalue) != 0) {
409 ast_verb(4, "CDR column '%s' with value '%s' does not match filter of"
410 " '%s'. Cancelling this CDR.\n",
411 entry->cdrname, colptr, entry->filtervalue);
416 if (ast_strlen_zero(entry->name))
419 LENGTHEN_BUF1(strlen(entry->name));
421 switch (entry->type) {
424 case SQL_LONGVARCHAR:
427 case SQL_LONGVARBINARY:
429 /* For these two field names, get the rendered form, instead of the raw
430 * form (but only when we're dealing with a character-based field).
432 if (strcasecmp(entry->name, "disposition") == 0) {
433 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
434 } else if (strcasecmp(entry->name, "amaflags") == 0) {
435 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
438 /* Truncate too-long fields */
439 if (entry->type != SQL_GUID) {
440 if (strlen(colptr) > entry->octetlen) {
441 colptr[entry->octetlen] = '\0';
445 ast_str_append(&sql, 0, "%s,", entry->name);
446 LENGTHEN_BUF2(strlen(colptr));
448 /* Encode value, with escaping */
449 ast_str_append(&sql2, 0, "'");
450 for (tmp = colptr; *tmp; tmp++) {
452 ast_str_append(&sql2, 0, "''");
453 } else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) {
454 ast_str_append(&sql2, 0, "\\\\");
456 ast_str_append(&sql2, 0, "%c", *tmp);
459 ast_str_append(&sql2, 0, "',");
463 int year = 0, month = 0, day = 0;
464 if (sscanf(colptr, "%d-%d-%d", &year, &month, &day) != 3 || year <= 0 ||
465 month <= 0 || month > 12 || day < 0 || day > 31 ||
466 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
467 (month == 2 && year % 400 == 0 && day > 29) ||
468 (month == 2 && year % 100 == 0 && day > 28) ||
469 (month == 2 && year % 4 == 0 && day > 29) ||
470 (month == 2 && year % 4 != 0 && day > 28)) {
471 ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
475 if (year > 0 && year < 100) {
479 ast_str_append(&sql, 0, "%s,", entry->name);
481 ast_str_append(&sql2, 0, "{ d '%04d-%02d-%02d' },", year, month, day);
486 int hour = 0, minute = 0, second = 0;
487 int count = sscanf(colptr, "%d:%d:%d", &hour, &minute, &second);
489 if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
490 ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
494 ast_str_append(&sql, 0, "%s,", entry->name);
496 ast_str_append(&sql2, 0, "{ t '%02d:%02d:%02d' },", hour, minute, second);
499 case SQL_TYPE_TIMESTAMP:
502 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
503 int count = sscanf(colptr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
505 if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
506 month <= 0 || month > 12 || day < 0 || day > 31 ||
507 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
508 (month == 2 && year % 400 == 0 && day > 29) ||
509 (month == 2 && year % 100 == 0 && day > 28) ||
510 (month == 2 && year % 4 == 0 && day > 29) ||
511 (month == 2 && year % 4 != 0 && day > 28) ||
512 hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
513 ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
517 if (year > 0 && year < 100) {
521 ast_str_append(&sql, 0, "%s,", entry->name);
523 ast_str_append(&sql2, 0, "{ ts '%04d-%02d-%02d %02d:%02d:%02d' },", year, month, day, hour, minute, second);
529 if (sscanf(colptr, "%d", &integer) != 1) {
530 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
534 ast_str_append(&sql, 0, "%s,", entry->name);
536 ast_str_append(&sql2, 0, "%d,", integer);
541 long long integer = 0;
542 if (sscanf(colptr, "%lld", &integer) != 1) {
543 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
547 ast_str_append(&sql, 0, "%s,", entry->name);
549 ast_str_append(&sql2, 0, "%lld,", integer);
555 if (sscanf(colptr, "%hd", &integer) != 1) {
556 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
560 ast_str_append(&sql, 0, "%s,", entry->name);
562 ast_str_append(&sql2, 0, "%d,", integer);
568 if (sscanf(colptr, "%hhd", &integer) != 1) {
569 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
573 ast_str_append(&sql, 0, "%s,", entry->name);
575 ast_str_append(&sql2, 0, "%d,", integer);
581 if (sscanf(colptr, "%hhd", &integer) != 1) {
582 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
588 ast_str_append(&sql, 0, "%s,", entry->name);
590 ast_str_append(&sql2, 0, "%d,", integer);
597 if (sscanf(colptr, "%lf", &number) != 1) {
598 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
602 ast_str_append(&sql, 0, "%s,", entry->name);
603 LENGTHEN_BUF2(entry->decimals);
604 ast_str_append(&sql2, 0, "%*.*lf,", entry->decimals, entry->radix, number);
612 if (sscanf(colptr, "%lf", &number) != 1) {
613 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
617 ast_str_append(&sql, 0, "%s,", entry->name);
618 LENGTHEN_BUF2(entry->decimals);
619 ast_str_append(&sql2, 0, "%lf,", number);
623 ast_log(LOG_WARNING, "Column type %d (field '%s:%s:%s') is unsupported at this time.\n", entry->type, tableptr->connection, tableptr->table, entry->name);
628 /* Concatenate the two constructed buffers */
629 LENGTHEN_BUF1(sql2->used);
630 sql->str[sql->used - 1] = ')';
631 sql2->str[sql2->used - 1] = ')';
632 ast_str_append(&sql, 0, "%s", sql2->str);
634 ast_verb(11, "[%s]\n", sql->str);
636 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql->str);
638 SQLRowCount(stmt, &rows);
639 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
642 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, sql->str);
645 ast_odbc_release_obj(obj);
647 AST_RWLIST_UNLOCK(&odbc_tables);
649 /* Next time, just allocate buffers that are that big to start with. */
650 if (sql->used > maxsize) {
653 if (sql2->used > maxsize2) {
654 maxsize2 = sql2->used;
662 static int unload_module(void)
664 ast_cdr_unregister(name);
666 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
667 ast_cdr_register(name, ast_module_info->description, odbc_log);
668 ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
673 AST_RWLIST_UNLOCK(&odbc_tables);
677 static int load_module(void)
679 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
680 ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n");
685 AST_RWLIST_UNLOCK(&odbc_tables);
686 ast_cdr_register(name, ast_module_info->description, odbc_log);
690 static int reload(void)
692 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
693 ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n");
699 AST_RWLIST_UNLOCK(&odbc_tables);
703 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive ODBC CDR backend",
705 .unload = unload_module,