Asterisk - The Open Source Telephony Project GIT-master-a358458
cdr_beanstalkd.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2017
5 *
6 * Nir Simionovich <nirs@greenfieldtech.net>
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, avaialble 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
70static const char name[] = "cdr_beanstalkd";
71
72static int enablecdr = 0;
73static char *bs_host;
74static int bs_port;
75static char *bs_tube;
76static int priority;
77
79
80static int beanstalk_put(struct ast_cdr *cdr);
81
82static 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) {
104 }
105 enablecdr = 0;
106 return -1;
107 }
108
109 if (reload) {
113 }
114
115 /* Bootstrap the default configuration */
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")) {
131 } else if (!strcasecmp(v->name, "port")) {
132 bs_port = atoi(v->value);
133 } else if (!strcasecmp(v->name, "tube")) {
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) {
147 }
148
150
151 if (!newenablecdr) {
153 } else if (newenablecdr) {
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
162static 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
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);
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
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
235static int unload_module(void) {
237 return -1;
238 }
239
242
243 return 0;
244}
245
246static int load_module(void) {
247 if (ast_cdr_register(name, "Asterisk CDR Beanstalkd Backend", beanstalk_put)) {
249 }
250
251 if (load_config(0)) {
254 }
255
257}
258
259static int reload(void) {
260 return load_config(1);
261}
262
263AST_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",
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_log
Definition: astobj2.c:42
Call Detail Record API.
int ast_cdr_backend_unsuspend(const char *name)
Unsuspend a CDR backend.
Definition: cdr.c:2946
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:3050
const char * ast_cdr_disp2str(int disposition)
Disposition to a string.
Definition: cdr.c:3492
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:3005
int ast_cdr_backend_suspend(const char *name)
Suspend a CDR backend temporarily.
Definition: cdr.c:2928
static char * bs_tube
static const char name[]
static int bs_port
static int beanstalk_put(struct ast_cdr *cdr)
#define CONF_FILE
#define DEFAULT_BEANSTALK_HOST
static int enablecdr
static ast_rwlock_t config_lock
#define BEANSTALK_JOB_DELAY
#define DEFAULT_BEANSTALK_TUBE
static int load_module(void)
static int priority
static int unload_module(void)
static int load_config(int reload)
static int reload(void)
static char * bs_host
#define BEANSTALK_JOB_PRIORITY
#define DATE_FORMAT
#define DEFAULT_BEANSTALK_PORT
#define BEANSTALK_JOB_TTR
General Asterisk PBX channel definitions.
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
Definition: channel.c:4373
Configuration File Parser.
#define ast_config_load(filename, flags)
Load a config file.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3326
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
@ CONFIG_FLAG_FILEUNCHANGED
#define LOG_DEBUG
#define LOG_ERROR
#define LOG_NOTICE
#define LOG_WARNING
Asterisk JSON abstraction layer.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
void ast_json_free(void *p)
Asterisk's custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:52
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
Definition: json.h:810
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
#define ast_rwlock_wrlock(a)
Definition: lock.h:236
#define ast_rwlock_rdlock(a)
Definition: lock.h:235
#define AST_RWLOCK_DEFINE_STATIC(rwlock)
Definition: lock.h:543
#define ast_rwlock_unlock(a)
Definition: lock.h:234
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODPRI_CDR_DRIVER
Definition: module.h:331
@ AST_MODULE_SUPPORT_EXTENDED
Definition: module.h:122
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
Core PBX routines and definitions.
#define NULL
Definition: resample.c:96
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
Responsible for call detail data.
Definition: cdr.h:279
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:291
long int disposition
Definition: cdr.h:307
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:295
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:321
long int billsec
Definition: cdr.h:305
struct timeval answer
Definition: cdr.h:299
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:289
long int duration
Definition: cdr.h:303
long int amaflags
Definition: cdr.h:309
char src[AST_MAX_EXTENSION]
Definition: cdr.h:283
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:285
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:281
char uniqueid[AST_MAX_UNIQUEID]
Definition: cdr.h:317
struct timeval start
Definition: cdr.h:297
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:311
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:293
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:287
struct timeval end
Definition: cdr.h:301
Structure used to handle boolean flags.
Definition: utils.h:199
Abstract JSON element (object, array, string, int, ...).
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
Time-related functions and macros.
Utility functions.