Wed Oct 28 11:45:28 2009

Asterisk developer's documentation


cdr_sqlite.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2004 - 2005, Holger Schurig
00005  *
00006  *
00007  * Ideas taken from other cdr_*.c files
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief Store CDR records in a SQLite database.
00023  * 
00024  * \author Holger Schurig <hs4233@mail.mn-solutions.de>
00025  * \extref SQLite http://www.sqlite.org/
00026  *
00027  * See also
00028  * \arg \ref Config_cdr
00029  * \arg http://www.sqlite.org/
00030  * 
00031  * Creates the database and table on-the-fly
00032  * \ingroup cdr_drivers
00033  *
00034  * \note This module has been marked deprecated in favor for cdr_sqlite3_custom
00035  */
00036 
00037 /*** MODULEINFO
00038    <depend>sqlite</depend>
00039  ***/
00040 
00041 #include "asterisk.h"
00042 
00043 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 89468 $")
00044 
00045 #include <sqlite.h>
00046 
00047 #include "asterisk/channel.h"
00048 #include "asterisk/module.h"
00049 #include "asterisk/utils.h"
00050 #include "asterisk/paths.h"
00051 
00052 #define LOG_UNIQUEID 0
00053 #define LOG_USERFIELD   0
00054 
00055 /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
00056 #define DATE_FORMAT "%Y-%m-%d %T"
00057 
00058 static char *name = "sqlite";
00059 static sqlite* db = NULL;
00060 
00061 AST_MUTEX_DEFINE_STATIC(sqlite_lock);
00062 
00063 /*! \brief SQL table format */
00064 static char sql_create_table[] = "CREATE TABLE cdr ("
00065 "  AcctId      INTEGER PRIMARY KEY,"
00066 "  clid     VARCHAR(80),"
00067 "  src      VARCHAR(80),"
00068 "  dst      VARCHAR(80),"
00069 "  dcontext VARCHAR(80),"
00070 "  channel     VARCHAR(80),"
00071 "  dstchannel  VARCHAR(80),"
00072 "  lastapp     VARCHAR(80),"
00073 "  lastdata VARCHAR(80),"
00074 "  start    CHAR(19),"
00075 "  answer      CHAR(19),"
00076 "  end      CHAR(19),"
00077 "  duration INTEGER,"
00078 "  billsec     INTEGER,"
00079 "  disposition INTEGER,"
00080 "  amaflags INTEGER,"
00081 "  accountcode VARCHAR(20)"
00082 #if LOG_UNIQUEID
00083 "  ,uniqueid   VARCHAR(32)"
00084 #endif
00085 #if LOG_USERFIELD
00086 "  ,userfield  VARCHAR(255)"
00087 #endif
00088 ");";
00089 
00090 static int sqlite_log(struct ast_cdr *cdr)
00091 {
00092    int res = 0;
00093    char *zErr = 0;
00094    struct ast_tm tm;
00095    char startstr[80], answerstr[80], endstr[80];
00096    int count;
00097 
00098    ast_mutex_lock(&sqlite_lock);
00099 
00100    ast_localtime(&cdr->start, &tm, NULL);
00101    ast_strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
00102 
00103    ast_localtime(&cdr->answer, &tm, NULL);
00104    ast_strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
00105 
00106    ast_localtime(&cdr->end, &tm, NULL);
00107    ast_strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
00108 
00109    for(count=0; count<5; count++) {
00110       res = sqlite_exec_printf(db,
00111          "INSERT INTO cdr ("
00112             "clid,src,dst,dcontext,"
00113             "channel,dstchannel,lastapp,lastdata, "
00114             "start,answer,end,"
00115             "duration,billsec,disposition,amaflags, "
00116             "accountcode"
00117 #           if LOG_UNIQUEID
00118             ",uniqueid"
00119 #           endif
00120 #           if LOG_USERFIELD
00121             ",userfield"
00122 #           endif
00123          ") VALUES ("
00124             "'%q', '%q', '%q', '%q', "
00125             "'%q', '%q', '%q', '%q', "
00126             "'%q', '%q', '%q', "
00127             "%d, %d, %d, %d, "
00128             "'%q'"
00129 #           if LOG_UNIQUEID
00130             ",'%q'"
00131 #           endif
00132 #           if LOG_USERFIELD
00133             ",'%q'"
00134 #           endif
00135          ")", NULL, NULL, &zErr,
00136             cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
00137             cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
00138             startstr, answerstr, endstr,
00139             cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
00140             cdr->accountcode
00141 #           if LOG_UNIQUEID
00142             ,cdr->uniqueid
00143 #           endif
00144 #           if LOG_USERFIELD
00145             ,cdr->userfield
00146 #           endif
00147          );
00148       if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
00149          break;
00150       usleep(200);
00151    }
00152    
00153    if (zErr) {
00154       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00155       ast_free(zErr);
00156    }
00157 
00158    ast_mutex_unlock(&sqlite_lock);
00159    return res;
00160 }
00161 
00162 static int unload_module(void)
00163 {
00164    if (db)
00165       sqlite_close(db);
00166    ast_cdr_unregister(name);
00167    return 0;
00168 }
00169 
00170 static int load_module(void)
00171 {
00172    char *zErr;
00173    char fn[PATH_MAX];
00174    int res;
00175 
00176    ast_log(LOG_WARNING, "This module has been marked deprecated in favor of "
00177       "using cdr_sqlite3_custom. (May be removed after Asterisk 1.6)\n");
00178 
00179    /* is the database there? */
00180    snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
00181    db = sqlite_open(fn, AST_FILE_MODE, &zErr);
00182    if (!db) {
00183       ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
00184       ast_free(zErr);
00185       return -1;
00186    }
00187 
00188    /* is the table there? */
00189    res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
00190    if (res) {
00191       res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
00192       if (res) {
00193          ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
00194          ast_free(zErr);
00195          goto err;
00196       }
00197 
00198       /* TODO: here we should probably create an index */
00199    }
00200    
00201    res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
00202    if (res) {
00203       ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
00204       return -1;
00205    }
00206    return 0;
00207 
00208 err:
00209    if (db)
00210       sqlite_close(db);
00211    return -1;
00212 }
00213 
00214 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SQLite CDR Backend");

Generated on Wed Oct 28 11:45:28 2009 for Asterisk - the Open Source PBX by  doxygen 1.5.6