Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


cdr_manager.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2004 - 2005
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16 
17 /*!
18  * \file
19  * \brief Asterisk Call Manager CDR records.
20  *
21  * See also
22  * \arg \ref AstCDR
23  * \arg \ref AstAMI
24  * \arg \ref Config_ami
25  * \ingroup cdr_drivers
26  */
27 
28 /*** MODULEINFO
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 337973 $")
35 
36 #include <time.h>
37 
38 #include "asterisk/channel.h"
39 #include "asterisk/cdr.h"
40 #include "asterisk/module.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/manager.h"
43 #include "asterisk/config.h"
44 #include "asterisk/pbx.h"
45 
46 #define DATE_FORMAT "%Y-%m-%d %T"
47 #define CONF_FILE "cdr_manager.conf"
48 #define CUSTOM_FIELDS_BUF_SIZE 1024
49 
50 static const char name[] = "cdr_manager";
51 
52 static int enablecdr = 0;
53 
54 static struct ast_str *customfields;
56 
57 static int manager_log(struct ast_cdr *cdr);
58 
59 static int load_config(int 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)
81  ast_cdr_unregister(name);
82  enablecdr = 0;
83  return -1;
84  }
85 
86  if (reload) {
88  }
89 
90  if (reload && customfields) {
91  ast_free(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")) {
105  customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
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)
130  ast_cdr_unregister(name);
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 }
137 
138 static int manager_log(struct ast_cdr *cdr)
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';
162  if (customfields && ast_str_strlen(customfields)) {
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 }
201 
202 static int unload_module(void)
203 {
204  ast_cdr_unregister(name);
205  if (customfields)
206  ast_free(customfields);
207 
208  return 0;
209 }
210 
211 static int load_module(void)
212 {
213  if (load_config(0)) {
215  }
216 
218 }
219 
220 static int reload(void)
221 {
222  return load_config(1);
223 }
224 
225 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
226  .load = load_module,
227  .unload = unload_module,
228  .reload = reload,
229  .load_pri = AST_MODPRI_CDR_DRIVER,
230  );
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
Asterisk main include file. File version handling, generic pbx functions.
#define AST_RWLOCK_DEFINE_STATIC(rwlock)
Definition: lock.h:549
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
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
long int billsec
Definition: cdr.h:108
#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
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
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
static int unload_module(void)
Definition: cdr_manager.c:202
struct ast_cdr * ast_cdr_dup(struct ast_cdr *cdr)
Duplicate a record.
Definition: cdr.c:213
char uniqueid[150]
Definition: cdr.h:121
static int load_config(int reload)
Definition: cdr_manager.c:59
Configuration File Parser.
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
static int load_module(void)
Definition: cdr_manager.c:211
struct ast_str * ast_str_create(size_t init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:420
struct ast_cdr * cdr
Definition: channel.h:766
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
char * ast_cdr_flags2str(int flags)
Definition: cdr.c:977
#define ast_rwlock_unlock(a)
Definition: lock.h:200
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
Utility functions.
Call Detail Record API.
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:98
long int amaflags
Definition: cdr.h:112
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
General Asterisk PBX channel definitions.
#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
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:88
const char * name
Definition: config.h:77
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:92
#define CONF_FILE
Definition: cdr_manager.c:47
Core PBX routines and definitions.
The AMI - Asterisk Manager Interface - is a TCP protocol created to manage Asterisk with third-party ...
struct timeval answer
Definition: cdr.h:102
Responsible for call detail data.
Definition: cdr.h:82
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:96
#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
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
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
struct timeval start
Definition: cdr.h:100
static const char name[]
#define ast_free(a)
Definition: astmm.h:97
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
Structure used to handle boolean flags.
Definition: utils.h:200
char src[AST_MAX_EXTENSION]
Definition: cdr.h:86
#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 timeval end
Definition: cdr.h:104
struct ast_variable * next
Definition: config.h:82
long int disposition
Definition: cdr.h:110
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:84
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
struct ast_channel * ast_dummy_channel_alloc(void)
Create a fake channel structure.
Definition: channel.c:1391
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
#define manager_event(category, event, contents,...)
External routines may send asterisk manager events this way.
Definition: manager.h:219
Asterisk module definitions.
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
void ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:165
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
#define DATE_FORMAT
Definition: cdr_manager.c:46
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:125
#define CONFIG_STATUS_FILEUNCHANGED
Definition: config.h:51