Asterisk - The Open Source Telephony Project  18.5.0
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 <[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, 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 
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 
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) {
104  }
105  enablecdr = 0;
106  return -1;
107  }
108 
109  if (reload) {
111  ast_free(bs_host);
112  ast_free(bs_tube);
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")) {
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) {
147  }
148 
149  ast_config_destroy(cfg);
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 
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 
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 
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)) {
249  }
250 
251  if (load_config(0)) {
254  }
255 
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 );
struct ast_variable * next
#define ast_rwlock_rdlock(a)
Definition: lock.h:233
int ast_cdr_backend_suspend(const char *name)
Suspend a CDR backend temporarily.
Definition: cdr.c:2866
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:308
Asterisk main include file. File version handling, generic pbx functions.
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:2988
#define AST_RWLOCK_DEFINE_STATIC(rwlock)
Definition: lock.h:541
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:288
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1216
Time-related functions and macros.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
long int billsec
Definition: cdr.h:302
#define LOG_WARNING
Definition: logger.h:274
int ast_cdr_backend_unsuspend(const char *name)
Unsuspend a CDR backend.
Definition: cdr.c:2884
#define CONFIG_STATUS_FILEINVALID
void ast_json_free(void *p)
Asterisk&#39;s custom JSON allocator. Exposed for use by unit tests.
Definition: json.c:52
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:284
#define DEFAULT_BEANSTALK_HOST
static int unload_module(void)
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
Structure for variables, used for configurations and for channel variables.
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
Definition: json.h:763
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
Definition: channel.c:4418
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3328
#define NULL
Definition: resample.c:96
#define CONF_FILE
#define LOG_DEBUG
Definition: logger.h:241
#define ast_rwlock_unlock(a)
Definition: lock.h:232
static int priority
Utility functions.
#define DATE_FORMAT
Call Detail Record API.
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:292
Configuration File Parser.
long int amaflags
Definition: cdr.h:306
#define ast_log
Definition: astobj2.c:42
#define ast_config_load(filename, flags)
Load a config file.
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:2943
General Asterisk PBX channel definitions.
Asterisk JSON abstraction layer.
static int beanstalk_put(struct ast_cdr *cdr)
static char * bs_host
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
char uniqueid[AST_MAX_UNIQUEID]
Definition: cdr.h:314
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:282
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:286
Core PBX routines and definitions.
#define BEANSTALK_JOB_TTR
#define CONFIG_STATUS_FILEUNCHANGED
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
struct timeval answer
Definition: cdr.h:296
Responsible for call detail data.
Definition: cdr.h:276
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:290
const char * ast_cdr_disp2str(int disposition)
Disposition to a string.
Definition: cdr.c:3430
#define LOG_ERROR
Definition: logger.h:285
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: main/utils.c:1951
#define BEANSTALK_JOB_PRIORITY
static ast_rwlock_t config_lock
static int bs_port
#define DEFAULT_BEANSTALK_PORT
#define LOG_NOTICE
Definition: logger.h:263
struct timeval start
Definition: cdr.h:294
#define ast_free(a)
Definition: astmm.h:182
long int duration
Definition: cdr.h:300
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
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
Structure used to handle boolean flags.
Definition: utils.h:199
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
char src[AST_MAX_EXTENSION]
Definition: cdr.h:280
#define ast_rwlock_wrlock(a)
Definition: lock.h:234
static char * bs_tube
struct timeval end
Definition: cdr.h:298
#define BEANSTALK_JOB_DELAY
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
#define DEFAULT_BEANSTALK_TUBE
static int load_config(int reload)
Abstract JSON element (object, array, string, int, ...).
static const char name[]
long int disposition
Definition: cdr.h:304
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:278
static int load_module(void)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
static int enablecdr
static int reload(void)
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:318