2 * Asterisk -- A telephony toolkit for Linux.
4 * Comma Separated Value CDR records.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License.
13 * Includes code and algorithms from the Zapata library.
17 #include <sys/types.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/cdr.h>
20 #include <asterisk/module.h>
21 #include <asterisk/logger.h>
22 #include "../asterisk.h"
23 #include "../astconf.h"
25 #define CSV_LOG_DIR "/cdr-csv"
26 #define CSV_MASTER "/Master.csv"
28 #define DATE_FORMAT "%Y-%m-%d %T"
37 /* The values are as follows:
40 "accountcode", // accountcode is the account name of detail records, Master.csv contains all records
41 // Detail records are configured on a channel basis, IAX and SIP are determined by user
42 // Zap is determined by channel in zaptel.conf
45 "destination context",
48 "destination channel", (if applicable)
49 "last application", // Last application run on the channel
50 "last app argument", // argument to the last channel
54 duration, // Duration is the whole length that the entire call lasted. ie. call rx'd to hangup
55 // "end time" minus "start time"
56 billable seconds, // the duration that a call was up after other end answered which will be <= to duration
57 // "end time" minus "answer time"
58 "disposition", // ANSWERED, NO ANSWER, BUSY
59 "amaflags", // DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode.
63 static char *desc = "Comma Separated Values CDR Backend";
65 static char *name = "csv";
67 static FILE *mf = NULL;
69 static int append_string(char *buf, char *s, int len)
71 int pos = strlen(buf);
78 while(pos < len - 3) {
94 static int append_int(char *buf, int s, int len)
97 int pos = strlen(buf);
98 snprintf(tmp, sizeof(tmp), "%d", s);
99 if (pos + strlen(tmp) > len - 3)
101 strncat(buf, tmp, len);
108 static int append_date(char *buf, struct timeval tv, int len)
114 if (strlen(buf) > len - 3)
116 if (!tv.tv_sec && !tv.tv_usec) {
117 strncat(buf, ",", len);
121 strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
122 return append_string(buf, tmp, len);
125 static int build_csv_record(char *buf, int len, struct ast_cdr *cdr)
129 append_string(buf, cdr->accountcode, len);
131 append_string(buf, cdr->src, len);
133 append_string(buf, cdr->dst, len);
134 /* Destination context */
135 append_string(buf, cdr->dcontext, len);
137 append_string(buf, cdr->clid, len);
139 append_string(buf, cdr->channel, len);
140 /* Destination Channel */
141 append_string(buf, cdr->dstchannel, len);
142 /* Last Application */
143 append_string(buf, cdr->lastapp, len);
145 append_string(buf, cdr->lastdata, len);
147 append_date(buf, cdr->start, len);
149 append_date(buf, cdr->answer, len);
151 append_date(buf, cdr->end, len);
153 append_int(buf, cdr->duration, len);
154 /* Billable seconds */
155 append_int(buf, cdr->billsec, len);
157 append_string(buf, ast_cdr_disp2str(cdr->disposition), len);
159 append_string(buf, ast_cdr_flags2str(cdr->amaflags), len);
161 /* If we hit the end of our buffer, log an error */
162 if (strlen(buf) < len - 5) {
163 /* Trim off trailing comma */
164 buf[strlen(buf) - 1] = '\0';
165 strncat(buf, "\n", len);
171 static int writefile(char *s, char *acc)
175 if (strchr(acc, '/') || (acc[0] == '.')) {
176 ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
179 snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
189 static int csv_log(struct ast_cdr *cdr)
191 /* Make sure we have a big enough buf */
193 char csvmaster[AST_CONFIG_MAX_PATH];
194 snprintf((char *)csvmaster,sizeof(csvmaster)-1,"%s/%s/%s",(char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR,CSV_MASTER);
196 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);
198 if (build_csv_record(buf, sizeof(buf), cdr)) {
199 ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", sizeof(buf));
201 /* because of the absolutely unconditional need for the
202 highest reliability possible in writing billing records,
203 we open write and close the log file each time */
204 mf = fopen(csvmaster, "a");
206 ast_log(LOG_ERROR, "Unable to re-open master file %s\n", csvmaster);
210 fflush(mf); /* be particularly anal here */
214 if (strlen(cdr->accountcode)) {
215 if (writefile(buf, cdr->accountcode))
216 ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s'\n", cdr->accountcode);
222 char *description(void)
227 int unload_module(void)
231 ast_cdr_unregister(name);
235 int load_module(void)
239 res = ast_cdr_register(name, desc, csv_log);
241 ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
260 return ASTERISK_GPL_KEY;