cel_manager.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008 - 2009, Digium, Inc.
  5. *
  6. * Steve Murphy <murf@digium.com>
  7. * who freely borrowed code from the cdr equivalents
  8. * (see cdr/cdr_manager.c)
  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. /*! \file
  21. *
  22. * \brief Asterisk Channel Event records.
  23. *
  24. * See also
  25. * \arg \ref AstCDR
  26. * \arg \ref AstAMI
  27. * \arg \ref Config_ami
  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/channel.h"
  36. #include "asterisk/cel.h"
  37. #include "asterisk/module.h"
  38. #include "asterisk/logger.h"
  39. #include "asterisk/utils.h"
  40. #include "asterisk/manager.h"
  41. #include "asterisk/config.h"
  42. static const char DATE_FORMAT[] = "%Y-%m-%d %T";
  43. static const char CONF_FILE[] = "cel.conf";
  44. /*! \brief AMI CEL is off by default */
  45. #define CEL_AMI_ENABLED_DEFAULT 0
  46. static int enablecel;
  47. /*! \brief show_user_def is off by default */
  48. #define CEL_SHOW_USERDEF_DEFAULT 0
  49. #define MANAGER_BACKEND_NAME "Manager Event Logging"
  50. /*! TRUE if we should set the EventName header to USER_DEFINED on user events. */
  51. static unsigned char cel_show_user_def;
  52. static void manager_log(struct ast_event *event)
  53. {
  54. struct ast_tm timeresult;
  55. char start_time[80] = "";
  56. char user_defined_header[160];
  57. const char *event_name;
  58. struct ast_cel_event_record record = {
  59. .version = AST_CEL_EVENT_RECORD_VERSION,
  60. };
  61. if (!enablecel) {
  62. return;
  63. }
  64. if (ast_cel_fill_record(event, &record)) {
  65. return;
  66. }
  67. ast_localtime(&record.event_time, &timeresult, NULL);
  68. ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
  69. event_name = record.event_name;
  70. user_defined_header[0] = '\0';
  71. if (record.event_type == AST_CEL_USER_DEFINED) {
  72. if (cel_show_user_def) {
  73. snprintf(user_defined_header, sizeof(user_defined_header),
  74. "UserDefType: %s\r\n", record.user_defined_name);
  75. } else {
  76. event_name = record.user_defined_name;
  77. }
  78. }
  79. manager_event(EVENT_FLAG_CALL, "CEL",
  80. "EventName: %s\r\n"
  81. "AccountCode: %s\r\n"
  82. "CallerIDnum: %s\r\n"
  83. "CallerIDname: %s\r\n"
  84. "CallerIDani: %s\r\n"
  85. "CallerIDrdnis: %s\r\n"
  86. "CallerIDdnid: %s\r\n"
  87. "Exten: %s\r\n"
  88. "Context: %s\r\n"
  89. "Channel: %s\r\n"
  90. "Application: %s\r\n"
  91. "AppData: %s\r\n"
  92. "EventTime: %s\r\n"
  93. "AMAFlags: %s\r\n"
  94. "UniqueID: %s\r\n"
  95. "LinkedID: %s\r\n"
  96. "Userfield: %s\r\n"
  97. "Peer: %s\r\n"
  98. "PeerAccount: %s\r\n"
  99. "%s"
  100. "Extra: %s\r\n",
  101. event_name,
  102. record.account_code,
  103. record.caller_id_num,
  104. record.caller_id_name,
  105. record.caller_id_ani,
  106. record.caller_id_rdnis,
  107. record.caller_id_dnid,
  108. record.extension,
  109. record.context,
  110. record.channel_name,
  111. record.application_name,
  112. record.application_data,
  113. start_time,
  114. ast_channel_amaflags2string(record.amaflag),
  115. record.unique_id,
  116. record.linked_id,
  117. record.user_field,
  118. record.peer,
  119. record.peer_account,
  120. user_defined_header,
  121. record.extra);
  122. }
  123. static int load_config(int reload)
  124. {
  125. const char *cat = NULL;
  126. struct ast_config *cfg;
  127. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  128. struct ast_variable *v;
  129. int newenablecel = CEL_AMI_ENABLED_DEFAULT;
  130. int new_cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
  131. cfg = ast_config_load(CONF_FILE, config_flags);
  132. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  133. return 0;
  134. }
  135. if (cfg == CONFIG_STATUS_FILEINVALID) {
  136. ast_log(LOG_WARNING, "Configuration file '%s' is invalid. CEL manager Module not activated.\n",
  137. CONF_FILE);
  138. enablecel = 0;
  139. return -1;
  140. } else if (!cfg) {
  141. ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
  142. enablecel = 0;
  143. return -1;
  144. }
  145. while ((cat = ast_category_browse(cfg, cat))) {
  146. if (strcasecmp(cat, "manager")) {
  147. continue;
  148. }
  149. for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
  150. if (!strcasecmp(v->name, "enabled")) {
  151. newenablecel = ast_true(v->value) ? 1 : 0;
  152. } else if (!strcasecmp(v->name, "show_user_defined")) {
  153. new_cel_show_user_def = ast_true(v->value) ? 1 : 0;
  154. } else {
  155. ast_log(LOG_NOTICE, "Unknown option '%s' specified "
  156. "for cel_manager.\n", v->name);
  157. }
  158. }
  159. }
  160. ast_config_destroy(cfg);
  161. cel_show_user_def = new_cel_show_user_def;
  162. if (enablecel && !newenablecel) {
  163. ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
  164. } else if (!enablecel && newenablecel) {
  165. if (ast_cel_backend_register(MANAGER_BACKEND_NAME, manager_log)) {
  166. ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
  167. }
  168. }
  169. enablecel = newenablecel;
  170. return 0;
  171. }
  172. static int unload_module(void)
  173. {
  174. ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
  175. return 0;
  176. }
  177. static int load_module(void)
  178. {
  179. if (load_config(0)) {
  180. return AST_MODULE_LOAD_DECLINE;
  181. }
  182. return AST_MODULE_LOAD_SUCCESS;
  183. }
  184. static int reload(void)
  185. {
  186. return load_config(1);
  187. }
  188. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
  189. .load = load_module,
  190. .unload = unload_module,
  191. .reload = reload,
  192. .load_pri = AST_MODPRI_CDR_DRIVER,
  193. );