cdr_syslog.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, malleable, LLC.
  5. *
  6. * Sean Bright <sean@malleable.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief syslog CDR logger
  21. *
  22. * \author Sean Bright <sean@malleable.com>
  23. *
  24. * See also
  25. * \arg \ref Config_cdr
  26. * \ingroup cdr_drivers
  27. */
  28. /*! \li \ref cdr_syslog.c uses the configuration file \ref cdr_syslog.conf
  29. * \addtogroup configuration_file Configuration Files
  30. */
  31. /*!
  32. * \page cdr_syslog.conf cdr_syslog.conf
  33. * \verbinclude cdr_syslog.conf.sample
  34. */
  35. /*** MODULEINFO
  36. <depend>syslog</depend>
  37. <support_level>core</support_level>
  38. ***/
  39. #include "asterisk.h"
  40. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  41. #include "asterisk/module.h"
  42. #include "asterisk/lock.h"
  43. #include "asterisk/cdr.h"
  44. #include "asterisk/pbx.h"
  45. #include <syslog.h>
  46. #include "asterisk/syslog.h"
  47. static const char CONFIG[] = "cdr_syslog.conf";
  48. AST_THREADSTORAGE(syslog_buf);
  49. static const char name[] = "cdr-syslog";
  50. struct cdr_syslog_config {
  51. AST_DECLARE_STRING_FIELDS(
  52. AST_STRING_FIELD(ident);
  53. AST_STRING_FIELD(format);
  54. );
  55. int facility;
  56. int priority;
  57. ast_mutex_t lock;
  58. AST_LIST_ENTRY(cdr_syslog_config) list;
  59. };
  60. static AST_RWLIST_HEAD_STATIC(sinks, cdr_syslog_config);
  61. static void free_config(void)
  62. {
  63. struct cdr_syslog_config *sink;
  64. while ((sink = AST_RWLIST_REMOVE_HEAD(&sinks, list))) {
  65. ast_mutex_destroy(&sink->lock);
  66. ast_string_field_free_memory(sink);
  67. ast_free(sink);
  68. }
  69. }
  70. static int syslog_log(struct ast_cdr *cdr)
  71. {
  72. struct ast_channel *dummy;
  73. struct ast_str *str;
  74. struct cdr_syslog_config *sink;
  75. /* Batching saves memory management here. Otherwise, it's the same as doing an
  76. allocation and free each time. */
  77. if (!(str = ast_str_thread_get(&syslog_buf, 16))) {
  78. return -1;
  79. }
  80. if (!(dummy = ast_dummy_channel_alloc())) {
  81. ast_log(AST_LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
  82. return -1;
  83. }
  84. /* We need to dup here since the cdr actually belongs to the other channel,
  85. so when we release this channel we don't want the CDR getting cleaned
  86. up prematurely. */
  87. ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
  88. AST_RWLIST_RDLOCK(&sinks);
  89. AST_LIST_TRAVERSE(&sinks, sink, list) {
  90. ast_str_substitute_variables(&str, 0, dummy, sink->format);
  91. /* Even though we have a lock on the list, we could be being chased by
  92. another thread and this lock ensures that we won't step on anyone's
  93. toes. Once each CDR backend gets it's own thread, this lock can be
  94. removed. */
  95. ast_mutex_lock(&sink->lock);
  96. openlog(sink->ident, LOG_CONS, sink->facility);
  97. syslog(sink->priority, "%s", ast_str_buffer(str));
  98. closelog();
  99. ast_mutex_unlock(&sink->lock);
  100. }
  101. AST_RWLIST_UNLOCK(&sinks);
  102. ast_channel_unref(dummy);
  103. return 0;
  104. }
  105. static int load_config(int reload)
  106. {
  107. struct ast_config *cfg;
  108. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  109. int default_facility = LOG_LOCAL4;
  110. int default_priority = LOG_INFO;
  111. const char *catg = NULL, *tmp;
  112. cfg = ast_config_load(CONFIG, config_flags);
  113. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  114. ast_log(AST_LOG_ERROR,
  115. "Unable to load %s. Not logging custom CSV CDRs to syslog.\n", CONFIG);
  116. return -1;
  117. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  118. return 0;
  119. }
  120. if (reload) {
  121. free_config();
  122. }
  123. if (!(ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "general", "facility")))) {
  124. int facility = ast_syslog_facility(tmp);
  125. if (facility < 0) {
  126. ast_log(AST_LOG_WARNING,
  127. "Invalid facility '%s' specified, defaulting to '%s'\n",
  128. tmp, ast_syslog_facility_name(default_facility));
  129. } else {
  130. default_facility = facility;
  131. }
  132. }
  133. if (!(ast_strlen_zero(tmp = ast_variable_retrieve(cfg, "general", "priority")))) {
  134. int priority = ast_syslog_priority(tmp);
  135. if (priority < 0) {
  136. ast_log(AST_LOG_WARNING,
  137. "Invalid priority '%s' specified, defaulting to '%s'\n",
  138. tmp, ast_syslog_priority_name(default_priority));
  139. } else {
  140. default_priority = priority;
  141. }
  142. }
  143. while ((catg = ast_category_browse(cfg, catg))) {
  144. struct cdr_syslog_config *sink;
  145. if (!strcasecmp(catg, "general")) {
  146. continue;
  147. }
  148. if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "template"))) {
  149. ast_log(AST_LOG_WARNING,
  150. "No 'template' parameter found for '%s'. Skipping.\n", catg);
  151. continue;
  152. }
  153. sink = ast_calloc_with_stringfields(1, struct cdr_syslog_config, 1024);
  154. if (!sink) {
  155. ast_log(AST_LOG_ERROR,
  156. "Unable to allocate memory for configuration settings.\n");
  157. free_config();
  158. break;
  159. }
  160. ast_mutex_init(&sink->lock);
  161. ast_string_field_set(sink, ident, catg);
  162. ast_string_field_set(sink, format, tmp);
  163. if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "facility"))) {
  164. sink->facility = default_facility;
  165. } else {
  166. int facility = ast_syslog_facility(tmp);
  167. if (facility < 0) {
  168. ast_log(AST_LOG_WARNING,
  169. "Invalid facility '%s' specified for '%s,' defaulting to '%s'\n",
  170. tmp, catg, ast_syslog_facility_name(default_facility));
  171. } else {
  172. sink->facility = facility;
  173. }
  174. }
  175. if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "priority"))) {
  176. sink->priority = default_priority;
  177. } else {
  178. int priority = ast_syslog_priority(tmp);
  179. if (priority < 0) {
  180. ast_log(AST_LOG_WARNING,
  181. "Invalid priority '%s' specified for '%s,' defaulting to '%s'\n",
  182. tmp, catg, ast_syslog_priority_name(default_priority));
  183. } else {
  184. sink->priority = priority;
  185. }
  186. }
  187. AST_RWLIST_INSERT_TAIL(&sinks, sink, list);
  188. }
  189. ast_config_destroy(cfg);
  190. return AST_RWLIST_EMPTY(&sinks) ? -1 : 0;
  191. }
  192. static int unload_module(void)
  193. {
  194. if (ast_cdr_unregister(name)) {
  195. return -1;
  196. }
  197. if (AST_RWLIST_WRLOCK(&sinks)) {
  198. ast_cdr_register(name, ast_module_info->description, syslog_log);
  199. ast_log(AST_LOG_ERROR, "Unable to lock sink list. Unload failed.\n");
  200. return -1;
  201. }
  202. free_config();
  203. AST_RWLIST_UNLOCK(&sinks);
  204. return 0;
  205. }
  206. static enum ast_module_load_result load_module(void)
  207. {
  208. int res;
  209. if (AST_RWLIST_WRLOCK(&sinks)) {
  210. ast_log(AST_LOG_ERROR, "Unable to lock sink list. Load failed.\n");
  211. return AST_MODULE_LOAD_DECLINE;
  212. }
  213. res = load_config(0);
  214. AST_RWLIST_UNLOCK(&sinks);
  215. if (res) {
  216. return AST_MODULE_LOAD_DECLINE;
  217. }
  218. ast_cdr_register(name, ast_module_info->description, syslog_log);
  219. return AST_MODULE_LOAD_SUCCESS;
  220. }
  221. static int reload(void)
  222. {
  223. int res;
  224. if (AST_RWLIST_WRLOCK(&sinks)) {
  225. ast_log(AST_LOG_ERROR, "Unable to lock sink list. Load failed.\n");
  226. return AST_MODULE_LOAD_DECLINE;
  227. }
  228. if ((res = load_config(1))) {
  229. free_config();
  230. }
  231. AST_RWLIST_UNLOCK(&sinks);
  232. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  233. }
  234. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Customizable syslog CDR Backend",
  235. .support_level = AST_MODULE_SUPPORT_CORE,
  236. .load = load_module,
  237. .unload = unload_module,
  238. .reload = reload,
  239. .load_pri = AST_MODPRI_CDR_DRIVER,
  240. );