718ef4838882b6f74595c2f96c2f5962bf328543
[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  * \ingroup cdr_drivers
32  */
33
34 /*** MODULEINFO
35         <depend>sqlite</depend>
36  ***/
37
38 #include "asterisk.h"
39
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <sqlite.h>
48
49 #include "asterisk/channel.h"
50 #include "asterisk/module.h"
51 #include "asterisk/logger.h"
52 #include "asterisk/utils.h"
53
54 #define LOG_UNIQUEID    0
55 #define LOG_USERFIELD   0
56
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"
59
60 static char *desc = "SQLite CDR Backend";
61 static char *name = "sqlite";
62 static sqlite* db = NULL;
63
64 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
65
66 /*! \brief SQL table format */
67 static char sql_create_table[] = "CREATE TABLE cdr ("
68 "       AcctId          INTEGER PRIMARY KEY,"
69 "       clid            VARCHAR(80),"
70 "       src             VARCHAR(80),"
71 "       dst             VARCHAR(80),"
72 "       dcontext        VARCHAR(80),"
73 "       channel         VARCHAR(80),"
74 "       dstchannel      VARCHAR(80),"
75 "       lastapp         VARCHAR(80),"
76 "       lastdata        VARCHAR(80),"
77 "       start           CHAR(19),"
78 "       answer          CHAR(19),"
79 "       end             CHAR(19),"
80 "       duration        INTEGER,"
81 "       billsec         INTEGER,"
82 "       disposition     INTEGER,"
83 "       amaflags        INTEGER,"
84 "       accountcode     VARCHAR(20)"
85 #if LOG_UNIQUEID
86 "       ,uniqueid       VARCHAR(32)"
87 #endif
88 #if LOG_USERFIELD
89 "       ,userfield      VARCHAR(255)"
90 #endif
91 ");";
92
93 static int sqlite_log(struct ast_cdr *cdr)
94 {
95         int res = 0;
96         char *zErr = 0;
97         struct tm tm;
98         time_t t;
99         char startstr[80], answerstr[80], endstr[80];
100         int count;
101
102         ast_mutex_lock(&sqlite_lock);
103
104         t = cdr->start.tv_sec;
105         localtime_r(&t, &tm);
106         strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
107
108         t = cdr->answer.tv_sec;
109         localtime_r(&t, &tm);
110         strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
111
112         t = cdr->end.tv_sec;
113         localtime_r(&t, &tm);
114         strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
115
116         for(count=0; count<5; count++) {
117                 res = sqlite_exec_printf(db,
118                         "INSERT INTO cdr ("
119                                 "clid,src,dst,dcontext,"
120                                 "channel,dstchannel,lastapp,lastdata, "
121                                 "start,answer,end,"
122                                 "duration,billsec,disposition,amaflags, "
123                                 "accountcode"
124 #                               if LOG_UNIQUEID
125                                 ",uniqueid"
126 #                               endif
127 #                               if LOG_USERFIELD
128                                 ",userfield"
129 #                               endif
130                         ") VALUES ("
131                                 "'%q', '%q', '%q', '%q', "
132                                 "'%q', '%q', '%q', '%q', "
133                                 "'%q', '%q', '%q', "
134                                 "%d, %d, %d, %d, "
135                                 "'%q'"
136 #                               if LOG_UNIQUEID
137                                 ",'%q'"
138 #                               endif
139 #                               if LOG_USERFIELD
140                                 ",'%q'"
141 #                               endif
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,
147                                 cdr->accountcode
148 #                               if LOG_UNIQUEID
149                                 ,cdr->uniqueid
150 #                               endif
151 #                               if LOG_USERFIELD
152                                 ,cdr->userfield
153 #                               endif
154                         );
155                 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
156                         break;
157                 usleep(200);
158         }
159         
160         if (zErr) {
161                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
162                 free(zErr);
163         }
164
165         ast_mutex_unlock(&sqlite_lock);
166         return res;
167 }
168
169
170 static const char *description(void)
171 {
172         return desc;
173 }
174
175 static int unload_module(void *mod)
176 {
177         if (db)
178                 sqlite_close(db);
179         ast_cdr_unregister(name);
180         return 0;
181 }
182
183 static int load_module(void *mod)
184 {
185         char *zErr;
186         char fn[PATH_MAX];
187         int res;
188
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);
192         if (!db) {
193                 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
194                 free(zErr);
195                 return -1;
196         }
197
198         /* is the table there? */
199         res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
200         if (res) {
201                 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
202                 if (res) {
203                         ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
204                         free(zErr);
205                         goto err;
206                 }
207
208                 /* TODO: here we should probably create an index */
209         }
210         
211         res = ast_cdr_register(name, desc, sqlite_log);
212         if (res) {
213                 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
214                 return -1;
215         }
216         return 0;
217
218 err:
219         if (db)
220                 sqlite_close(db);
221         return -1;
222 }
223
224 static int reload(void *mod)
225 {
226         return 0;
227 }
228
229 static const char *key(void)
230 {
231         return ASTERISK_GPL_KEY;
232 }
233
234 STD_MOD(MOD_0, reload, NULL, NULL);