Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (C) 1999 - 2005, Digium, Inc.
* Mark Spencer <markster@digium.com>
*
* Includes code and algorithms from the Zapata library.
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
* \brief Comma Separated Value CDR records.
*
* \author Mark Spencer <markster@digium.com>
/*! \li \ref cdr_csv.c uses the configuration file \ref cdr.conf
* \addtogroup configuration_file Configuration Files
*/
/*** MODULEINFO
<support_level>extended</support_level>
***/
Kevin P. Fleming
committed
#include "asterisk.h"
#include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
Kevin P. Fleming
committed
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
#define CSV_LOG_DIR "/cdr-csv"
#define CSV_MASTER "/Master.csv"
static int loguniqueid = 0;
static int loguserfield = 0;
Rodrigo Ramírez Norambuena
committed
static int newcdrcolumns = 0;
static int maxrow = 100; /* max row stored in csv file before rotate */
static const char config[] = "cdr.conf";
Rodrigo Ramírez Norambuena
committed
static char file_csv_master[PATH_MAX];
/* #define CSV_LOGUSERFIELD 1 */
/*----------------------------------------------------
The values are as follows:
"accountcode", accountcode is the account name of detail records, Master.csv contains all records *
Detail records are configured on a channel basis, IAX and SIP are determined by user *
DAHDI is determined by channel in dahdi.conf
"callerid",
"channel",
"destination channel", (if applicable)
"last application", Last application run on the channel
"last app argument", argument to the last channel
"start time",
"answer time",
"end time",
duration, Duration is the whole length that the entire call lasted. ie. call rx'd to hangup
"end time" minus "start time"
billable seconds, the duration that a call was up after other end answered which will be <= to duration
"end time" minus "answer time"
"disposition", ANSWERED, NO ANSWER, BUSY
"amaflags", DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode.
"uniqueid", unique call identifier
"userfield" user field set via SetCDRUserField
----------------------------------------------------------*/
Rodrigo Ramírez Norambuena
committed
AST_MUTEX_DEFINE_STATIC(f_lock);
static int load_config(int reload)
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
Terry Wilson
committed
if (!(cfg = ast_config_load(config, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
ast_log(LOG_WARNING, "unable to load config: %s\n", config);
return 0;
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
Olle Johansson
committed
accountlogs = 1;
Terry Wilson
committed
usegmtime = 0;
loguniqueid = 0;
loguserfield = 0;
Rodrigo Ramírez Norambuena
committed
newcdrcolumns = 0;
Terry Wilson
committed
if (!(v = ast_variable_browse(cfg, "csv"))) {
return 0;
Rodrigo Ramírez Norambuena
committed
/* compute the location of the csv master file */
ast_mutex_lock(&f_lock);
snprintf(file_csv_master, sizeof(file_csv_master),
"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
snprintf(file_csv_temp, sizeof(file_csv_temp),
"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_TEMP);
Rodrigo Ramírez Norambuena
committed
ast_mutex_unlock(&f_lock);
for (; v; v = v->next) {
if (!strcasecmp(v->name, "usegmtime")) {
usegmtime = ast_true(v->value);
} else if (!strcasecmp(v->name, "accountlogs")) {
/* Turn on/off separate files per accountcode. Default is on (as before) */
accountlogs = ast_true(v->value);
} else if (!strcasecmp(v->name, "loguniqueid")) {
loguniqueid = ast_true(v->value);
} else if (!strcasecmp(v->name, "loguserfield")) {
loguserfield = ast_true(v->value);
Rodrigo Ramírez Norambuena
committed
} else if (!strcasecmp(v->name, "newcdrcolumns")) {
newcdrcolumns = ast_true(v->value);
} else if (!strcasecmp(v->name, "maxrow")) {
maxrow = atoi(v->value);
} else if (!strcasecmp(v->name, "prefdir")) {
snprintf(file_csv_master, sizeof(file_csv_master),
"%s/%s/%s", v->value, CSV_LOG_DIR, CSV_MASTER);
Rodrigo Ramírez Norambuena
committed
return 1;
static int append_string(char *buf, const char *s, size_t bufsize)
int pos = strlen(buf), spos = 0, error = -1;
if (!s[spos]) {
error = 0;
break;
}
if (s[spos] == '\"')
buf[pos++] = '\"';
buf[pos++] = s[spos];
spos++;
}
buf[pos++] = '\"';
buf[pos++] = ',';
buf[pos++] = '\0';
static int append_int(char *buf, int s, size_t bufsize)
if (pos + strlen(tmp) > bufsize - 3)
strncat(buf, tmp, bufsize - strlen(buf) - 1);
pos = strlen(buf);
buf[pos++] = ',';
buf[pos++] = '\0';
Wenpeng Song
committed
static int append_unsigned_int(char *buf, unsigned int s, size_t bufsize)
{
char tmp[32];
int pos = strlen(buf);
snprintf(tmp, sizeof(tmp), "%lu", s);
if (pos + strlen(tmp) > bufsize - 3)
return -1;
strncat(buf, tmp, bufsize - strlen(buf) - 1);
pos = strlen(buf);
buf[pos++] = ',';
buf[pos++] = '\0';
return 0;
}
static int append_date(char *buf, struct timeval when, size_t bufsize)
Tilghman Lesher
committed
struct ast_tm tm;
if (strlen(buf) > bufsize - 3)
if (ast_tvzero(when)) {
strncat(buf, ",", bufsize - strlen(buf) - 1);
ast_localtime(&when, &tm, usegmtime ? "GMT" : NULL);
Tilghman Lesher
committed
ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
return append_string(buf, tmp, bufsize);
static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
append_string(buf, cdr->accountcode, bufsize);
append_string(buf, cdr->src, bufsize);
append_string(buf, cdr->dst, bufsize);
append_string(buf, cdr->dcontext, bufsize);
append_string(buf, cdr->clid, bufsize);
append_string(buf, cdr->channel, bufsize);
append_string(buf, cdr->dstchannel, bufsize);
append_string(buf, cdr->lastapp, bufsize);
append_string(buf, cdr->lastdata, bufsize);
append_date(buf, cdr->start, bufsize);
append_date(buf, cdr->answer, bufsize);
append_date(buf, cdr->end, bufsize);
append_int(buf, cdr->duration, bufsize);
append_int(buf, cdr->billsec, bufsize);
append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
Wenpeng Song
committed
append_unsigned_int(buf, cdr->sessionId, bufsize);
/* SIPSessionID */
append_string(buf, cdr->SIPSessionID, bufsize);
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* SIP IP Address */
append_string(buf, cdr->sipIpAddress, bufsize);
/* Far End IP Address */
append_string(buf, cdr->farEndIPAddress, bufsize);
/* Sip Response Code */
append_int(buf, cdr->sipResponseCode, bufsize);
/* codec */
append_string(buf, cdr->codec, bufsize);
/* RTP statistics */
if (cdr->rtp_stats) {
append_int(buf, cdr->rtp_stats->localBurstDensity, bufsize);
append_int(buf, cdr->rtp_stats->remoteBurstDensity, bufsize);
append_int(buf, cdr->rtp_stats->localBurstDuration, bufsize);
append_int(buf, cdr->rtp_stats->remoteBurstDuration, bufsize);
append_int(buf, cdr->rtp_stats->localGapDensity, bufsize);
append_int(buf, cdr->rtp_stats->remoteGapDensity, bufsize);
append_int(buf, cdr->rtp_stats->localGapDuration, bufsize);
append_int(buf, cdr->rtp_stats->remoteGapDuration, bufsize);
append_int(buf, cdr->rtp_stats->localJbRate, bufsize);
append_int(buf, cdr->rtp_stats->remoteJbRate, bufsize);
append_int(buf, cdr->rtp_stats->localJbMax, bufsize);
append_int(buf, cdr->rtp_stats->remoteJbMax, bufsize);
append_int(buf, cdr->rtp_stats->localJbNominal, bufsize);
append_int(buf, cdr->rtp_stats->remoteJbNominal, bufsize);
append_int(buf, cdr->rtp_stats->localJbAbsMax, bufsize);
append_int(buf, cdr->rtp_stats->remoteJbAbsMax, bufsize);
append_int(buf, cdr->rtp_stats->jbAvg, bufsize);
append_int(buf, cdr->rtp_stats->localLossRate, bufsize);
append_int(buf, cdr->rtp_stats->remoteLossRate, bufsize);
append_int(buf, cdr->rtp_stats->discarded, bufsize);
append_int(buf, cdr->rtp_stats->lost, bufsize);
append_int(buf, cdr->rtp_stats->rxpkts, bufsize);
append_int(buf, cdr->rtp_stats->txpkts, bufsize);
append_int(buf, cdr->rtp_stats->jitter, bufsize);
append_int(buf, cdr->rtp_stats->maxJitter, bufsize);
append_int(buf, cdr->rtp_stats->averageRoundTripDelay, bufsize);
append_int(buf, cdr->rtp_stats->farEndInterarrivalJitter, bufsize);
append_int(buf, cdr->rtp_stats->averageFarEndInterarrivalJitter, bufsize);
append_int(buf, cdr->rtp_stats->receiveInterarrivalJitter, bufsize);
append_int(buf, cdr->rtp_stats->averageReceiveInterarrivalJitter, bufsize);
}
append_string(buf, ast_channel_amaflags2string(cdr->amaflags), bufsize);
if (loguniqueid)
append_string(buf, cdr->uniqueid, bufsize);
append_string(buf, cdr->userfield, bufsize);
Rodrigo Ramírez Norambuena
committed
if (newcdrcolumns) {
append_string(buf, cdr->peeraccount, bufsize);
append_string(buf, cdr->linkedid, bufsize);
append_int(buf, cdr->sequence, bufsize);
}
/* If we hit the end of our buffer, log an error */
if (strlen(buf) < bufsize - 5) {
/* Trim off trailing comma */
buf[strlen(buf) - 1] = '\0';
strncat(buf, "\n", bufsize - strlen(buf) - 1);
static int writefile(char *s, char *file_path)
FILE *f, *ft;
int c = 0;
char buf[1024];
/* because of the absolutely unconditional need for the
highest reliability possible in writing billing records,
we open write and close the log file each time */
Wenpeng Song
committed
if (rowcount == -1) {
/* get current rowcount after config changes or reload */
if (!(f = fopen(file_path, "r"))) {
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_path, strerror(errno));
return -1;
}
rowcount = 0;
while (!feof(f)) {
if (fgets(buf, sizeof(buf), f) != NULL) {
rowcount++;
}
}
fclose(f);
}
if (rowcount >= maxrow) {
if (!(f = fopen(file_path, "r+"))) {
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_path, strerror(errno));
return -1;
}
if (!(ft = fopen(file_csv_temp, "w"))) {
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_csv_temp, strerror(errno));
fclose(f);
return -1;
}
while (!feof(f)) {
if (fgets(buf, sizeof(buf), f) != NULL) {
c++;
Wenpeng Song
committed
if (c > rowcount - maxrow + 1) {
/* skip the extra row(s) from the start in case of maxrow decreased
and one more record for rotation */
fprintf(ft, "%s", buf); /* cp the rest to Temp.csv */
Wenpeng Song
committed
rowcount = maxrow;
fclose(f);
fputs(s, ft); /* add the new record at the end */
fflush(ft);
fclose(ft);
remove(file_path); /* rm the original file and rename the temp file to it */
rename(file_csv_temp, file_path);
} else {
if (!(f = fopen(file_path, "a"))) {
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_path, strerror(errno));
return -1;
}
fputs(s, f);
fflush(f); /* be particularly anal here */
fclose(f);
rowcount++; /* update row count record if not reach max */
}
Rodrigo Ramírez Norambuena
committed
static int writefile_account(char *s, char *acc)
{
char file_account[PATH_MAX];
if (strchr(acc, '/') || (acc[0] == '.')) {
ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
return -1;
}
snprintf(file_account, sizeof(file_account), "%s/%s/%s.csv", ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
Rodrigo Ramírez Norambuena
committed
}
static int csv_log(struct ast_cdr *cdr)
{
/* Make sure we have a big enough buf */
char buf[1024];
/* Don't create records for CDRs where dcontext = "hangup" */
if (ast_strlen_zero(cdr->dcontext) || !strcasecmp(cdr->dcontext, "hangup") || ast_strlen_zero(cdr->dstchannel)) {
ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
return 0;
}
Rodrigo Ramírez Norambuena
committed
ast_mutex_lock(&f_lock);
Rodrigo Ramírez Norambuena
committed
ast_log(LOG_WARNING, "Unable to write CSV record to master '%s' : %s\n", file_csv_master, strerror(errno));
Olle Johansson
committed
if (accountlogs && !ast_strlen_zero(cdr->accountcode)) {
Rodrigo Ramírez Norambuena
committed
if (writefile_account(buf, cdr->accountcode))
ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
Rodrigo Ramírez Norambuena
committed
ast_mutex_unlock(&f_lock);
static int unload_module(void)
if (ast_cdr_unregister(name)) {
return -1;
}
static int load_module(void)
return AST_MODULE_LOAD_DECLINE;
if ((res = ast_cdr_register(name, ast_module_info->description, csv_log))) {
ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
} else {
loaded = 1;
}
static int reload(void)
if (load_config(1)) {
loaded = 1;
} else {
loaded = 0;
ast_log(LOG_WARNING, "No [csv] section in cdr.conf. Unregistering backend.\n");
ast_cdr_unregister(name);
}
Tilghman Lesher
committed
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Comma Separated Values CDR Backend",
.support_level = AST_MODULE_SUPPORT_EXTENDED,
.load = load_module,
.unload = unload_module,
.reload = reload,
.load_pri = AST_MODPRI_CDR_DRIVER,