Sat Mar 24 23:27:06 2007

Asterisk developer's documentation


cdr_sqlite.c File Reference

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

#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite.h>
#include "asterisk.h"
#include "asterisk/channel.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/utils.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_UNIQUEID   0
#define LOG_USERFIELD   0

Functions

 AST_MUTEX_DEFINE_STATIC (sqlite_lock)
char * description (void)
 Provides a description of the module.
char * key ()
 Returns the ASTERISK_GPL_KEY.
int load_module (void)
 Initialize the module.
int reload (void)
 Reload stuff.
static int sqlite_log (struct ast_cdr *cdr)
int unload_module (void)
 Cleanup all module structures, sockets, etc.
int usecount (void)
 Provides a usecount.

Variables

static sqlite * db = NULL
static char * desc = "SQLite CDR Backend"
static char * name = "sqlite"
static char sql_create_table []
 SQL table format.


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

#define LOG_UNIQUEID   0
 

Definition at line 50 of file cdr_sqlite.c.

Referenced by sqlite_log().

#define LOG_USERFIELD   0
 

Definition at line 51 of file cdr_sqlite.c.

Referenced by sqlite_log().


Function Documentation

AST_MUTEX_DEFINE_STATIC sqlite_lock   ) 
 

char* description void   ) 
 

Provides a description of the module.

Returns:
a short description of your module

Definition at line 166 of file cdr_sqlite.c.

00167 {
00168    return desc;
00169 }

char* key void   ) 
 

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

 char *key(void) {
         return ASTERISK_GPL_KEY;
 }

Returns:
ASTERISK_GPL_KEY

Definition at line 230 of file cdr_sqlite.c.

References ASTERISK_GPL_KEY.

00231 {
00232    return ASTERISK_GPL_KEY;
00233 }

int load_module void   ) 
 

Initialize the module.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
int Always 0.

Definition at line 179 of file cdr_sqlite.c.

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

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

int reload void   ) 
 

Reload stuff.

This function is where any reload routines take place. Re-read config files, change signalling, whatever is appropriate on a reload.

Returns:
The return value is not used.

Definition at line 220 of file cdr_sqlite.c.

00221 {
00222    return 0;
00223 }

static int sqlite_log struct ast_cdr cdr  )  [static]
 

Definition at line 89 of file cdr_sqlite.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_mutex_lock(), 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, ast_cdr::lastapp, ast_cdr::lastdata, LOG_UNIQUEID, LOG_USERFIELD, ast_cdr::src, ast_cdr::start, t, ast_cdr::uniqueid, and ast_cdr::userfield.

Referenced by load_module().

00090 {
00091    int res = 0;
00092    char *zErr = 0;
00093    struct tm tm;
00094    time_t t;
00095    char startstr[80], answerstr[80], endstr[80];
00096    int count;
00097 
00098    ast_mutex_lock(&sqlite_lock);
00099 
00100    t = cdr->start.tv_sec;
00101    localtime_r(&t, &tm);
00102    strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
00103 
00104    t = cdr->answer.tv_sec;
00105    localtime_r(&t, &tm);
00106    strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
00107 
00108    t = cdr->end.tv_sec;
00109    localtime_r(&t, &tm);
00110    strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
00111 
00112    for(count=0; count<5; count++) {
00113       res = sqlite_exec_printf(db,
00114          "INSERT INTO cdr ("
00115             "clid,src,dst,dcontext,"
00116             "channel,dstchannel,lastapp,lastdata, "
00117             "start,answer,end,"
00118             "duration,billsec,disposition,amaflags, "
00119             "accountcode"
00120 #           if LOG_UNIQUEID
00121             ",uniqueid"
00122 #           endif
00123 #           if LOG_USERFIELD
00124             ",userfield"
00125 #           endif
00126          ") VALUES ("
00127             "'%q', '%q', '%q', '%q', "
00128             "'%q', '%q', '%q', '%q', "
00129             "'%q', '%q', '%q', "
00130             "%d, %d, %d, %d, "
00131             "'%q'"
00132 #           if LOG_UNIQUEID
00133             ",'%q'"
00134 #           endif
00135 #           if LOG_USERFIELD
00136             ",'%q'"
00137 #           endif
00138          ")", NULL, NULL, &zErr,
00139             cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
00140             cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
00141             startstr, answerstr, endstr,
00142             cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
00143             cdr->accountcode
00144 #           if LOG_UNIQUEID
00145             ,cdr->uniqueid
00146 #           endif
00147 #           if LOG_USERFIELD
00148             ,cdr->userfield
00149 #           endif
00150          );
00151       if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
00152          break;
00153       usleep(200);
00154    }
00155    
00156    if (zErr) {
00157       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00158       free(zErr);
00159    }
00160 
00161    ast_mutex_unlock(&sqlite_lock);
00162    return res;
00163 }

int unload_module void   ) 
 

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
Zero on success, or non-zero on error.

Definition at line 171 of file cdr_sqlite.c.

References ast_cdr_unregister().

00172 {
00173    if (db)
00174       sqlite_close(db);
00175    ast_cdr_unregister(name);
00176    return 0;
00177 }

int usecount void   ) 
 

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
The module's usecount.

Definition at line 225 of file cdr_sqlite.c.

00226 {
00227    return 0;
00228 }


Variable Documentation

sqlite* db = NULL [static]
 

Definition at line 58 of file cdr_sqlite.c.

Referenced by ast_config_internal_load(), ast_load_realtime(), ast_load_realtime_multientry(), and ast_update_realtime().

char* desc = "SQLite CDR Backend" [static]
 

Definition at line 56 of file cdr_sqlite.c.

char* name = "sqlite" [static]
 

Definition at line 57 of file cdr_sqlite.c.

char sql_create_table[] [static]
 

SQL table format.

Definition at line 63 of file cdr_sqlite.c.


Generated on Sat Mar 24 23:27:06 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.6