Wed Jan 8 2020 09:49:59

Asterisk developer's documentation


cdr_manager.c File Reference

Asterisk Call Manager CDR records. More...

#include "asterisk.h"
#include <time.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"

Go to the source code of this file.

Macros

#define CONF_FILE   "cdr_manager.conf"
 
#define CUSTOM_FIELDS_BUF_SIZE   1024
 
#define DATE_FORMAT   "%Y-%m-%d %T"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int load_config (int reload)
 
static int load_module (void)
 
static int manager_log (struct ast_cdr *cdr)
 
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 = "Asterisk Manager Interface CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_strcustomfields
 
static ast_rwlock_t customfields_lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, 1 }
 
static int enablecdr = 0
 
static const char name [] = "cdr_manager"
 

Detailed Description

Asterisk Call Manager CDR records.

See also

Definition in file cdr_manager.c.

Macro Definition Documentation

#define CONF_FILE   "cdr_manager.conf"

Definition at line 47 of file cdr_manager.c.

Referenced by load_config().

#define CUSTOM_FIELDS_BUF_SIZE   1024

Definition at line 48 of file cdr_manager.c.

Referenced by load_config(), and manager_log().

#define DATE_FORMAT   "%Y-%m-%d %T"

Definition at line 46 of file cdr_manager.c.

Referenced by manager_log().

Function Documentation

static void __reg_module ( void  )
static

Definition at line 230 of file cdr_manager.c.

static void __unreg_module ( void  )
static

Definition at line 230 of file cdr_manager.c.

static int load_config ( int  reload)
static

Definition at line 59 of file cdr_manager.c.

References ast_category_browse(), ast_cdr_register(), ast_cdr_unregister(), ast_config_destroy(), ast_config_load, ast_free, ast_log(), ast_rwlock_unlock, ast_rwlock_wrlock, ast_str_append(), ast_str_create(), ast_str_size(), ast_str_strlen(), ast_strlen_zero(), ast_true(), ast_variable_browse(), CONF_FILE, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, CUSTOM_FIELDS_BUF_SIZE, customfields_lock, LOG_ERROR, LOG_NOTICE, LOG_WARNING, manager_log(), ast_variable::name, ast_variable::next, and ast_variable::value.

Referenced by load_module(), and reload().

60 {
61  char *cat = NULL;
62  struct ast_config *cfg;
63  struct ast_variable *v;
64  struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
65  int newenablecdr = 0;
66 
67  cfg = ast_config_load(CONF_FILE, config_flags);
68  if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
69  return 0;
70  }
71 
72  if (cfg == CONFIG_STATUS_FILEINVALID) {
73  ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
74  return -1;
75  }
76 
77  if (!cfg) {
78  /* Standard configuration */
79  ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
80  if (enablecdr)
82  enablecdr = 0;
83  return -1;
84  }
85 
86  if (reload) {
88  }
89 
90  if (reload && customfields) {
92  customfields = NULL;
93  }
94 
95  while ( (cat = ast_category_browse(cfg, cat)) ) {
96  if (!strcasecmp(cat, "general")) {
97  v = ast_variable_browse(cfg, cat);
98  while (v) {
99  if (!strcasecmp(v->name, "enabled"))
100  newenablecdr = ast_true(v->value);
101 
102  v = v->next;
103  }
104  } else if (!strcasecmp(cat, "mappings")) {
106  v = ast_variable_browse(cfg, cat);
107  while (v) {
108  if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
109  if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
110  ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
111  ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
112  } else {
113  ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
114  break;
115  }
116 
117  }
118  v = v->next;
119  }
120  }
121  }
122 
123  if (reload) {
125  }
126 
127  ast_config_destroy(cfg);
128 
129  if (enablecdr && !newenablecdr)
131  else if (!enablecdr && newenablecdr)
132  ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log);
133  enablecdr = newenablecdr;
134 
135  return 0;
136 }
static int enablecdr
Definition: cdr_manager.c:52
static struct ast_str * customfields
Definition: cdr_manager.c:54
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:482
#define LOG_WARNING
Definition: logger.h:144
static int reload(void)
Definition: cdr_manager.c:220
#define CUSTOM_FIELDS_BUF_SIZE
Definition: cdr_manager.c:48
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Definition: config.c:597
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:900
struct ast_str * ast_str_create(size_t init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:420
#define ast_rwlock_unlock(a)
Definition: lock.h:200
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
const char * value
Definition: config.h:79
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:130
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
char * ast_category_browse(struct ast_config *config, const char *prev)
Goes through categories.
Definition: config.c:810
const char * name
Definition: config.h:77
#define CONF_FILE
Definition: cdr_manager.c:47
#define LOG_ERROR
Definition: logger.h:155
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is &quot;true&quot;. This function checks to see whether a string passed to it is an indication of an &quot;true&quot; value. It checks to see if the string is &quot;yes&quot;, &quot;true&quot;, &quot;y&quot;, &quot;t&quot;, &quot;on&quot; or &quot;1&quot;.
Definition: utils.c:1533
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static int manager_log(struct ast_cdr *cdr)
Definition: cdr_manager.c:138
#define LOG_NOTICE
Definition: logger.h:133
static const char name[]
#define ast_free(a)
Definition: astmm.h:97
Structure used to handle boolean flags.
Definition: utils.h:200
#define ast_rwlock_wrlock(a)
Definition: lock.h:202
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:471
struct ast_variable * next
Definition: config.h:82
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
static ast_rwlock_t customfields_lock
Definition: cdr_manager.c:55
void ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:165
#define CONFIG_STATUS_FILEUNCHANGED
Definition: config.h:51
static int load_module ( void  )
static

Definition at line 211 of file cdr_manager.c.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and load_config().

212 {
213  if (load_config(0)) {
215  }
216 
218 }
static int load_config(int reload)
Definition: cdr_manager.c:59
static int manager_log ( struct ast_cdr cdr)
static

Definition at line 138 of file cdr_manager.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_cdr_disp2str(), ast_cdr_dup(), ast_cdr_flags2str(), ast_channel_unref, ast_dummy_channel_alloc(), ast_localtime(), ast_log(), ast_rwlock_rdlock, ast_rwlock_unlock, ast_str_buffer(), ast_str_strlen(), ast_strftime(), ast_cdr::billsec, ast_channel::cdr, ast_cdr::channel, ast_cdr::clid, CUSTOM_FIELDS_BUF_SIZE, customfields_lock, DATE_FORMAT, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, dummy(), ast_cdr::duration, ast_cdr::end, EVENT_FLAG_CDR, ast_cdr::lastapp, ast_cdr::lastdata, LOG_ERROR, manager_event, pbx_substitute_variables_helper(), ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, and ast_cdr::userfield.

Referenced by load_config().

139 {
140  struct ast_tm timeresult;
141  char strStartTime[80] = "";
142  char strAnswerTime[80] = "";
143  char strEndTime[80] = "";
144  char buf[CUSTOM_FIELDS_BUF_SIZE];
145 
146  if (!enablecdr)
147  return 0;
148 
149  ast_localtime(&cdr->start, &timeresult, NULL);
150  ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
151 
152  if (cdr->answer.tv_sec) {
153  ast_localtime(&cdr->answer, &timeresult, NULL);
154  ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
155  }
156 
157  ast_localtime(&cdr->end, &timeresult, NULL);
158  ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
159 
160  buf[0] = '\0';
164  if (!dummy) {
165  ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
166  return 0;
167  }
168  dummy->cdr = ast_cdr_dup(cdr);
169  pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
170  ast_channel_unref(dummy);
171  }
173 
175  "AccountCode: %s\r\n"
176  "Source: %s\r\n"
177  "Destination: %s\r\n"
178  "DestinationContext: %s\r\n"
179  "CallerID: %s\r\n"
180  "Channel: %s\r\n"
181  "DestinationChannel: %s\r\n"
182  "LastApplication: %s\r\n"
183  "LastData: %s\r\n"
184  "StartTime: %s\r\n"
185  "AnswerTime: %s\r\n"
186  "EndTime: %s\r\n"
187  "Duration: %ld\r\n"
188  "BillableSeconds: %ld\r\n"
189  "Disposition: %s\r\n"
190  "AMAFlags: %s\r\n"
191  "UniqueID: %s\r\n"
192  "UserField: %s\r\n"
193  "%s",
194  cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
195  cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
196  cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
197  ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
198 
199  return 0;
200 }
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
Definition: pbx.c:4676
#define ast_rwlock_rdlock(a)
Definition: lock.h:201
#define EVENT_FLAG_CDR
Definition: manager.h:81
Main Channel structure associated with a channel.
Definition: channel.h:742
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:114
static int enablecdr
Definition: cdr_manager.c:52
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:94
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2502
static struct ast_str * customfields
Definition: cdr_manager.c:54
long int billsec
Definition: cdr.h:108
#define CUSTOM_FIELDS_BUF_SIZE
Definition: cdr_manager.c:48
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
static void dummy(char *unused,...)
Definition: chan_unistim.c:188
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:90
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:1570
struct ast_cdr * ast_cdr_dup(struct ast_cdr *cdr)
Duplicate a record.
Definition: cdr.c:213
char uniqueid[150]
Definition: cdr.h:121
struct ast_cdr * cdr
Definition: channel.h:766
char * ast_cdr_flags2str(int flags)
Definition: cdr.c:977
#define ast_rwlock_unlock(a)
Definition: lock.h:200
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:98
long int amaflags
Definition: cdr.h:112
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:88
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:92
struct timeval answer
Definition: cdr.h:102
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:96
#define LOG_ERROR
Definition: logger.h:155
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
struct timeval start
Definition: cdr.h:100
long int duration
Definition: cdr.h:106
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:2351
char src[AST_MAX_EXTENSION]
Definition: cdr.h:86
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:471
struct timeval end
Definition: cdr.h:104
long int disposition
Definition: cdr.h:110
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:84
struct ast_channel * ast_dummy_channel_alloc(void)
Create a fake channel structure.
Definition: channel.c:1391
#define manager_event(category, event, contents,...)
External routines may send asterisk manager events this way.
Definition: manager.h:219
char * ast_cdr_disp2str(int disposition)
Disposition to a string.
Definition: cdr.c:959
static ast_rwlock_t customfields_lock
Definition: cdr_manager.c:55
#define DATE_FORMAT
Definition: cdr_manager.c:46
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:125
static int reload ( void  )
static

Definition at line 220 of file cdr_manager.c.

References load_config().

221 {
222  return load_config(1);
223 }
static int load_config(int reload)
Definition: cdr_manager.c:59
static int unload_module ( void  )
static

Definition at line 202 of file cdr_manager.c.

References ast_cdr_unregister(), and ast_free.

203 {
205  if (customfields)
207 
208  return 0;
209 }
static struct ast_str * customfields
Definition: cdr_manager.c:54
static const char name[]
#define ast_free(a)
Definition: astmm.h:97
void ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:165

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk Manager Interface CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, }
static

Definition at line 230 of file cdr_manager.c.

Definition at line 230 of file cdr_manager.c.

struct ast_str* customfields
static

Definition at line 54 of file cdr_manager.c.

ast_rwlock_t customfields_lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, 1 }
static

Definition at line 55 of file cdr_manager.c.

Referenced by load_config(), and manager_log().

int enablecdr = 0
static

Definition at line 52 of file cdr_manager.c.

const char name[] = "cdr_manager"
static

Definition at line 50 of file cdr_manager.c.