#include "asterisk.h"
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sqlite.h>
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
Go to the source code of this file.
Defines | |
#define | DATE_FORMAT "%Y-%m-%d %T" |
#define | LOG_UNIQUEID 0 |
#define | LOG_USERFIELD 0 |
Functions | |
AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"SQLite CDR Backend") | |
AST_MUTEX_DEFINE_STATIC (sqlite_lock) | |
static int | load_module (void) |
static int | sqlite_log (struct ast_cdr *cdr) |
static int | unload_module (void) |
Variables | |
static sqlite * | db = NULL |
static char * | name = "sqlite" |
static char | sql_create_table [] |
SQL table format. |
Definition in file cdr_sqlite.c.
#define DATE_FORMAT "%Y-%m-%d %T" |
Definition at line 58 of file cdr_sqlite.c.
#define LOG_UNIQUEID 0 |
#define LOG_USERFIELD 0 |
AST_MODULE_INFO_STANDARD | ( | ASTERISK_GPL_KEY | , | |
"SQLite CDR Backend" | ||||
) |
AST_MUTEX_DEFINE_STATIC | ( | sqlite_lock | ) |
static int load_module | ( | void | ) | [static] |
Definition at line 176 of file cdr_sqlite.c.
References ast_cdr_register(), ast_config_AST_LOG_DIR, ast_log(), free, LOG_ERROR, and sqlite_log().
00177 { 00178 char *zErr; 00179 char fn[PATH_MAX]; 00180 int res; 00181 00182 /* is the database there? */ 00183 snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR); 00184 db = sqlite_open(fn, 0660, &zErr); 00185 if (!db) { 00186 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr); 00187 free(zErr); 00188 return -1; 00189 } 00190 00191 /* is the table there? */ 00192 res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL); 00193 if (res) { 00194 res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr); 00195 if (res) { 00196 ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr); 00197 free(zErr); 00198 goto err; 00199 } 00200 00201 /* TODO: here we should probably create an index */ 00202 } 00203 00204 res = ast_cdr_register(name, ast_module_info->description, sqlite_log); 00205 if (res) { 00206 ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n"); 00207 return -1; 00208 } 00209 return 0; 00210 00211 err: 00212 if (db) 00213 sqlite_close(db); 00214 return -1; 00215 }
static int sqlite_log | ( | struct ast_cdr * | cdr | ) | [static] |
Definition at line 92 of file cdr_sqlite.c.
References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_localtime(), ast_log(), ast_mutex_lock(), ast_mutex_unlock(), ast_cdr::billsec, ast_cdr::channel, ast_cdr::clid, DATE_FORMAT, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, ast_cdr::duration, ast_cdr::end, free, ast_cdr::lastapp, ast_cdr::lastdata, LOG_ERROR, LOG_UNIQUEID, LOG_USERFIELD, ast_cdr::src, ast_cdr::start, t, ast_cdr::uniqueid, and ast_cdr::userfield.
Referenced by load_module().
00093 { 00094 int res = 0; 00095 char *zErr = 0; 00096 struct tm tm; 00097 time_t t; 00098 char startstr[80], answerstr[80], endstr[80]; 00099 int count; 00100 00101 ast_mutex_lock(&sqlite_lock); 00102 00103 t = cdr->start.tv_sec; 00104 ast_localtime(&t, &tm, NULL); 00105 strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm); 00106 00107 t = cdr->answer.tv_sec; 00108 ast_localtime(&t, &tm, NULL); 00109 strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm); 00110 00111 t = cdr->end.tv_sec; 00112 ast_localtime(&t, &tm, NULL); 00113 strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm); 00114 00115 for(count=0; count<5; count++) { 00116 res = sqlite_exec_printf(db, 00117 "INSERT INTO cdr (" 00118 "clid,src,dst,dcontext," 00119 "channel,dstchannel,lastapp,lastdata, " 00120 "start,answer,end," 00121 "duration,billsec,disposition,amaflags, " 00122 "accountcode" 00123 # if LOG_UNIQUEID 00124 ",uniqueid" 00125 # endif 00126 # if LOG_USERFIELD 00127 ",userfield" 00128 # endif 00129 ") VALUES (" 00130 "'%q', '%q', '%q', '%q', " 00131 "'%q', '%q', '%q', '%q', " 00132 "'%q', '%q', '%q', " 00133 "%d, %d, %d, %d, " 00134 "'%q'" 00135 # if LOG_UNIQUEID 00136 ",'%q'" 00137 # endif 00138 # if LOG_USERFIELD 00139 ",'%q'" 00140 # endif 00141 ")", NULL, NULL, &zErr, 00142 cdr->clid, cdr->src, cdr->dst, cdr->dcontext, 00143 cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata, 00144 startstr, answerstr, endstr, 00145 cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags, 00146 cdr->accountcode 00147 # if LOG_UNIQUEID 00148 ,cdr->uniqueid 00149 # endif 00150 # if LOG_USERFIELD 00151 ,cdr->userfield 00152 # endif 00153 ); 00154 if (res != SQLITE_BUSY && res != SQLITE_LOCKED) 00155 break; 00156 usleep(200); 00157 } 00158 00159 if (zErr) { 00160 ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr); 00161 free(zErr); 00162 } 00163 00164 ast_mutex_unlock(&sqlite_lock); 00165 return res; 00166 }
static int unload_module | ( | void | ) | [static] |
Definition at line 168 of file cdr_sqlite.c.
References ast_cdr_unregister().
00169 { 00170 if (db) 00171 sqlite_close(db); 00172 ast_cdr_unregister(name); 00173 return 0; 00174 }
sqlite* db = NULL [static] |
Definition at line 61 of file cdr_sqlite.c.
Referenced by ast_config_internal_load(), ast_load_realtime(), ast_load_realtime_multientry(), and ast_update_realtime().
char* name = "sqlite" [static] |
Definition at line 60 of file cdr_sqlite.c.
char sql_create_table[] [static] |