Wed Jan 8 2020 09:49:59

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.

Macros

#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 = "ac1f6a56484a8820659555499174e588" , .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. More...
 
static ast_mutex_t sqlite_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 }
 

Detailed Description

Store CDR records in a SQLite database.

Author
Holger Schurig hs423.nosp@m.3@ma.nosp@m.il.mn.nosp@m.-sol.nosp@m.ution.nosp@m.s.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.

Macro Definition Documentation

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

Definition at line 59 of file cdr_sqlite.c.

Referenced by format_date().

#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

static void __reg_module ( void  )
static

Definition at line 248 of file cdr_sqlite.c.

static void __unreg_module ( void  )
static

Definition at line 248 of file cdr_sqlite.c.

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().

99 {
100  struct ast_tm tm;
101 
102  ast_localtime(when, &tm, NULL);
103  ast_strftime(buffer, length, DATE_FORMAT, &tm);
104 }
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1570
#define DATE_FORMAT
Definition: cdr_sqlite.c:59
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2351
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, ast_module_info::description, LOG_ERROR, LOG_NOTICE, and sqlite_log().

201 {
202  char *zErr;
203  char fn[PATH_MAX];
204  int res;
205 
206  ast_log(LOG_NOTICE, "This module has been marked deprecated in favor of "
207  "using cdr_sqlite3_custom.\n");
208 
209  /* is the database there? */
210  snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
211  db = sqlite_open(fn, AST_FILE_MODE, &zErr);
212  if (!db) {
213  ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
214  ast_free(zErr);
216  }
217 
218  /* is the table there? */
219  res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
220  if (res) {
221  res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
222  if (res) {
223  ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
224  ast_free(zErr);
225  goto err;
226  }
227 
228  /* TODO: here we should probably create an index */
229  }
230 
232  if (res) {
233  ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
235  }
237 
238 err:
239  if (db)
240  sqlite_close(db);
242 }
const char * description
Definition: module.h:234
#define AST_FILE_MODE
Definition: asterisk.h:36
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:130
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
const char * ast_config_AST_LOG_DIR
Definition: asterisk.c:263
static const char name[]
#define ast_free(a)
Definition: astmm.h:97
static sqlite * db
Definition: cdr_sqlite.c:62
static int sqlite_log(struct ast_cdr *cdr)
Definition: cdr_sqlite.c:106
static const char sql_create_table[]
SQL table format.
Definition: cdr_sqlite.c:67
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, sqlite_lock, ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield.

Referenced by load_module().

107 {
108  int res = 0;
109  char *zErr = 0;
110  char startstr[80], answerstr[80], endstr[80];
111  int count;
112 #if LOG_HRTIME
113  double hrbillsec = 0.0;
114  double hrduration;
115 #endif
116 
118 
119  format_date(startstr, sizeof(startstr), &cdr->start);
120  format_date(answerstr, sizeof(answerstr), &cdr->answer);
121  format_date(endstr, sizeof(endstr), &cdr->end);
122 
123 #if LOG_HRTIME
124  if (!ast_tvzero(cdr->answer)) {
125  hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
126  }
127  hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
128 #endif
129 
130  for(count=0; count<5; count++) {
131  res = sqlite_exec_printf(db,
132  "INSERT INTO cdr ("
133  "clid,src,dst,dcontext,"
134  "channel,dstchannel,lastapp,lastdata, "
135  "start,answer,end,"
136  "duration,billsec,disposition,amaflags, "
137  "accountcode"
138 # if LOG_UNIQUEID
139  ",uniqueid"
140 # endif
141 # if LOG_USERFIELD
142  ",userfield"
143 # endif
144  ") VALUES ("
145  "'%q', '%q', '%q', '%q', "
146  "'%q', '%q', '%q', '%q', "
147  "'%q', '%q', '%q', "
148 #if LOG_HRTIME
149  "%f, %f, %d, %d, "
150 #else
151  "%d, %d, %d, %d, "
152 #endif
153  "'%q'"
154 # if LOG_UNIQUEID
155  ",'%q'"
156 # endif
157 # if LOG_USERFIELD
158  ",'%q'"
159 # endif
160  ")", NULL, NULL, &zErr,
161  cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
162  cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
163  startstr, answerstr, endstr,
164 #if LOG_HRTIME
165  hrduration, hrbillsec, cdr->disposition, cdr->amaflags,
166 #else
167  cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
168 #endif
169  cdr->accountcode
170 # if LOG_UNIQUEID
171  ,cdr->uniqueid
172 # endif
173 # if LOG_USERFIELD
174  ,cdr->userfield
175 # endif
176  );
177  if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
178  break;
179  usleep(200);
180  }
181 
182  if (zErr) {
183  ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
184  ast_free(zErr);
185  }
186 
188  return res;
189 }
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:114
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:94
long int billsec
Definition: cdr.h:108
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:90
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:100
char uniqueid[150]
Definition: cdr.h:121
#define ast_mutex_lock(a)
Definition: lock.h:155
static ast_mutex_t sqlite_lock
Definition: cdr_sqlite.c:64
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:98
long int amaflags
Definition: cdr.h:112
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:88
#define LOG_USERFIELD
Definition: cdr_sqlite.c:55
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:92
struct timeval answer
Definition: cdr.h:102
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:96
#define LOG_ERROR
Definition: logger.h:155
#define LOG_UNIQUEID
Definition: cdr_sqlite.c:54
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_HRTIME
Definition: cdr_sqlite.c:56
struct timeval start
Definition: cdr.h:100
#define ast_free(a)
Definition: astmm.h:97
static sqlite * db
Definition: cdr_sqlite.c:62
long int duration
Definition: cdr.h:106
static void format_date(char *buffer, size_t length, struct timeval *when)
Definition: cdr_sqlite.c:98
char src[AST_MAX_EXTENSION]
Definition: cdr.h:86
struct timeval end
Definition: cdr.h:104
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
Definition: time.h:70
long int disposition
Definition: cdr.h:110
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:84
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:125
#define ast_mutex_unlock(a)
Definition: lock.h:156
static int unload_module ( void  )
static

Definition at line 191 of file cdr_sqlite.c.

References ast_cdr_unregister().

192 {
194  if (db) {
195  sqlite_close(db);
196  }
197  return 0;
198 }
static const char name[]
static sqlite * db
Definition: cdr_sqlite.c:62
void ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:165

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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, }
static

Definition at line 248 of file cdr_sqlite.c.

Definition at line 248 of file cdr_sqlite.c.

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.

ast_mutex_t sqlite_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, 1 }
static

Definition at line 64 of file cdr_sqlite.c.

Referenced by sqlite_log().