cel_custom.c 5.7 KB

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