Tue Aug 20 16:34:49 2013

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

 AST_MODULE_INFO (ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER,"SQLite CDR Backend",.load=load_module,.unload=unload_module,.load_pri=AST_MODPRI_CDR_DRIVER,)
 AST_MUTEX_DEFINE_STATIC (sqlite_lock)
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 sqlite * db = NULL
static const char name [] = "sqlite"
static const char sql_create_table []
 SQL table format.

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 59 of file cdr_sqlite.c.

#define LOG_HRTIME   0

Definition at line 56 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_UNIQUEID   0

Definition at line 54 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_USERFIELD   0

Definition at line 55 of file cdr_sqlite.c.

Referenced by sqlite_log().


Function Documentation

AST_MODULE_INFO ( ASTERISK_GPL_KEY  ,
AST_MODFLAG_LOAD_ORDER  ,
"SQLite CDR Backend"  ,
load = load_module,
unload = unload_module,
load_pri = AST_MODPRI_CDR_DRIVER 
)
AST_MUTEX_DEFINE_STATIC ( sqlite_lock   ) 
static void format_date ( char *  buffer,
size_t  length,
struct timeval *  when 
) [static]

Definition at line 98 of file cdr_sqlite.c.

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

Referenced by sqlite_log().

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

static int load_module ( void   )  [static]

Definition at line 200 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().

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

static int sqlite_log ( struct ast_cdr cdr  )  [static]

Definition at line 106 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, ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield.

Referenced by load_module().

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

static int unload_module ( void   )  [static]

Definition at line 191 of file cdr_sqlite.c.

References ast_cdr_unregister().

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


Variable Documentation

sqlite* db = NULL [static]
const char name[] = "sqlite" [static]

Definition at line 61 of file cdr_sqlite.c.

const char sql_create_table[] [static]

SQL table format.

Definition at line 67 of file cdr_sqlite.c.


Generated on 20 Aug 2013 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1