edb009686ec38d0de9ea2f1db58abe606ae397a2
[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 mappings = 0;
86         int res = 0;
87
88         cfg = ast_config_load(CONFIG, config_flags);
89         if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
90                 ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging CEL to custom CSVs.\n");
91                 return -1;
92         }
93
94         if (!(var = ast_variable_browse(cfg, "mappings"))) {
95                 ast_log(LOG_NOTICE, "No mappings found in " CONFIG ". Not logging CEL to custom CSVs.\n");
96         }
97
98         while (var) {
99                 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
100                         struct cel_config *sink = ast_calloc_with_stringfields(1, struct cel_config, 1024);
101
102                         if (!sink) {
103                                 ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
104                                 res = -2;
105                                 break;
106                         }
107
108                         ast_string_field_build(sink, format, "%s\n", var->value);
109                         ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
110                         ast_mutex_init(&sink->lock);
111
112                         ast_verb(3, "Added CEL CSV mapping for '%s'.\n", sink->filename);
113                         mappings += 1;
114                         AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
115                 } else {
116                         ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
117                 }
118                 var = var->next;
119         }
120         ast_config_destroy(cfg);
121
122         ast_verb(1, "Added CEL CSV mapping for %d files.\n", mappings);
123
124         return res;
125 }
126
127 static void custom_log(struct ast_event *event)
128 {
129         struct ast_channel *dummy;
130         struct ast_str *str;
131         struct cel_config *config;
132
133         /* Batching saves memory management here.  Otherwise, it's the same as doing an allocation and free each time. */
134         if (!(str = ast_str_thread_get(&custom_buf, 16))) {
135                 return;
136         }
137
138         dummy = ast_cel_fabricate_channel_from_event(event);
139         if (!dummy) {
140                 ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
141                 return;
142         }
143
144         AST_RWLIST_RDLOCK(&sinks);
145
146         AST_LIST_TRAVERSE(&sinks, config, list) {
147                 FILE *out;
148
149                 ast_str_substitute_variables(&str, 0, dummy, config->format);
150
151                 /* Even though we have a lock on the list, we could be being chased by
152                    another thread and this lock ensures that we won't step on anyone's
153                    toes.  Once each CEL backend gets it's own thread, this lock can be
154                    removed. */
155                 ast_mutex_lock(&config->lock);
156
157                 /* Because of the absolutely unconditional need for the
158                    highest reliability possible in writing billing records,
159                    we open write and close the log file each time */
160                 if ((out = fopen(config->filename, "a"))) {
161                         fputs(ast_str_buffer(str), out);
162                         fflush(out); /* be particularly anal here */
163                         fclose(out);
164                 } else {
165                         ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
166                 }
167
168                 ast_mutex_unlock(&config->lock);
169         }
170
171         AST_RWLIST_UNLOCK(&sinks);
172
173         ast_channel_unref(dummy);
174 }
175
176 static int unload_module(void)
177 {
178
179         if (AST_RWLIST_WRLOCK(&sinks)) {
180                 ast_log(LOG_ERROR, "Unable to lock sink list.  Unload failed.\n");
181                 return -1;
182         }
183
184         free_config();
185         AST_RWLIST_UNLOCK(&sinks);
186         ast_cel_backend_unregister(CUSTOM_BACKEND_NAME);
187         return 0;
188 }
189
190 static enum ast_module_load_result load_module(void)
191 {
192         if (AST_RWLIST_WRLOCK(&sinks)) {
193                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
194                 return AST_MODULE_LOAD_FAILURE;
195         }
196
197         load_config();
198         AST_RWLIST_UNLOCK(&sinks);
199
200         if (ast_cel_backend_register(CUSTOM_BACKEND_NAME, custom_log)) {
201                 return AST_MODULE_LOAD_FAILURE;
202         }
203         return AST_MODULE_LOAD_SUCCESS;
204 }
205
206 static int reload(void)
207 {
208         if (AST_RWLIST_WRLOCK(&sinks)) {
209                 ast_log(LOG_ERROR, "Unable to lock sink list.  Load failed.\n");
210                 return AST_MODULE_LOAD_FAILURE;
211         }
212
213         free_config();
214         load_config();
215         AST_RWLIST_UNLOCK(&sinks);
216         return AST_MODULE_LOAD_SUCCESS;
217 }
218
219 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CEL Backend",
220         .support_level = AST_MODULE_SUPPORT_CORE,
221         .load = load_module,
222         .unload = unload_module,
223         .reload = reload,
224         .load_pri = AST_MODPRI_CDR_DRIVER,
225         );
226