2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Includes code and algorithms from the Zapata library.
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
23 * \brief Comma Separated Value CDR records.
25 * \author Mark Spencer <markster@digium.com>
27 * \arg See also \ref AstCDR
28 * \ingroup cdr_drivers
31 #include <sys/types.h>
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include "asterisk/config.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/cdr.h"
47 #include "asterisk/module.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/utils.h"
51 #define CSV_LOG_DIR "/cdr-csv"
52 #define CSV_MASTER "/Master.csv"
54 #define DATE_FORMAT "%Y-%m-%d %T"
56 static int usegmtime = 0;
57 static int loguniqueid = 0;
58 static int loguserfield = 0;
59 static char *config = "cdr.conf";
61 /* #define CSV_LOGUNIQUEID 1 */
62 /* #define CSV_LOGUSERFIELD 1 */
64 /*----------------------------------------------------
65 The values are as follows:
68 "accountcode", accountcode is the account name of detail records, Master.csv contains all records *
69 Detail records are configured on a channel basis, IAX and SIP are determined by user *
70 Zap is determined by channel in zaptel.conf
73 "destination context",
76 "destination channel", (if applicable)
77 "last application", Last application run on the channel
78 "last app argument", argument to the last channel
82 duration, Duration is the whole length that the entire call lasted. ie. call rx'd to hangup
83 "end time" minus "start time"
84 billable seconds, the duration that a call was up after other end answered which will be <= to duration
85 "end time" minus "answer time"
86 "disposition", ANSWERED, NO ANSWER, BUSY
87 "amaflags", DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode.
88 "uniqueid", unique call identifier
89 "userfield" user field set via SetCDRUserField
90 ----------------------------------------------------------*/
92 static char *desc = "Comma Separated Values CDR Backend";
94 static char *name = "csv";
96 static FILE *mf = NULL;
99 static int load_config(void)
101 struct ast_config *cfg;
102 struct ast_variable *var;
109 cfg = ast_config_load(config);
112 ast_log(LOG_WARNING, "unable to load config: %s\n", config);
116 var = ast_variable_browse(cfg, "csv");
118 ast_config_destroy(cfg);
122 tmp = ast_variable_retrieve(cfg, "csv", "usegmtime");
124 usegmtime = ast_true(tmp);
126 ast_log(LOG_DEBUG, "logging time in GMT\n");
130 tmp = ast_variable_retrieve(cfg, "csv", "loguniqueid");
132 loguniqueid = ast_true(tmp);
134 ast_log(LOG_DEBUG, "logging CDR field UNIQUEID\n");
138 tmp = ast_variable_retrieve(cfg, "csv", "loguserfield");
140 loguserfield = ast_true(tmp);
142 ast_log(LOG_DEBUG, "logging CDR user-defined field\n");
146 ast_config_destroy(cfg);
150 static int append_string(char *buf, char *s, size_t bufsize)
152 int pos = strlen(buf);
155 if (pos >= bufsize - 4)
159 while(pos < bufsize - 3) {
166 buf[pos++] = s[spos];
175 static int append_int(char *buf, int s, size_t bufsize)
178 int pos = strlen(buf);
179 snprintf(tmp, sizeof(tmp), "%d", s);
180 if (pos + strlen(tmp) > bufsize - 3)
182 strncat(buf, tmp, bufsize - strlen(buf) - 1);
189 static int append_date(char *buf, struct timeval tv, size_t bufsize)
195 if (strlen(buf) > bufsize - 3)
197 if (ast_tvzero(tv)) {
198 strncat(buf, ",", bufsize - strlen(buf) - 1);
206 strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
207 return append_string(buf, tmp, bufsize);
210 static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
215 append_string(buf, cdr->accountcode, bufsize);
217 append_string(buf, cdr->src, bufsize);
219 append_string(buf, cdr->dst, bufsize);
220 /* Destination context */
221 append_string(buf, cdr->dcontext, bufsize);
223 append_string(buf, cdr->clid, bufsize);
225 append_string(buf, cdr->channel, bufsize);
226 /* Destination Channel */
227 append_string(buf, cdr->dstchannel, bufsize);
228 /* Last Application */
229 append_string(buf, cdr->lastapp, bufsize);
231 append_string(buf, cdr->lastdata, bufsize);
233 append_date(buf, cdr->start, bufsize);
235 append_date(buf, cdr->answer, bufsize);
237 append_date(buf, cdr->end, bufsize);
239 append_int(buf, cdr->duration, bufsize);
240 /* Billable seconds */
241 append_int(buf, cdr->billsec, bufsize);
243 append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
245 append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
248 append_string(buf, cdr->uniqueid, bufsize);
249 /* append the user field */
251 append_string(buf, cdr->userfield,bufsize);
252 /* If we hit the end of our buffer, log an error */
253 if (strlen(buf) < bufsize - 5) {
254 /* Trim off trailing comma */
255 buf[strlen(buf) - 1] = '\0';
256 strncat(buf, "\n", bufsize - strlen(buf) - 1);
262 static int writefile(char *s, char *acc)
264 char tmp[AST_CONFIG_MAX_PATH];
266 if (strchr(acc, '/') || (acc[0] == '.')) {
267 ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
270 snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
281 static int csv_log(struct ast_cdr *cdr)
283 /* Make sure we have a big enough buf */
285 char csvmaster[AST_CONFIG_MAX_PATH];
286 snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
288 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);
290 if (build_csv_record(buf, sizeof(buf), cdr)) {
291 ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
293 /* because of the absolutely unconditional need for the
294 highest reliability possible in writing billing records,
295 we open write and close the log file each time */
296 mf = fopen(csvmaster, "a");
298 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
302 fflush(mf); /* be particularly anal here */
306 if (!ast_strlen_zero(cdr->accountcode)) {
307 if (writefile(buf, cdr->accountcode))
308 ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
314 const char *description(void)
319 int unload_module(void)
323 ast_cdr_unregister(name);
327 int load_module(void)
333 res = ast_cdr_register(name, desc, csv_log);
335 ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
355 return ASTERISK_GPL_KEY;