Merge the new Channel Event Logging (CEL) subsystem.
[asterisk/asterisk.git] / cdr / cdr_custom.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2009, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * Includes code and algorithms from the Zapata library.
9  *
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.
15  *
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.
19  */
20
21 /*! \file
22  *
23  * \brief Custom Comma Separated Value CDR records.
24  *
25  * \author Mark Spencer <markster@digium.com>
26  *
27  * \arg See also \ref AstCDR
28  *
29  * Logs in LOG_DIR/cdr_custom
30  * \ingroup cdr_drivers
31  */
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <time.h>
38
39 #include "asterisk/paths.h"     /* use ast_config_AST_LOG_DIR */
40 #include "asterisk/channel.h"
41 #include "asterisk/cdr.h"
42 #include "asterisk/module.h"
43 #include "asterisk/config.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/utils.h"
46 #include "asterisk/lock.h"
47 #include "asterisk/threadstorage.h"
48 #include "asterisk/strings.h"
49
50 #define CUSTOM_LOG_DIR "/cdr_custom"
51 #define CONFIG         "cdr_custom.conf"
52
53 AST_THREADSTORAGE(custom_buf);
54
55 static const char name[] = "cdr-custom";
56
57 struct cdr_config {
58         AST_DECLARE_STRING_FIELDS(
59                 AST_STRING_FIELD(filename);
60                 AST_STRING_FIELD(format);
61                 );
62         ast_mutex_t lock;
63         AST_RWLIST_ENTRY(cdr_config) list;
64 };
65
66 static AST_RWLIST_HEAD_STATIC(sinks, cdr_config);
67
68 static void free_config(void)
69 {
70         struct cdr_config *sink;
71         while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
72                 ast_mutex_destroy(&sink->lock);
73                 ast_free(sink);
74         }
75 }
76
77 static int load_config(void)
78 {
79         struct ast_config *cfg;
80         struct ast_variable *var;
81         struct ast_flags config_flags = { 0 };
82         int res = 0;
83
84         cfg = ast_config_load(CONFIG, config_flags);
85         if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
86                 ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n");
87                 return -1;
88         }
89
90         var = ast_variable_browse(cfg, "mappings");
91         while (var) {
92                 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
93                         struct cdr_config *sink = ast_calloc_with_stringfields(1, struct cdr_config, 1024);
94
95                         if (!sink) {
96                                 ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
97                                 res = -2;
98                                 break;
99                         }
100
101                         ast_string_field_build(sink, format, "%s\n", var->value);
102                         ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
103                         ast_mutex_init(&sink->lock);
104
105                         AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
106                 } else {
107                         ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
108                 }
109                 var = var->next;
110         }
111         ast_config_destroy(cfg);
112
113         return res;
114 }
115
116 static int custom_log(struct ast_cdr *cdr)
117 {
118         struct ast_channel *dummy;
119         struct ast_str *str;
120         struct cdr_config *config;
121
122         /* Batching saves memory management here.  Otherwise, it's the same as doing an allocation and free each time. */
123         if (!(str = ast_str_thread_get(&custom_buf, 16))) {
124                 return -1;
125         }
126
127         dummy = ast_dummy_channel_alloc();
128
129         if (!dummy) {
130                 ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
131                 return -1;
132         }
133
134         /* We need to dup here since the cdr actually belongs to the other channel,
135            so when we release this channel we don't want the CDR getting cleaned
136            up prematurely. */
137         dummy->cdr = ast_cdr_dup(cdr);
138
139         AST_RWLIST_RDLOCK(&sinks);
140
141         AST_LIST_TRAVERSE(&sinks, config, list) {
142                 FILE *out;
143
144                 ast_str_substitute_variables(&str, 0, dummy, config->format);
145
146                 /* Even though we have a lock on the list, we could be being chased by
147                    another thread and this lock ensures that we won't step on anyone's
148                    toes.  Once each CDR backend gets it's own thread, this lock can be
149                    removed. */
150                 ast_mutex_lock(&config->lock);
151
152                 /* Because of the absolutely unconditional need for the
153                    highest reliability possible in writing billing records,
154                    we open write and close the log file each time */
155                 if ((out = fopen(config->filename, "a"))) {
156                         fputs(ast_str_buffer(str), out);
157                         fflush(out); /* be particularly anal here */
158                         fclose(out);
159                 } else {
160                         ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
161                 }
162
163                 ast_mutex_unlock(&config->lock);
164         }
165
166         AST_RWLIST_UNLOCK(&sinks);
167
168         ast_channel_release(dummy);
169
170         return 0;
171 }
172
173 static int unload_module(void)
174 {
175         ast_cdr_unregister(name);
176
177         if (AST_RWLIST_WRLOCK(&sinks)) {
178                 ast_cdr_register(name, ast_module_info->description, custom_log);
179                 ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n");
180                 return -1;
181         }
182
183         free_config();
184         AST_RWLIST_UNLOCK(&sinks);
185         return 0;
186 }
187
188 static enum ast_module_load_result load_module(void)
189 {
190         if (AST_RWLIST_WRLOCK(&sinks)) {
191                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
192                 return AST_MODULE_LOAD_FAILURE;
193         }
194
195         load_config();
196         AST_RWLIST_UNLOCK(&sinks);
197         ast_cdr_register(name, ast_module_info->description, custom_log);
198         return AST_MODULE_LOAD_SUCCESS;
199 }
200
201 static int reload(void)
202 {
203         if (AST_RWLIST_WRLOCK(&sinks)) {
204                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
205                 return AST_MODULE_LOAD_FAILURE;
206         }
207
208         free_config();
209         load_config();
210         AST_RWLIST_UNLOCK(&sinks);
211         return AST_MODULE_LOAD_SUCCESS;
212 }
213
214 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Customizable Comma Separated Values CDR Backend",
215                 .load = load_module,
216                 .unload = unload_module,
217                 .reload = reload,
218                );
219