Newer
Older
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Comma Separated Value CDR records.
*
* Copyright (C) 1999, Mark Spencer
*
* Mark Spencer <markster@linux-support.net>
*
* This program is free software, distributed under the terms of
* the GNU General Public License.
*
* Includes code and algorithms from the Zapata library.
*
*/
#include <asterisk/channel.h>
#include <asterisk/cdr.h>
#include <asterisk/module.h>
#include <asterisk/logger.h>
#define CSV_LOG_DIR "/cdr-csv"
#define CSV_MASTER "/Master.csv"
/* #define CSV_LOGUSERFIELD 1 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
/* 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
// Zap is determined by channel in zaptel.conf
"source",
"destination",
"destination context",
"callerid",
"channel",
"destination channel", (if applicable)
"last application", // Last application run on the channel
"last app argument", // argument to the last channel
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.
"userfield" // user field set via SetCDRUserField
*/
static char *desc = "Comma Separated Values CDR Backend";
static char *name = "csv";
static FILE *mf = NULL;
static int append_string(char *buf, char *s, size_t bufsize)
{
int pos = strlen(buf);
int spos = 0;
int error = 0;
if (!s[spos]) {
error = 0;
break;
}
if (s[spos] == '\"')
buf[pos++] = '\"';
buf[pos++] = s[spos];
spos++;
}
buf[pos++] = '\"';
buf[pos++] = ',';
buf[pos++] = '\0';
return error;
}
static int append_int(char *buf, int s, size_t bufsize)
{
char tmp[32];
int pos = strlen(buf);
snprintf(tmp, sizeof(tmp), "%d", s);
if (pos + strlen(tmp) > bufsize - 3)
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 tv, size_t bufsize)
if (strlen(buf) > bufsize - 3)
strncat(buf, ",", bufsize - strlen(buf) - 1);
localtime_r(&t,&tm);
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);
append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
append_string(buf, cdr->uniqueid, bufsize);
#endif
#ifdef CSV_LOGUSERFIELD
/* append the user field */
append_string(buf, cdr->userfield,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);
return 0;
}
return -1;
}
static int writefile(char *s, char *acc)
{
FILE *f;
if (strchr(acc, '/') || (acc[0] == '.')) {
ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
return -1;
}
snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
f = fopen(tmp, "a");
if (!f)
return -1;
fputs(s, f);
fclose(f);
return 0;
}
static int csv_log(struct ast_cdr *cdr)
{
/* Make sure we have a big enough buf */
char buf[1024];
snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
#if 0
printf("[CDR] %s ('%s' -> '%s') Dur: %ds Bill: %ds Disp: %s Flags: %s Account: [%s]\n", cdr->channel, cdr->src, cdr->dst, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), ast_cdr_flags2str(cdr->amaflags), cdr->accountcode);
#endif
if (build_csv_record(buf, sizeof(buf), cdr)) {
ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
} else {
/* 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 */
ast_log(LOG_ERROR, "Unable to re-open master file %s\n", csvmaster);
}
if (mf) {
fputs(buf, mf);
fflush(mf); /* be particularly anal here */
fclose(mf);
mf = NULL;
}
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
if (writefile(buf, cdr->accountcode))
ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s'\n", cdr->accountcode);
}
}
return 0;
}
char *description(void)
{
return desc;
}
int unload_module(void)
{
if (mf)
fclose(mf);
ast_cdr_unregister(name);
return 0;
}
int load_module(void)
{
int res;
res = ast_cdr_register(name, desc, csv_log);
if (res) {
ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
if (mf)
fclose(mf);
}
return res;
}
int reload(void)
{
return 0;
}
int usecount(void)
{
return 0;
}
char *key()
{
return ASTERISK_GPL_KEY;
}