Store CDR records in a SQLite database. More...
#include "asterisk.h"#include <sqlite.h>#include "asterisk/channel.h"#include "asterisk/module.h"#include "asterisk/utils.h"#include "asterisk/paths.h"
Go to the source code of this file.
Defines | |
| #define | DATE_FORMAT "%Y-%m-%d %T" |
| #define | LOG_HRTIME 0 |
| #define | LOG_UNIQUEID 0 |
| #define | LOG_USERFIELD 0 |
Functions | |
| static void | __reg_module (void) |
| static void | __unreg_module (void) |
| static void | format_date (char *buffer, size_t length, struct timeval *when) |
| static int | load_module (void) |
| static int | sqlite_log (struct ast_cdr *cdr) |
| static int | unload_module (void) |
Variables | |
| static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SQLite CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, } |
| static struct ast_module_info * | ast_module_info = &__mod_info |
| static sqlite * | db = NULL |
| static const char | name [] = "sqlite" |
| static const char | sql_create_table [] = ");" |
| SQL table format. | |
| static ast_mutex_t | sqlite_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 } |
Store CDR records in a SQLite database.
See also
Creates the database and table on-the-fly
Definition in file cdr_sqlite.c.
| #define DATE_FORMAT "%Y-%m-%d %T" |
Definition at line 60 of file cdr_sqlite.c.
Referenced by format_date().
| #define LOG_HRTIME 0 |
Definition at line 57 of file cdr_sqlite.c.
Referenced by sqlite_log().
| #define LOG_UNIQUEID 0 |
Definition at line 55 of file cdr_sqlite.c.
Referenced by sqlite_log().
| #define LOG_USERFIELD 0 |
Definition at line 56 of file cdr_sqlite.c.
Referenced by sqlite_log().
| static void __reg_module | ( | void | ) | [static] |
Definition at line 249 of file cdr_sqlite.c.
| static void __unreg_module | ( | void | ) | [static] |
Definition at line 249 of file cdr_sqlite.c.
| static void format_date | ( | char * | buffer, |
| size_t | length, | ||
| struct timeval * | when | ||
| ) | [static] |
Definition at line 99 of file cdr_sqlite.c.
References ast_localtime(), ast_strftime(), and DATE_FORMAT.
Referenced by sqlite_log().
{
struct ast_tm tm;
ast_localtime(when, &tm, NULL);
ast_strftime(buffer, length, DATE_FORMAT, &tm);
}
| static int load_module | ( | void | ) | [static] |
Definition at line 201 of file cdr_sqlite.c.
References ast_cdr_register(), ast_config_AST_LOG_DIR, ast_free, ast_log(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_module_info::description, LOG_ERROR, LOG_NOTICE, and sqlite_log().
{
char *zErr;
char fn[PATH_MAX];
int res;
ast_log(LOG_NOTICE, "This module has been marked deprecated in favor of "
"using cdr_sqlite3_custom.\n");
/* is the database there? */
snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
db = sqlite_open(fn, AST_FILE_MODE, &zErr);
if (!db) {
ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
ast_free(zErr);
return AST_MODULE_LOAD_DECLINE;
}
/* is the table there? */
res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
if (res) {
res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
if (res) {
ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
ast_free(zErr);
goto err;
}
/* TODO: here we should probably create an index */
}
res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
if (res) {
ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
return AST_MODULE_LOAD_DECLINE;
}
return AST_MODULE_LOAD_SUCCESS;
err:
if (db)
sqlite_close(db);
return AST_MODULE_LOAD_DECLINE;
}
| static int sqlite_log | ( | struct ast_cdr * | cdr | ) | [static] |
Definition at line 107 of file cdr_sqlite.c.
References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_free, ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_tvdiff_us(), ast_tvzero(), ast_cdr::billsec, ast_cdr::channel, ast_cdr::clid, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, ast_cdr::duration, ast_cdr::end, format_date(), ast_cdr::lastapp, ast_cdr::lastdata, LOG_ERROR, LOG_HRTIME, LOG_UNIQUEID, LOG_USERFIELD, sqlite_lock, ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield.
Referenced by load_module().
{
int res = 0;
char *zErr = 0;
char startstr[80], answerstr[80], endstr[80];
int count;
#if LOG_HRTIME
double hrbillsec = 0.0;
double hrduration;
#endif
ast_mutex_lock(&sqlite_lock);
format_date(startstr, sizeof(startstr), &cdr->start);
format_date(answerstr, sizeof(answerstr), &cdr->answer);
format_date(endstr, sizeof(endstr), &cdr->end);
#if LOG_HRTIME
if (!ast_tvzero(cdr->answer)) {
hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
}
hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
#endif
for(count=0; count<5; count++) {
res = sqlite_exec_printf(db,
"INSERT INTO cdr ("
"clid,src,dst,dcontext,"
"channel,dstchannel,lastapp,lastdata, "
"start,answer,end,"
"duration,billsec,disposition,amaflags, "
"accountcode"
# if LOG_UNIQUEID
",uniqueid"
# endif
# if LOG_USERFIELD
",userfield"
# endif
") VALUES ("
"'%q', '%q', '%q', '%q', "
"'%q', '%q', '%q', '%q', "
"'%q', '%q', '%q', "
#if LOG_HRTIME
"%f, %f, %d, %d, "
#else
"%d, %d, %d, %d, "
#endif
"'%q'"
# if LOG_UNIQUEID
",'%q'"
# endif
# if LOG_USERFIELD
",'%q'"
# endif
")", NULL, NULL, &zErr,
cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
startstr, answerstr, endstr,
#if LOG_HRTIME
hrduration, hrbillsec, cdr->disposition, cdr->amaflags,
#else
cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
#endif
cdr->accountcode
# if LOG_UNIQUEID
,cdr->uniqueid
# endif
# if LOG_USERFIELD
,cdr->userfield
# endif
);
if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
break;
usleep(200);
}
if (zErr) {
ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
ast_free(zErr);
}
ast_mutex_unlock(&sqlite_lock);
return res;
}
| static int unload_module | ( | void | ) | [static] |
Definition at line 192 of file cdr_sqlite.c.
References ast_cdr_unregister().
{
ast_cdr_unregister(name);
if (db) {
sqlite_close(db);
}
return 0;
}
struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SQLite CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, } [static] |
Definition at line 249 of file cdr_sqlite.c.
struct ast_module_info* ast_module_info = &__mod_info [static] |
Definition at line 249 of file cdr_sqlite.c.
sqlite* db = NULL [static] |
Definition at line 63 of file cdr_sqlite.c.
Referenced by ast_config_internal_load(), ast_destroy_realtime(), ast_load_realtime_helper(), ast_load_realtime_multientry(), ast_realtime_require_field(), ast_store_realtime(), ast_unload_realtime(), ast_update2_realtime(), ast_update_realtime(), my_swap_subchannels(), new_realtime_sqlite3_db(), parse_config(), realtime_sqlite3_execute(), and realtime_sqlite3_require().
const char name[] = "sqlite" [static] |
Definition at line 62 of file cdr_sqlite.c.
const char sql_create_table[] = ");" [static] |
SQL table format.
Definition at line 68 of file cdr_sqlite.c.
ast_mutex_t sqlite_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 } [static] |
Definition at line 65 of file cdr_sqlite.c.
Referenced by sqlite_log().