2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2005, Holger Schurig
7 * Ideas taken from other cdr_*.c files
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Store CDR records in a SQLite database.
24 * \author Holger Schurig <hs4233@mail.mn-solutions.de>
27 * \arg \ref Config_cdr
28 * \arg http://www.sqlite.org/
30 * Creates the database and table on-the-fly
31 * \ingroup cdr_drivers
35 <depend>sqlite</depend>
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42 #include <sys/types.h>
49 #include "asterisk/channel.h"
50 #include "asterisk/module.h"
51 #include "asterisk/logger.h"
52 #include "asterisk/utils.h"
54 #define LOG_UNIQUEID 0
55 #define LOG_USERFIELD 0
57 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
58 #define DATE_FORMAT "%Y-%m-%d %T"
60 static char *desc = "SQLite CDR Backend";
61 static char *name = "sqlite";
62 static sqlite* db = NULL;
64 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
66 /*! \brief SQL table format */
67 static char sql_create_table[] = "CREATE TABLE cdr ("
68 " AcctId INTEGER PRIMARY KEY,"
72 " dcontext VARCHAR(80),"
73 " channel VARCHAR(80),"
74 " dstchannel VARCHAR(80),"
75 " lastapp VARCHAR(80),"
76 " lastdata VARCHAR(80),"
82 " disposition INTEGER,"
84 " accountcode VARCHAR(20)"
86 " ,uniqueid VARCHAR(32)"
89 " ,userfield VARCHAR(255)"
93 static int sqlite_log(struct ast_cdr *cdr)
99 char startstr[80], answerstr[80], endstr[80];
102 ast_mutex_lock(&sqlite_lock);
104 t = cdr->start.tv_sec;
105 localtime_r(&t, &tm);
106 strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
108 t = cdr->answer.tv_sec;
109 localtime_r(&t, &tm);
110 strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
113 localtime_r(&t, &tm);
114 strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
116 for(count=0; count<5; count++) {
117 res = sqlite_exec_printf(db,
119 "clid,src,dst,dcontext,"
120 "channel,dstchannel,lastapp,lastdata, "
122 "duration,billsec,disposition,amaflags, "
131 "'%q', '%q', '%q', '%q', "
132 "'%q', '%q', '%q', '%q', "
142 ")", NULL, NULL, &zErr,
143 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
144 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
145 startstr, answerstr, endstr,
146 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
155 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
161 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
165 ast_mutex_unlock(&sqlite_lock);
170 static const char *description(void)
175 static int unload_module(void *mod)
179 ast_cdr_unregister(name);
183 static int load_module(void *mod)
189 /* is the database there? */
190 snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
191 db = sqlite_open(fn, 0660, &zErr);
193 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
198 /* is the table there? */
199 res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
201 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
203 ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
208 /* TODO: here we should probably create an index */
211 res = ast_cdr_register(name, desc, sqlite_log);
213 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
224 static int reload(void *mod)
229 static const char *key(void)
231 return ASTERISK_GPL_KEY;
234 STD_MOD(MOD_0, reload, NULL, NULL);