1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2004 - 2005, Holger Schurig
*
*
* Ideas taken from other cdr_*.c files
*
* 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 Store CDR records in a SQLite database.
*
* \author Holger Schurig <hs4233@mail.mn-solutions.de>
* \extref SQLite http://www.sqlite.org/
*
* See also
* \arg \ref Config_cdr
* \arg http://www.sqlite.org/
*
* Creates the database and table on-the-fly
* \ingroup cdr_drivers
*
* \note This module has been marked deprecated in favor for cdr_sqlite3_custom
*/
/*** MODULEINFO
<depend>sqlite</depend>
<defaultenabled>no</defaultenabled>
<support_level>deprecated</support_level>
<replacement>sqlite3_custom</replacement>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328259 $")
#include <sqlite.h>
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "asterisk/paths.h"
#define LOG_UNIQUEID 0
#define LOG_USERFIELD 0
#define LOG_HRTIME 0
/* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
#define DATE_FORMAT "%Y-%m-%d %T"
static const char name[] = "sqlite";
static sqlite* db = NULL;
AST_MUTEX_DEFINE_STATIC(sqlite_lock);
/*! \brief SQL table format */
static const char sql_create_table[] = "CREATE TABLE cdr ("
" AcctId INTEGER PRIMARY KEY,"
" clid VARCHAR(80),"
" src VARCHAR(80),"
" dst VARCHAR(80),"
" dcontext VARCHAR(80),"
" channel VARCHAR(80),"
" dstchannel VARCHAR(80),"
" lastapp VARCHAR(80),"
" lastdata VARCHAR(80),"
" start CHAR(19),"
" answer CHAR(19),"
" end CHAR(19),"
#if LOG_HRTIME
" duration FLOAT,"
" billsec FLOAT,"
#else
" duration INTEGER,"
" billsec INTEGER,"
#endif
" disposition INTEGER,"
" amaflags INTEGER,"
" accountcode VARCHAR(20)"
#if LOG_UNIQUEID
" ,uniqueid VARCHAR(32)"
#endif
#if LOG_USERFIELD
" ,userfield VARCHAR(255)"
#endif
");";
static void format_date(char *buffer, size_t length, struct timeval *when)
{
struct ast_tm tm;
ast_localtime(when, &tm, NULL);
ast_strftime(buffer, length, DATE_FORMAT, &tm);
}
static int sqlite_log(struct ast_cdr *cdr)
{
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)
{
ast_cdr_unregister(name);
if (db) {
sqlite_close(db);
}
return 0;
}
static int load_module(void)
{
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;
}
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite CDR Backend",
.load = load_module,
.unload = unload_module,
.load_pri = AST_MODPRI_CDR_DRIVER,
);
|