[Asterisk-code-review] This is the actual commit... didn't add the new files in... (asterisk[master])

Nir Simionovich (GreenfieldTech - Israel) asteriskteam at digium.com
Mon Oct 16 10:30:45 CDT 2017


Nir Simionovich (GreenfieldTech - Israel) has uploaded this change for review. ( https://gerrit.asterisk.org/6816


Change subject: This is the actual commit... didn't add the new files in...
......................................................................

This is the actual commit... didn't add the new files in...

Change-Id: I6b7d2964ae5b39a35ec23da460a725e6c0d7c214
---
A cdr/cdr_beanstalkd.c
A configs/samples/cdr_beanstalkd.conf.sample
2 files changed, 404 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/16/6816/1

diff --git a/cdr/cdr_beanstalkd.c b/cdr/cdr_beanstalkd.c
new file mode 100644
index 0000000..e70b047
--- /dev/null
+++ b/cdr/cdr_beanstalkd.c
@@ -0,0 +1,383 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2004 - 2005
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Asterisk Beanstalkd CDR records.
+ *
+ * See also
+ * \arg \ref AstCDR
+ * \ingroup cdr_drivers
+ */
+
+/*! \li \ref cdr_beanstalkd.c uses the configuration file \ref cdr_beanstalkd.conf
+ * \addtogroup configuration_file Configuration Files
+ */
+
+/*!
+ * \page cdr_beanstalkd.conf cdr_beanstalkd.conf
+ * \verbinclude cdr_beanstalkd.conf.sample
+ */
+
+/*** MODULEINFO
+	<depend>beanstalk</depend>
+ ***/
+
+/*** DOCUMENTATION
+	<managerEvent language="en_US" name="Cdr">
+		<managerEventInstance class="EVENT_FLAG_CDR">
+			<synopsis>Raised when a CDR is generated.</synopsis>
+			<syntax>
+				<parameter name="AccountCode">
+					<para>The account code of the Party A channel.</para>
+				</parameter>
+				<parameter name="Source">
+					<para>The Caller ID number associated with the Party A in the CDR.</para>
+				</parameter>
+				<parameter name="Destination">
+					<para>The dialplan extension the Party A was executing.</para>
+				</parameter>
+				<parameter name="DestinationContext">
+					<para>The dialplan context the Party A was executing.</para>
+				</parameter>
+				<parameter name="CallerID">
+					<para>The Caller ID name associated with the Party A in the CDR.</para>
+				</parameter>
+				<parameter name="Channel">
+					<para>The channel name of the Party A.</para>
+				</parameter>
+				<parameter name="DestinationChannel">
+					<para>The channel name of the Party B.</para>
+				</parameter>
+				<parameter name="LastApplication">
+					<para>The last dialplan application the Party A executed.</para>
+				</parameter>
+				<parameter name="LastData">
+					<para>
+						The parameters passed to the last dialplan application the
+						Party A executed.
+					</para>
+				</parameter>
+				<parameter name="StartTime">
+					<para>The time the CDR was created.</para>
+				</parameter>
+				<parameter name="AnswerTime">
+					<para>
+						The earliest of either the time when Party A answered, or
+						the start time of this CDR.
+					</para>
+				</parameter>
+				<parameter name="EndTime">
+					<para>
+						The time when the CDR was finished. This occurs when the
+						Party A hangs up or when the bridge between Party A and
+						Party B is broken.
+					</para>
+				</parameter>
+				<parameter name="Duration">
+					<para>The time, in seconds, of <replaceable>EndTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
+				</parameter>
+				<parameter name="BillableSeconds">
+					<para>The time, in seconds, of <replaceable>AnswerTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
+				</parameter>
+				<parameter name="Disposition">
+					<para>The final known disposition of the CDR.</para>
+					<enumlist>
+						<enum name="NO ANSWER">
+							<para>The channel was not answered. This is the default disposition.</para>
+						</enum>
+						<enum name="FAILED">
+							<para>The channel attempted to dial but the call failed.</para>
+							<note>
+								<para>The congestion setting in <filename>cdr.conf</filename> can result
+								in the <literal>AST_CAUSE_CONGESTION</literal> hang up cause or the
+								<literal>CONGESTION</literal> dial status to map to this disposition.
+								</para>
+							</note>
+						</enum>
+						<enum name="BUSY">
+							<para>The channel attempted to dial but the remote party was busy.</para>
+						</enum>
+						<enum name="ANSWERED">
+							<para>The channel was answered. The hang up cause will no longer
+							impact the disposition of the CDR.</para>
+						</enum>
+						<enum name="CONGESTION">
+							<para>The channel attempted to dial but the remote party was congested.</para>
+						</enum>
+					</enumlist>
+				</parameter>
+				<parameter name="AMAFlags">
+					<para>A flag that informs a billing system how to treat the CDR.</para>
+					<enumlist>
+						<enum name="OMIT">
+							<para>This CDR should be ignored.</para>
+						</enum>
+						<enum name="BILLING">
+							<para>This CDR contains valid billing data.</para>
+						</enum>
+						<enum name="DOCUMENTATION">
+							<para>This CDR is for documentation purposes.</para>
+						</enum>
+					</enumlist>
+				</parameter>
+				<parameter name="UniqueID">
+					<para>A unique identifier for the Party A channel.</para>
+				</parameter>
+				<parameter name="UserField">
+					<para>
+						A user defined field set on the channels. If set on both the Party A
+						and Party B channel, the userfields of both are concatenated and
+						separated by a <literal>;</literal>.
+					</para>
+				</parameter>
+			</syntax>
+			<description>
+				<para>
+					The <replaceable>Cdr</replaceable> job is only raised when the
+					<filename>cdr_beanstalk</filename> backend is loaded and registered with
+					the CDR engine.
+				</para>
+				<note>
+					<para>
+						This job can contain additional fields depending on the configuration
+						provided by <filename>cdr_beanstalkd.conf</filename>.
+					</para>
+				</note>
+			</description>
+		</managerEventInstance>
+	</managerEvent>
+ ***/
+
+#include "beanstalk.h"
+
+#include "asterisk.h"
+
+#include <time.h>
+#include <stdio.h>
+
+#include "asterisk/channel.h"
+#include "asterisk/cdr.h"
+#include "asterisk/module.h"
+#include "asterisk/utils.h"
+#include "asterisk/manager.h"
+#include "asterisk/config.h"
+#include "asterisk/pbx.h"
+
+#define DATE_FORMAT    "%Y-%m-%d %T"
+#define CONF_FILE    "cdr_beanstalkd.conf"
+#define CUSTOM_FIELDS_BUF_SIZE 1024
+#define BEANSTALK_JOB_SIZE 4096
+#define BEANSTALK_JOB_PRIORITY 99
+#define BEANSTALK_JOB_TTR 60
+#define BEANSTALK_JOB_DELAY 0
+
+static const char name[] = "cdr_beanstalkd";
+
+static int enablecdr = 0;
+static char *bs_host = "127.0.0.1";
+static int bs_port = 13000;
+static char *bs_tube = "asterisk-cdr";
+static int priority = BEANSTALK_JOB_PRIORITY;
+
+static struct ast_str *customfields;
+AST_RWLOCK_DEFINE_STATIC(customfields_lock);
+
+static int beanstalk_put(struct ast_cdr *cdr);
+
+static int load_config(int reload) {
+    char *cat = NULL;
+    struct ast_config *cfg;
+    struct ast_variable *v;
+    struct ast_flags config_flags = {reload ? CONFIG_FLAG_FILEUNCHANGED : 0};
+    int newenablecdr = 0;
+
+    cfg = ast_config_load(CONF_FILE, config_flags);
+    if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
+        return 0;
+    }
+
+    if (cfg == CONFIG_STATUS_FILEINVALID) {
+        ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
+        return -1;
+    }
+
+    if (!cfg) {
+        /* Standard configuration */
+        ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
+        if (enablecdr) {
+            ast_cdr_backend_suspend(name);
+        }
+        enablecdr = 0;
+        return -1;
+    }
+
+    if (reload) {
+        ast_rwlock_wrlock(&customfields_lock);
+    }
+
+    if (reload && customfields) {
+        ast_free(customfields);
+        customfields = NULL;
+    }
+
+    while ((cat = ast_category_browse(cfg, cat))) {
+        if (!strcasecmp(cat, "general")) {
+            v = ast_variable_browse(cfg, cat);
+            while (v) {
+                if (!strcasecmp(v->name, "enabled"))
+                    newenablecdr = ast_true(v->value);
+
+                if (!newenablecdr) {
+                    enablecdr = newenablecdr;
+                    return 0;
+                }
+
+                if (!strcasecmp(v->name, "host"))
+                    bs_host = ast_strdup(v->value);
+                if (!strcasecmp(v->name, "port"))
+                    bs_port = atoi(v->value);
+                if (!strcasecmp(v->name, "tube"))
+                    bs_tube = ast_strdup(v->value);
+                if (!strcasecmp(v->name, "priority"))
+                    priority = atoi(v->value);
+
+                v = v->next;
+            }
+        }
+        ast_log(LOG_NOTICE, "Added beanstalkd server %s at port %d with tube %s", bs_host, bs_port, bs_tube);
+    }
+
+    if (reload) {
+        ast_rwlock_unlock(&customfields_lock);
+    }
+
+    ast_config_destroy(cfg);
+
+    if (!newenablecdr) {
+        ast_cdr_backend_suspend(name);
+    } else if (newenablecdr) {
+        ast_cdr_backend_unsuspend(name);
+    }
+    enablecdr = newenablecdr;
+
+    return 0;
+}
+
+static int beanstalk_put(struct ast_cdr *cdr) {
+    struct ast_tm timeresult;
+    char strStartTime[80] = "";
+    char strAnswerTime[80] = "";
+    char strEndTime[80] = "";
+    char buf[CUSTOM_FIELDS_BUF_SIZE];
+    char cdr_buffer[BEANSTALK_JOB_SIZE];
+
+    if (!enablecdr)
+        return 0;
+
+    int bs_id, bs_socket = bs_connect(bs_host, bs_port);
+
+    if (bs_use(bs_socket, bs_tube) != BS_STATUS_OK) {
+        ast_log(LOG_ERROR, "Connection to Beanstalk tube %s @ %s:%d had failed", bs_tube, bs_host, bs_port);
+        return 0;
+    }
+
+    ast_localtime(&cdr->start, &timeresult, NULL);
+    ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
+
+    if (cdr->answer.tv_sec) {
+        ast_localtime(&cdr->answer, &timeresult, NULL);
+        ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
+    }
+
+    ast_localtime(&cdr->end, &timeresult, NULL);
+    ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
+
+    buf[0] = '\0';
+    cdr_buffer[0] = '\0';
+
+    ast_rwlock_rdlock(&customfields_lock);
+    if (customfields && ast_str_strlen(customfields)) {
+        struct ast_channel *dummy = ast_dummy_channel_alloc();
+        if (!dummy) {
+            ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
+            return 0;
+        }
+        ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
+        pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
+        ast_channel_unref(dummy);
+    }
+    ast_rwlock_unlock(&customfields_lock);
+
+    snprintf(cdr_buffer, BEANSTALK_JOB_SIZE,
+            "{ \"AccountCode\": \"%s\", \"Source\": \"%s\", \"Destination\": \"%s\", \"DestinationContext\": \"%s\", "
+                    "\"CallerID\": \"%s\", \"Channel\": \"%s\", \"DestinationChannel\": \"%s\", \"LastApplication\": \"%s\", "
+                    "\"LastData\": \"%s\", \"StartTime\": \"%s\", \"AnswerTime\": \"%s\", \"EndTime\": \"%s\", "
+                    "\"Duration\": \"%ld\", \"BillableSeconds\": \"%ld\", \"Disposition\": \"%s\", \"AMAFlags\": \"%s\", "
+                    "\"UniqueID\": \"%s\", \"UserField\": \"%s\", \"CustomFields\": \"%s\" }",
+            cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
+            cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
+            cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
+            ast_channel_amaflags2string(cdr->amaflags), cdr->uniqueid, cdr->userfield, buf
+    );
+
+    bs_id = bs_put(bs_socket, priority, BEANSTALK_JOB_DELAY, BEANSTALK_JOB_TTR, cdr_buffer, strlen(cdr_buffer));
+
+    if (bs_id > 0)
+        ast_log(LOG_NOTICE, "Successfully created job %d with %s\n", bs_id, cdr_buffer);
+    else
+        ast_log(LOG_ERROR, "CDR job creation failed for %s\n", cdr_buffer);
+
+    bs_disconnect(bs_socket);
+    return 0;
+}
+
+static int unload_module(void) {
+    if (ast_cdr_unregister(name)) {
+        return -1;
+    }
+
+    if (customfields)
+        ast_free(customfields);
+
+    return 0;
+}
+
+static int load_module(void) {
+    if (ast_cdr_register(name, "Asterisk CDR Beanstalkd Backend", beanstalk_put)) {
+        return AST_MODULE_LOAD_DECLINE;
+    }
+
+    if (load_config(0)) {
+        ast_cdr_unregister(name);
+        return AST_MODULE_LOAD_DECLINE;
+    }
+
+    return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int reload(void) {
+    return load_config(1);
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Beanstalkd CDR Backend",
+.support_level = AST_MODULE_SUPPORT_CORE,
+.load = load_module,
+.unload = unload_module,
+.reload = reload,
+.load_pri = AST_MODPRI_CDR_DRIVER,
+);
+
diff --git a/configs/samples/cdr_beanstalkd.conf.sample b/configs/samples/cdr_beanstalkd.conf.sample
new file mode 100644
index 0000000..368ac6c
--- /dev/null
+++ b/configs/samples/cdr_beanstalkd.conf.sample
@@ -0,0 +1,21 @@
+;
+; Asterisk Call Management CDR via Beanstalkd job queue
+;
+; Beanstalkd is a simple job queue server, that is highly versatile and simple to use.
+; Beanstalkd includes the capability of using multiple queues at the same time, with priorities.
+
+[general]
+enabled = no
+
+host = 127.0.0.1    ; Specify the remote IP address of the Beanstalkd server
+port = 11300        ; Specify the remote PORT of the the Beanstalkd server
+tube = asterisk-cdr ; Specify the default CDR job queue to use
+priority = 99       ; Specify the default job priority for the queue. This parameter is useful when building
+                    ; platform with multiple Asterisk servers, that are used for different functions. For example,
+                    ; none billable CDR records can be inserted with a priority of 99, while billable ones be
+                    ; inserted with a priority of 1
+
+
+
+
+

-- 
To view, visit https://gerrit.asterisk.org/6816
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I6b7d2964ae5b39a35ec23da460a725e6c0d7c214
Gerrit-Change-Number: 6816
Gerrit-PatchSet: 1
Gerrit-Owner: Nir Simionovich (GreenfieldTech - Israel) <nirs at greenfieldtech.net>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20171016/0afabdad/attachment-0001.html>


More information about the asterisk-code-review mailing list