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>
42 #include "asterisk/config.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/lock.h"
45 #include "asterisk/linkedlists.h"
46 #include "asterisk/res_odbc.h"
47 #include "asterisk/cdr.h"
48 #include "asterisk/module.h"
50 #define CONFIG "cdr_adaptive_odbc.conf"
52 static char *name = "Adaptive ODBC";
53 /* Optimization to reduce number of memory allocations */
54 static int maxsize = 512, maxsize2 = 512;
65 AST_LIST_ENTRY(columns) list;
71 AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
72 AST_RWLIST_ENTRY(tables) list;
75 static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
77 static int load_config(void)
79 struct ast_config *cfg;
80 struct ast_variable *var;
81 const char *tmp, *catg;
82 struct tables *tableptr;
83 struct columns *entry;
88 int lenconnection, lentable;
92 struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
94 cfg = ast_config_load(CONFIG, config_flags);
96 ast_log(LOG_WARNING, "Unable to load " CONFIG ". No adaptive ODBC CDRs.\n");
100 for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
101 var = ast_variable_browse(cfg, catg);
105 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
106 ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg);
109 ast_copy_string(connection, tmp, sizeof(connection));
110 lenconnection = strlen(connection);
112 /* When loading, we want to be sure we can connect. */
113 obj = ast_odbc_request_obj(connection, 1);
115 ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg);
119 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
120 ast_log(LOG_NOTICE, "No table name found. Assuming 'cdr'.\n");
123 ast_copy_string(table, tmp, sizeof(table));
124 lentable = strlen(table);
126 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
127 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
128 ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
129 ast_odbc_release_obj(obj);
133 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
134 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
135 ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection);
136 ast_odbc_release_obj(obj);
140 tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
142 ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
143 ast_odbc_release_obj(obj);
148 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
149 tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
150 ast_copy_string(tableptr->connection, connection, lenconnection + 1);
151 ast_copy_string(tableptr->table, table, lentable + 1);
153 ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
155 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
158 SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
160 /* Is there an alias for this column? */
162 /* NOTE: This seems like a non-optimal parse method, but I'm going
163 * for user configuration readability, rather than fast parsing. We
164 * really don't parse this file all that often, anyway.
166 for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
167 if (strcasecmp(var->value, columnname) == 0) {
168 char *tmp = ast_strdupa(var->name + 5);
169 cdrvar = ast_strip(tmp);
170 ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
174 entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1);
176 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
180 entry->name = (char *)entry + sizeof(*entry);
181 strcpy(entry->name, columnname);
183 if (!ast_strlen_zero(cdrvar)) {
184 entry->cdrname = entry->name + strlen(columnname) + 1;
185 strcpy(entry->cdrname, cdrvar);
186 } else /* Point to same place as the column name */
187 entry->cdrname = (char *)entry + sizeof(*entry);
189 SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
190 SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
191 SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
192 SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
193 SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
194 SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
196 /* Specification states that the octenlen should be the maximum number of bytes
197 * returned in a char or binary column, but it seems that some drivers just set
198 * it to NULL. (Bad Postgres! No biscuit!) */
199 if (entry->octetlen == 0)
200 entry->octetlen = entry->size;
202 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);
203 /* Insert column info into column list */
204 AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
208 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
209 ast_odbc_release_obj(obj);
211 if (AST_LIST_FIRST(&(tableptr->columns)))
212 AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
219 static int free_config(void)
221 struct tables *table;
222 struct columns *entry;
223 while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
224 while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
232 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
237 SQLINTEGER nativeerror = 0, numfields = 0;
238 SQLSMALLINT diagbytes = 0;
239 unsigned char state[10], diagnostic[256];
241 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
242 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
243 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
247 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
248 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
249 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
250 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
251 for (i = 0; i < numfields; i++) {
252 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
253 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
255 ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
259 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
266 #define LENGTHEN_BUF1(size) \
268 /* Lengthen buffer, if necessary */ \
269 if ((newsize = lensql + (size) + 3) > sizesql) { \
270 if ((tmp = ast_realloc(sql, (newsize / 512 + 1) * 512))) { \
272 sizesql = (newsize / 512 + 1) * 512; \
274 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
277 AST_RWLIST_UNLOCK(&odbc_tables); \
283 #define LENGTHEN_BUF2(size) \
285 if ((newsize = lensql2 + (size) + 3) > sizesql2) { \
286 if ((tmp = ast_realloc(sql2, (newsize / 512 + 1) * 512))) { \
288 sizesql2 = (newsize / 512 + 1) * 512; \
290 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
293 AST_RWLIST_UNLOCK(&odbc_tables); \
299 static int odbc_log(struct ast_cdr *cdr)
301 struct tables *tableptr;
302 struct columns *entry;
303 struct odbc_obj *obj;
304 int lensql, lensql2, sizesql = maxsize, sizesql2 = maxsize2, newsize;
305 /* Allocated, so we can realloc() */
306 char *sql = ast_calloc(sizeof(char), sizesql), *sql2 = ast_calloc(sizeof(char), sizesql2), *tmp;
307 char colbuf[1024], *colptr;
308 SQLHSTMT stmt = NULL;
319 if (AST_RWLIST_RDLOCK(&odbc_tables)) {
320 ast_log(LOG_ERROR, "Unable to lock table list. Insert CDR(s) failed.\n");
326 AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
327 lensql = snprintf(sql, sizesql, "INSERT INTO %s (", tableptr->table);
328 lensql2 = snprintf(sql2, sizesql2, " VALUES (");
330 /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
331 if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) {
332 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
336 AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
337 /* Check if we have a similarly named variable */
338 ast_cdr_getvar(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), 0,
339 (strcasecmp(entry->cdrname, "start") == 0 ||
340 strcasecmp(entry->cdrname, "answer") == 0 ||
341 strcasecmp(entry->cdrname, "end") == 0) ? 0 : 1);
344 LENGTHEN_BUF1(strlen(entry->name));
346 switch (entry->type) {
349 case SQL_LONGVARCHAR:
352 case SQL_LONGVARBINARY:
354 /* For these two field names, get the rendered form, instead of the raw
355 * form (but only when we're dealing with a character-based field).
357 if (strcasecmp(entry->name, "disposition") == 0)
358 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
359 else if (strcasecmp(entry->name, "amaflags") == 0)
360 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
362 /* Truncate too-long fields */
363 if (entry->type != SQL_GUID) {
364 if (strlen(colptr) > entry->octetlen)
365 colptr[entry->octetlen] = '\0';
368 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
369 LENGTHEN_BUF2(strlen(colptr));
371 /* Encode value, with escaping */
372 strcpy(sql2 + lensql2, "'");
374 for (tmp = colptr; *tmp; tmp++) {
376 strcpy(sql2 + lensql2, "''");
378 } else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) {
379 strcpy(sql2 + lensql2, "\\\\");
382 sql2[lensql2++] = *tmp;
383 sql2[lensql2] = '\0';
386 strcpy(sql2 + lensql2, "',");
391 int year = 0, month = 0, day = 0;
392 if (sscanf(colptr, "%d-%d-%d", &year, &month, &day) != 3 || year <= 0 ||
393 month <= 0 || month > 12 || day < 0 || day > 31 ||
394 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
395 (month == 2 && year % 400 == 0 && day > 29) ||
396 (month == 2 && year % 100 == 0 && day > 28) ||
397 (month == 2 && year % 4 == 0 && day > 29) ||
398 (month == 2 && year % 4 != 0 && day > 28)) {
399 ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
403 if (year > 0 && year < 100)
406 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
408 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "{ d '%04d-%02d-%02d' },", year, month, day);
413 int hour = 0, minute = 0, second = 0;
414 int count = sscanf(colptr, "%d:%d:%d", &hour, &minute, &second);
416 if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
417 ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
421 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
423 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "{ t '%02d:%02d:%02d' },", hour, minute, second);
426 case SQL_TYPE_TIMESTAMP:
429 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
430 int count = sscanf(colptr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
432 if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
433 month <= 0 || month > 12 || day < 0 || day > 31 ||
434 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
435 (month == 2 && year % 400 == 0 && day > 29) ||
436 (month == 2 && year % 100 == 0 && day > 28) ||
437 (month == 2 && year % 4 == 0 && day > 29) ||
438 (month == 2 && year % 4 != 0 && day > 28) ||
439 hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
440 ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
444 if (year > 0 && year < 100)
447 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
449 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "{ ts '%04d-%02d-%02d %02d:%02d:%02d' },", year, month, day, hour, minute, second);
455 if (sscanf(colptr, "%d", &integer) != 1) {
456 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
460 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
462 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
467 long long integer = 0;
468 if (sscanf(colptr, "%lld", &integer) != 1) {
469 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
473 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
475 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lld,", integer);
481 if (sscanf(colptr, "%hd", &integer) != 1) {
482 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
486 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
488 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
494 if (sscanf(colptr, "%hhd", &integer) != 1) {
495 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
499 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
501 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
507 if (sscanf(colptr, "%hhd", &integer) != 1) {
508 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
514 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
516 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
523 if (sscanf(colptr, "%lf", &number) != 1) {
524 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
528 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
529 LENGTHEN_BUF2(entry->decimals);
530 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%*.*lf,", entry->decimals, entry->radix, number);
538 if (sscanf(colptr, "%lf", &number) != 1) {
539 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
543 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
544 LENGTHEN_BUF2(entry->decimals);
545 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lf,", number);
549 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);
554 /* Concatenate the two constructed buffers */
555 LENGTHEN_BUF1(lensql2);
556 sql[lensql - 1] = ')';
557 sql2[lensql2 - 1] = ')';
558 strcat(sql + lensql, sql2);
560 ast_verb(11, "[%s]\n", sql);
562 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
564 SQLRowCount(stmt, &rows);
565 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
568 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
570 ast_odbc_release_obj(obj);
572 AST_RWLIST_UNLOCK(&odbc_tables);
574 /* Next time, just allocate buffers that are that big to start with. */
575 if (sizesql > maxsize)
577 if (sizesql2 > maxsize2)
585 static int unload_module(void)
587 ast_cdr_unregister(name);
589 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
590 ast_cdr_register(name, ast_module_info->description, odbc_log);
591 ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
596 AST_RWLIST_UNLOCK(&odbc_tables);
600 static int load_module(void)
602 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
603 ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n");
608 AST_RWLIST_UNLOCK(&odbc_tables);
609 ast_cdr_register(name, ast_module_info->description, odbc_log);
613 static int reload(void)
615 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
616 ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n");
622 AST_RWLIST_UNLOCK(&odbc_tables);
626 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive ODBC CDR backend",
628 .unload = unload_module,