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
28 <depend>unixodbc</depend>
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <sys/types.h>
46 #include "asterisk/config.h"
47 #include "asterisk/options.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/lock.h"
50 #include "asterisk/linkedlists.h"
51 #include "asterisk/res_odbc.h"
52 #include "asterisk/cdr.h"
53 #include "asterisk/module.h"
54 #include "asterisk/logger.h"
56 #define CONFIG "cdr_adaptive_odbc.conf"
58 static char *name = "Adaptive ODBC";
59 /* Optimization to reduce number of memory allocations */
60 static int maxsize = 512, maxsize2 = 512;
71 AST_LIST_ENTRY(columns) list;
77 AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
78 AST_RWLIST_ENTRY(tables) list;
81 static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
83 static int load_config(void)
85 struct ast_config *cfg;
86 struct ast_variable *var;
87 const char *tmp, *catg;
88 struct tables *tableptr;
89 struct columns *entry;
94 int lenconnection, lentable;
98 struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
100 cfg = ast_config_load(CONFIG, config_flags);
102 ast_log(LOG_WARNING, "Unable to load " CONFIG ". No adaptive ODBC CDRs.\n");
106 for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
107 var = ast_variable_browse(cfg, catg);
111 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
112 ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg);
115 ast_copy_string(connection, tmp, sizeof(connection));
116 lenconnection = strlen(connection);
118 /* When loading, we want to be sure we can connect. */
119 obj = ast_odbc_request_obj(connection, 1);
121 ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg);
125 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
126 ast_log(LOG_NOTICE, "No table name found. Assuming 'cdr'.\n");
129 ast_copy_string(table, tmp, sizeof(table));
130 lentable = strlen(table);
132 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
133 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
134 ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
135 ast_odbc_release_obj(obj);
139 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
140 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
141 ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection);
142 ast_odbc_release_obj(obj);
146 tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
148 ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
149 ast_odbc_release_obj(obj);
154 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
155 tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
156 ast_copy_string(tableptr->connection, connection, lenconnection + 1);
157 ast_copy_string(tableptr->table, table, lentable + 1);
159 ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
161 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
164 SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
166 /* Is there an alias for this column? */
168 /* NOTE: This seems like a non-optimal parse method, but I'm going
169 * for user configuration readability, rather than fast parsing. We
170 * really don't parse this file all that often, anyway.
172 for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
173 if (strcasecmp(var->value, columnname) == 0) {
174 char *tmp = ast_strdupa(var->name + 5);
175 cdrvar = ast_strip(tmp);
176 ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
180 entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1);
182 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
186 entry->name = (char *)entry + sizeof(*entry);
187 strcpy(entry->name, columnname);
189 if (!ast_strlen_zero(cdrvar)) {
190 entry->cdrname = entry->name + strlen(columnname) + 1;
191 strcpy(entry->cdrname, cdrvar);
192 } else /* Point to same place as the column name */
193 entry->cdrname = (char *)entry + sizeof(*entry);
195 SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
196 SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
197 SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
198 SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
199 SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
200 SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
202 /* Specification states that the octenlen should be the maximum number of bytes
203 * returned in a char or binary column, but it seems that some drivers just set
204 * it to NULL. (Bad Postgres! No biscuit!) */
205 if (entry->octetlen == 0)
206 entry->octetlen = entry->size;
208 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);
209 /* Insert column info into column list */
210 AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
214 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
215 ast_odbc_release_obj(obj);
217 if (AST_LIST_FIRST(&(tableptr->columns)))
218 AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
225 static int free_config(void)
227 struct tables *table;
228 struct columns *entry;
229 while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
230 while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
238 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
243 SQLINTEGER nativeerror = 0, numfields = 0;
244 SQLSMALLINT diagbytes = 0;
245 unsigned char state[10], diagnostic[256];
247 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
248 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
249 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
253 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
254 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
255 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
256 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
257 for (i = 0; i < numfields; i++) {
258 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
259 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
261 ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
265 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
272 #define LENGTHEN_BUF1(size) \
274 /* Lengthen buffer, if necessary */ \
275 if ((newsize = lensql + (size) + 3) > sizesql) { \
276 if ((tmp = ast_realloc(sql, (newsize / 512 + 1) * 512))) { \
278 sizesql = (newsize / 512 + 1) * 512; \
280 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
283 AST_RWLIST_UNLOCK(&odbc_tables); \
289 #define LENGTHEN_BUF2(size) \
291 if ((newsize = lensql2 + (size) + 3) > sizesql2) { \
292 if ((tmp = ast_realloc(sql2, (newsize / 512 + 1) * 512))) { \
294 sizesql2 = (newsize / 512 + 1) * 512; \
296 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
299 AST_RWLIST_UNLOCK(&odbc_tables); \
305 static int odbc_log(struct ast_cdr *cdr)
307 struct tables *tableptr;
308 struct columns *entry;
309 struct odbc_obj *obj;
310 int lensql, lensql2, sizesql = maxsize, sizesql2 = maxsize2, newsize;
311 /* Allocated, so we can realloc() */
312 char *sql = ast_calloc(sizeof(char), sizesql), *sql2 = ast_calloc(sizeof(char), sizesql2), *tmp;
313 char colbuf[1024], *colptr;
314 SQLHSTMT stmt = NULL;
317 if (AST_RWLIST_RDLOCK(&odbc_tables)) {
318 ast_log(LOG_ERROR, "Unable to lock table list. Insert CDR(s) failed.\n");
322 AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
323 lensql = snprintf(sql, sizesql, "INSERT INTO %s (", tableptr->table);
324 lensql2 = snprintf(sql2, sizesql2, " VALUES (");
326 AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
327 /* Check if we have a similarly named variable */
328 ast_cdr_getvar(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), 0,
329 (strcasecmp(entry->cdrname, "start") == 0 ||
330 strcasecmp(entry->cdrname, "answer") == 0 ||
331 strcasecmp(entry->cdrname, "end") == 0) ? 0 : 1);
334 LENGTHEN_BUF1(strlen(entry->name));
336 switch (entry->type) {
339 case SQL_LONGVARCHAR:
342 case SQL_LONGVARBINARY:
344 /* For these two field names, get the rendered form, instead of the raw
345 * form (but only when we're dealing with a character-based field).
347 if (strcasecmp(entry->name, "disposition") == 0)
348 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
349 else if (strcasecmp(entry->name, "amaflags") == 0)
350 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
352 /* Truncate too-long fields */
353 if (entry->type != SQL_GUID) {
354 if (strlen(colptr) > entry->octetlen)
355 colptr[entry->octetlen] = '\0';
358 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
359 LENGTHEN_BUF2(strlen(colptr));
361 /* Encode value, with escaping */
362 strcpy(sql2 + lensql2, "'");
364 for (tmp = colptr; *tmp; tmp++) {
366 strcpy(sql2 + lensql2, "''");
368 } else if (*tmp == '\\') {
369 strcpy(sql2 + lensql2, "\\\\");
372 sql2[lensql2++] = *tmp;
373 sql2[lensql2] = '\0';
376 strcpy(sql2 + lensql2, "',");
381 int year = 0, month = 0, day = 0;
382 if (sscanf(colptr, "%d-%d-%d", &year, &month, &day) != 3 || year <= 0 ||
383 month <= 0 || month > 12 || day < 0 || day > 31 ||
384 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
385 (month == 2 && year % 400 == 0 && day > 29) ||
386 (month == 2 && year % 100 == 0 && day > 28) ||
387 (month == 2 && year % 4 == 0 && day > 29) ||
388 (month == 2 && year % 4 != 0 && day > 28)) {
389 ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
393 if (year > 0 && year < 100)
396 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
398 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d',", year, month, day);
403 int hour = 0, minute = 0, second = 0;
404 int count = sscanf(colptr, "%d:%d:%d", &hour, &minute, &second);
406 if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
407 ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
411 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
413 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%02d:%02d:%02d',", hour, minute, second);
416 case SQL_TYPE_TIMESTAMP:
419 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
420 int count = sscanf(colptr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
422 if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
423 month <= 0 || month > 12 || day < 0 || day > 31 ||
424 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
425 (month == 2 && year % 400 == 0 && day > 29) ||
426 (month == 2 && year % 100 == 0 && day > 28) ||
427 (month == 2 && year % 4 == 0 && day > 29) ||
428 (month == 2 && year % 4 != 0 && day > 28) ||
429 hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
430 ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
434 if (year > 0 && year < 100)
437 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
439 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d %02d:%02d:%02d',", year, month, day, hour, minute, second);
445 if (sscanf(colptr, "%d", &integer) != 1) {
446 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
450 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
452 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
457 long long integer = 0;
458 if (sscanf(colptr, "%lld", &integer) != 1) {
459 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
463 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
465 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lld,", integer);
471 if (sscanf(colptr, "%hd", &integer) != 1) {
472 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
476 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
478 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
484 if (sscanf(colptr, "%hhd", &integer) != 1) {
485 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
489 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
491 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
497 if (sscanf(colptr, "%hhd", &integer) != 1) {
498 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
504 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
506 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
513 if (sscanf(colptr, "%lf", &number) != 1) {
514 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
518 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
519 LENGTHEN_BUF2(entry->decimals);
520 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%*.*lf,", entry->decimals, entry->radix, number);
528 if (sscanf(colptr, "%lf", &number) != 1) {
529 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
533 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
534 LENGTHEN_BUF2(entry->decimals);
535 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lf,", number);
539 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);
544 /* Concatenate the two constructed buffers */
545 LENGTHEN_BUF1(lensql2);
546 sql[lensql - 1] = ')';
547 sql2[lensql2 - 1] = ')';
548 strcat(sql + lensql, sql2);
550 ast_verb(11, "[%s]\n", sql);
551 /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
552 obj = ast_odbc_request_obj(tableptr->connection, 0);
554 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
556 SQLRowCount(stmt, &rows);
557 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
560 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
562 ast_odbc_release_obj(obj);
564 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
567 AST_RWLIST_UNLOCK(&odbc_tables);
569 /* Next time, just allocate buffers that are that big to start with. */
570 if (sizesql > maxsize)
572 if (sizesql2 > maxsize2)
580 static int unload_module(void)
582 ast_cdr_unregister(name);
584 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
585 ast_cdr_register(name, ast_module_info->description, odbc_log);
586 ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
591 AST_RWLIST_UNLOCK(&odbc_tables);
595 static int load_module(void)
597 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
598 ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n");
603 AST_RWLIST_UNLOCK(&odbc_tables);
604 ast_cdr_register(name, ast_module_info->description, odbc_log);
608 static int reload(void)
610 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
611 ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n");
617 AST_RWLIST_UNLOCK(&odbc_tables);
621 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive ODBC CDR backend",
623 .unload = unload_module,