Add CLI command "cdr show pgsql status" based on "cdr mysql status"
[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 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <time.h>
37
38 #include "asterisk/channel.h"
39 #include "asterisk/cdr.h"
40 #include "asterisk/module.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/manager.h"
43 #include "asterisk/config.h"
44 #include "asterisk/pbx.h"
45
46 #define DATE_FORMAT     "%Y-%m-%d %T"
47 #define CONF_FILE       "cdr_manager.conf"
48 #define CUSTOM_FIELDS_BUF_SIZE 1024
49
50 static const char name[] = "cdr_manager";
51
52 static int enablecdr = 0;
53
54 static struct ast_str *customfields;
55 AST_RWLOCK_DEFINE_STATIC(customfields_lock);
56
57 static int manager_log(struct ast_cdr *cdr);
58
59 static int load_config(int reload)
60 {
61         char *cat = NULL;
62         struct ast_config *cfg;
63         struct ast_variable *v;
64         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
65         int newenablecdr = 0;
66
67         cfg = ast_config_load(CONF_FILE, config_flags);
68         if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
69                 return 0;
70         }
71
72         if (cfg == CONFIG_STATUS_FILEINVALID) {
73                 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
74                 return -1;
75         }
76
77         if (!cfg) {
78                 /* Standard configuration */
79                 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
80                 if (enablecdr)
81                         ast_cdr_unregister(name);
82                 enablecdr = 0;
83                 return -1;
84         }
85
86         if (reload) {
87                 ast_rwlock_wrlock(&customfields_lock);
88         }
89
90         if (reload && customfields) {
91                 ast_free(customfields);
92                 customfields = NULL;
93         }
94
95         while ( (cat = ast_category_browse(cfg, cat)) ) {
96                 if (!strcasecmp(cat, "general")) {
97                         v = ast_variable_browse(cfg, cat);
98                         while (v) {
99                                 if (!strcasecmp(v->name, "enabled"))
100                                         newenablecdr = ast_true(v->value);
101
102                                 v = v->next;
103                         }
104                 } else if (!strcasecmp(cat, "mappings")) {
105                         customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
106                         v = ast_variable_browse(cfg, cat);
107                         while (v) {
108                                 if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
109                                         if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
110                                                 ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
111                                                 ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
112                                         } else {
113                                                 ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
114                                                 break;
115                                         }
116
117                                 }
118                                 v = v->next;
119                         }
120                 }
121         }
122
123         if (reload) {
124                 ast_rwlock_unlock(&customfields_lock);
125         }
126
127         ast_config_destroy(cfg);
128
129         if (enablecdr && !newenablecdr)
130                 ast_cdr_unregister(name);
131         else if (!enablecdr && newenablecdr)
132                 ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
133         enablecdr = newenablecdr;
134
135         return 0;
136 }
137
138 static int manager_log(struct ast_cdr *cdr)
139 {
140         struct ast_tm timeresult;
141         char strStartTime[80] = "";
142         char strAnswerTime[80] = "";
143         char strEndTime[80] = "";
144         char buf[CUSTOM_FIELDS_BUF_SIZE];
145
146         if (!enablecdr)
147                 return 0;
148
149         ast_localtime(&cdr->start, &timeresult, NULL);
150         ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
151
152         if (cdr->answer.tv_sec) {
153                 ast_localtime(&cdr->answer, &timeresult, NULL);
154                 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
155         }
156
157         ast_localtime(&cdr->end, &timeresult, NULL);
158         ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
159
160         buf[0] = '\0';
161         ast_rwlock_rdlock(&customfields_lock);
162         if (customfields && ast_str_strlen(customfields)) {
163                 struct ast_channel *dummy = ast_dummy_channel_alloc();
164                 if (!dummy) {
165                         ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
166                         return 0;
167                 }
168                 dummy->cdr = ast_cdr_dup(cdr);
169                 pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
170                 ast_channel_unref(dummy);
171         }
172         ast_rwlock_unlock(&customfields_lock);
173
174         manager_event(EVENT_FLAG_CDR, "Cdr",
175             "AccountCode: %s\r\n"
176             "Source: %s\r\n"
177             "Destination: %s\r\n"
178             "DestinationContext: %s\r\n"
179             "CallerID: %s\r\n"
180             "Channel: %s\r\n"
181             "DestinationChannel: %s\r\n"
182             "LastApplication: %s\r\n"
183             "LastData: %s\r\n"
184             "StartTime: %s\r\n"
185             "AnswerTime: %s\r\n"
186             "EndTime: %s\r\n"
187             "Duration: %ld\r\n"
188             "BillableSeconds: %ld\r\n"
189             "Disposition: %s\r\n"
190             "AMAFlags: %s\r\n"
191             "UniqueID: %s\r\n"
192             "UserField: %s\r\n"
193             "%s",
194             cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
195             cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
196             cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
197             ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
198
199         return 0;
200 }
201
202 static int unload_module(void)
203 {
204         ast_cdr_unregister(name);
205         if (customfields)
206                 ast_free(customfields);
207
208         return 0;
209 }
210
211 static int load_module(void)
212 {
213         if (load_config(0)) {
214                 return AST_MODULE_LOAD_DECLINE;
215         }
216
217         return AST_MODULE_LOAD_SUCCESS;
218 }
219
220 static int reload(void)
221 {
222         return load_config(1);
223 }
224
225 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
226                 .load = load_module,
227                 .unload = unload_module,
228                 .reload = reload,
229                 .load_pri = AST_MODPRI_CDR_DRIVER,
230                );