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$")
37 #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
38 #include "asterisk/config.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/cdr.h"
41 #include "asterisk/module.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/lock.h"
45 #define CSV_LOG_DIR "/cdr-csv"
46 #define CSV_MASTER "/Master.csv"
48 #define DATE_FORMAT "%Y-%m-%d %T"
50 static int usegmtime
= 0;
51 static int loguniqueid
= 0;
52 static int loguserfield
= 0;
53 static int loaded
= 0;
54 static char *config
= "cdr.conf";
56 /* #define CSV_LOGUNIQUEID 1 */
57 /* #define CSV_LOGUSERFIELD 1 */
59 /*----------------------------------------------------
60 The values are as follows:
63 "accountcode", accountcode is the account name of detail records, Master.csv contains all records *
64 Detail records are configured on a channel basis, IAX and SIP are determined by user *
65 Zap is determined by channel in zaptel.conf
68 "destination context",
71 "destination channel", (if applicable)
72 "last application", Last application run on the channel
73 "last app argument", argument to the last channel
77 duration, Duration is the whole length that the entire call lasted. ie. call rx'd to hangup
78 "end time" minus "start time"
79 billable seconds, the duration that a call was up after other end answered which will be <= to duration
80 "end time" minus "answer time"
81 "disposition", ANSWERED, NO ANSWER, BUSY
82 "amaflags", DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode.
83 "uniqueid", unique call identifier
84 "userfield" user field set via SetCDRUserField
85 ----------------------------------------------------------*/
87 static char *name
= "csv";
89 AST_MUTEX_DEFINE_STATIC(mf_lock
);
90 AST_MUTEX_DEFINE_STATIC(acf_lock
);
92 static int load_config(int reload
)
94 struct ast_config
*cfg
;
95 struct ast_variable
*var
;
97 struct ast_flags config_flags
= { reload
? CONFIG_FLAG_FILEUNCHANGED
: 0 };
103 if (!(cfg
= ast_config_load(config
, config_flags
))) {
104 ast_log(LOG_WARNING
, "unable to load config: %s\n", config
);
106 } else if (cfg
== CONFIG_STATUS_FILEUNCHANGED
)
109 if (!(var
= ast_variable_browse(cfg
, "csv"))) {
110 ast_config_destroy(cfg
);
114 if ((tmp
= ast_variable_retrieve(cfg
, "csv", "usegmtime"))) {
115 usegmtime
= ast_true(tmp
);
117 ast_debug(1, "logging time in GMT\n");
120 if ((tmp
= ast_variable_retrieve(cfg
, "csv", "loguniqueid"))) {
121 loguniqueid
= ast_true(tmp
);
123 ast_debug(1, "logging CDR field UNIQUEID\n");
126 if ((tmp
= ast_variable_retrieve(cfg
, "csv", "loguserfield"))) {
127 loguserfield
= ast_true(tmp
);
129 ast_debug(1, "logging CDR user-defined field\n");
132 ast_config_destroy(cfg
);
136 static int append_string(char *buf
, char *s
, size_t bufsize
)
138 int pos
= strlen(buf
), spos
= 0, error
= -1;
140 if (pos
>= bufsize
- 4)
145 while(pos
< bufsize
- 3) {
152 buf
[pos
++] = s
[spos
];
163 static int append_int(char *buf
, int s
, size_t bufsize
)
166 int pos
= strlen(buf
);
168 snprintf(tmp
, sizeof(tmp
), "%d", s
);
170 if (pos
+ strlen(tmp
) > bufsize
- 3)
173 strncat(buf
, tmp
, bufsize
- strlen(buf
) - 1);
181 static int append_date(char *buf
, struct timeval tv
, size_t bufsize
)
186 if (strlen(buf
) > bufsize
- 3)
189 if (ast_tvzero(tv
)) {
190 strncat(buf
, ",", bufsize
- strlen(buf
) - 1);
194 ast_localtime(&tv
, &tm
, usegmtime
? "GMT" : NULL
);
195 ast_strftime(tmp
, sizeof(tmp
), DATE_FORMAT
, &tm
);
197 return append_string(buf
, tmp
, bufsize
);
200 static int build_csv_record(char *buf
, size_t bufsize
, struct ast_cdr
*cdr
)
205 append_string(buf
, cdr
->accountcode
, bufsize
);
207 append_string(buf
, cdr
->src
, bufsize
);
209 append_string(buf
, cdr
->dst
, bufsize
);
210 /* Destination context */
211 append_string(buf
, cdr
->dcontext
, bufsize
);
213 append_string(buf
, cdr
->clid
, bufsize
);
215 append_string(buf
, cdr
->channel
, bufsize
);
216 /* Destination Channel */
217 append_string(buf
, cdr
->dstchannel
, bufsize
);
218 /* Last Application */
219 append_string(buf
, cdr
->lastapp
, bufsize
);
221 append_string(buf
, cdr
->lastdata
, bufsize
);
223 append_date(buf
, cdr
->start
, bufsize
);
225 append_date(buf
, cdr
->answer
, bufsize
);
227 append_date(buf
, cdr
->end
, bufsize
);
229 append_int(buf
, cdr
->duration
, bufsize
);
230 /* Billable seconds */
231 append_int(buf
, cdr
->billsec
, bufsize
);
233 append_string(buf
, ast_cdr_disp2str(cdr
->disposition
), bufsize
);
235 append_string(buf
, ast_cdr_flags2str(cdr
->amaflags
), bufsize
);
238 append_string(buf
, cdr
->uniqueid
, bufsize
);
239 /* append the user field */
241 append_string(buf
, cdr
->userfield
,bufsize
);
242 /* If we hit the end of our buffer, log an error */
243 if (strlen(buf
) < bufsize
- 5) {
244 /* Trim off trailing comma */
245 buf
[strlen(buf
) - 1] = '\0';
246 strncat(buf
, "\n", bufsize
- strlen(buf
) - 1);
252 static int writefile(char *s
, char *acc
)
257 if (strchr(acc
, '/') || (acc
[0] == '.')) {
258 ast_log(LOG_WARNING
, "Account code '%s' insecure for writing file\n", acc
);
262 snprintf(tmp
, sizeof(tmp
), "%s/%s/%s.csv", ast_config_AST_LOG_DIR
,CSV_LOG_DIR
, acc
);
264 ast_mutex_lock(&acf_lock
);
265 if (!(f
= fopen(tmp
, "a"))) {
266 ast_mutex_unlock(&acf_lock
);
267 ast_log(LOG_ERROR
, "Unable to open file %s : %s\n", tmp
, strerror(errno
));
273 ast_mutex_unlock(&acf_lock
);
279 static int csv_log(struct ast_cdr
*cdr
)
282 /* Make sure we have a big enough buf */
284 char csvmaster
[PATH_MAX
];
285 snprintf(csvmaster
, sizeof(csvmaster
),"%s/%s/%s", ast_config_AST_LOG_DIR
, CSV_LOG_DIR
, CSV_MASTER
);
287 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
);
289 if (build_csv_record(buf
, sizeof(buf
), cdr
)) {
290 ast_log(LOG_WARNING
, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf
));
294 /* because of the absolutely unconditional need for the
295 highest reliability possible in writing billing records,
296 we open write and close the log file each time */
297 ast_mutex_lock(&mf_lock
);
298 if ((mf
= fopen(csvmaster
, "a"))) {
300 fflush(mf
); /* be particularly anal here */
303 ast_mutex_unlock(&mf_lock
);
305 ast_mutex_unlock(&mf_lock
);
306 ast_log(LOG_ERROR
, "Unable to re-open master file %s : %s\n", csvmaster
, strerror(errno
));
309 if (!ast_strlen_zero(cdr
->accountcode
)) {
310 if (writefile(buf
, cdr
->accountcode
))
311 ast_log(LOG_WARNING
, "Unable to write CSV record to account file '%s' : %s\n", cdr
->accountcode
, strerror(errno
));
317 static int unload_module(void)
319 ast_cdr_unregister(name
);
324 static int load_module(void)
329 return AST_MODULE_LOAD_DECLINE
;
331 if ((res
= ast_cdr_register(name
, ast_module_info
->description
, csv_log
))) {
332 ast_log(LOG_ERROR
, "Unable to register CSV CDR handling\n");
339 static int reload(void)
341 if (load_config(1)) {
345 ast_log(LOG_WARNING
, "No [csv] section in cdr.conf. Unregistering backend.\n");
346 ast_cdr_unregister(name
);
352 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "Comma Separated Values CDR Backend",
354 .unload
= unload_module
,