cel_custom.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_string_field_free_memory(sink);
  65. ast_free(sink);
  66. }
  67. }
  68. static int load_config(void)
  69. {
  70. struct ast_config *cfg;
  71. struct ast_variable *var;
  72. struct ast_flags config_flags = { 0 };
  73. int mappings = 0;
  74. int res = 0;
  75. cfg = ast_config_load(CONFIG, config_flags);
  76. if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
  77. ast_log(LOG_ERROR, "Unable to load " CONFIG ". Not logging CEL to custom CSVs.\n");
  78. return -1;
  79. }
  80. if (!(var = ast_variable_browse(cfg, "mappings"))) {
  81. ast_log(LOG_NOTICE, "No mappings found in " CONFIG ". Not logging CEL to custom CSVs.\n");
  82. }
  83. while (var) {
  84. if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
  85. struct cel_config *sink = ast_calloc_with_stringfields(1, struct cel_config, 1024);
  86. if (!sink) {
  87. ast_log(LOG_ERROR, "Unable to allocate memory for configuration settings.\n");
  88. res = -2;
  89. break;
  90. }
  91. ast_string_field_build(sink, format, "%s\n", var->value);
  92. ast_string_field_build(sink, filename, "%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
  93. ast_mutex_init(&sink->lock);
  94. ast_verb(3, "Added CEL CSV mapping for '%s'.\n", sink->filename);
  95. mappings += 1;
  96. AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
  97. } else {
  98. ast_log(LOG_NOTICE, "Mapping must have both a filename and a format at line %d\n", var->lineno);
  99. }
  100. var = var->next;
  101. }
  102. ast_config_destroy(cfg);
  103. ast_verb(1, "Added CEL CSV mapping for %d files.\n", mappings);
  104. return res;
  105. }
  106. static void custom_log(struct ast_event *event)
  107. {
  108. struct ast_channel *dummy;
  109. struct ast_str *str;
  110. struct cel_config *config;
  111. /* Batching saves memory management here. Otherwise, it's the same as doing an allocation and free each time. */
  112. if (!(str = ast_str_thread_get(&custom_buf, 16))) {
  113. return;
  114. }
  115. dummy = ast_cel_fabricate_channel_from_event(event);
  116. if (!dummy) {
  117. ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event.\n");
  118. return;
  119. }
  120. AST_RWLIST_RDLOCK(&sinks);
  121. AST_LIST_TRAVERSE(&sinks, config, list) {
  122. FILE *out;
  123. ast_str_substitute_variables(&str, 0, dummy, config->format);
  124. /* Even though we have a lock on the list, we could be being chased by
  125. another thread and this lock ensures that we won't step on anyone's
  126. toes. Once each CEL backend gets it's own thread, this lock can be
  127. removed. */
  128. ast_mutex_lock(&config->lock);
  129. /* Because of the absolutely unconditional need for the
  130. highest reliability possible in writing billing records,
  131. we open write and close the log file each time */
  132. if ((out = fopen(config->filename, "a"))) {
  133. fputs(ast_str_buffer(str), out);
  134. fflush(out); /* be particularly anal here */
  135. fclose(out);
  136. } else {
  137. ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", config->filename, strerror(errno));
  138. }
  139. ast_mutex_unlock(&config->lock);
  140. }
  141. AST_RWLIST_UNLOCK(&sinks);
  142. ast_channel_unref(dummy);
  143. }
  144. static int unload_module(void)
  145. {
  146. if (AST_RWLIST_WRLOCK(&sinks)) {
  147. ast_log(LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
  148. return -1;
  149. }
  150. free_config();
  151. AST_RWLIST_UNLOCK(&sinks);
  152. ast_cel_backend_unregister(CUSTOM_BACKEND_NAME);
  153. return 0;
  154. }
  155. static enum ast_module_load_result load_module(void)
  156. {
  157. if (AST_RWLIST_WRLOCK(&sinks)) {
  158. ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
  159. return AST_MODULE_LOAD_DECLINE;
  160. }
  161. load_config();
  162. AST_RWLIST_UNLOCK(&sinks);
  163. if (ast_cel_backend_register(CUSTOM_BACKEND_NAME, custom_log)) {
  164. free_config();
  165. return AST_MODULE_LOAD_DECLINE;
  166. }
  167. return AST_MODULE_LOAD_SUCCESS;
  168. }
  169. static int reload(void)
  170. {
  171. if (AST_RWLIST_WRLOCK(&sinks)) {
  172. ast_log(LOG_ERROR, "Unable to lock sink list. Load failed.\n");
  173. return AST_MODULE_LOAD_DECLINE;
  174. }
  175. free_config();
  176. load_config();
  177. AST_RWLIST_UNLOCK(&sinks);
  178. return AST_MODULE_LOAD_SUCCESS;
  179. }
  180. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable Comma Separated Values CEL Backend",
  181. .support_level = AST_MODULE_SUPPORT_CORE,
  182. .load = load_module,
  183. .unload = unload_module,
  184. .reload = reload,
  185. .load_pri = AST_MODPRI_CDR_DRIVER,
  186. );