Merged revisions 328247 via svnmerge from
[asterisk/asterisk.git] / cel / cel_custom.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2009, Digium, Inc.
5  *
6  * Steve Murphy <murf@digium.com>
7  * much borrowed from cdr code (cdr_custom.c), author Mark Spencer
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! \file
21  *
22  * \brief Custom Comma Separated Value CEL records.
23  *
24  * \author Steve Murphy <murf@digium.com>
25  *
26  * \arg See also \ref AstCEL
27  *
28  * Logs in LOG_DIR/cel_custom
29  * \ingroup cel_drivers
30  */
31
32 /*** MODULEINFO
33         <support_level>core</support_level>
34  ***/
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
40 #include "asterisk/paths.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/cel.h"
43 #include "asterisk/module.h"
44 #include "asterisk/config.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/lock.h"
48 #include "asterisk/threadstorage.h"
49 #include "asterisk/strings.h"
50
51 #define CUSTOM_LOG_DIR "/cel_custom"
52 #define CONFIG         "cel_custom.conf"
53
54 AST_THREADSTORAGE(custom_buf);
55
56 static const char name[] = "cel-custom";
57
58 struct cel_config {
59         AST_DECLARE_STRING_FIELDS(
60                 AST_STRING_FIELD(filename);
61                 AST_STRING_FIELD(format);
62         );
63         ast_mutex_t lock;
64         AST_RWLIST_ENTRY(cel_config) list;
65 };
66
67 static struct ast_event_sub *event_sub = NULL;
68
69 static AST_RWLIST_HEAD_STATIC(sinks, cel_config);
70
71 static void free_config(void)
72 {
73         struct cel_config *sink;
74         while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
75                 ast_mutex_destroy(&sink->lock);
76                 ast_free(sink);
77         }
78 }
79
80 static int load_config(void)
81 {
82         struct ast_config *cfg;
83         struct ast_variable *var;
84         struct ast_flags config_flags = { 0 };
85         int res = 0;
86
87         cfg = ast_config_load(CONFIG, config_flags);
88         if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
89                 ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging CEL to custom CSVs.\n");
90                 return -1;
91         }
92
93         var = ast_variable_browse(cfg, "mappings");
94         while (var) {
95                 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
96                         struct cel_config *sink = ast_calloc_with_stringfields(1, struct cel_config, 1024);
97
98                         if (!sink) {
99                                 ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
100                                 res = -2;
101                                 break;
102                         }
103
104                         ast_string_field_build(sink, format, "%s\n", var->value);
105                         ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
106                         ast_mutex_init(&sink->lock);
107
108                         AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
109                 } else {
110                         ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
111                 }
112                 var = var->next;
113         }
114         ast_config_destroy(cfg);
115
116         return res;
117 }
118
119 static void custom_log(const struct ast_event *event, void *userdata)
120 {
121         struct ast_channel *dummy;
122         struct ast_str *str;
123         struct cel_config *config;
124
125         /* Batching saves memory management here.  Otherwise, it's the same as doing an allocation and free each time. */
126         if (!(str = ast_str_thread_get(&custom_buf, 16))) {
127                 return;
128         }
129
130         dummy = ast_cel_fabricate_channel_from_event(event);
131
132         if (!dummy) {
133                 ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
134                 return;
135         }
136
137         AST_RWLIST_RDLOCK(&sinks);
138
139         AST_LIST_TRAVERSE(&sinks, config, list) {
140                 FILE *out;
141
142                 ast_str_substitute_variables(&str, 0, dummy, config->format);
143
144                 /* Even though we have a lock on the list, we could be being chased by
145                    another thread and this lock ensures that we won't step on anyone's
146                    toes.  Once each CEL backend gets it's own thread, this lock can be
147                    removed. */
148                 ast_mutex_lock(&config->lock);
149
150                 /* Because of the absolutely unconditional need for the
151                    highest reliability possible in writing billing records,
152                    we open write and close the log file each time */
153                 if ((out = fopen(config->filename, "a"))) {
154                         fputs(ast_str_buffer(str), out);
155                         fflush(out); /* be particularly anal here */
156                         fclose(out);
157                 } else {
158                         ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
159                 }
160
161                 ast_mutex_unlock(&config->lock);
162         }
163
164         AST_RWLIST_UNLOCK(&sinks);
165
166         ast_channel_release(dummy);
167 }
168
169 static int unload_module(void)
170 {
171         if (event_sub) {
172                 event_sub = ast_event_unsubscribe(event_sub);
173         }
174
175         if (AST_RWLIST_WRLOCK(&sinks)) {
176                 event_sub = ast_event_subscribe(AST_EVENT_CEL, custom_log, "CEL Custom CSV Logging",
177                         NULL, AST_EVENT_IE_END);
178                 ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n");
179                 return -1;
180         }
181
182         free_config();
183         AST_RWLIST_UNLOCK(&sinks);
184         return 0;
185 }
186
187 static enum ast_module_load_result load_module(void)
188 {
189         if (AST_RWLIST_WRLOCK(&sinks)) {
190                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
191                 return AST_MODULE_LOAD_FAILURE;
192         }
193
194         load_config();
195         AST_RWLIST_UNLOCK(&sinks);
196
197         event_sub = ast_event_subscribe(AST_EVENT_CEL, custom_log, "CEL Custom CSV Logging",
198                 NULL, AST_EVENT_IE_END);
199         return AST_MODULE_LOAD_SUCCESS;
200 }
201
202 static int reload(void)
203 {
204         if (AST_RWLIST_WRLOCK(&sinks)) {
205                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
206                 return AST_MODULE_LOAD_FAILURE;
207         }
208
209         free_config();
210         load_config();
211         AST_RWLIST_UNLOCK(&sinks);
212         return AST_MODULE_LOAD_SUCCESS;
213 }
214
215 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CEL Backend",
216         .load = load_module,
217         .unload = unload_module,
218         .reload = reload,
219         .load_pri = AST_MODPRI_CDR_DRIVER,
220         );
221