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/options.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/lock.h"
46 #include "asterisk/linkedlists.h"
47 #include "asterisk/res_odbc.h"
48 #include "asterisk/cdr.h"
49 #include "asterisk/module.h"
50 #include "asterisk/logger.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;
67 AST_LIST_ENTRY(columns) list;
73 AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
74 AST_RWLIST_ENTRY(tables) list;
77 static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
79 static int load_config(void)
81 struct ast_config *cfg;
82 struct ast_variable *var;
83 const char *tmp, *catg;
84 struct tables *tableptr;
85 struct columns *entry;
90 int lenconnection, lentable;
94 struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
96 cfg = ast_config_load(CONFIG, config_flags);
98 ast_log(LOG_WARNING, "Unable to load " CONFIG ". No adaptive ODBC CDRs.\n");
102 for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
103 var = ast_variable_browse(cfg, catg);
107 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
108 ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg);
111 ast_copy_string(connection, tmp, sizeof(connection));
112 lenconnection = strlen(connection);
114 /* When loading, we want to be sure we can connect. */
115 obj = ast_odbc_request_obj(connection, 1);
117 ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg);
121 if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
122 ast_log(LOG_NOTICE, "No table name found. Assuming 'cdr'.\n");
125 ast_copy_string(table, tmp, sizeof(table));
126 lentable = strlen(table);
128 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
129 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
130 ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
131 ast_odbc_release_obj(obj);
135 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
136 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
137 ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection);
138 ast_odbc_release_obj(obj);
142 tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
144 ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
145 ast_odbc_release_obj(obj);
150 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
151 tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
152 ast_copy_string(tableptr->connection, connection, lenconnection + 1);
153 ast_copy_string(tableptr->table, table, lentable + 1);
155 ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
157 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
160 SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
162 /* Is there an alias for this column? */
164 /* NOTE: This seems like a non-optimal parse method, but I'm going
165 * for user configuration readability, rather than fast parsing. We
166 * really don't parse this file all that often, anyway.
168 for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
169 if (strcasecmp(var->value, columnname) == 0) {
170 char *tmp = ast_strdupa(var->name + 5);
171 cdrvar = ast_strip(tmp);
172 ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
176 entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1);
178 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
182 entry->name = (char *)entry + sizeof(*entry);
183 strcpy(entry->name, columnname);
185 if (!ast_strlen_zero(cdrvar)) {
186 entry->cdrname = entry->name + strlen(columnname) + 1;
187 strcpy(entry->cdrname, cdrvar);
188 } else /* Point to same place as the column name */
189 entry->cdrname = (char *)entry + sizeof(*entry);
191 SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
192 SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
193 SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
194 SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
195 SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
196 SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
198 /* Specification states that the octenlen should be the maximum number of bytes
199 * returned in a char or binary column, but it seems that some drivers just set
200 * it to NULL. (Bad Postgres! No biscuit!) */
201 if (entry->octetlen == 0)
202 entry->octetlen = entry->size;
204 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);
205 /* Insert column info into column list */
206 AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
210 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
211 ast_odbc_release_obj(obj);
213 if (AST_LIST_FIRST(&(tableptr->columns)))
214 AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
221 static int free_config(void)
223 struct tables *table;
224 struct columns *entry;
225 while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
226 while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
234 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
239 SQLINTEGER nativeerror = 0, numfields = 0;
240 SQLSMALLINT diagbytes = 0;
241 unsigned char state[10], diagnostic[256];
243 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
244 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
245 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
249 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
250 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
251 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
252 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
253 for (i = 0; i < numfields; i++) {
254 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
255 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
257 ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
261 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
268 #define LENGTHEN_BUF1(size) \
270 /* Lengthen buffer, if necessary */ \
271 if ((newsize = lensql + (size) + 3) > sizesql) { \
272 if ((tmp = ast_realloc(sql, (newsize / 512 + 1) * 512))) { \
274 sizesql = (newsize / 512 + 1) * 512; \
276 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
279 AST_RWLIST_UNLOCK(&odbc_tables); \
285 #define LENGTHEN_BUF2(size) \
287 if ((newsize = lensql2 + (size) + 3) > sizesql2) { \
288 if ((tmp = ast_realloc(sql2, (newsize / 512 + 1) * 512))) { \
290 sizesql2 = (newsize / 512 + 1) * 512; \
292 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
295 AST_RWLIST_UNLOCK(&odbc_tables); \
301 static int odbc_log(struct ast_cdr *cdr)
303 struct tables *tableptr;
304 struct columns *entry;
305 struct odbc_obj *obj;
306 int lensql, lensql2, sizesql = maxsize, sizesql2 = maxsize2, newsize;
307 /* Allocated, so we can realloc() */
308 char *sql = ast_calloc(sizeof(char), sizesql), *sql2 = ast_calloc(sizeof(char), sizesql2), *tmp;
309 char colbuf[1024], *colptr;
310 SQLHSTMT stmt = NULL;
321 if (AST_RWLIST_RDLOCK(&odbc_tables)) {
322 ast_log(LOG_ERROR, "Unable to lock table list. Insert CDR(s) failed.\n");
328 AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
329 lensql = snprintf(sql, sizesql, "INSERT INTO %s (", tableptr->table);
330 lensql2 = snprintf(sql2, sizesql2, " VALUES (");
332 AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
333 /* Check if we have a similarly named variable */
334 ast_cdr_getvar(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), 0,
335 (strcasecmp(entry->cdrname, "start") == 0 ||
336 strcasecmp(entry->cdrname, "answer") == 0 ||
337 strcasecmp(entry->cdrname, "end") == 0) ? 0 : 1);
340 LENGTHEN_BUF1(strlen(entry->name));
342 switch (entry->type) {
345 case SQL_LONGVARCHAR:
348 case SQL_LONGVARBINARY:
350 /* For these two field names, get the rendered form, instead of the raw
351 * form (but only when we're dealing with a character-based field).
353 if (strcasecmp(entry->name, "disposition") == 0)
354 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
355 else if (strcasecmp(entry->name, "amaflags") == 0)
356 ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
358 /* Truncate too-long fields */
359 if (entry->type != SQL_GUID) {
360 if (strlen(colptr) > entry->octetlen)
361 colptr[entry->octetlen] = '\0';
364 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
365 LENGTHEN_BUF2(strlen(colptr));
367 /* Encode value, with escaping */
368 strcpy(sql2 + lensql2, "'");
370 for (tmp = colptr; *tmp; tmp++) {
372 strcpy(sql2 + lensql2, "''");
374 } else if (*tmp == '\\') {
375 strcpy(sql2 + lensql2, "\\\\");
378 sql2[lensql2++] = *tmp;
379 sql2[lensql2] = '\0';
382 strcpy(sql2 + lensql2, "',");
387 int year = 0, month = 0, day = 0;
388 if (sscanf(colptr, "%d-%d-%d", &year, &month, &day) != 3 || year <= 0 ||
389 month <= 0 || month > 12 || day < 0 || day > 31 ||
390 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
391 (month == 2 && year % 400 == 0 && day > 29) ||
392 (month == 2 && year % 100 == 0 && day > 28) ||
393 (month == 2 && year % 4 == 0 && day > 29) ||
394 (month == 2 && year % 4 != 0 && day > 28)) {
395 ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
399 if (year > 0 && year < 100)
402 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
404 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d',", year, month, day);
409 int hour = 0, minute = 0, second = 0;
410 int count = sscanf(colptr, "%d:%d:%d", &hour, &minute, &second);
412 if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
413 ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
417 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
419 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%02d:%02d:%02d',", hour, minute, second);
422 case SQL_TYPE_TIMESTAMP:
425 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
426 int count = sscanf(colptr, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
428 if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
429 month <= 0 || month > 12 || day < 0 || day > 31 ||
430 ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
431 (month == 2 && year % 400 == 0 && day > 29) ||
432 (month == 2 && year % 100 == 0 && day > 28) ||
433 (month == 2 && year % 4 == 0 && day > 29) ||
434 (month == 2 && year % 4 != 0 && day > 28) ||
435 hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
436 ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
440 if (year > 0 && year < 100)
443 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
445 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "'%04d-%02d-%02d %02d:%02d:%02d',", year, month, day, hour, minute, second);
451 if (sscanf(colptr, "%d", &integer) != 1) {
452 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
456 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
458 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
463 long long integer = 0;
464 if (sscanf(colptr, "%lld", &integer) != 1) {
465 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
469 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
471 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lld,", integer);
477 if (sscanf(colptr, "%hd", &integer) != 1) {
478 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
482 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
484 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
490 if (sscanf(colptr, "%hhd", &integer) != 1) {
491 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
495 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
497 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
503 if (sscanf(colptr, "%hhd", &integer) != 1) {
504 ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
510 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
512 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%d,", integer);
519 if (sscanf(colptr, "%lf", &number) != 1) {
520 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
524 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
525 LENGTHEN_BUF2(entry->decimals);
526 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%*.*lf,", entry->decimals, entry->radix, number);
534 if (sscanf(colptr, "%lf", &number) != 1) {
535 ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
539 lensql += snprintf(sql + lensql, sizesql - lensql, "%s,", entry->name);
540 LENGTHEN_BUF2(entry->decimals);
541 lensql2 += snprintf(sql2 + lensql2, sizesql2 - lensql2, "%lf,", number);
545 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);
550 /* Concatenate the two constructed buffers */
551 LENGTHEN_BUF1(lensql2);
552 sql[lensql - 1] = ')';
553 sql2[lensql2 - 1] = ')';
554 strcat(sql + lensql, sql2);
556 ast_verb(11, "[%s]\n", sql);
557 /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
558 obj = ast_odbc_request_obj(tableptr->connection, 0);
560 stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql);
562 SQLRowCount(stmt, &rows);
563 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
566 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
568 ast_odbc_release_obj(obj);
570 ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, sql);
573 AST_RWLIST_UNLOCK(&odbc_tables);
575 /* Next time, just allocate buffers that are that big to start with. */
576 if (sizesql > maxsize)
578 if (sizesql2 > maxsize2)
586 static int unload_module(void)
588 ast_cdr_unregister(name);
590 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
591 ast_cdr_register(name, ast_module_info->description, odbc_log);
592 ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
597 AST_RWLIST_UNLOCK(&odbc_tables);
601 static int load_module(void)
603 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
604 ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n");
609 AST_RWLIST_UNLOCK(&odbc_tables);
610 ast_cdr_register(name, ast_module_info->description, odbc_log);
614 static int reload(void)
616 if (AST_RWLIST_WRLOCK(&odbc_tables)) {
617 ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n");
623 AST_RWLIST_UNLOCK(&odbc_tables);
627 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive ODBC CDR backend",
629 .unload = unload_module,