2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2005, Holger Schurig
6 * Holger Schurig <hs4233@mail.mn-solutions.de>
8 * Ideas taken from other cdr_*.c files
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
23 * Store CDR records in a SQLite database.
27 #include <sys/types.h>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/channel.h"
39 #include "asterisk/module.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/utils.h"
43 #define LOG_UNIQUEID 0
44 #define LOG_USERFIELD 0
46 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
47 #define DATE_FORMAT "%Y-%m-%d %T"
49 static char *desc = "SQLite CDR Backend";
50 static char *name = "sqlite";
51 static sqlite* db = NULL;
53 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
55 static char sql_create_table[] = "CREATE TABLE cdr ("
56 " AcctId INTEGER PRIMARY KEY,"
60 " dcontext VARCHAR(80),"
61 " channel VARCHAR(80),"
62 " dstchannel VARCHAR(80),"
63 " lastapp VARCHAR(80),"
64 " lastdata VARCHAR(80),"
70 " disposition INTEGER,"
72 " accountcode VARCHAR(20)"
74 " ,uniqueid VARCHAR(32)"
77 " ,userfield VARCHAR(255)"
81 static int sqlite_log(struct ast_cdr *cdr)
87 char startstr[80], answerstr[80], endstr[80];
90 ast_mutex_lock(&sqlite_lock);
92 t = cdr->start.tv_sec;
94 strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
96 t = cdr->answer.tv_sec;
98 strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
101 localtime_r(&t, &tm);
102 strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
104 for(count=0; count<5; count++) {
105 res = sqlite_exec_printf(db,
107 "clid,src,dst,dcontext,"
108 "channel,dstchannel,lastapp,lastdata, "
110 "duration,billsec,disposition,amaflags, "
119 "'%q', '%q', '%q', '%q', "
120 "'%q', '%q', '%q', '%q', "
130 ")", NULL, NULL, &zErr,
131 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
132 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
133 startstr, answerstr, endstr,
134 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
143 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
149 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
153 ast_mutex_unlock(&sqlite_lock);
158 char *description(void)
163 int unload_module(void)
167 ast_cdr_unregister(name);
171 int load_module(void)
177 /* is the database there? */
178 snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
179 db = sqlite_open(fn, 0660, &zErr);
181 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
186 /* is the table there? */
187 res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
189 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
191 ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
196 /* TODO: here we should probably create an index */
199 res = ast_cdr_register(name, desc, sqlite_log);
201 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
224 return ASTERISK_GPL_KEY;