Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
Macros | Functions | Variables
cel_beanstalkd.c File Reference

Asterisk Channel Event Beanstalkd backend. More...

#include "asterisk.h"
#include "asterisk/channel.h"
#include "asterisk/cel.h"
#include "asterisk/module.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/manager.h"
#include "asterisk/config.h"
#include "asterisk/json.h"
#include "beanstalk.h"
Include dependency graph for cel_beanstalkd.c:

Go to the source code of this file.

Macros

#define BEANSTALK_JOB_DELAY   0
 
#define BEANSTALK_JOB_PRIORITY   99
 
#define BEANSTALK_JOB_SIZE   4096
 
#define BEANSTALK_JOB_TTR   60
 
#define CEL_BACKEND_NAME   "Beanstalk Event Logging"
 
#define CEL_BEANSTALK_ENABLED_DEFAULT   0
 Beanstalk CEL is off by default.
 
#define CEL_SHOW_USERDEF_DEFAULT   0
 show_user_def is off by default
 
#define DEFAULT_BEANSTALK_HOST   "127.0.0.1"
 
#define DEFAULT_BEANSTALK_PORT   11300
 
#define DEFAULT_BEANSTALK_TUBE   "asterisk-cel"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static void cel_bs_put (struct ast_event *event)
 
static int load_config (int reload)
 
static int load_module (void)
 
static int reload (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Beanstalkd CEL Backend" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, .requires = "cel", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static char * bs_host
 
static int bs_port
 
static char * bs_tube
 
static const char CONF_FILE [] = "cel_beanstalkd.conf"
 
static ast_rwlock_t config_lock = AST_RWLOCK_INIT_VALUE
 
static const char DATE_FORMAT [] = "%Y-%m-%d %T"
 
static int enablecel
 
static int priority
 

Detailed Description

Asterisk Channel Event Beanstalkd backend.

This module requires the beanstalk-client library, avaialble from https://github.com/deepfryed/beanstalk-client

Definition in file cel_beanstalkd.c.

Macro Definition Documentation

◆ BEANSTALK_JOB_DELAY

#define BEANSTALK_JOB_DELAY   0

Definition at line 74 of file cel_beanstalkd.c.

◆ BEANSTALK_JOB_PRIORITY

#define BEANSTALK_JOB_PRIORITY   99

Definition at line 72 of file cel_beanstalkd.c.

◆ BEANSTALK_JOB_SIZE

#define BEANSTALK_JOB_SIZE   4096

Definition at line 71 of file cel_beanstalkd.c.

◆ BEANSTALK_JOB_TTR

#define BEANSTALK_JOB_TTR   60

Definition at line 73 of file cel_beanstalkd.c.

◆ CEL_BACKEND_NAME

#define CEL_BACKEND_NAME   "Beanstalk Event Logging"

Definition at line 69 of file cel_beanstalkd.c.

◆ CEL_BEANSTALK_ENABLED_DEFAULT

#define CEL_BEANSTALK_ENABLED_DEFAULT   0

Beanstalk CEL is off by default.

Definition at line 62 of file cel_beanstalkd.c.

◆ CEL_SHOW_USERDEF_DEFAULT

#define CEL_SHOW_USERDEF_DEFAULT   0

show_user_def is off by default

Definition at line 67 of file cel_beanstalkd.c.

◆ DEFAULT_BEANSTALK_HOST

#define DEFAULT_BEANSTALK_HOST   "127.0.0.1"

Definition at line 75 of file cel_beanstalkd.c.

◆ DEFAULT_BEANSTALK_PORT

#define DEFAULT_BEANSTALK_PORT   11300

Definition at line 76 of file cel_beanstalkd.c.

◆ DEFAULT_BEANSTALK_TUBE

#define DEFAULT_BEANSTALK_TUBE   "asterisk-cel"

Definition at line 77 of file cel_beanstalkd.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 273 of file cel_beanstalkd.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 273 of file cel_beanstalkd.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 273 of file cel_beanstalkd.c.

◆ cel_bs_put()

static void cel_bs_put ( struct ast_event event)
static

Definition at line 86 of file cel_beanstalkd.c.

87{
88 struct ast_tm timeresult;
89 char start_time[80];
90 char *cel_buffer;
91 int bs_id;
92 int bs_socket;
93 struct ast_json *t_cel_json;
94
95 struct ast_cel_event_record record = {
97 };
98
99 if (!enablecel) {
100 return;
101 }
102
103 if (ast_cel_fill_record(event, &record)) {
104 return;
105 }
106
108 bs_socket = bs_connect(bs_host, bs_port);
109
110 if (bs_use(bs_socket, bs_tube) != BS_STATUS_OK) {
111 ast_log(LOG_ERROR, "Connection to Beanstalk tube %s @ %s:%d had failed", bs_tube, bs_host, bs_port);
113 return;
114 }
115
116 ast_localtime(&record.event_time, &timeresult, NULL);
117 ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
118
120
121 t_cel_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:s, s:s, s:s, s:s, s:s, s:s, s:s, s:s}",
122 "EventName", S_OR(record.event_name, ""),
123 "AccountCode", S_OR(record.account_code, ""),
124 "CallerIDnum", S_OR(record.caller_id_num, ""),
125 "CallerIDname", S_OR(record.caller_id_name, ""),
126 "CallerIDani", S_OR(record.caller_id_ani, ""),
127 "CallerIDrdnis", S_OR(record.caller_id_rdnis, ""),
128 "CallerIDdnid", S_OR(record.caller_id_dnid, ""),
129 "Exten", S_OR(record.extension, ""),
130 "Context", S_OR(record.context, ""),
131 "Channel", S_OR(record.channel_name, ""),
132 "Application", S_OR(record.application_name, ""),
133 "AppData", S_OR(record.application_data, ""),
134 "EventTime", S_OR(start_time, ""),
135 "AMAFlags", S_OR(ast_channel_amaflags2string(record.amaflag), ""),
136 "UniqueID", S_OR(record.unique_id, ""),
137 "LinkedID", S_OR(record.linked_id, ""),
138 "Userfield", S_OR(record.user_field, ""),
139 "Peer", S_OR(record.peer_account, ""),
140 "PeerAccount", S_OR(record.peer_account, ""),
141 "Extra", S_OR(record.extra, "")
142
143 );
144
145 cel_buffer = ast_json_dump_string(t_cel_json);
146
147 ast_json_unref(t_cel_json);
148
149 bs_id = bs_put(bs_socket, priority, BEANSTALK_JOB_DELAY, BEANSTALK_JOB_TTR, cel_buffer, strlen(cel_buffer));
150
151 if (bs_id > 0) {
152 ast_log(LOG_DEBUG, "Successfully created job %d with %s\n", bs_id, cel_buffer);
153 } else {
154 ast_log(LOG_ERROR, "CDR job creation failed for %s\n", cel_buffer);
155 }
156
157 bs_disconnect(bs_socket);
158 ast_json_free(cel_buffer);
159}
#define ast_log
Definition astobj2.c:42
int ast_cel_fill_record(const struct ast_event *event, struct ast_cel_event_record *r)
Fill in an ast_cel_event_record from a CEL event.
Definition cel.c:843
#define AST_CEL_EVENT_RECORD_VERSION
struct ABI version
Definition cel.h:149
static char * bs_tube
static int bs_port
static ast_rwlock_t config_lock
#define BEANSTALK_JOB_DELAY
static const char DATE_FORMAT[]
static int enablecel
static int priority
static char * bs_host
#define BEANSTALK_JOB_TTR
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
Definition channel.c:4367
#define LOG_DEBUG
#define LOG_ERROR
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_rdlock(a)
Definition lock.h:242
#define ast_rwlock_unlock(a)
Definition lock.h:241
#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
Helper struct for getting the fields out of a CEL event.
Definition cel.h:144
const char * caller_id_dnid
Definition cel.h:163
const char * application_data
Definition cel.h:168
const char * account_code
Definition cel.h:169
const char * caller_id_rdnis
Definition cel.h:162
const char * extra
Definition cel.h:177
const char * extension
Definition cel.h:164
const char * caller_id_num
Definition cel.h:160
const char * channel_name
Definition cel.h:166
const char * linked_id
Definition cel.h:172
const char * peer_account
Definition cel.h:170
const char * unique_id
Definition cel.h:171
const char * context
Definition cel.h:165
const char * application_name
Definition cel.h:167
struct timeval event_time
Definition cel.h:156
uint32_t version
struct ABI version
Definition cel.h:154
const char * user_field
Definition cel.h:175
const char * caller_id_ani
Definition cel.h:161
const char * caller_id_name
Definition cel.h:159
const char * event_name
Definition cel.h:157
Abstract JSON element (object, array, string, int, ...).

References ast_cel_event_record::account_code, ast_cel_event_record::amaflag, ast_cel_event_record::application_data, ast_cel_event_record::application_name, AST_CEL_EVENT_RECORD_VERSION, ast_cel_fill_record(), ast_channel_amaflags2string(), ast_json_dump_string, ast_json_free(), ast_json_pack(), ast_json_unref(), ast_localtime(), ast_log, ast_rwlock_rdlock, ast_rwlock_unlock, ast_strftime(), BEANSTALK_JOB_DELAY, BEANSTALK_JOB_TTR, bs_host, bs_port, bs_tube, ast_cel_event_record::caller_id_ani, ast_cel_event_record::caller_id_dnid, ast_cel_event_record::caller_id_name, ast_cel_event_record::caller_id_num, ast_cel_event_record::caller_id_rdnis, ast_cel_event_record::channel_name, config_lock, ast_cel_event_record::context, DATE_FORMAT, enablecel, ast_cel_event_record::event_name, ast_cel_event_record::event_time, ast_cel_event_record::extension, ast_cel_event_record::extra, ast_cel_event_record::linked_id, LOG_DEBUG, LOG_ERROR, NULL, ast_cel_event_record::peer_account, priority, S_OR, ast_cel_event_record::unique_id, ast_cel_event_record::user_field, and ast_cel_event_record::version.

Referenced by load_config().

◆ load_config()

static int load_config ( int  reload)
static

Definition at line 161 of file cel_beanstalkd.c.

162{
163 const char *cat = NULL;
164 struct ast_config *cfg;
165 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
166 struct ast_variable *v;
167 int newenablecel = CEL_BEANSTALK_ENABLED_DEFAULT;
168
169 cfg = ast_config_load(CONF_FILE, config_flags);
170 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
171 return 0;
172 }
173
174 if (cfg == CONFIG_STATUS_FILEINVALID) {
175 ast_log(LOG_WARNING, "Configuration file '%s' is invalid. CEL Beanstalkd Module not activated.\n",
176 CONF_FILE);
177 return -1;
178 } else if (!cfg) {
179 ast_log(LOG_WARNING, "Failed to load configuration file. CEL Beanstalkd Module not activated.\n");
180 if (enablecel) {
182 }
183 enablecel = 0;
184 return -1;
185 }
186
187 if (reload) {
191 }
192
193 /* Bootstrap the default configuration */
198
199 while ((cat = ast_category_browse(cfg, cat))) {
200
201 if (strcasecmp(cat, "general")) {
202 continue;
203 }
204
205 for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
206 if (!strcasecmp(v->name, "enabled")) {
207 newenablecel = ast_true(v->value) ? 1 : 0;
208 } else if (!strcasecmp(v->name, "host")) {
211 } else if (!strcasecmp(v->name, "port")) {
212 bs_port = atoi(v->value);
213 } else if (!strcasecmp(v->name, "tube")) {
216 } else if (!strcasecmp(v->name, "priority")) {
217 priority = atoi(v->value);
218 } else {
219 ast_log(LOG_NOTICE, "Unknown option '%s' specified "
220 "for CEL beanstalk backend.\n", v->name);
221 }
222 }
223 }
224
225 if (reload) {
227 }
228
230
231 if (enablecel && !newenablecel) {
233 } else if (!enablecel && newenablecel) {
235 ast_log(LOG_ERROR, "Unable to register Beanstalkd CEL handling\n");
236 }
237 }
238
239 enablecel = newenablecel;
240
241 return 0;
242}
#define ast_free(a)
Definition astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition astmm.h:241
int ast_cel_backend_unregister(const char *name)
Unregister a CEL backend.
Definition cel.c:1797
int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
Register a CEL backend.
Definition cel.c:1809
static void cel_bs_put(struct ast_event *event)
#define DEFAULT_BEANSTALK_HOST
#define CEL_BEANSTALK_ENABLED_DEFAULT
Beanstalk CEL is off by default.
#define DEFAULT_BEANSTALK_TUBE
#define CEL_BACKEND_NAME
static int reload(void)
#define BEANSTALK_JOB_PRIORITY
#define DEFAULT_BEANSTALK_PORT
static const char CONF_FILE[]
#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:3324
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition extconf.c:1287
@ CONFIG_FLAG_FILEUNCHANGED
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition extconf.c:1213
#define LOG_NOTICE
#define LOG_WARNING
#define ast_rwlock_wrlock(a)
Definition lock.h:243
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:2235
Structure used to handle boolean flags.
Definition utils.h:220
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next

References ast_category_browse(), ast_cel_backend_register(), ast_cel_backend_unregister(), ast_config_destroy(), ast_config_load, ast_free, ast_log, ast_rwlock_unlock, ast_rwlock_wrlock, ast_strdup, ast_true(), ast_variable_browse(), BEANSTALK_JOB_PRIORITY, bs_host, bs_port, bs_tube, CEL_BACKEND_NAME, CEL_BEANSTALK_ENABLED_DEFAULT, cel_bs_put(), CONF_FILE, CONFIG_FLAG_FILEUNCHANGED, config_lock, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, DEFAULT_BEANSTALK_HOST, DEFAULT_BEANSTALK_PORT, DEFAULT_BEANSTALK_TUBE, enablecel, LOG_ERROR, LOG_NOTICE, LOG_WARNING, ast_variable::name, ast_variable::next, NULL, priority, reload(), and ast_variable::value.

◆ load_module()

static int load_module ( void  )
static

Definition at line 252 of file cel_beanstalkd.c.

253{
254 if (load_config(0)) {
256 }
257
259}
static int load_config(void)
@ 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

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and load_config().

◆ reload()

static int reload ( void  )
static

Definition at line 261 of file cel_beanstalkd.c.

262{
263 return load_config(1);
264}

References load_config().

Referenced by load_config().

◆ unload_module()

static int unload_module ( void  )
static

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Beanstalkd CEL Backend" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, .requires = "cel", }
static

Definition at line 273 of file cel_beanstalkd.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 273 of file cel_beanstalkd.c.

◆ bs_host

char* bs_host
static

Definition at line 79 of file cel_beanstalkd.c.

Referenced by cel_bs_put(), load_config(), and unload_module().

◆ bs_port

int bs_port
static

Definition at line 80 of file cel_beanstalkd.c.

Referenced by cel_bs_put(), and load_config().

◆ bs_tube

char* bs_tube
static

Definition at line 81 of file cel_beanstalkd.c.

Referenced by cel_bs_put(), load_config(), and unload_module().

◆ CONF_FILE

const char CONF_FILE[] = "cel_beanstalkd.conf"
static

Definition at line 59 of file cel_beanstalkd.c.

Referenced by load_config().

◆ config_lock

ast_rwlock_t config_lock = AST_RWLOCK_INIT_VALUE
static

Definition at line 84 of file cel_beanstalkd.c.

Referenced by cel_bs_put(), and load_config().

◆ DATE_FORMAT

const char DATE_FORMAT[] = "%Y-%m-%d %T"
static

Definition at line 57 of file cel_beanstalkd.c.

Referenced by cel_bs_put().

◆ enablecel

int enablecel
static

Definition at line 64 of file cel_beanstalkd.c.

Referenced by cel_bs_put(), and load_config().

◆ priority

int priority
static

Definition at line 82 of file cel_beanstalkd.c.

Referenced by cel_bs_put(), and load_config().