cdr_custom.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2009, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * Includes code and algorithms from the Zapata library.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*!
  21. * \file
  22. * \brief Custom Comma Separated Value CDR records.
  23. *
  24. * \author Mark Spencer <markster@digium.com>
  25. *
  26. * \arg See also \ref AstCDR
  27. *
  28. * Logs in LOG_DIR/cdr_custom
  29. * \ingroup cdr_drivers
  30. */
  31. /*! \li \ref cdr_custom.c uses the configuration file \ref cdr_custom.conf
  32. * \addtogroup configuration_file Configuration Files
  33. */
  34. /*!
  35. * \page cdr_custom.conf cdr_custom.conf
  36. * \verbinclude cdr_custom.conf.sample
  37. */
  38. /*** MODULEINFO
  39. <support_level>core</support_level>
  40. ***/
  41. #include "asterisk.h"
  42. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  43. #include <time.h>
  44. #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
  45. #include "asterisk/channel.h"
  46. #include "asterisk/cdr.h"
  47. #include "asterisk/module.h"
  48. #include "asterisk/config.h"
  49. #include "asterisk/pbx.h"
  50. #include "asterisk/utils.h"
  51. #include "asterisk/lock.h"
  52. #include "asterisk/threadstorage.h"
  53. #include "asterisk/strings.h"
  54. #define CUSTOM_LOG_DIR "/cdr_custom"
  55. #define CONFIG "cdr_custom.conf"
  56. AST_THREADSTORAGE(custom_buf);
  57. static const char name[] = "cdr-custom";
  58. struct cdr_custom_config {
  59. AST_DECLARE_STRING_FIELDS(
  60. AST_STRING_FIELD(filename);
  61. AST_STRING_FIELD(format);
  62. );
  63. ast_mutex_t lock;
  64. AST_RWLIST_ENTRY(cdr_custom_config) list;
  65. };
  66. static AST_RWLIST_HEAD_STATIC(sinks, cdr_custom_config);
  67. static void free_config(void)
  68. {
  69. struct cdr_custom_config *sink;
  70. while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
  71. ast_mutex_destroy(&sink->lock);
  72. ast_string_field_free_memory(sink);
  73. ast_free(sink);
  74. }
  75. }
  76. static int load_config(void)
  77. {
  78. struct ast_config *cfg;
  79. struct ast_variable *var;
  80. struct ast_flags config_flags = { 0 };
  81. int res = 0;
  82. cfg = ast_config_load(CONFIG, config_flags);
  83. if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
  84. ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging custom CSV CDRs.\n");
  85. return -1;
  86. }
  87. var = ast_variable_browse(cfg, "mappings");
  88. while (var) {
  89. if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
  90. struct cdr_custom_config *sink = ast_calloc_with_stringfields(1, struct cdr_custom_config, 1024);
  91. if (!sink) {
  92. ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
  93. res = -2;
  94. break;
  95. }
  96. ast_string_field_build(sink, format, "%s\n", var->value);
  97. ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
  98. ast_mutex_init(&sink->lock);
  99. AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
  100. } else {
  101. ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
  102. }
  103. var = var->next;
  104. }
  105. ast_config_destroy(cfg);
  106. return res;
  107. }
  108. static int custom_log(struct ast_cdr *cdr)
  109. {
  110. struct ast_channel *dummy;
  111. struct ast_str *str;
  112. struct cdr_custom_config *config;
  113. /* Batching saves memory management here. Otherwise, it's the same as doing an allocation and free each time. */
  114. if (!(str = ast_str_thread_get(&custom_buf, 16))) {
  115. return -1;
  116. }
  117. dummy = ast_dummy_channel_alloc();
  118. if (!dummy) {
  119. ast_log(LOG_ERROR, "Unable to allocate channel for variable subsitution.\n");
  120. return -1;
  121. }
  122. /* We need to dup here since the cdr actually belongs to the other channel,
  123. so when we release this channel we don't want the CDR getting cleaned
  124. up prematurely. */
  125. ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
  126. AST_RWLIST_RDLOCK(&sinks);
  127. AST_LIST_TRAVERSE(&sinks, config, list) {
  128. FILE *out;
  129. ast_str_substitute_variables(&str, 0, dummy, config->format);
  130. /* Even though we have a lock on the list, we could be being chased by
  131. another thread and this lock ensures that we won't step on anyone's
  132. toes. Once each CDR backend gets it's own thread, this lock can be
  133. removed. */
  134. ast_mutex_lock(&config->lock);
  135. /* Because of the absolutely unconditional need for the
  136. highest reliability possible in writing billing records,
  137. we open write and close the log file each time */
  138. if ((out = fopen(config->filename, "a"))) {
  139. fputs(ast_str_buffer(str), out);
  140. fflush(out); /* be particularly anal here */
  141. fclose(out);
  142. } else {
  143. ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
  144. }
  145. ast_mutex_unlock(&config->lock);
  146. }
  147. AST_RWLIST_UNLOCK(&sinks);
  148. ast_channel_unref(dummy);
  149. return 0;
  150. }
  151. static int unload_module(void)
  152. {
  153. if (ast_cdr_unregister(name)) {
  154. return -1;
  155. }
  156. if (AST_RWLIST_WRLOCK(&sinks)) {
  157. ast_cdr_register(name, ast_module_info->description, custom_log);
  158. ast_log(LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
  159. return -1;
  160. }
  161. free_config();
  162. AST_RWLIST_UNLOCK(&sinks);
  163. return 0;
  164. }
  165. static enum ast_module_load_result load_module(void)
  166. {
  167. if (AST_RWLIST_WRLOCK(&sinks)) {
  168. ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
  169. return AST_MODULE_LOAD_DECLINE;
  170. }
  171. load_config();
  172. AST_RWLIST_UNLOCK(&sinks);
  173. ast_cdr_register(name, ast_module_info->description, custom_log);
  174. return AST_MODULE_LOAD_SUCCESS;
  175. }
  176. static int reload(void)
  177. {
  178. if (AST_RWLIST_WRLOCK(&sinks)) {
  179. ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
  180. return AST_MODULE_LOAD_DECLINE;
  181. }
  182. free_config();
  183. load_config();
  184. AST_RWLIST_UNLOCK(&sinks);
  185. return AST_MODULE_LOAD_SUCCESS;
  186. }
  187. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CDR Backend",
  188. .support_level = AST_MODULE_SUPPORT_CORE,
  189. .load = load_module,
  190. .unload = unload_module,
  191. .reload = reload,
  192. .load_pri = AST_MODPRI_CDR_DRIVER,
  193. );