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"

Include dependency graph for cdr_sqlite.c:

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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_DEPRECATED, .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 = { PTHREAD_MUTEX_INITIALIZER , NULL, 1 }


Detailed Description

Store CDR records in a SQLite database.

Author:
Holger Schurig <hs4233@mail.mn-solutions.de> 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 60 of file cdr_sqlite.c.

#define LOG_HRTIME   0

Definition at line 57 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_UNIQUEID   0

Definition at line 55 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_USERFIELD   0

Definition at line 56 of file cdr_sqlite.c.

Referenced by sqlite_log().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 253 of file cdr_sqlite.c.

static void __unreg_module ( void   )  [static]

Definition at line 253 of file cdr_sqlite.c.

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

Definition at line 99 of file cdr_sqlite.c.

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

Referenced by sqlite_log().

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

static int load_module ( void   )  [static]

Definition at line 204 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, NULL, and sqlite_log().

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

static int sqlite_log ( struct ast_cdr cdr  )  [static]

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

Referenced by load_module().

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 }

static int unload_module ( void   )  [static]

Definition at line 192 of file cdr_sqlite.c.

References ast_cdr_unregister().

00193 {
00194    if (ast_cdr_unregister(name)) {
00195       return -1;
00196    }
00197 
00198    if (db) {
00199       sqlite_close(db);
00200    }
00201    return 0;
00202 }


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 = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_DEPRECATED, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, } [static]

Definition at line 253 of file cdr_sqlite.c.

Definition at line 253 of file cdr_sqlite.c.

sqlite* db = NULL [static]

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

Definition at line 62 of file cdr_sqlite.c.

const char sql_create_table[] [static]

SQL table format.

Definition at line 68 of file cdr_sqlite.c.

ast_mutex_t sqlite_lock = { PTHREAD_MUTEX_INITIALIZER , NULL, 1 } [static]

Definition at line 65 of file cdr_sqlite.c.

Referenced by sqlite_log().


Generated on Thu Apr 16 06:29:25 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6