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/utils.h>
23 #include "../asterisk.h"
24 #include "../astconf.h"
26 #define CSV_LOG_DIR "/cdr-csv"
27 #define CSV_MASTER "/Master.csv"
29 #define DATE_FORMAT "%Y-%m-%d %T"
31 /* #define CSV_LOGUNIQUEID 1 */
32 /* #define CSV_LOGUSERFIELD 1 */
41 /* The values are as follows:
44 "accountcode", // accountcode is the account name of detail records, Master.csv contains all records
45 // Detail records are configured on a channel basis, IAX and SIP are determined by user
46 // Zap is determined by channel in zaptel.conf
49 "destination context",
52 "destination channel", (if applicable)
53 "last application", // Last application run on the channel
54 "last app argument", // argument to the last channel
58 duration, // Duration is the whole length that the entire call lasted. ie. call rx'd to hangup
59 // "end time" minus "start time"
60 billable seconds, // the duration that a call was up after other end answered which will be <= to duration
61 // "end time" minus "answer time"
62 "disposition", // ANSWERED, NO ANSWER, BUSY
63 "amaflags", // DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode.
64 "uniqueid", // unique call identifier
65 "userfield" // user field set via SetCDRUserField
68 static char *desc = "Comma Separated Values CDR Backend";
70 static char *name = "csv";
72 static FILE *mf = NULL;
74 static int append_string(char *buf, char *s, int len)
76 int pos = strlen(buf);
83 while(pos < len - 3) {
99 static int append_int(char *buf, int s, int len)
102 int pos = strlen(buf);
103 snprintf(tmp, sizeof(tmp), "%d", s);
104 if (pos + strlen(tmp) > len - 3)
106 strncat(buf, tmp, len);
113 static int append_date(char *buf, struct timeval tv, int len)
119 if (strlen(buf) > len - 3)
121 if (!tv.tv_sec && !tv.tv_usec) {
122 strncat(buf, ",", len);
126 strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
127 return append_string(buf, tmp, len);
130 static int build_csv_record(char *buf, int len, struct ast_cdr *cdr)
135 append_string(buf, cdr->accountcode, len);
137 append_string(buf, cdr->src, len);
139 append_string(buf, cdr->dst, len);
140 /* Destination context */
141 append_string(buf, cdr->dcontext, len);
143 append_string(buf, cdr->clid, len);
145 append_string(buf, cdr->channel, len);
146 /* Destination Channel */
147 append_string(buf, cdr->dstchannel, len);
148 /* Last Application */
149 append_string(buf, cdr->lastapp, len);
151 append_string(buf, cdr->lastdata, len);
153 append_date(buf, cdr->start, len);
155 append_date(buf, cdr->answer, len);
157 append_date(buf, cdr->end, len);
159 append_int(buf, cdr->duration, len);
160 /* Billable seconds */
161 append_int(buf, cdr->billsec, len);
163 append_string(buf, ast_cdr_disp2str(cdr->disposition), len);
165 append_string(buf, ast_cdr_flags2str(cdr->amaflags), len);
167 #ifdef CSV_LOGUNIQUEID
169 append_string(buf, cdr->uniqueid, len);
171 #ifdef CSV_LOGUSERFIELD
172 /* append the user field */
173 append_string(buf, cdr->userfield,len);
175 /* If we hit the end of our buffer, log an error */
176 if (strlen(buf) < len - 5) {
177 /* Trim off trailing comma */
178 buf[strlen(buf) - 1] = '\0';
179 strncat(buf, "\n", len);
185 static int writefile(char *s, char *acc)
189 if (strchr(acc, '/') || (acc[0] == '.')) {
190 ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
193 snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
203 static int csv_log(struct ast_cdr *cdr)
205 /* Make sure we have a big enough buf */
207 char csvmaster[AST_CONFIG_MAX_PATH];
208 snprintf((char *)csvmaster,sizeof(csvmaster)-1,"%s/%s/%s",(char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR,CSV_MASTER);
210 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);
212 if (build_csv_record(buf, sizeof(buf), cdr)) {
213 ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
215 /* because of the absolutely unconditional need for the
216 highest reliability possible in writing billing records,
217 we open write and close the log file each time */
218 mf = fopen(csvmaster, "a");
220 ast_log(LOG_ERROR, "Unable to re-open master file %s\n", csvmaster);
224 fflush(mf); /* be particularly anal here */
228 if (!ast_strlen_zero(cdr->accountcode)) {
229 if (writefile(buf, cdr->accountcode))
230 ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s'\n", cdr->accountcode);
236 char *description(void)
241 int unload_module(void)
245 ast_cdr_unregister(name);
249 int load_module(void)
253 res = ast_cdr_register(name, desc, csv_log);
255 ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
274 return ASTERISK_GPL_KEY;