Wed Apr 6 11:29:54 2011

Asterisk developer's documentation


cdr_sqlite.c File Reference

Store CDR records in a SQLite database. More...

#include "asterisk.h"
#include <sqlite.h>
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#include "asterisk/paths.h"

Go to the source code of this file.

Defines

#define DATE_FORMAT   "%Y-%m-%d %T"
#define LOG_HRTIME   0
#define LOG_UNIQUEID   0
#define LOG_USERFIELD   0

Functions

static void __reg_module (void)
static void __unreg_module (void)
static void format_date (char *buffer, size_t length, struct timeval *when)
static int load_module (void)
static int sqlite_log (struct ast_cdr *cdr)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SQLite CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, }
static struct ast_module_infoast_module_info = &__mod_info
static sqlite * db = NULL
static const char name [] = "sqlite"
static const char sql_create_table []
 SQL table format.
static ast_mutex_t sqlite_lock = { { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } , 1, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP }


Detailed Description

Store CDR records in a SQLite database.

Author:
Holger Schurig <hs4233@mail.mn-solutions.de>
ExtRef:
SQLite http://www.sqlite.org/
See also Creates the database and table on-the-fly

Note:
This module has been marked deprecated in favor for cdr_sqlite3_custom

Definition in file cdr_sqlite.c.


Define Documentation

#define DATE_FORMAT   "%Y-%m-%d %T"

Definition at line 57 of file cdr_sqlite.c.

#define LOG_HRTIME   0

Definition at line 54 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_UNIQUEID   0

Definition at line 52 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_USERFIELD   0

Definition at line 53 of file cdr_sqlite.c.

Referenced by sqlite_log().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 246 of file cdr_sqlite.c.

static void __unreg_module ( void   )  [static]

Definition at line 246 of file cdr_sqlite.c.

static void format_date ( char *  buffer,
size_t  length,
struct timeval *  when 
) [static]

Definition at line 96 of file cdr_sqlite.c.

References ast_localtime(), ast_strftime(), and DATE_FORMAT.

Referenced by sqlite_log().

00097 {
00098    struct ast_tm tm;
00099 
00100    ast_localtime(when, &tm, NULL);
00101    ast_strftime(buffer, length, DATE_FORMAT, &tm);
00102 }

static int load_module ( void   )  [static]

Definition at line 198 of file cdr_sqlite.c.

References ast_cdr_register(), ast_config_AST_LOG_DIR, AST_FILE_MODE, ast_free, ast_log(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, LOG_ERROR, LOG_NOTICE, and sqlite_log().

00199 {
00200    char *zErr;
00201    char fn[PATH_MAX];
00202    int res;
00203 
00204    ast_log(LOG_NOTICE, "This module has been marked deprecated in favor of "
00205       "using cdr_sqlite3_custom.\n");
00206 
00207    /* is the database there? */
00208    snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
00209    db = sqlite_open(fn, AST_FILE_MODE, &zErr);
00210    if (!db) {
00211       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00212       ast_free(zErr);
00213       return AST_MODULE_LOAD_DECLINE;
00214    }
00215 
00216    /* is the table there? */
00217    res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
00218    if (res) {
00219       res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
00220       if (res) {
00221          ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
00222          ast_free(zErr);
00223          goto err;
00224       }
00225 
00226       /* TODO: here we should probably create an index */
00227    }
00228 
00229    res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
00230    if (res) {
00231       ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
00232       return AST_MODULE_LOAD_DECLINE;
00233    }
00234    return AST_MODULE_LOAD_SUCCESS;
00235 
00236 err:
00237    if (db)
00238       sqlite_close(db);
00239    return AST_MODULE_LOAD_DECLINE;
00240 }

static int sqlite_log ( struct ast_cdr cdr  )  [static]

Definition at line 104 of file cdr_sqlite.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_free, ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_tvdiff_us(), ast_tvzero(), ast_cdr::billsec, ast_cdr::channel, ast_cdr::clid, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, ast_cdr::duration, ast_cdr::end, format_date(), ast_cdr::lastapp, ast_cdr::lastdata, LOG_ERROR, LOG_HRTIME, LOG_UNIQUEID, LOG_USERFIELD, sqlite_lock, ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield.

Referenced by load_module().

00105 {
00106    int res = 0;
00107    char *zErr = 0;
00108    char startstr[80], answerstr[80], endstr[80];
00109    int count;
00110 #if LOG_HRTIME
00111    double hrbillsec = 0.0;
00112    double hrduration;
00113 #endif
00114 
00115    ast_mutex_lock(&sqlite_lock);
00116 
00117    format_date(startstr, sizeof(startstr), &cdr->start);
00118    format_date(answerstr, sizeof(answerstr), &cdr->answer);
00119    format_date(endstr, sizeof(endstr), &cdr->end);
00120 
00121 #if LOG_HRTIME
00122    if (!ast_tvzero(cdr->answer)) {
00123       hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
00124    }
00125    hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
00126 #endif
00127 
00128    for(count=0; count<5; count++) {
00129       res = sqlite_exec_printf(db,
00130          "INSERT INTO cdr ("
00131             "clid,src,dst,dcontext,"
00132             "channel,dstchannel,lastapp,lastdata, "
00133             "start,answer,end,"
00134             "duration,billsec,disposition,amaflags, "
00135             "accountcode"
00136 #           if LOG_UNIQUEID
00137             ",uniqueid"
00138 #           endif
00139 #           if LOG_USERFIELD
00140             ",userfield"
00141 #           endif
00142          ") VALUES ("
00143             "'%q', '%q', '%q', '%q', "
00144             "'%q', '%q', '%q', '%q', "
00145             "'%q', '%q', '%q', "
00146 #if LOG_HRTIME
00147             "%f, %f, %d, %d, "
00148 #else
00149             "%d, %d, %d, %d, "
00150 #endif
00151             "'%q'"
00152 #           if LOG_UNIQUEID
00153             ",'%q'"
00154 #           endif
00155 #           if LOG_USERFIELD
00156             ",'%q'"
00157 #           endif
00158          ")", NULL, NULL, &zErr,
00159             cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
00160             cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
00161             startstr, answerstr, endstr,
00162 #if LOG_HRTIME
00163             hrduration, hrbillsec, cdr->disposition, cdr->amaflags,
00164 #else
00165             cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
00166 #endif
00167             cdr->accountcode
00168 #           if LOG_UNIQUEID
00169             ,cdr->uniqueid
00170 #           endif
00171 #           if LOG_USERFIELD
00172             ,cdr->userfield
00173 #           endif
00174          );
00175       if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
00176          break;
00177       usleep(200);
00178    }
00179 
00180    if (zErr) {
00181       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00182       ast_free(zErr);
00183    }
00184 
00185    ast_mutex_unlock(&sqlite_lock);
00186    return res;
00187 }

static int unload_module ( void   )  [static]

Definition at line 189 of file cdr_sqlite.c.

References ast_cdr_unregister().

00190 {
00191    ast_cdr_unregister(name);
00192    if (db) {
00193       sqlite_close(db);
00194    }
00195    return 0;
00196 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "SQLite CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "8586c2a7d357cb591cc3a6607a8f62d1" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, } [static]

Definition at line 246 of file cdr_sqlite.c.

struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 246 of file cdr_sqlite.c.

sqlite* db = NULL [static]

Definition at line 60 of file cdr_sqlite.c.

Referenced by ast_config_internal_load(), ast_destroy_realtime(), ast_load_realtime_helper(), ast_load_realtime_multientry(), ast_realtime_require_field(), ast_store_realtime(), ast_unload_realtime(), ast_update2_realtime(), ast_update_realtime(), and my_swap_subchannels().

const char name[] = "sqlite" [static]

Definition at line 59 of file cdr_sqlite.c.

const char sql_create_table[] [static]

SQL table format.

Definition at line 65 of file cdr_sqlite.c.

ast_mutex_t sqlite_lock = { { { NULL }, { 0 }, 0, { NULL }, { 0 }, {{{ 0 }}}, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } , 1, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP } [static]

Definition at line 62 of file cdr_sqlite.c.

Referenced by sqlite_log().


Generated on Wed Apr 6 11:29:54 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7