Sat Aug 6 00:39:40 2011

Asterisk developer's documentation


cdr_sqlite.c File Reference

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

#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

static void __reg_module (void)
static void __unreg_module (void)
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_DEFAULT | AST_MODFLAG_BUILDSUM, .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 = "361d7bb937402d51e4658efb5b4d76e4" , .load = load_module, .unload = unload_module, }
static const struct ast_module_infoast_module_info = &__mod_info
static sqlite * db = NULL
static char * name = "sqlite"
static char sql_create_table []
 SQL table format.
static ast_mutex_t sqlite_lock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER )


Detailed Description

Store CDR records in a SQLite database.

Author:
Holger Schurig <hs4233@mail.mn-solutions.de>
See also Creates the database and table on-the-fly

Definition in file cdr_sqlite.c.


Define Documentation

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

Definition at line 58 of file cdr_sqlite.c.

#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

static void __reg_module ( void   )  [static]

Definition at line 218 of file cdr_sqlite.c.

static void __unreg_module ( void   )  [static]

Definition at line 218 of file cdr_sqlite.c.

static int load_module ( void   )  [static]

Definition at line 177 of file cdr_sqlite.c.

References ast_cdr_register(), ast_config_AST_LOG_DIR, ast_log(), free, LOG_ERROR, and sqlite_log().

00178 {
00179    char *zErr;
00180    char fn[PATH_MAX];
00181    int res;
00182 
00183    /* is the database there? */
00184    snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
00185    db = sqlite_open(fn, 0660, &zErr);
00186    if (!db) {
00187       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00188       free(zErr);
00189       return -1;
00190    }
00191 
00192    /* is the table there? */
00193    res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
00194    if (res) {
00195       res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
00196       if (res) {
00197          ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
00198          free(zErr);
00199          goto err;
00200       }
00201 
00202       /* TODO: here we should probably create an index */
00203    }
00204    
00205    res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
00206    if (res) {
00207       ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
00208       return -1;
00209    }
00210    return 0;
00211 
00212 err:
00213    if (db)
00214       sqlite_close(db);
00215    return -1;
00216 }

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, sqlite_lock, 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    ast_cdr_unregister(name);
00171    if (db) {
00172       sqlite_close(db);
00173    }
00174    return 0;
00175 }


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT | AST_MODFLAG_BUILDSUM, .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 = "361d7bb937402d51e4658efb5b4d76e4" , .load = load_module, .unload = unload_module, } [static]

Definition at line 218 of file cdr_sqlite.c.

const struct ast_module_info* ast_module_info = &__mod_info [static]

Definition at line 218 of file cdr_sqlite.c.

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]

SQL table format.

Definition at line 66 of file cdr_sqlite.c.

ast_mutex_t sqlite_lock = ((ast_mutex_t) PTHREAD_MUTEX_INITIALIZER ) [static]

Definition at line 63 of file cdr_sqlite.c.

Referenced by sqlite_log().


Generated on Sat Aug 6 00:39:40 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7