Prevent CDR backends from unregistering while billing data is in flight
[asterisk/asterisk.git] / cdr / cdr_manager.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2005
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*!
18  * \file
19  * \brief Asterisk Call Manager CDR records.
20  *
21  * See also
22  * \arg \ref AstCDR
23  * \arg \ref AstAMI
24  * \arg \ref Config_ami
25  * \ingroup cdr_drivers
26  */
27
28 /*! \li \ref cdr_manager.c uses the configuration file \ref cdr_manager.conf
29  * \addtogroup configuration_file Configuration Files
30  */
31
32 /*!
33  * \page cdr_manager.conf cdr_manager.conf
34  * \verbinclude cdr_manager.conf.sample
35  */
36
37 /*** MODULEINFO
38         <support_level>core</support_level>
39  ***/
40
41 #include "asterisk.h"
42
43 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44
45 #include <time.h>
46
47 #include "asterisk/channel.h"
48 #include "asterisk/cdr.h"
49 #include "asterisk/module.h"
50 #include "asterisk/utils.h"
51 #include "asterisk/manager.h"
52 #include "asterisk/config.h"
53 #include "asterisk/pbx.h"
54
55 #define DATE_FORMAT     "%Y-%m-%d %T"
56 #define CONF_FILE       "cdr_manager.conf"
57 #define CUSTOM_FIELDS_BUF_SIZE 1024
58
59 static const char name[] = "cdr_manager";
60
61 static int enablecdr = 0;
62
63 static struct ast_str *customfields;
64 AST_RWLOCK_DEFINE_STATIC(customfields_lock);
65
66 static int manager_log(struct ast_cdr *cdr);
67
68 static int load_config(int reload)
69 {
70         char *cat = NULL;
71         struct ast_config *cfg;
72         struct ast_variable *v;
73         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
74         int newenablecdr = 0;
75
76         cfg = ast_config_load(CONF_FILE, config_flags);
77         if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
78                 return 0;
79         }
80
81         if (cfg == CONFIG_STATUS_FILEINVALID) {
82                 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
83                 return -1;
84         }
85
86         if (!cfg) {
87                 /* Standard configuration */
88                 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
89                 if (enablecdr) {
90                         ast_cdr_backend_suspend(name);
91                 }
92                 enablecdr = 0;
93                 return -1;
94         }
95
96         if (reload) {
97                 ast_rwlock_wrlock(&customfields_lock);
98         }
99
100         if (reload && customfields) {
101                 ast_free(customfields);
102                 customfields = NULL;
103         }
104
105         while ( (cat = ast_category_browse(cfg, cat)) ) {
106                 if (!strcasecmp(cat, "general")) {
107                         v = ast_variable_browse(cfg, cat);
108                         while (v) {
109                                 if (!strcasecmp(v->name, "enabled"))
110                                         newenablecdr = ast_true(v->value);
111
112                                 v = v->next;
113                         }
114                 } else if (!strcasecmp(cat, "mappings")) {
115                         customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
116                         v = ast_variable_browse(cfg, cat);
117                         while (v) {
118                                 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
119                                         if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
120                                                 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
121                                                 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
122                                         } else {
123                                                 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
124                                                 break;
125                                         }
126
127                                 }
128                                 v = v->next;
129                         }
130                 }
131         }
132
133         if (reload) {
134                 ast_rwlock_unlock(&customfields_lock);
135         }
136
137         ast_config_destroy(cfg);
138
139         if (!newenablecdr) {
140                 ast_cdr_backend_suspend(name);
141         } else if (newenablecdr) {
142                 ast_cdr_backend_unsuspend(name);
143         }
144         enablecdr = newenablecdr;
145
146         return 0;
147 }
148
149 static int manager_log(struct ast_cdr *cdr)
150 {
151         struct ast_tm timeresult;
152         char strStartTime[80] = "";
153         char strAnswerTime[80] = "";
154         char strEndTime[80] = "";
155         char buf[CUSTOM_FIELDS_BUF_SIZE];
156
157         if (!enablecdr)
158                 return 0;
159
160         ast_localtime(&cdr->start, &timeresult, NULL);
161         ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
162
163         if (cdr->answer.tv_sec) {
164                 ast_localtime(&cdr->answer, &timeresult, NULL);
165                 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
166         }
167
168         ast_localtime(&cdr->end, &timeresult, NULL);
169         ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
170
171         buf[0] = '\0';
172         ast_rwlock_rdlock(&customfields_lock);
173         if (customfields && ast_str_strlen(customfields)) {
174                 struct ast_channel *dummy = ast_dummy_channel_alloc();
175                 if (!dummy) {
176                         ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
177                         return 0;
178                 }
179                 ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
180                 pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
181                 ast_channel_unref(dummy);
182         }
183         ast_rwlock_unlock(&customfields_lock);
184
185         manager_event(EVENT_FLAG_CDR, "Cdr",
186             "AccountCode: %s\r\n"
187             "Source: %s\r\n"
188             "Destination: %s\r\n"
189             "DestinationContext: %s\r\n"
190             "CallerID: %s\r\n"
191             "Channel: %s\r\n"
192             "DestinationChannel: %s\r\n"
193             "LastApplication: %s\r\n"
194             "LastData: %s\r\n"
195             "StartTime: %s\r\n"
196             "AnswerTime: %s\r\n"
197             "EndTime: %s\r\n"
198             "Duration: %ld\r\n"
199             "BillableSeconds: %ld\r\n"
200             "Disposition: %s\r\n"
201             "AMAFlags: %s\r\n"
202             "UniqueID: %s\r\n"
203             "UserField: %s\r\n"
204             "%s",
205             cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
206             cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
207             cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
208             ast_channel_amaflags2string(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
209
210         return 0;
211 }
212
213 static int unload_module(void)
214 {
215         if (ast_cdr_unregister(name)) {
216                 return -1;
217         }
218
219         if (customfields)
220                 ast_free(customfields);
221
222         return 0;
223 }
224
225 static int load_module(void)
226 {
227         if (ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log)) {
228                 return AST_MODULE_LOAD_DECLINE;
229         }
230
231         if (load_config(0)) {
232                 ast_cdr_unregister(name);
233                 return AST_MODULE_LOAD_DECLINE;
234         }
235
236         return AST_MODULE_LOAD_SUCCESS;
237 }
238
239 static int reload(void)
240 {
241         return load_config(1);
242 }
243
244 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
245                 .load = load_module,
246                 .unload = unload_module,
247                 .reload = reload,
248                 .load_pri = AST_MODPRI_CDR_DRIVER,
249                );