Files
asterisk/cdr/cdr_csv.c
T

295 lines
7.3 KiB
C
Raw Normal View History

2001-10-18 15:18:45 +00:00
/*
* Asterisk -- An open source telephony toolkit.
2001-10-18 15:18:45 +00:00
*
* Copyright (C) 1999 - 2005, Digium, Inc.
2001-10-18 15:18:45 +00:00
*
2004-12-19 21:13:41 +00:00
* Mark Spencer <markster@digium.com>
2001-10-18 15:18:45 +00:00
*
* 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.
*/
2005-10-26 13:03:17 +00:00
/*! \file
*
2005-10-26 13:03:17 +00:00
* \brief Comma Separated Value CDR records.
2005-12-30 21:18:06 +00:00
*
* \author Mark Spencer <markster@digium.com>
*
2005-10-26 13:03:17 +00:00
* \arg See also \ref AstCDR
2005-11-06 15:09:47 +00:00
* \ingroup cdr_drivers
2001-10-18 15:18:45 +00:00
*/
2003-04-23 19:09:13 +00:00
#include <sys/types.h>
2005-11-10 23:42:45 +00:00
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "asterisk.h"
2005-06-06 22:12:19 +00:00
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
2001-10-18 15:18:45 +00:00
2003-01-30 15:03:20 +00:00
#define CSV_LOG_DIR "/cdr-csv"
#define CSV_MASTER "/Master.csv"
2001-10-18 15:18:45 +00:00
#define DATE_FORMAT "%Y-%m-%d %T"
2003-05-30 04:41:18 +00:00
/* #define CSV_LOGUNIQUEID 1 */
2004-01-11 05:04:16 +00:00
/* #define CSV_LOGUSERFIELD 1 */
2003-05-30 04:41:18 +00:00
2004-12-19 21:13:41 +00:00
/*----------------------------------------------------
The values are as follows:
2001-10-18 15:18:45 +00:00
2004-12-19 21:13:41 +00:00
"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
2001-10-18 15:18:45 +00:00
"source",
"destination",
"destination context",
"callerid",
"channel",
"destination channel", (if applicable)
2004-12-19 21:13:41 +00:00
"last application", Last application run on the channel
"last app argument", argument to the last channel
2001-10-18 15:18:45 +00:00
"start time",
"answer time",
"end time",
2004-12-19 21:13:41 +00:00
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
----------------------------------------------------------*/
2001-10-18 15:18:45 +00:00
static char *desc = "Comma Separated Values CDR Backend";
static char *name = "csv";
static FILE *mf = NULL;
2004-07-14 13:57:15 +00:00
static int append_string(char *buf, char *s, size_t bufsize)
2001-10-18 15:18:45 +00:00
{
int pos = strlen(buf);
int spos = 0;
int error = 0;
2004-07-14 13:57:15 +00:00
if (pos >= bufsize - 4)
2001-10-18 15:18:45 +00:00
return -1;
buf[pos++] = '\"';
error = -1;
2004-07-14 13:57:15 +00:00
while(pos < bufsize - 3) {
2001-10-18 15:18:45 +00:00
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;
}
2004-07-14 13:57:15 +00:00
static int append_int(char *buf, int s, size_t bufsize)
2001-10-18 15:18:45 +00:00
{
char tmp[32];
int pos = strlen(buf);
snprintf(tmp, sizeof(tmp), "%d", s);
2004-07-14 13:57:15 +00:00
if (pos + strlen(tmp) > bufsize - 3)
2001-10-18 15:18:45 +00:00
return -1;
2004-07-14 13:57:15 +00:00
strncat(buf, tmp, bufsize - strlen(buf) - 1);
2001-10-18 15:18:45 +00:00
pos = strlen(buf);
buf[pos++] = ',';
buf[pos++] = '\0';
return 0;
}
2004-07-14 13:57:15 +00:00
static int append_date(char *buf, struct timeval tv, size_t bufsize)
2001-10-18 15:18:45 +00:00
{
2004-07-14 13:57:15 +00:00
char tmp[80] = "";
2003-03-31 03:19:34 +00:00
struct tm tm;
2001-10-18 15:18:45 +00:00
time_t t;
t = tv.tv_sec;
2004-07-14 13:57:15 +00:00
if (strlen(buf) > bufsize - 3)
2001-10-18 15:18:45 +00:00
return -1;
if (ast_tvzero(tv)) {
2004-07-14 13:57:15 +00:00
strncat(buf, ",", bufsize - strlen(buf) - 1);
2001-10-18 15:18:45 +00:00
return 0;
}
2003-03-31 03:19:34 +00:00
localtime_r(&t,&tm);
strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
2004-07-14 13:57:15 +00:00
return append_string(buf, tmp, bufsize);
2001-10-18 15:18:45 +00:00
}
2004-07-14 13:57:15 +00:00
static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
2001-10-18 15:18:45 +00:00
{
2004-01-11 05:04:16 +00:00
2001-10-18 15:18:45 +00:00
buf[0] = '\0';
/* Account code */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->accountcode, bufsize);
2001-10-18 15:18:45 +00:00
/* Source */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->src, bufsize);
2001-10-18 15:18:45 +00:00
/* Destination */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->dst, bufsize);
2001-10-18 15:18:45 +00:00
/* Destination context */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->dcontext, bufsize);
2001-10-18 15:18:45 +00:00
/* Caller*ID */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->clid, bufsize);
2001-10-18 15:18:45 +00:00
/* Channel */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->channel, bufsize);
2001-10-18 15:18:45 +00:00
/* Destination Channel */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->dstchannel, bufsize);
2001-10-18 15:18:45 +00:00
/* Last Application */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->lastapp, bufsize);
2001-10-18 15:18:45 +00:00
/* Last Data */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->lastdata, bufsize);
2001-10-18 15:18:45 +00:00
/* Start Time */
2004-07-14 13:57:15 +00:00
append_date(buf, cdr->start, bufsize);
2001-10-18 15:18:45 +00:00
/* Answer Time */
2004-07-14 13:57:15 +00:00
append_date(buf, cdr->answer, bufsize);
2001-10-18 15:18:45 +00:00
/* End Time */
2004-07-14 13:57:15 +00:00
append_date(buf, cdr->end, bufsize);
2001-10-18 15:18:45 +00:00
/* Duration */
2004-07-14 13:57:15 +00:00
append_int(buf, cdr->duration, bufsize);
2001-10-18 15:18:45 +00:00
/* Billable seconds */
2004-07-14 13:57:15 +00:00
append_int(buf, cdr->billsec, bufsize);
2001-10-18 15:18:45 +00:00
/* Disposition */
2004-07-14 13:57:15 +00:00
append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
2001-10-18 15:18:45 +00:00
/* AMA Flags */
2004-07-14 13:57:15 +00:00
append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
2001-10-18 15:18:45 +00:00
2003-05-30 04:41:18 +00:00
#ifdef CSV_LOGUNIQUEID
/* Unique ID */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->uniqueid, bufsize);
2004-01-11 05:04:16 +00:00
#endif
#ifdef CSV_LOGUSERFIELD
/* append the user field */
2004-07-14 13:57:15 +00:00
append_string(buf, cdr->userfield,bufsize);
2003-05-30 04:41:18 +00:00
#endif
2001-10-18 15:18:45 +00:00
/* If we hit the end of our buffer, log an error */
2004-07-14 13:57:15 +00:00
if (strlen(buf) < bufsize - 5) {
2001-10-18 15:18:45 +00:00
/* Trim off trailing comma */
buf[strlen(buf) - 1] = '\0';
2004-07-14 13:57:15 +00:00
strncat(buf, "\n", bufsize - strlen(buf) - 1);
2001-10-18 15:18:45 +00:00
return 0;
}
return -1;
}
static int writefile(char *s, char *acc)
{
2004-10-07 17:50:15 +00:00
char tmp[AST_CONFIG_MAX_PATH];
2001-10-18 15:18:45 +00:00
FILE *f;
if (strchr(acc, '/') || (acc[0] == '.')) {
ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
return -1;
}
2003-01-30 15:03:20 +00:00
snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
2001-10-18 15:18:45 +00:00
f = fopen(tmp, "a");
if (!f)
return -1;
fputs(s, f);
2004-10-07 17:50:15 +00:00
fflush(f);
2001-10-18 15:18:45 +00:00
fclose(f);
return 0;
}
static int csv_log(struct ast_cdr *cdr)
{
/* Make sure we have a big enough buf */
char buf[1024];
2003-01-30 15:03:20 +00:00
char csvmaster[AST_CONFIG_MAX_PATH];
2004-07-14 13:57:15 +00:00
snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
2001-10-18 15:18:45 +00:00
#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)) {
2004-06-13 21:25:10 +00:00
ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
2001-10-18 15:18:45 +00:00
} 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 */
2003-01-30 15:03:20 +00:00
mf = fopen(csvmaster, "a");
2001-10-18 15:18:45 +00:00
if (!mf) {
2004-12-15 20:49:33 +00:00
ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
2001-10-18 15:18:45 +00:00
}
if (mf) {
fputs(buf, mf);
fflush(mf); /* be particularly anal here */
fclose(mf);
mf = NULL;
}
2004-05-08 08:07:47 +00:00
if (!ast_strlen_zero(cdr->accountcode)) {
2001-10-18 15:18:45 +00:00
if (writefile(buf, cdr->accountcode))
2004-12-15 20:49:33 +00:00
ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
2001-10-18 15:18:45 +00:00
}
}
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;
}