Wed Jan 8 2020 09:49:59

Asterisk developer's documentation


cdr_custom.c File Reference

Custom Comma Separated Value CDR records. More...

#include "asterisk.h"
#include <time.h>
#include "asterisk/paths.h"
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/config.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/threadstorage.h"
#include "asterisk/strings.h"

Go to the source code of this file.

Data Structures

struct  cdr_config
 
struct  sinks
 

Macros

#define CONFIG   "cdr_custom.conf"
 
#define CUSTOM_LOG_DIR   "/cdr_custom"
 

Functions

static void __init_custom_buf (void)
 
static void __reg_module (void)
 
static void __unreg_module (void)
 
static int custom_log (struct ast_cdr *cdr)
 
static void free_config (void)
 
static int load_config (void)
 
static enum ast_module_load_result 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 = "Customizable Comma Separated Values 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_threadstorage custom_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_custom_buf , .custom_init = NULL , }
 
static const char name [] = "cdr-custom"
 
static struct sinks sinks = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, 1 } , }
 

Detailed Description

Custom Comma Separated Value CDR records.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Logs in LOG_DIR/cdr_custom

Definition in file cdr_custom.c.

Macro Definition Documentation

#define CONFIG   "cdr_custom.conf"

Definition at line 55 of file cdr_custom.c.

Referenced by load_config().

#define CUSTOM_LOG_DIR   "/cdr_custom"

Definition at line 54 of file cdr_custom.c.

Function Documentation

static void __init_custom_buf ( void  )
static

Definition at line 57 of file cdr_custom.c.

61 {
static void __reg_module ( void  )
static

Definition at line 222 of file cdr_custom.c.

static void __unreg_module ( void  )
static

Definition at line 222 of file cdr_custom.c.

static int custom_log ( struct ast_cdr cdr)
static

Definition at line 120 of file cdr_custom.c.

References ast_cdr_dup(), ast_channel_unref, ast_dummy_channel_alloc(), AST_LIST_TRAVERSE, ast_log(), ast_mutex_lock, ast_mutex_unlock, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_str_buffer(), ast_str_substitute_variables(), ast_str_thread_get(), ast_channel::cdr, config, custom_buf, dummy(), errno, cdr_config::filename, cdr_config::format, cdr_config::list, cdr_config::lock, LOG_ERROR, and str.

Referenced by load_module(), and unload_module().

121 {
122  struct ast_channel *dummy;
123  struct ast_str *str;
124  struct cdr_config *config;
125 
126  /* Batching saves memory management here. Otherwise, it's the same as doing an allocation and free each time. */
127  if (!(str = ast_str_thread_get(&custom_buf, 16))) {
128  return -1;
129  }
130 
131  dummy = ast_dummy_channel_alloc();
132  if (!dummy) {
133  ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
134  return -1;
135  }
136 
137  /* We need to dup here since the cdr actually belongs to the other channel,
138  so when we release this channel we don't want the CDR getting cleaned
139  up prematurely. */
140  dummy->cdr = ast_cdr_dup(cdr);
141 
143 
144  AST_LIST_TRAVERSE(&sinks, config, list) {
145  FILE *out;
146 
147  ast_str_substitute_variables(&str, 0, dummy, config->format);
148 
149  /* Even though we have a lock on the list, we could be being chased by
150  another thread and this lock ensures that we won't step on anyone's
151  toes. Once each CDR backend gets it's own thread, this lock can be
152  removed. */
153  ast_mutex_lock(&config->lock);
154 
155  /* Because of the absolutely unconditional need for the
156  highest reliability possible in writing billing records,
157  we open write and close the log file each time */
158  if ((out = fopen(config->filename, "a"))) {
159  fputs(ast_str_buffer(str), out);
160  fflush(out); /* be particularly anal here */
161  fclose(out);
162  } else {
163  ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
164  }
165 
166  ast_mutex_unlock(&config->lock);
167  }
168 
170 
171  ast_channel_unref(dummy);
172 
173  return 0;
174 }
Main Channel structure associated with a channel.
Definition: channel.h:742
static const char config[]
Definition: cdr_csv.c:57
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2502
const ast_string_field filename
Definition: cdr_custom.c:65
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
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
struct ast_cdr * ast_cdr_dup(struct ast_cdr *cdr)
Duplicate a record.
Definition: cdr.c:213
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
Definition: pbx.c:4468
#define ast_mutex_lock(a)
Definition: lock.h:155
struct ast_cdr * cdr
Definition: channel.h:766
const char * str
Definition: app_jack.c:144
static struct ast_threadstorage custom_buf
Definition: cdr_custom.c:57
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
struct cdr_config::@76 list
#define LOG_ERROR
Definition: logger.h:155
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
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
int errno
const ast_string_field format
Definition: cdr_custom.c:65
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:669
struct ast_channel * ast_dummy_channel_alloc(void)
Create a fake channel structure.
Definition: channel.c:1391
#define ast_mutex_unlock(a)
Definition: lock.h:156
ast_mutex_t lock
Definition: cdr_custom.c:66
static void free_config ( void  )
static

Definition at line 72 of file cdr_custom.c.

References ast_free, ast_mutex_destroy, AST_RWLIST_REMOVE_HEAD, and cdr_config::lock.

Referenced by reload(), and unload_module().

73 {
74  struct cdr_config *sink;
75  while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
76  ast_mutex_destroy(&sink->lock);
77  ast_free(sink);
78  }
79 }
struct cdr_config::@76 list
#define ast_free(a)
Definition: astmm.h:97
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:829
#define ast_mutex_destroy(a)
Definition: lock.h:154
ast_mutex_t lock
Definition: cdr_custom.c:66
static int load_config ( void  )
static

Definition at line 81 of file cdr_custom.c.

References ast_calloc_with_stringfields, ast_config_AST_LOG_DIR, ast_config_destroy(), ast_config_load, ast_log(), ast_mutex_init, AST_RWLIST_INSERT_TAIL, ast_string_field_build, ast_strlen_zero(), ast_variable_browse(), CONFIG, CONFIG_STATUS_FILEINVALID, cdr_config::filename, cdr_config::format, ast_variable::lineno, cdr_config::list, cdr_config::lock, LOG_ERROR, LOG_NOTICE, ast_variable::name, ast_variable::next, ast_variable::value, and var.

Referenced by load_module(), and reload().

82 {
83  struct ast_config *cfg;
84  struct ast_variable *var;
85  struct ast_flags config_flags = { 0 };
86  int res = 0;
87 
88  cfg = ast_config_load(CONFIG, config_flags);
89  if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
90  ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n");
91  return -1;
92  }
93 
94  var = ast_variable_browse(cfg, "mappings");
95  while (var) {
96  if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
97  struct cdr_config *sink = ast_calloc_with_stringfields(1, struct cdr_config, 1024);
98 
99  if (!sink) {
100  ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
101  res = -2;
102  break;
103  }
104 
105  ast_string_field_build(sink, format, "%s\n", var->value);
107  ast_mutex_init(&sink->lock);
108 
110  } else {
111  ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
112  }
113  var = var->next;
114  }
115  ast_config_destroy(cfg);
116 
117  return res;
118 }
#define CONFIG
Definition: cdr_custom.c:55
const ast_string_field filename
Definition: cdr_custom.c:65
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Definition: config.c:597
int lineno
Definition: config.h:87
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
#define var
Definition: ast_expr2f.c:606
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
Definition: stringfields.h:275
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
const char * value
Definition: config.h:79
struct cdr_config::@76 list
#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
const char * name
Definition: config.h:77
#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
#define LOG_NOTICE
Definition: logger.h:133
const char * ast_config_AST_LOG_DIR
Definition: asterisk.c:263
static const char name[]
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
Definition: stringfields.h:367
Structure used to handle boolean flags.
Definition: utils.h:200
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:726
struct ast_variable * next
Definition: config.h:82
#define ast_mutex_init(pmutex)
Definition: lock.h:152
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
static snd_pcm_format_t format
Definition: chan_alsa.c:93
ast_mutex_t lock
Definition: cdr_custom.c:66
static enum ast_module_load_result load_module ( void  )
static

Definition at line 191 of file cdr_custom.c.

References ast_cdr_register(), ast_log(), AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, custom_log(), ast_module_info::description, load_config(), and LOG_ERROR.

192 {
193  if (AST_RWLIST_WRLOCK(&sinks)) {
194  ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
196  }
197 
198  load_config();
202 }
const char * description
Definition: module.h:234
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static int custom_log(struct ast_cdr *cdr)
Definition: cdr_custom.c:120
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:130
#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
static const char name[]
static int load_config(void)
Definition: cdr_custom.c:81
static int reload ( void  )
static

Definition at line 204 of file cdr_custom.c.

References ast_log(), AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, free_config(), load_config(), and LOG_ERROR.

205 {
206  if (AST_RWLIST_WRLOCK(&sinks)) {
207  ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
209  }
210 
211  free_config();
212  load_config();
215 }
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
#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
static void free_config(void)
Definition: cdr_custom.c:72
static int load_config(void)
Definition: cdr_custom.c:81
static int unload_module ( void  )
static

Definition at line 176 of file cdr_custom.c.

References ast_cdr_register(), ast_cdr_unregister(), ast_log(), AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, custom_log(), ast_module_info::description, free_config(), and LOG_ERROR.

177 {
179 
180  if (AST_RWLIST_WRLOCK(&sinks)) {
182  ast_log(LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
183  return -1;
184  }
185 
186  free_config();
188  return 0;
189 }
const char * description
Definition: module.h:234
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
static int custom_log(struct ast_cdr *cdr)
Definition: cdr_custom.c:120
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:130
#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
static const char name[]
static void free_config(void)
Definition: cdr_custom.c:72
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 = "Customizable Comma Separated Values 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 222 of file cdr_custom.c.

Definition at line 222 of file cdr_custom.c.

struct ast_threadstorage custom_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_custom_buf , .custom_init = NULL , }
static

Definition at line 57 of file cdr_custom.c.

Referenced by custom_log().

const char name[] = "cdr-custom"
static

Definition at line 59 of file cdr_custom.c.

struct sinks sinks = { .first = NULL, .last = NULL, .lock = { PTHREAD_RWLOCK_INITIALIZER , NULL, 1 } , }
static