00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "asterisk.h"
00038
00039 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 337973 $")
00040
00041 #include <time.h>
00042
00043 #include "asterisk/paths.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/cdr.h"
00046 #include "asterisk/module.h"
00047 #include "asterisk/config.h"
00048 #include "asterisk/pbx.h"
00049 #include "asterisk/utils.h"
00050 #include "asterisk/lock.h"
00051 #include "asterisk/threadstorage.h"
00052 #include "asterisk/strings.h"
00053
00054 #define CUSTOM_LOG_DIR "/cdr_custom"
00055 #define CONFIG "cdr_custom.conf"
00056
00057 AST_THREADSTORAGE(custom_buf);
00058
00059 static const char name[] = "cdr-custom";
00060
00061 struct cdr_config {
00062 AST_DECLARE_STRING_FIELDS(
00063 AST_STRING_FIELD(filename);
00064 AST_STRING_FIELD(format);
00065 );
00066 ast_mutex_t lock;
00067 AST_RWLIST_ENTRY(cdr_config) list;
00068 };
00069
00070 static AST_RWLIST_HEAD_STATIC(sinks, cdr_config);
00071
00072 static void free_config(void)
00073 {
00074 struct cdr_config *sink;
00075 while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
00076 ast_mutex_destroy(&sink->lock);
00077 ast_free(sink);
00078 }
00079 }
00080
00081 static int load_config(void)
00082 {
00083 struct ast_config *cfg;
00084 struct ast_variable *var;
00085 struct ast_flags config_flags = { 0 };
00086 int res = 0;
00087
00088 cfg = ast_config_load(CONFIG, config_flags);
00089 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
00090 ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n");
00091 return -1;
00092 }
00093
00094 var = ast_variable_browse(cfg, "mappings");
00095 while (var) {
00096 if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
00097 struct cdr_config *sink = ast_calloc_with_stringfields(1, struct cdr_config, 1024);
00098
00099 if (!sink) {
00100 ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
00101 res = -2;
00102 break;
00103 }
00104
00105 ast_string_field_build(sink, format, "%s\n", var->value);
00106 ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
00107 ast_mutex_init(&sink->lock);
00108
00109 AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
00110 } else {
00111 ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
00112 }
00113 var = var->next;
00114 }
00115 ast_config_destroy(cfg);
00116
00117 return res;
00118 }
00119
00120 static int custom_log(struct ast_cdr *cdr)
00121 {
00122 struct ast_channel *dummy;
00123 struct ast_str *str;
00124 struct cdr_config *config;
00125
00126
00127 if (!(str = ast_str_thread_get(&custom_buf, 16))) {
00128 return -1;
00129 }
00130
00131 dummy = ast_dummy_channel_alloc();
00132 if (!dummy) {
00133 ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
00134 return -1;
00135 }
00136
00137
00138
00139
00140 dummy->cdr = ast_cdr_dup(cdr);
00141
00142 AST_RWLIST_RDLOCK(&sinks);
00143
00144 AST_LIST_TRAVERSE(&sinks, config, list) {
00145 FILE *out;
00146
00147 ast_str_substitute_variables(&str, 0, dummy, config->format);
00148
00149
00150
00151
00152
00153 ast_mutex_lock(&config->lock);
00154
00155
00156
00157
00158 if ((out = fopen(config->filename, "a"))) {
00159 fputs(ast_str_buffer(str), out);
00160 fflush(out);
00161 fclose(out);
00162 } else {
00163 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
00164 }
00165
00166 ast_mutex_unlock(&config->lock);
00167 }
00168
00169 AST_RWLIST_UNLOCK(&sinks);
00170
00171 ast_channel_unref(dummy);
00172
00173 return 0;
00174 }
00175
00176 static int unload_module(void)
00177 {
00178 ast_cdr_unregister(name);
00179
00180 if (AST_RWLIST_WRLOCK(&sinks)) {
00181 ast_cdr_register(name, ast_module_info->description, custom_log);
00182 ast_log(LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
00183 return -1;
00184 }
00185
00186 free_config();
00187 AST_RWLIST_UNLOCK(&sinks);
00188 return 0;
00189 }
00190
00191 static enum ast_module_load_result load_module(void)
00192 {
00193 if (AST_RWLIST_WRLOCK(&sinks)) {
00194 ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
00195 return AST_MODULE_LOAD_FAILURE;
00196 }
00197
00198 load_config();
00199 AST_RWLIST_UNLOCK(&sinks);
00200 ast_cdr_register(name, ast_module_info->description, custom_log);
00201 return AST_MODULE_LOAD_SUCCESS;
00202 }
00203
00204 static int reload(void)
00205 {
00206 if (AST_RWLIST_WRLOCK(&sinks)) {
00207 ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
00208 return AST_MODULE_LOAD_FAILURE;
00209 }
00210
00211 free_config();
00212 load_config();
00213 AST_RWLIST_UNLOCK(&sinks);
00214 return AST_MODULE_LOAD_SUCCESS;
00215 }
00216
00217 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CDR Backend",
00218 .load = load_module,
00219 .unload = unload_module,
00220 .reload = reload,
00221 .load_pri = AST_MODPRI_CDR_DRIVER,
00222 );
00223