74f3078e3d5259b1b32d83476a8cf3ea0ae9d1e8
[asterisk/asterisk.git] / cdr / cdr_sqlite.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2005, Holger Schurig
5  *
6  *
7  * Ideas taken from other cdr_*.c files
8  *
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.
14  *
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.
18  */
19
20 /*! \file
21  *
22  * \brief Store CDR records in a SQLite database.
23  * 
24  * \author Holger Schurig <hs4233@mail.mn-solutions.de>
25  *
26  * See also
27  * \arg \ref Config_cdr
28  * \arg http://www.sqlite.org/
29  * 
30  * Creates the database and table on-the-fly
31  */
32
33 #include <sys/types.h>
34
35 #include <unistd.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <sqlite.h>
39
40 #include "asterisk.h"
41
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43
44 #include "asterisk/channel.h"
45 #include "asterisk/module.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/utils.h"
48
49 #define LOG_UNIQUEID    0
50 #define LOG_USERFIELD   0
51
52 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
53 #define DATE_FORMAT "%Y-%m-%d %T"
54
55 static char *desc = "SQLite CDR Backend";
56 static char *name = "sqlite";
57 static sqlite* db = NULL;
58
59 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
60
61 /*! \brief SQL table format */
62 static char sql_create_table[] = "CREATE TABLE cdr ("
63 "       AcctId          INTEGER PRIMARY KEY,"
64 "       clid            VARCHAR(80),"
65 "       src             VARCHAR(80),"
66 "       dst             VARCHAR(80),"
67 "       dcontext        VARCHAR(80),"
68 "       channel         VARCHAR(80),"
69 "       dstchannel      VARCHAR(80),"
70 "       lastapp         VARCHAR(80),"
71 "       lastdata        VARCHAR(80),"
72 "       start           CHAR(19),"
73 "       answer          CHAR(19),"
74 "       end             CHAR(19),"
75 "       duration        INTEGER,"
76 "       billsec         INTEGER,"
77 "       disposition     INTEGER,"
78 "       amaflags        INTEGER,"
79 "       accountcode     VARCHAR(20)"
80 #if LOG_UNIQUEID
81 "       ,uniqueid       VARCHAR(32)"
82 #endif
83 #if LOG_USERFIELD
84 "       ,userfield      VARCHAR(255)"
85 #endif
86 ");";
87
88 static int sqlite_log(struct ast_cdr *cdr)
89 {
90         int res = 0;
91         char *zErr = 0;
92         struct tm tm;
93         time_t t;
94         char startstr[80], answerstr[80], endstr[80];
95         int count;
96
97         ast_mutex_lock(&sqlite_lock);
98
99         t = cdr->start.tv_sec;
100         localtime_r(&t, &tm);
101         strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
102
103         t = cdr->answer.tv_sec;
104         localtime_r(&t, &tm);
105         strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
106
107         t = cdr->end.tv_sec;
108         localtime_r(&t, &tm);
109         strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
110
111         for(count=0; count<5; count++) {
112                 res = sqlite_exec_printf(db,
113                         "INSERT INTO cdr ("
114                                 "clid,src,dst,dcontext,"
115                                 "channel,dstchannel,lastapp,lastdata, "
116                                 "start,answer,end,"
117                                 "duration,billsec,disposition,amaflags, "
118                                 "accountcode"
119 #                               if LOG_UNIQUEID
120                                 ",uniqueid"
121 #                               endif
122 #                               if LOG_USERFIELD
123                                 ",userfield"
124 #                               endif
125                         ") VALUES ("
126                                 "'%q', '%q', '%q', '%q', "
127                                 "'%q', '%q', '%q', '%q', "
128                                 "'%q', '%q', '%q', "
129                                 "%d, %d, %d, %d, "
130                                 "'%q'"
131 #                               if LOG_UNIQUEID
132                                 ",'%q'"
133 #                               endif
134 #                               if LOG_USERFIELD
135                                 ",'%q'"
136 #                               endif
137                         ")", NULL, NULL, &zErr,
138                                 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
139                                 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
140                                 startstr, answerstr, endstr,
141                                 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
142                                 cdr->accountcode
143 #                               if LOG_UNIQUEID
144                                 ,cdr->uniqueid
145 #                               endif
146 #                               if LOG_USERFIELD
147                                 ,cdr->userfield
148 #                               endif
149                         );
150                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
151                         break;
152                 usleep(200);
153         }
154         
155         if (zErr) {
156                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
157                 free(zErr);
158         }
159
160         ast_mutex_unlock(&sqlite_lock);
161         return res;
162 }
163
164
165 char *description(void)
166 {
167         return desc;
168 }
169
170 int unload_module(void)
171 {
172         if (db)
173                 sqlite_close(db);
174         ast_cdr_unregister(name);
175         return 0;
176 }
177
178 int load_module(void)
179 {
180         char *zErr;
181         char fn[PATH_MAX];
182         int res;
183
184         /* is the database there? */
185         snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
186         db = sqlite_open(fn, 0660, &zErr);
187         if (!db) {
188                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
189                 free(zErr);
190                 return -1;
191         }
192
193         /* is the table there? */
194         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
195         if (res) {
196                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
197                 if (res) {
198                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
199                         free(zErr);
200                         goto err;
201                 }
202
203                 /* TODO: here we should probably create an index */
204         }
205         
206         res = ast_cdr_register(name, desc, sqlite_log);
207         if (res) {
208                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
209                 return -1;
210         }
211         return 0;
212
213 err:
214         if (db)
215                 sqlite_close(db);
216         return -1;
217 }
218
219 int reload(void)
220 {
221         return 0;
222 }
223
224 int usecount(void)
225 {
226         return 0;
227 }
228
229 char *key()
230 {
231         return ASTERISK_GPL_KEY;
232 }