2007-03-13 21:22:33 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999 - 2007, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com> and others.
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief Custom SQLite3 CDR records.
|
|
|
|
*
|
|
|
|
* \author Adapted by Alejandro Rios <alejandro.rios@avatar.com.co> and
|
2008-11-20 17:48:58 +00:00
|
|
|
* Russell Bryant <russell@digium.com> from
|
2007-03-13 21:22:33 +00:00
|
|
|
* cdr_mysql_custom by Edward Eastman <ed@dm3.co.uk>,
|
|
|
|
* and cdr_sqlite by Holger Schurig <hs4233@mail.mn-solutions.de>
|
2008-11-20 17:48:58 +00:00
|
|
|
*
|
2007-03-13 21:22:33 +00:00
|
|
|
*
|
|
|
|
* \arg See also \ref AstCDR
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* \ingroup cdr_drivers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MODULEINFO
|
|
|
|
<depend>sqlite3</depend>
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
2007-11-20 23:16:15 +00:00
|
|
|
#include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
|
2007-03-13 21:22:33 +00:00
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/cdr.h"
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/config.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/cli.h"
|
|
|
|
|
|
|
|
AST_MUTEX_DEFINE_STATIC(lock);
|
|
|
|
|
|
|
|
static const char config_file[] = "cdr_sqlite3_custom.conf";
|
|
|
|
|
2009-05-21 21:13:09 +00:00
|
|
|
static const char desc[] = "Customizable SQLite3 CDR Backend";
|
|
|
|
static const char name[] = "cdr_sqlite3_custom";
|
2007-03-13 21:22:33 +00:00
|
|
|
static sqlite3 *db = NULL;
|
|
|
|
|
|
|
|
static char table[80];
|
2007-12-06 21:57:28 +00:00
|
|
|
static char *columns;
|
|
|
|
|
|
|
|
struct values {
|
|
|
|
char *expression;
|
|
|
|
AST_LIST_ENTRY(values) list;
|
|
|
|
};
|
|
|
|
|
|
|
|
static AST_LIST_HEAD_STATIC(sql_values, values);
|
|
|
|
|
2009-10-09 17:14:38 +00:00
|
|
|
static void free_config(int reload);
|
2007-12-06 21:57:28 +00:00
|
|
|
|
|
|
|
static int load_column_config(const char *tmp)
|
|
|
|
{
|
|
|
|
char *col = NULL;
|
2008-08-14 18:47:28 +00:00
|
|
|
char *cols = NULL, *save = NULL;
|
2007-12-06 21:57:28 +00:00
|
|
|
char *escaped = NULL;
|
|
|
|
struct ast_str *column_string = NULL;
|
|
|
|
|
|
|
|
if (ast_strlen_zero(tmp)) {
|
|
|
|
ast_log(LOG_WARNING, "Column names not specified. Module not loaded.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!(column_string = ast_str_create(1024))) {
|
|
|
|
ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-08-14 18:47:28 +00:00
|
|
|
if (!(save = cols = ast_strdup(tmp))) {
|
2007-12-06 21:57:28 +00:00
|
|
|
ast_log(LOG_ERROR, "Out of memory creating temporary buffer for column list for table '%s.'\n", table);
|
|
|
|
ast_free(column_string);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while ((col = strsep(&cols, ","))) {
|
|
|
|
col = ast_strip(col);
|
|
|
|
escaped = sqlite3_mprintf("%q", col);
|
|
|
|
if (!escaped) {
|
|
|
|
ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s.'\n", col, table);
|
|
|
|
ast_free(column_string);
|
2008-08-14 18:47:28 +00:00
|
|
|
ast_free(save);
|
2007-12-06 21:57:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-12-13 08:36:35 +00:00
|
|
|
ast_str_append(&column_string, 0, "%s%s", ast_str_strlen(column_string) ? "," : "", escaped);
|
2007-12-06 21:57:28 +00:00
|
|
|
sqlite3_free(escaped);
|
|
|
|
}
|
2008-12-13 08:36:35 +00:00
|
|
|
if (!(columns = ast_strdup(ast_str_buffer(column_string)))) {
|
2007-12-06 21:57:28 +00:00
|
|
|
ast_log(LOG_ERROR, "Out of memory copying columns string for table '%s.'\n", table);
|
|
|
|
ast_free(column_string);
|
2008-08-14 18:47:28 +00:00
|
|
|
ast_free(save);
|
2007-12-06 21:57:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ast_free(column_string);
|
2008-08-14 18:47:28 +00:00
|
|
|
ast_free(save);
|
2007-12-06 21:57:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_values_config(const char *tmp)
|
|
|
|
{
|
|
|
|
char *val = NULL;
|
2008-08-14 18:47:28 +00:00
|
|
|
char *vals = NULL, *save = NULL;
|
2007-12-06 21:57:28 +00:00
|
|
|
struct values *value = NULL;
|
|
|
|
|
|
|
|
if (ast_strlen_zero(tmp)) {
|
|
|
|
ast_log(LOG_WARNING, "Values not specified. Module not loaded.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-08-14 18:47:28 +00:00
|
|
|
if (!(save = vals = ast_strdup(tmp))) {
|
2007-12-06 21:57:28 +00:00
|
|
|
ast_log(LOG_ERROR, "Out of memory creating temporary buffer for value '%s'\n", tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while ((val = strsep(&vals, ","))) {
|
|
|
|
/* Strip the single quotes off if they are there */
|
|
|
|
val = ast_strip_quoted(val, "'", "'");
|
|
|
|
value = ast_calloc(sizeof(char), sizeof(*value) + strlen(val) + 1);
|
|
|
|
if (!value) {
|
|
|
|
ast_log(LOG_ERROR, "Out of memory creating entry for value '%s'\n", val);
|
2008-08-14 18:47:28 +00:00
|
|
|
ast_free(save);
|
2007-12-06 21:57:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
value->expression = (char *) value + sizeof(*value);
|
|
|
|
ast_copy_string(value->expression, val, strlen(val) + 1);
|
|
|
|
AST_LIST_INSERT_TAIL(&sql_values, value, list);
|
|
|
|
}
|
2008-08-14 18:47:28 +00:00
|
|
|
ast_free(save);
|
2007-12-06 21:57:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-03-13 21:22:33 +00:00
|
|
|
|
|
|
|
static int load_config(int reload)
|
|
|
|
{
|
|
|
|
struct ast_config *cfg;
|
2007-08-16 21:09:46 +00:00
|
|
|
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
|
2007-03-13 21:22:33 +00:00
|
|
|
struct ast_variable *mappingvar;
|
|
|
|
const char *tmp;
|
|
|
|
|
2008-12-13 08:36:35 +00:00
|
|
|
if ((cfg = ast_config_load(config_file, config_flags)) == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
|
|
|
|
ast_log(LOG_WARNING, "Failed to %sload configuration file. %s\n", reload ? "re" : "", reload ? "" : "Module not activated.");
|
2007-03-13 21:22:33 +00:00
|
|
|
return -1;
|
2008-12-13 08:36:35 +00:00
|
|
|
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
|
2007-08-16 21:09:46 +00:00
|
|
|
return 0;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
2007-03-13 21:22:33 +00:00
|
|
|
|
2008-12-13 08:36:35 +00:00
|
|
|
if (reload) {
|
2009-10-09 17:14:38 +00:00
|
|
|
free_config(1);
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
2007-12-06 21:57:28 +00:00
|
|
|
|
2007-03-13 21:22:33 +00:00
|
|
|
if (!(mappingvar = ast_variable_browse(cfg, "master"))) {
|
2007-12-06 21:57:28 +00:00
|
|
|
/* Nothing configured */
|
2007-03-13 21:22:33 +00:00
|
|
|
ast_config_destroy(cfg);
|
2009-02-27 21:32:13 +00:00
|
|
|
return -1;
|
2007-03-13 21:22:33 +00:00
|
|
|
}
|
2008-11-20 17:48:58 +00:00
|
|
|
|
2007-03-13 21:22:33 +00:00
|
|
|
/* Mapping must have a table name */
|
2008-12-13 08:36:35 +00:00
|
|
|
if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "master", "table"))) {
|
2007-03-13 21:22:33 +00:00
|
|
|
ast_copy_string(table, tmp, sizeof(table));
|
2008-12-13 08:36:35 +00:00
|
|
|
} else {
|
2007-12-06 21:57:28 +00:00
|
|
|
ast_log(LOG_WARNING, "Table name not specified. Assuming cdr.\n");
|
2007-03-13 21:22:33 +00:00
|
|
|
strcpy(table, "cdr");
|
|
|
|
}
|
|
|
|
|
2007-12-06 21:57:28 +00:00
|
|
|
/* Columns */
|
2008-12-13 08:36:35 +00:00
|
|
|
if (load_column_config(ast_variable_retrieve(cfg, "master", "columns"))) {
|
2007-03-13 21:22:33 +00:00
|
|
|
ast_config_destroy(cfg);
|
2009-10-09 17:14:38 +00:00
|
|
|
free_config(0);
|
2007-03-13 21:22:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-12-06 21:57:28 +00:00
|
|
|
/* Values */
|
2008-12-13 08:36:35 +00:00
|
|
|
if (load_values_config(ast_variable_retrieve(cfg, "master", "values"))) {
|
2007-03-13 21:22:33 +00:00
|
|
|
ast_config_destroy(cfg);
|
2009-10-09 17:14:38 +00:00
|
|
|
free_config(0);
|
2007-03-13 21:22:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-12-06 21:57:28 +00:00
|
|
|
ast_verb(3, "cdr_sqlite3_custom: Logging CDR records to table '%s' in 'master.db'\n", table);
|
2007-03-13 21:22:33 +00:00
|
|
|
|
|
|
|
ast_config_destroy(cfg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-09 17:14:38 +00:00
|
|
|
static void free_config(int reload)
|
2007-03-13 21:22:33 +00:00
|
|
|
{
|
2007-12-06 21:57:28 +00:00
|
|
|
struct values *value;
|
|
|
|
|
2009-10-09 17:14:38 +00:00
|
|
|
if (!reload && db) {
|
2007-12-06 21:57:28 +00:00
|
|
|
sqlite3_close(db);
|
|
|
|
db = NULL;
|
|
|
|
}
|
2007-03-13 21:22:33 +00:00
|
|
|
|
2007-12-06 21:57:28 +00:00
|
|
|
if (columns) {
|
|
|
|
ast_free(columns);
|
|
|
|
columns = NULL;
|
2007-03-13 21:22:33 +00:00
|
|
|
}
|
2007-12-06 21:57:28 +00:00
|
|
|
|
2008-12-13 08:36:35 +00:00
|
|
|
while ((value = AST_LIST_REMOVE_HEAD(&sql_values, list))) {
|
2007-12-06 21:57:28 +00:00
|
|
|
ast_free(value);
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
2007-03-13 21:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sqlite3_log(struct ast_cdr *cdr)
|
|
|
|
{
|
|
|
|
int res = 0;
|
2007-12-06 21:57:28 +00:00
|
|
|
char *error = NULL;
|
|
|
|
char *sql = NULL;
|
|
|
|
int count = 0;
|
2007-03-13 21:22:33 +00:00
|
|
|
|
2009-03-02 17:18:48 +00:00
|
|
|
if (db == NULL) {
|
|
|
|
/* Should not have loaded, but be failsafe. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-22 16:09:50 +00:00
|
|
|
ast_mutex_lock(&lock);
|
|
|
|
|
2007-12-06 21:57:28 +00:00
|
|
|
{ /* Make it obvious that only sql should be used outside of this block */
|
|
|
|
char *escaped;
|
|
|
|
char subst_buf[2048];
|
|
|
|
struct values *value;
|
2009-05-26 13:56:30 +00:00
|
|
|
struct ast_channel *dummy;
|
2007-12-06 21:57:28 +00:00
|
|
|
struct ast_str *value_string = ast_str_create(1024);
|
2009-05-26 13:56:30 +00:00
|
|
|
|
2009-06-26 15:28:53 +00:00
|
|
|
dummy = ast_dummy_channel_alloc();
|
2009-05-26 13:56:30 +00:00
|
|
|
if (!dummy) {
|
|
|
|
ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
|
|
|
|
ast_free(value_string);
|
2009-06-22 16:09:50 +00:00
|
|
|
ast_mutex_unlock(&lock);
|
2009-05-26 13:56:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dummy->cdr = ast_cdr_dup(cdr);
|
2007-12-06 21:57:28 +00:00
|
|
|
AST_LIST_TRAVERSE(&sql_values, value, list) {
|
2009-05-26 13:56:30 +00:00
|
|
|
pbx_substitute_variables_helper(dummy, value->expression, subst_buf, sizeof(subst_buf) - 1);
|
2007-12-06 21:57:28 +00:00
|
|
|
escaped = sqlite3_mprintf("%q", subst_buf);
|
2008-12-13 08:36:35 +00:00
|
|
|
ast_str_append(&value_string, 0, "%s'%s'", ast_str_strlen(value_string) ? "," : "", escaped);
|
2007-12-06 21:57:28 +00:00
|
|
|
sqlite3_free(escaped);
|
|
|
|
}
|
2008-12-13 08:36:35 +00:00
|
|
|
sql = sqlite3_mprintf("INSERT INTO %q (%s) VALUES (%s)", table, columns, ast_str_buffer(value_string));
|
2007-12-06 21:57:28 +00:00
|
|
|
ast_debug(1, "About to log: %s\n", sql);
|
2009-05-26 13:56:30 +00:00
|
|
|
ast_channel_release(dummy);
|
2007-12-06 21:57:28 +00:00
|
|
|
ast_free(value_string);
|
2007-03-13 21:22:33 +00:00
|
|
|
}
|
|
|
|
|
2007-12-06 21:57:28 +00:00
|
|
|
/* XXX This seems awful arbitrary... */
|
2007-03-13 21:22:33 +00:00
|
|
|
for (count = 0; count < 5; count++) {
|
2007-12-06 21:57:28 +00:00
|
|
|
res = sqlite3_exec(db, sql, NULL, NULL, &error);
|
2008-12-13 08:36:35 +00:00
|
|
|
if (res != SQLITE_BUSY && res != SQLITE_LOCKED) {
|
2007-03-13 21:22:33 +00:00
|
|
|
break;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
2007-03-13 21:22:33 +00:00
|
|
|
usleep(200);
|
|
|
|
}
|
|
|
|
|
2007-12-06 21:57:28 +00:00
|
|
|
if (error) {
|
|
|
|
ast_log(LOG_ERROR, "%s. SQL: %s.\n", error, sql);
|
|
|
|
sqlite3_free(error);
|
2007-03-13 21:22:33 +00:00
|
|
|
}
|
|
|
|
|
2008-12-13 08:36:35 +00:00
|
|
|
if (sql) {
|
2007-12-06 21:57:28 +00:00
|
|
|
sqlite3_free(sql);
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
2007-12-06 21:57:28 +00:00
|
|
|
|
2007-03-13 21:22:33 +00:00
|
|
|
ast_mutex_unlock(&lock);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int unload_module(void)
|
|
|
|
{
|
|
|
|
ast_cdr_unregister(name);
|
|
|
|
|
2009-10-09 17:14:38 +00:00
|
|
|
free_config(0);
|
2009-06-22 16:09:50 +00:00
|
|
|
|
2007-03-13 21:22:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_module(void)
|
|
|
|
{
|
2007-12-06 21:57:28 +00:00
|
|
|
char *error;
|
|
|
|
char filename[PATH_MAX];
|
2007-03-13 21:22:33 +00:00
|
|
|
int res;
|
2007-12-06 21:57:28 +00:00
|
|
|
char *sql;
|
2007-03-13 21:22:33 +00:00
|
|
|
|
2009-06-22 16:09:50 +00:00
|
|
|
if (load_config(0)) {
|
2007-09-17 21:52:07 +00:00
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
2007-03-13 21:22:33 +00:00
|
|
|
|
|
|
|
/* is the database there? */
|
2007-12-06 21:57:28 +00:00
|
|
|
snprintf(filename, sizeof(filename), "%s/master.db", ast_config_AST_LOG_DIR);
|
|
|
|
res = sqlite3_open(filename, &db);
|
|
|
|
if (res != SQLITE_OK) {
|
|
|
|
ast_log(LOG_ERROR, "Could not open database %s.\n", filename);
|
2009-10-09 17:14:38 +00:00
|
|
|
free_config(0);
|
2007-03-13 21:22:33 +00:00
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* is the table there? */
|
2007-12-06 21:57:28 +00:00
|
|
|
sql = sqlite3_mprintf("SELECT COUNT(AcctId) FROM %q;", table);
|
|
|
|
res = sqlite3_exec(db, sql, NULL, NULL, NULL);
|
|
|
|
sqlite3_free(sql);
|
|
|
|
if (res != SQLITE_OK) {
|
|
|
|
/* We don't use %q for the column list here since we already escaped when building it */
|
|
|
|
sql = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY, %s)", table, columns);
|
|
|
|
res = sqlite3_exec(db, sql, NULL, NULL, &error);
|
|
|
|
sqlite3_free(sql);
|
|
|
|
if (res != SQLITE_OK) {
|
|
|
|
ast_log(LOG_WARNING, "Unable to create table '%s': %s.\n", table, error);
|
|
|
|
sqlite3_free(error);
|
2009-10-09 17:14:38 +00:00
|
|
|
free_config(0);
|
2007-03-13 21:22:33 +00:00
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-22 16:09:50 +00:00
|
|
|
res = ast_cdr_register(name, desc, sqlite3_log);
|
|
|
|
if (res) {
|
|
|
|
ast_log(LOG_ERROR, "Unable to register custom SQLite3 CDR handling\n");
|
2009-10-09 17:14:38 +00:00
|
|
|
free_config(0);
|
2009-06-22 16:09:50 +00:00
|
|
|
return AST_MODULE_LOAD_DECLINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
2007-03-13 21:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int reload(void)
|
|
|
|
{
|
2009-06-22 16:09:50 +00:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
ast_mutex_lock(&lock);
|
|
|
|
res = load_config(1);
|
|
|
|
ast_mutex_unlock(&lock);
|
|
|
|
|
|
|
|
return res;
|
2007-03-13 21:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "SQLite3 Custom CDR Module",
|
|
|
|
.load = load_module,
|
|
|
|
.unload = unload_module,
|
|
|
|
.reload = reload,
|
|
|
|
);
|