00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "asterisk.h"
00045
00046 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328259 $")
00047
00048 #include <sqlite.h>
00049
00050 #include "asterisk/channel.h"
00051 #include "asterisk/module.h"
00052 #include "asterisk/utils.h"
00053 #include "asterisk/paths.h"
00054
00055 #define LOG_UNIQUEID 0
00056 #define LOG_USERFIELD 0
00057 #define LOG_HRTIME 0
00058
00059
00060 #define DATE_FORMAT "%Y-%m-%d %T"
00061
00062 static const char name[] = "sqlite";
00063 static sqlite* db = NULL;
00064
00065 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
00066
00067
00068 static const char sql_create_table[] = "CREATE TABLE cdr ("
00069 " AcctId INTEGER PRIMARY KEY,"
00070 " clid VARCHAR(80),"
00071 " src VARCHAR(80),"
00072 " dst VARCHAR(80),"
00073 " dcontext VARCHAR(80),"
00074 " channel VARCHAR(80),"
00075 " dstchannel VARCHAR(80),"
00076 " lastapp VARCHAR(80),"
00077 " lastdata VARCHAR(80),"
00078 " start CHAR(19),"
00079 " answer CHAR(19),"
00080 " end CHAR(19),"
00081 #if LOG_HRTIME
00082 " duration FLOAT,"
00083 " billsec FLOAT,"
00084 #else
00085 " duration INTEGER,"
00086 " billsec INTEGER,"
00087 #endif
00088 " disposition INTEGER,"
00089 " amaflags INTEGER,"
00090 " accountcode VARCHAR(20)"
00091 #if LOG_UNIQUEID
00092 " ,uniqueid VARCHAR(32)"
00093 #endif
00094 #if LOG_USERFIELD
00095 " ,userfield VARCHAR(255)"
00096 #endif
00097 ");";
00098
00099 static void format_date(char *buffer, size_t length, struct timeval *when)
00100 {
00101 struct ast_tm tm;
00102
00103 ast_localtime(when, &tm, NULL);
00104 ast_strftime(buffer, length, DATE_FORMAT, &tm);
00105 }
00106
00107 static int sqlite_log(struct ast_cdr *cdr)
00108 {
00109 int res = 0;
00110 char *zErr = 0;
00111 char startstr[80], answerstr[80], endstr[80];
00112 int count;
00113 #if LOG_HRTIME
00114 double hrbillsec = 0.0;
00115 double hrduration;
00116 #endif
00117
00118 ast_mutex_lock(&sqlite_lock);
00119
00120 format_date(startstr, sizeof(startstr), &cdr->start);
00121 format_date(answerstr, sizeof(answerstr), &cdr->answer);
00122 format_date(endstr, sizeof(endstr), &cdr->end);
00123
00124 #if LOG_HRTIME
00125 if (!ast_tvzero(cdr->answer)) {
00126 hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
00127 }
00128 hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
00129 #endif
00130
00131 for(count=0; count<5; count++) {
00132 res = sqlite_exec_printf(db,
00133 "INSERT INTO cdr ("
00134 "clid,src,dst,dcontext,"
00135 "channel,dstchannel,lastapp,lastdata, "
00136 "start,answer,end,"
00137 "duration,billsec,disposition,amaflags, "
00138 "accountcode"
00139 # if LOG_UNIQUEID
00140 ",uniqueid"
00141 # endif
00142 # if LOG_USERFIELD
00143 ",userfield"
00144 # endif
00145 ") VALUES ("
00146 "'%q', '%q', '%q', '%q', "
00147 "'%q', '%q', '%q', '%q', "
00148 "'%q', '%q', '%q', "
00149 #if LOG_HRTIME
00150 "%f, %f, %d, %d, "
00151 #else
00152 "%d, %d, %d, %d, "
00153 #endif
00154 "'%q'"
00155 # if LOG_UNIQUEID
00156 ",'%q'"
00157 # endif
00158 # if LOG_USERFIELD
00159 ",'%q'"
00160 # endif
00161 ")", NULL, NULL, &zErr,
00162 cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
00163 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
00164 startstr, answerstr, endstr,
00165 #if LOG_HRTIME
00166 hrduration, hrbillsec, cdr->disposition, cdr->amaflags,
00167 #else
00168 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
00169 #endif
00170 cdr->accountcode
00171 # if LOG_UNIQUEID
00172 ,cdr->uniqueid
00173 # endif
00174 # if LOG_USERFIELD
00175 ,cdr->userfield
00176 # endif
00177 );
00178 if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
00179 break;
00180 usleep(200);
00181 }
00182
00183 if (zErr) {
00184 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00185 ast_free(zErr);
00186 }
00187
00188 ast_mutex_unlock(&sqlite_lock);
00189 return res;
00190 }
00191
00192 static int unload_module(void)
00193 {
00194 ast_cdr_unregister(name);
00195 if (db) {
00196 sqlite_close(db);
00197 }
00198 return 0;
00199 }
00200
00201 static int load_module(void)
00202 {
00203 char *zErr;
00204 char fn[PATH_MAX];
00205 int res;
00206
00207 ast_log(LOG_NOTICE, "This module has been marked deprecated in favor of "
00208 "using cdr_sqlite3_custom.\n");
00209
00210
00211 snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
00212 db = sqlite_open(fn, AST_FILE_MODE, &zErr);
00213 if (!db) {
00214 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00215 ast_free(zErr);
00216 return AST_MODULE_LOAD_DECLINE;
00217 }
00218
00219
00220 res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
00221 if (res) {
00222 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
00223 if (res) {
00224 ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
00225 ast_free(zErr);
00226 goto err;
00227 }
00228
00229
00230 }
00231
00232 res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
00233 if (res) {
00234 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
00235 return AST_MODULE_LOAD_DECLINE;
00236 }
00237 return AST_MODULE_LOAD_SUCCESS;
00238
00239 err:
00240 if (db)
00241 sqlite_close(db);
00242 return AST_MODULE_LOAD_DECLINE;
00243 }
00244
00245 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SQLite CDR Backend",
00246 .load = load_module,
00247 .unload = unload_module,
00248 .load_pri = AST_MODPRI_CDR_DRIVER,
00249 );