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
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <sys/types.h>
44 #include "asterisk/config.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/cdr.h"
47 #include "asterisk/module.h"
48 #include "asterisk/options.h"
49 #include "asterisk/logger.h"
50 #include "asterisk/utils.h"
52 #define CSV_LOG_DIR "/cdr-csv"
53 #define CSV_MASTER "/Master.csv"
55 #define DATE_FORMAT "%Y-%m-%d %T"
57 static int usegmtime = 0;
58 static int loguniqueid = 0;
59 static int loguserfield = 0;
60 static char *config = "cdr.conf";
62 /* #define CSV_LOGUNIQUEID 1 */
63 /* #define CSV_LOGUSERFIELD 1 */
65 /*----------------------------------------------------
66 The values are as follows:
69 "accountcode", accountcode is the account name of detail records, Master.csv contains all records *
70 Detail records are configured on a channel basis, IAX and SIP are determined by user *
71 Zap is determined by channel in zaptel.conf
74 "destination context",
77 "destination channel", (if applicable)
78 "last application", Last application run on the channel
79 "last app argument", argument to the last channel
83 duration, Duration is the whole length that the entire call lasted. ie. call rx'd to hangup
84 "end time" minus "start time"
85 billable seconds, the duration that a call was up after other end answered which will be <= to duration
86 "end time" minus "answer time"
87 "disposition", ANSWERED, NO ANSWER, BUSY
88 "amaflags", DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode.
89 "uniqueid", unique call identifier
90 "userfield" user field set via SetCDRUserField
91 ----------------------------------------------------------*/
93 static char *name = "csv";
95 static FILE *mf = NULL;
98 static int load_config(int reload)
100 struct ast_config *cfg;
101 struct ast_variable *var;
103 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
109 cfg = ast_config_load(config, config_flags);
112 ast_log(LOG_WARNING, "unable to load config: %s\n", config);
114 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
117 var = ast_variable_browse(cfg, "csv");
119 ast_config_destroy(cfg);
123 tmp = ast_variable_retrieve(cfg, "csv", "usegmtime");
125 usegmtime = ast_true(tmp);
127 ast_debug(1, "logging time in GMT\n");
131 tmp = ast_variable_retrieve(cfg, "csv", "loguniqueid");
133 loguniqueid = ast_true(tmp);
135 ast_debug(1, "logging CDR field UNIQUEID\n");
139 tmp = ast_variable_retrieve(cfg, "csv", "loguserfield");
141 loguserfield = ast_true(tmp);
143 ast_debug(1, "logging CDR user-defined field\n");
147 ast_config_destroy(cfg);
151 static int append_string(char *buf, char *s, size_t bufsize)
153 int pos = strlen(buf);
156 if (pos >= bufsize - 4)
160 while(pos < bufsize - 3) {
167 buf[pos++] = s[spos];
176 static int append_int(char *buf, int s, size_t bufsize)
179 int pos = strlen(buf);
180 snprintf(tmp, sizeof(tmp), "%d", s);
181 if (pos + strlen(tmp) > bufsize - 3)
183 strncat(buf, tmp, bufsize - strlen(buf) - 1);
190 static int append_date(char *buf, struct timeval tv, size_t bufsize)
194 if (strlen(buf) > bufsize - 3)
196 if (ast_tvzero(tv)) {
197 strncat(buf, ",", bufsize - strlen(buf) - 1);
200 ast_localtime(&tv, &tm, usegmtime ? "GMT" : NULL);
201 ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
202 return append_string(buf, tmp, bufsize);
205 static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
210 append_string(buf, cdr->accountcode, bufsize);
212 append_string(buf, cdr->src, bufsize);
214 append_string(buf, cdr->dst, bufsize);
215 /* Destination context */
216 append_string(buf, cdr->dcontext, bufsize);
218 append_string(buf, cdr->clid, bufsize);
220 append_string(buf, cdr->channel, bufsize);
221 /* Destination Channel */
222 append_string(buf, cdr->dstchannel, bufsize);
223 /* Last Application */
224 append_string(buf, cdr->lastapp, bufsize);
226 append_string(buf, cdr->lastdata, bufsize);
228 append_date(buf, cdr->start, bufsize);
230 append_date(buf, cdr->answer, bufsize);
232 append_date(buf, cdr->end, bufsize);
234 append_int(buf, cdr->duration, bufsize);
235 /* Billable seconds */
236 append_int(buf, cdr->billsec, bufsize);
238 append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
240 append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
243 append_string(buf, cdr->uniqueid, bufsize);
244 /* append the user field */
246 append_string(buf, cdr->userfield,bufsize);
247 /* If we hit the end of our buffer, log an error */
248 if (strlen(buf) < bufsize - 5) {
249 /* Trim off trailing comma */
250 buf[strlen(buf) - 1] = '\0';
251 strncat(buf, "\n", bufsize - strlen(buf) - 1);
257 static int writefile(char *s, char *acc)
261 if (strchr(acc, '/') || (acc[0] == '.')) {
262 ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
265 snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
276 static int csv_log(struct ast_cdr *cdr)
278 /* Make sure we have a big enough buf */
280 char csvmaster[PATH_MAX];
281 snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
283 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);
285 if (build_csv_record(buf, sizeof(buf), cdr)) {
286 ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
288 /* because of the absolutely unconditional need for the
289 highest reliability possible in writing billing records,
290 we open write and close the log file each time */
291 mf = fopen(csvmaster, "a");
293 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
297 fflush(mf); /* be particularly anal here */
301 if (!ast_strlen_zero(cdr->accountcode)) {
302 if (writefile(buf, cdr->accountcode))
303 ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
309 static int unload_module(void)
313 ast_cdr_unregister(name);
317 static int load_module(void)
322 return AST_MODULE_LOAD_DECLINE;
324 res = ast_cdr_register(name, ast_module_info->description, csv_log);
326 ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
333 static int reload(void)
340 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Comma Separated Values CDR Backend",
342 .unload = unload_module,