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