Stop ignoring .lastclean
[asterisk-bristuff.git] / cdr / cdr_csv.c
blobcdaef685366cd0c6261cea109ef545e63e23e5b7
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 <sys/types.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <time.h>
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"
50 #include "asterisk/lock.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 int loaded = 0;
61 static char *config = "cdr.conf";
63 /* #define CSV_LOGUNIQUEID 1 */
64 /* #define CSV_LOGUSERFIELD 1 */
66 /*----------------------------------------------------
67 The values are as follows:
70 "accountcode", accountcode is the account name of detail records, Master.csv contains all records *
71 Detail records are configured on a channel basis, IAX and SIP are determined by user *
72 DAHDI is determined by channel in chan_dahdi.conf
73 "source",
74 "destination",
75 "destination context",
76 "callerid",
77 "channel",
78 "destination channel", (if applicable)
79 "last application", Last application run on the channel
80 "last app argument", argument to the last channel
81 "start time",
82 "answer time",
83 "end time",
84 duration, Duration is the whole length that the entire call lasted. ie. call rx'd to hangup
85 "end time" minus "start time"
86 billable seconds, the duration that a call was up after other end answered which will be <= to duration
87 "end time" minus "answer time"
88 "disposition", ANSWERED, NO ANSWER, BUSY
89 "amaflags", DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode.
90 "uniqueid", unique call identifier
91 "userfield" user field set via SetCDRUserField
92 ----------------------------------------------------------*/
94 static char *name = "csv";
96 AST_MUTEX_DEFINE_STATIC(mf_lock);
97 AST_MUTEX_DEFINE_STATIC(acf_lock);
99 static int load_config(void)
101 struct ast_config *cfg;
102 struct ast_variable *var;
103 const char *tmp;
105 usegmtime = 0;
106 loguniqueid = 0;
107 loguserfield = 0;
109 cfg = ast_config_load(config);
111 if (!cfg) {
112 ast_log(LOG_WARNING, "unable to load config: %s\n", config);
113 return 0;
116 var = ast_variable_browse(cfg, "csv");
117 if (!var) {
118 ast_config_destroy(cfg);
119 return 0;
122 tmp = ast_variable_retrieve(cfg, "csv", "usegmtime");
123 if (tmp) {
124 usegmtime = ast_true(tmp);
125 if (usegmtime) {
126 ast_log(LOG_DEBUG, "logging time in GMT\n");
130 tmp = ast_variable_retrieve(cfg, "csv", "loguniqueid");
131 if (tmp) {
132 loguniqueid = ast_true(tmp);
133 if (loguniqueid) {
134 ast_log(LOG_DEBUG, "logging CDR field UNIQUEID\n");
138 tmp = ast_variable_retrieve(cfg, "csv", "loguserfield");
139 if (tmp) {
140 loguserfield = ast_true(tmp);
141 if (loguserfield) {
142 ast_log(LOG_DEBUG, "logging CDR user-defined field\n");
146 ast_config_destroy(cfg);
147 return 1;
150 static int append_string(char *buf, char *s, size_t bufsize)
152 int pos = strlen(buf);
153 int spos = 0;
154 int error = 0;
155 if (pos >= bufsize - 4)
156 return -1;
157 buf[pos++] = '\"';
158 error = -1;
159 while(pos < bufsize - 3) {
160 if (!s[spos]) {
161 error = 0;
162 break;
164 if (s[spos] == '\"')
165 buf[pos++] = '\"';
166 buf[pos++] = s[spos];
167 spos++;
169 buf[pos++] = '\"';
170 buf[pos++] = ',';
171 buf[pos++] = '\0';
172 return error;
175 static int append_int(char *buf, int s, size_t bufsize)
177 char tmp[32];
178 int pos = strlen(buf);
179 snprintf(tmp, sizeof(tmp), "%d", s);
180 if (pos + strlen(tmp) > bufsize - 3)
181 return -1;
182 strncat(buf, tmp, bufsize - strlen(buf) - 1);
183 pos = strlen(buf);
184 buf[pos++] = ',';
185 buf[pos++] = '\0';
186 return 0;
189 static int append_date(char *buf, struct timeval tv, size_t bufsize)
191 char tmp[80] = "";
192 struct tm tm;
193 time_t t;
194 t = tv.tv_sec;
195 if (strlen(buf) > bufsize - 3)
196 return -1;
197 if (ast_tvzero(tv)) {
198 strncat(buf, ",", bufsize - strlen(buf) - 1);
199 return 0;
201 if (usegmtime) {
202 gmtime_r(&t,&tm);
203 } else {
204 ast_localtime(&t, &tm, NULL);
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)
213 buf[0] = '\0';
214 /* Account code */
215 append_string(buf, cdr->accountcode, bufsize);
216 /* Source */
217 append_string(buf, cdr->src, bufsize);
218 /* Destination */
219 append_string(buf, cdr->dst, bufsize);
220 /* Destination context */
221 append_string(buf, cdr->dcontext, bufsize);
222 /* Caller*ID */
223 append_string(buf, cdr->clid, bufsize);
224 /* Channel */
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);
230 /* Last Data */
231 append_string(buf, cdr->lastdata, bufsize);
232 /* Start Time */
233 append_date(buf, cdr->start, bufsize);
234 /* Answer Time */
235 append_date(buf, cdr->answer, bufsize);
236 /* End Time */
237 append_date(buf, cdr->end, bufsize);
238 /* Duration */
239 append_int(buf, cdr->duration, bufsize);
240 /* Billable seconds */
241 append_int(buf, cdr->billsec, bufsize);
242 /* Disposition */
243 append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
244 /* AMA Flags */
245 append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
246 /* Unique ID */
247 if (loguniqueid)
248 append_string(buf, cdr->uniqueid, bufsize);
249 /* append the user field */
250 if(loguserfield)
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);
257 return 0;
259 return -1;
262 static int writefile(char *s, char *acc)
264 char tmp[PATH_MAX];
265 FILE *f;
266 if (strchr(acc, '/') || (acc[0] == '.')) {
267 ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
268 return -1;
270 snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
272 ast_mutex_lock(&acf_lock);
273 f = fopen(tmp, "a");
274 if (!f) {
275 ast_mutex_unlock(&acf_lock);
276 ast_log(LOG_ERROR, "Unable to open file %s : %s\n", tmp, strerror(errno));
277 return -1;
279 fputs(s, f);
280 fflush(f);
281 fclose(f);
282 ast_mutex_unlock(&acf_lock);
284 return 0;
288 static int csv_log(struct ast_cdr *cdr)
290 FILE *mf = NULL;
291 /* Make sure we have a big enough buf */
292 char buf[1024];
293 char csvmaster[PATH_MAX];
294 snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
295 #if 0
296 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);
297 #endif
298 if (build_csv_record(buf, sizeof(buf), cdr)) {
299 ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
300 } else {
301 /* because of the absolutely unconditional need for the
302 highest reliability possible in writing billing records,
303 we open write and close the log file each time */
304 ast_mutex_lock(&mf_lock);
305 mf = fopen(csvmaster, "a");
306 if (mf) {
307 fputs(buf, mf);
308 fflush(mf); /* be particularly anal here */
309 fclose(mf);
310 mf = NULL;
311 ast_mutex_unlock(&mf_lock);
312 } else {
313 ast_mutex_unlock(&mf_lock);
314 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
317 if (!ast_strlen_zero(cdr->accountcode)) {
318 if (writefile(buf, cdr->accountcode))
319 ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
322 return 0;
325 static int unload_module(void)
327 ast_cdr_unregister(name);
328 loaded = 0;
329 return 0;
332 static int load_module(void)
334 int res;
336 if(!load_config())
337 return AST_MODULE_LOAD_DECLINE;
339 res = ast_cdr_register(name, ast_module_info->description, csv_log);
340 if (res) {
341 ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
342 } else {
343 loaded = 1;
345 return res;
348 static int reload(void)
350 if (load_config()) {
351 loaded = 1;
352 } else {
353 loaded = 0;
354 ast_log(LOG_WARNING, "No [csv] section in cdr.conf. Unregistering backend.\n");
355 ast_cdr_unregister(name);
358 return 0;
361 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Comma Separated Values CDR Backend",
362 .load = load_module,
363 .unload = unload_module,
364 .reload = reload,