Refactor CEL to avoid using the event system core
[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 #define CUSTOM_BACKEND_NAME "CEL Custom CSV Logging"
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(struct ast_event *event)
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         if (!dummy) {
132                 ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
133                 return;
134         }
135
136         AST_RWLIST_RDLOCK(&sinks);
137
138         AST_LIST_TRAVERSE(&sinks, config, list) {
139                 FILE *out;
140
141                 ast_str_substitute_variables(&str, 0, dummy, config->format);
142
143                 /* Even though we have a lock on the list, we could be being chased by
144                    another thread and this lock ensures that we won't step on anyone's
145                    toes.  Once each CEL backend gets it's own thread, this lock can be
146                    removed. */
147                 ast_mutex_lock(&config->lock);
148
149                 /* Because of the absolutely unconditional need for the
150                    highest reliability possible in writing billing records,
151                    we open write and close the log file each time */
152                 if ((out = fopen(config->filename, "a"))) {
153                         fputs(ast_str_buffer(str), out);
154                         fflush(out); /* be particularly anal here */
155                         fclose(out);
156                 } else {
157                         ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
158                 }
159
160                 ast_mutex_unlock(&config->lock);
161         }
162
163         AST_RWLIST_UNLOCK(&sinks);
164
165         ast_channel_unref(dummy);
166 }
167
168 static int unload_module(void)
169 {
170
171         if (AST_RWLIST_WRLOCK(&sinks)) {
172                 ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n");
173                 return -1;
174         }
175
176         free_config();
177         AST_RWLIST_UNLOCK(&sinks);
178         ast_cel_backend_unregister(CUSTOM_BACKEND_NAME);
179         return 0;
180 }
181
182 static enum ast_module_load_result load_module(void)
183 {
184         if (AST_RWLIST_WRLOCK(&sinks)) {
185                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
186                 return AST_MODULE_LOAD_FAILURE;
187         }
188
189         load_config();
190         AST_RWLIST_UNLOCK(&sinks);
191
192         if (ast_cel_backend_register(CUSTOM_BACKEND_NAME, custom_log)) {
193                 return AST_MODULE_LOAD_FAILURE;
194         }
195         return AST_MODULE_LOAD_SUCCESS;
196 }
197
198 static int reload(void)
199 {
200         if (AST_RWLIST_WRLOCK(&sinks)) {
201                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
202                 return AST_MODULE_LOAD_FAILURE;
203         }
204
205         free_config();
206         load_config();
207         AST_RWLIST_UNLOCK(&sinks);
208         return AST_MODULE_LOAD_SUCCESS;
209 }
210
211 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CEL Backend",
212         .load = load_module,
213         .unload = unload_module,
214         .reload = reload,
215         .load_pri = AST_MODPRI_CDR_DRIVER,
216         );
217