syslog CDR logger More...
#include "asterisk.h"#include "asterisk/module.h"#include "asterisk/lock.h"#include "asterisk/cdr.h"#include "asterisk/pbx.h"#include <syslog.h>
Go to the source code of this file.
Data Structures | |
| struct | cdr_config |
| struct | sinks |
Functions | |
| static void | __init_syslog_buf (void) |
| static void | __reg_module (void) |
| static void | __unreg_module (void) |
| static void | free_config (void) |
| static int | load_config (int reload) |
| static enum ast_module_load_result | load_module (void) |
| static int | reload (void) |
| static int | syslog_log (struct ast_cdr *cdr) |
| static int | unload_module (void) |
Variables | |
| static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Customizable syslog 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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, } |
| static struct ast_module_info * | ast_module_info = &__mod_info |
| static const char | CONFIG [] = "cdr_syslog.conf" |
| static const char | name [] = "cdr-syslog" |
| static struct sinks | sinks |
| static struct ast_threadstorage | syslog_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_syslog_buf , .custom_init = NULL , } |
| static void __init_syslog_buf | ( | void | ) | [static] |
Definition at line 50 of file cdr_syslog.c.
{
| static void __reg_module | ( | void | ) | [static] |
Definition at line 282 of file cdr_syslog.c.
| static void __unreg_module | ( | void | ) | [static] |
Definition at line 282 of file cdr_syslog.c.
| static void free_config | ( | void | ) | [static] |
Definition at line 67 of file cdr_syslog.c.
References ast_free, ast_mutex_destroy, AST_RWLIST_REMOVE_HEAD, and cdr_config::lock.
Referenced by load_config(), reload(), and unload_module().
{
struct cdr_config *sink;
while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
ast_mutex_destroy(&sink->lock);
ast_free(sink);
}
}
| static int load_config | ( | int | reload | ) | [static] |
Definition at line 124 of file cdr_syslog.c.
References ast_calloc_with_stringfields, ast_category_browse(), ast_config_destroy(), ast_config_load, ast_log(), AST_LOG_ERROR, AST_LOG_WARNING, ast_mutex_init, AST_RWLIST_EMPTY, AST_RWLIST_INSERT_TAIL, ast_string_field_set, ast_strlen_zero(), ast_syslog_facility(), ast_syslog_facility_name(), ast_syslog_priority(), ast_syslog_priority_name(), ast_variable_retrieve(), CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, cdr_config::facility, cdr_config::format, free_config(), cdr_config::ident, cdr_config::list, cdr_config::lock, and cdr_config::priority.
Referenced by load_module(), and reload().
{
struct ast_config *cfg;
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
int default_facility = LOG_LOCAL4;
int default_priority = LOG_INFO;
const char *catg = NULL, *tmp;
cfg = ast_config_load(CONFIG, config_flags);
if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
ast_log(AST_LOG_ERROR,
"Unable to load %s. Not logging custom CSV CDRs to syslog.\n", CONFIG);
return -1;
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
return 0;
}
if (reload) {
free_config();
}
if (!(ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "general", "facility")))) {
int facility = ast_syslog_facility(tmp);
if (facility < 0) {
ast_log(AST_LOG_WARNING,
"Invalid facility '%s' specified, defaulting to '%s'\n",
tmp, ast_syslog_facility_name(default_facility));
} else {
default_facility = facility;
}
}
if (!(ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "general", "priority")))) {
int priority = ast_syslog_priority(tmp);
if (priority < 0) {
ast_log(AST_LOG_WARNING,
"Invalid priority '%s' specified, defaulting to '%s'\n",
tmp, ast_syslog_priority_name(default_priority));
} else {
default_priority = priority;
}
}
while ((catg = ast_category_browse(cfg, catg))) {
struct cdr_config *sink;
if (!strcasecmp(catg, "general")) {
continue;
}
if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "template"))) {
ast_log(AST_LOG_WARNING,
"No 'template' parameter found for '%s'. Skipping.\n", catg);
continue;
}
sink = ast_calloc_with_stringfields(1, struct cdr_config, 1024);
if (!sink) {
ast_log(AST_LOG_ERROR,
"Unable to allocate memory for configuration settings.\n");
free_config();
break;
}
ast_mutex_init(&sink->lock);
ast_string_field_set(sink, ident, catg);
ast_string_field_set(sink, format, tmp);
if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "facility"))) {
sink->facility = default_facility;
} else {
int facility = ast_syslog_facility(tmp);
if (facility < 0) {
ast_log(AST_LOG_WARNING,
"Invalid facility '%s' specified for '%s,' defaulting to '%s'\n",
tmp, catg, ast_syslog_facility_name(default_facility));
} else {
sink->facility = facility;
}
}
if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "priority"))) {
sink->priority = default_priority;
} else {
int priority = ast_syslog_priority(tmp);
if (priority < 0) {
ast_log(AST_LOG_WARNING,
"Invalid priority '%s' specified for '%s,' defaulting to '%s'\n",
tmp, catg, ast_syslog_priority_name(default_priority));
} else {
sink->priority = priority;
}
}
AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
}
ast_config_destroy(cfg);
return AST_RWLIST_EMPTY(&sinks) ? -1 : 0;
}
| static enum ast_module_load_result load_module | ( | void | ) | [static] |
Definition at line 242 of file cdr_syslog.c.
References ast_cdr_register(), ast_log(), AST_LOG_ERROR, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_module_info::description, load_config(), and syslog_log().
{
int res;
if (AST_RWLIST_WRLOCK(&sinks)) {
ast_log(AST_LOG_ERROR, "Unable to lock sink list. Load failed.\n");
return AST_MODULE_LOAD_DECLINE;
}
res = load_config(0);
AST_RWLIST_UNLOCK(&sinks);
if (res) {
return AST_MODULE_LOAD_DECLINE;
}
ast_cdr_register(name, ast_module_info->description, syslog_log);
return AST_MODULE_LOAD_SUCCESS;
}
| static int reload | ( | void | ) | [static] |
Definition at line 260 of file cdr_syslog.c.
References ast_log(), AST_LOG_ERROR, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, free_config(), and load_config().
{
int res;
if (AST_RWLIST_WRLOCK(&sinks)) {
ast_log(AST_LOG_ERROR, "Unable to lock sink list. Load failed.\n");
return AST_MODULE_LOAD_DECLINE;
}
if ((res = load_config(1))) {
free_config();
}
AST_RWLIST_UNLOCK(&sinks);
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
}
| static int syslog_log | ( | struct ast_cdr * | cdr | ) | [static] |
Definition at line 76 of file cdr_syslog.c.
References ast_cdr_dup(), ast_channel_unref, ast_dummy_channel_alloc(), AST_LIST_TRAVERSE, ast_log(), AST_LOG_ERROR, 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, dummy(), cdr_config::facility, cdr_config::format, cdr_config::ident, cdr_config::list, cdr_config::lock, cdr_config::priority, str, and syslog_buf.
Referenced by load_module(), and unload_module().
{
struct ast_channel *dummy;
struct ast_str *str;
struct cdr_config *sink;
/* Batching saves memory management here. Otherwise, it's the same as doing an
allocation and free each time. */
if (!(str = ast_str_thread_get(&syslog_buf, 16))) {
return -1;
}
if (!(dummy = ast_dummy_channel_alloc())) {
ast_log(AST_LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
return -1;
}
/* We need to dup here since the cdr actually belongs to the other channel,
so when we release this channel we don't want the CDR getting cleaned
up prematurely. */
dummy->cdr = ast_cdr_dup(cdr);
AST_RWLIST_RDLOCK(&sinks);
AST_LIST_TRAVERSE(&sinks, sink, list) {
ast_str_substitute_variables(&str, 0, dummy, sink->format);
/* Even though we have a lock on the list, we could be being chased by
another thread and this lock ensures that we won't step on anyone's
toes. Once each CDR backend gets it's own thread, this lock can be
removed. */
ast_mutex_lock(&sink->lock);
openlog(sink->ident, LOG_CONS, sink->facility);
syslog(sink->priority, "%s", ast_str_buffer(str));
closelog();
ast_mutex_unlock(&sink->lock);
}
AST_RWLIST_UNLOCK(&sinks);
ast_channel_unref(dummy);
return 0;
}
| static int unload_module | ( | void | ) | [static] |
Definition at line 227 of file cdr_syslog.c.
References ast_cdr_register(), ast_cdr_unregister(), ast_log(), AST_LOG_ERROR, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_module_info::description, free_config(), and syslog_log().
{
ast_cdr_unregister(name);
if (AST_RWLIST_WRLOCK(&sinks)) {
ast_cdr_register(name, ast_module_info->description, syslog_log);
ast_log(AST_LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
return -1;
}
free_config();
AST_RWLIST_UNLOCK(&sinks);
return 0;
}
struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Customizable syslog 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 = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .reload = reload, .load_pri = AST_MODPRI_CDR_DRIVER, } [static] |
Definition at line 282 of file cdr_syslog.c.
struct ast_module_info* ast_module_info = &__mod_info [static] |
Definition at line 282 of file cdr_syslog.c.
const char CONFIG[] = "cdr_syslog.conf" [static] |
Definition at line 48 of file cdr_syslog.c.
const char name[] = "cdr-syslog" [static] |
Definition at line 52 of file cdr_syslog.c.
struct ast_threadstorage syslog_buf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_syslog_buf , .custom_init = NULL , } [static] |
Definition at line 50 of file cdr_syslog.c.
Referenced by syslog_log().