Debian Codegraph @

cdr/cdr_beanstalkd.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2017
5 *
6 * Nir Simionovich <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 * \brief Asterisk Beanstalkd CDR records.
22 *
23 * This module requires the beanstalk-client library, available from
24 * https://github.com/deepfryed/beanstalk-client
25 *
26 * See also
27 * \arg \ref AstCDR
28 * \ingroup cdr_drivers
29 */
30
31 /*! \li \ref cdr_beanstalkd.c uses the configuration file \ref cdr_beanstalkd.conf
32 * \addtogroup configuration_file Configuration Files
33 */
34
35 /*!
36 * \page cdr_beanstalkd.conf cdr_beanstalkd.conf
37 * \verbinclude cdr_beanstalkd.conf.sample
38 */
39
40 /*** MODULEINFO
41 <depend>beanstalk</depend>
42 <support_level>extended</support_level>
43 ***/
44
45 #include "asterisk.h"
46
47 #include <time.h>
48 #include <stdio.h>
49
50 #include "beanstalk.h"
51 #include "asterisk/channel.h"
52 #include "asterisk/cdr.h"
53 #include "asterisk/module.h"
54 #include "asterisk/utils.h"
55 #include "asterisk/manager.h"
56 #include "asterisk/config.h"
57 #include "asterisk/pbx.h"
58 #include "asterisk/json.h"
59
60 #define DATE_FORMAT "%Y-%m-%d %T"
61 #define CONF_FILE "cdr_beanstalkd.conf"
62 #define BEANSTALK_JOB_SIZE 4096
63 #define BEANSTALK_JOB_PRIORITY 99
64 #define BEANSTALK_JOB_TTR 60
65 #define BEANSTALK_JOB_DELAY 0
66 #define DEFAULT_BEANSTALK_HOST "127.0.0.1"
67 #define DEFAULT_BEANSTALK_PORT 11300
68 #define DEFAULT_BEANSTALK_TUBE "asterisk-cdr"
69
70 static const char name[] = "cdr_beanstalkd";
71
72 static int enablecdr = 0;
73 static char *bs_host;
74 static int bs_port;
75 static char *bs_tube;
76 static int priority;
77
78 AST_RWLOCK_DEFINE_STATIC(config_lock);
79
80 static int beanstalk_put(struct ast_cdr *cdr);
81
82 static int load_config(int reload) {
83 char *cat = NULL;
84 struct ast_config *cfg;
85 struct ast_variable *v;
86 struct ast_flags config_flags = {reload ? CONFIG_FLAG_FILEUNCHANGED : 0};
87 int newenablecdr = 0;
88
89 cfg = ast_config_load(CONF_FILE, config_flags);
90 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
91 return 0;
92 }
93
94 if (cfg == CONFIG_STATUS_FILEINVALID) {
95 ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
96 return -1;
97 }
98
99 if (!cfg) {
100 /* Standard configuration */
101 ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
102 if (enablecdr) {
103 ast_cdr_backend_suspend(name);
104 }
105 enablecdr = 0;
106 return -1;
107 }
108
109 if (reload) {
110 ast_rwlock_wrlock(&config_lock);
111 ast_free(bs_host);
112 ast_free(bs_tube);
113 }
114
115 /* Bootstrap the default configuration */
116 bs_host = ast_strdup(DEFAULT_BEANSTALK_HOST);
117 bs_port = DEFAULT_BEANSTALK_PORT;
118 bs_tube = ast_strdup(DEFAULT_BEANSTALK_TUBE);
119 priority = BEANSTALK_JOB_PRIORITY;
120
121 while ((cat = ast_category_browse(cfg, cat))) {
122 if (!strcasecmp(cat, "general")) {
123 v = ast_variable_browse(cfg, cat);
124 while (v) {
125
126 if (!strcasecmp(v->name, "enabled")) {
127 newenablecdr = ast_true(v->value);
128 } else if (!strcasecmp(v->name, "host")) {
129 ast_free(bs_host);
130 bs_host = ast_strdup(v->value);
131 } else if (!strcasecmp(v->name, "port")) {
132 bs_port = atoi(v->value);
133 } else if (!strcasecmp(v->name, "tube")) {
134 ast_free(bs_tube);
135 bs_tube = ast_strdup(v->value);
136 } else if (!strcasecmp(v->name, "priority")) {
137 priority = atoi(v->value);
138 }
139 v = v->next;
140
141 }
142 }
143 }
144
145 if (reload) {
146 ast_rwlock_unlock(&config_lock);
147 }
148
149 ast_config_destroy(cfg);
150
151 if (!newenablecdr) {
152 ast_cdr_backend_suspend(name);
153 } else if (newenablecdr) {
154 ast_cdr_backend_unsuspend(name);
155 ast_log(LOG_NOTICE, "Added beanstalkd server %s at port %d with tube %s", bs_host, bs_port, bs_tube);
156 }
157 enablecdr = newenablecdr;
158
159 return 0;
160 }
161
162 static int beanstalk_put(struct ast_cdr *cdr) {
163 struct ast_tm timeresult;
164 char strAnswerTime[80] = "";
165 char strStartTime[80];
166 char strEndTime[80];
167 char *cdr_buffer;
168 int bs_id;
169 int bs_socket;
170 struct ast_json *t_cdr_json;
171
172 if (!enablecdr) {
173 return 0;
174 }
175
176 ast_rwlock_rdlock(&config_lock);
177 bs_socket = bs_connect(bs_host, bs_port);
178
179 if (bs_use(bs_socket, bs_tube) != BS_STATUS_OK) {
180 ast_log(LOG_ERROR, "Connection to Beanstalk tube %s @ %s:%d had failed", bs_tube, bs_host, bs_port);
181 ast_rwlock_unlock(&config_lock);
182 return 0;
183 }
184
185 ast_localtime(&cdr->start, &timeresult, NULL);
186 ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
187
188 if (cdr->answer.tv_sec) {
189 ast_localtime(&cdr->answer, &timeresult, NULL);
190 ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
191 }
192
193 ast_localtime(&cdr->end, &timeresult, NULL);
194 ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
195
196 ast_rwlock_unlock(&config_lock);
197
198 t_cdr_json = ast_json_pack("{s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s, s:i, s:i, s:s, s:s, s:s, s:s}",
199 "AccountCode", S_OR(cdr->accountcode, ""),
200 "Source", S_OR(cdr->src, ""),
201 "Destination", S_OR(cdr->dst, ""),
202 "DestinationContext", S_OR(cdr->dcontext, ""),
203 "CallerID", S_OR(cdr->clid, ""),
204 "Channel", S_OR(cdr->channel, ""),
205 "DestinationChannel", S_OR(cdr->dstchannel, ""),
206 "LastApplication", S_OR(cdr->lastapp, ""),
207 "LastData", S_OR(cdr->lastdata, ""),
208 "StartTime", S_OR(strStartTime, ""),
209 "AnswerTime", S_OR(strAnswerTime, ""),
210 "EndTime", S_OR(strEndTime, ""),
211 "Duration", cdr->duration,
212 "Billsec", cdr->billsec,
213 "Disposition", S_OR(ast_cdr_disp2str(cdr->disposition), ""),
214 "AMAFlags", S_OR(ast_channel_amaflags2string(cdr->amaflags), ""),
215 "UniqueID", S_OR(cdr->uniqueid, ""),
216 "UserField", S_OR(cdr->userfield, ""));
217
218 cdr_buffer = ast_json_dump_string(t_cdr_json);
219
220 ast_json_unref(t_cdr_json);
221
222 bs_id = bs_put(bs_socket, priority, BEANSTALK_JOB_DELAY, BEANSTALK_JOB_TTR, cdr_buffer, strlen(cdr_buffer));
223
224 if (bs_id > 0) {
225 ast_log(LOG_DEBUG, "Successfully created job %d with %s\n", bs_id, cdr_buffer);
226 } else {
227 ast_log(LOG_ERROR, "CDR job creation failed for %s\n", cdr_buffer);
228 }
229
230 bs_disconnect(bs_socket);
231 ast_json_free(cdr_buffer);
232 return 0;
233 }
234
235 static int unload_module(void) {
236 if (ast_cdr_unregister(name)) {
237 return -1;
238 }
239
240 ast_free(bs_host);
241 ast_free(bs_tube);
242
243 return 0;
244 }
245
246 static int load_module(void) {
247 if (ast_cdr_register(name, "Asterisk CDR Beanstalkd Backend", beanstalk_put)) {
248 return AST_MODULE_LOAD_DECLINE;
249 }
250
251 if (load_config(0)) {
252 ast_cdr_unregister(name);
253 return AST_MODULE_LOAD_DECLINE;
254 }
255
256 return AST_MODULE_LOAD_SUCCESS;
257 }
258
259 static int reload(void) {
260 return load_config(1);
261 }
262
263 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Beanstalkd CDR Backend",
264 .support_level = AST_MODULE_SUPPORT_EXTENDED,
265 .load = load_module,
266 .unload = unload_module,
267 .reload = reload,
268 .load_pri = AST_MODPRI_CDR_DRIVER,
269 .requires = "cdr",
270 );