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: 328209 $")
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
00133 if (!dummy) {
00134 ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
00135 return -1;
00136 }
00137
00138
00139
00140
00141 dummy->cdr = ast_cdr_dup(cdr);
00142
00143 AST_RWLIST_RDLOCK(&sinks);
00144
00145 AST_LIST_TRAVERSE(&sinks, config, list) {
00146 FILE *out;
00147
00148 ast_str_substitute_variables(&str, 0, dummy, config->format);
00149
00150
00151
00152
00153
00154 ast_mutex_lock(&config->lock);
00155
00156
00157
00158
00159 if ((out = fopen(config->filename, "a"))) {
00160 fputs(ast_str_buffer(str), out);
00161 fflush(out);
00162 fclose(out);
00163 } else {
00164 ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
00165 }
00166
00167 ast_mutex_unlock(&config->lock);
00168 }
00169
00170 AST_RWLIST_UNLOCK(&sinks);
00171
00172 ast_channel_release(dummy);
00173
00174 return 0;
00175 }
00176
00177 static int unload_module(void)
00178 {
00179 ast_cdr_unregister(name);
00180
00181 if (AST_RWLIST_WRLOCK(&sinks)) {
00182 ast_cdr_register(name, ast_module_info->description, custom_log);
00183 ast_log(LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
00184 return -1;
00185 }
00186
00187 free_config();
00188 AST_RWLIST_UNLOCK(&sinks);
00189 return 0;
00190 }
00191
00192 static enum ast_module_load_result load_module(void)
00193 {
00194 if (AST_RWLIST_WRLOCK(&sinks)) {
00195 ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
00196 return AST_MODULE_LOAD_FAILURE;
00197 }
00198
00199 load_config();
00200 AST_RWLIST_UNLOCK(&sinks);
00201 ast_cdr_register(name, ast_module_info->description, custom_log);
00202 return AST_MODULE_LOAD_SUCCESS;
00203 }
00204
00205 static int reload(void)
00206 {
00207 if (AST_RWLIST_WRLOCK(&sinks)) {
00208 ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
00209 return AST_MODULE_LOAD_FAILURE;
00210 }
00211
00212 free_config();
00213 load_config();
00214 AST_RWLIST_UNLOCK(&sinks);
00215 return AST_MODULE_LOAD_SUCCESS;
00216 }
00217
00218 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CDR Backend",
00219 .load = load_module,
00220 .unload = unload_module,
00221 .reload = reload,
00222 .load_pri = AST_MODPRI_CDR_DRIVER,
00223 );
00224