Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / cdr / cdr_csv.c
blobcb872f1310b39b31099aab24ba28c2f2dec2bd20
1 /*
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.
21 /*! \file
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 "asterisk.h"
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <time.h>
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
66 "source",
67 "destination",
68 "destination context",
69 "callerid",
70 "channel",
71 "destination channel", (if applicable)
72 "last application", Last application run on the channel
73 "last app argument", argument to the last channel
74 "start time",
75 "answer time",
76 "end time",
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;
96 const char *tmp;
97 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
99 usegmtime = 0;
100 loguniqueid = 0;
101 loguserfield = 0;
103 if (!(cfg = ast_config_load(config, config_flags))) {
104 ast_log(LOG_WARNING, "unable to load config: %s\n", config);
105 return 0;
106 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
107 return 1;
109 if (!(var = ast_variable_browse(cfg, "csv"))) {
110 ast_config_destroy(cfg);
111 return 0;
114 if ((tmp = ast_variable_retrieve(cfg, "csv", "usegmtime"))) {
115 usegmtime = ast_true(tmp);
116 if (usegmtime)
117 ast_debug(1, "logging time in GMT\n");
120 if ((tmp = ast_variable_retrieve(cfg, "csv", "loguniqueid"))) {
121 loguniqueid = ast_true(tmp);
122 if (loguniqueid)
123 ast_debug(1, "logging CDR field UNIQUEID\n");
126 if ((tmp = ast_variable_retrieve(cfg, "csv", "loguserfield"))) {
127 loguserfield = ast_true(tmp);
128 if (loguserfield)
129 ast_debug(1, "logging CDR user-defined field\n");
132 ast_config_destroy(cfg);
133 return 1;
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)
141 return -1;
143 buf[pos++] = '\"';
145 while(pos < bufsize - 3) {
146 if (!s[spos]) {
147 error = 0;
148 break;
150 if (s[spos] == '\"')
151 buf[pos++] = '\"';
152 buf[pos++] = s[spos];
153 spos++;
156 buf[pos++] = '\"';
157 buf[pos++] = ',';
158 buf[pos++] = '\0';
160 return error;
163 static int append_int(char *buf, int s, size_t bufsize)
165 char tmp[32];
166 int pos = strlen(buf);
168 snprintf(tmp, sizeof(tmp), "%d", s);
170 if (pos + strlen(tmp) > bufsize - 3)
171 return -1;
173 strncat(buf, tmp, bufsize - strlen(buf) - 1);
174 pos = strlen(buf);
175 buf[pos++] = ',';
176 buf[pos++] = '\0';
178 return 0;
181 static int append_date(char *buf, struct timeval tv, size_t bufsize)
183 char tmp[80] = "";
184 struct ast_tm tm;
186 if (strlen(buf) > bufsize - 3)
187 return -1;
189 if (ast_tvzero(tv)) {
190 strncat(buf, ",", bufsize - strlen(buf) - 1);
191 return 0;
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)
203 buf[0] = '\0';
204 /* Account code */
205 append_string(buf, cdr->accountcode, bufsize);
206 /* Source */
207 append_string(buf, cdr->src, bufsize);
208 /* Destination */
209 append_string(buf, cdr->dst, bufsize);
210 /* Destination context */
211 append_string(buf, cdr->dcontext, bufsize);
212 /* Caller*ID */
213 append_string(buf, cdr->clid, bufsize);
214 /* Channel */
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);
220 /* Last Data */
221 append_string(buf, cdr->lastdata, bufsize);
222 /* Start Time */
223 append_date(buf, cdr->start, bufsize);
224 /* Answer Time */
225 append_date(buf, cdr->answer, bufsize);
226 /* End Time */
227 append_date(buf, cdr->end, bufsize);
228 /* Duration */
229 append_int(buf, cdr->duration, bufsize);
230 /* Billable seconds */
231 append_int(buf, cdr->billsec, bufsize);
232 /* Disposition */
233 append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
234 /* AMA Flags */
235 append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
236 /* Unique ID */
237 if (loguniqueid)
238 append_string(buf, cdr->uniqueid, bufsize);
239 /* append the user field */
240 if(loguserfield)
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);
247 return 0;
249 return -1;
252 static int writefile(char *s, char *acc)
254 char tmp[PATH_MAX];
255 FILE *f;
257 if (strchr(acc, '/') || (acc[0] == '.')) {
258 ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
259 return -1;
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));
268 return -1;
270 fputs(s, f);
271 fflush(f);
272 fclose(f);
273 ast_mutex_unlock(&acf_lock);
275 return 0;
279 static int csv_log(struct ast_cdr *cdr)
281 FILE *mf = NULL;
282 /* Make sure we have a big enough buf */
283 char buf[1024];
284 char csvmaster[PATH_MAX];
285 snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
286 #if 0
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);
288 #endif
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));
291 return 0;
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"))) {
299 fputs(buf, mf);
300 fflush(mf); /* be particularly anal here */
301 fclose(mf);
302 mf = NULL;
303 ast_mutex_unlock(&mf_lock);
304 } else {
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));
314 return 0;
317 static int unload_module(void)
319 ast_cdr_unregister(name);
320 loaded = 0;
321 return 0;
324 static int load_module(void)
326 int res;
328 if(!load_config(0))
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");
333 } else {
334 loaded = 1;
336 return res;
339 static int reload(void)
341 if (load_config(1)) {
342 loaded = 1;
343 } else {
344 loaded = 0;
345 ast_log(LOG_WARNING, "No [csv] section in cdr.conf. Unregistering backend.\n");
346 ast_cdr_unregister(name);
349 return 0;
352 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Comma Separated Values CDR Backend",
353 .load = load_module,
354 .unload = unload_module,
355 .reload = reload,