cel_manager.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. /*** DOCUMENTATION
  34. <managerEvent language="en_US" name="CEL">
  35. <managerEventInstance class="EVENT_FLAG_CEL">
  36. <synopsis>Raised when a Channel Event Log is generated for a channel.</synopsis>
  37. <syntax>
  38. <parameter name="EventName">
  39. <para>
  40. The name of the CEL event being raised. This can include
  41. both the system defined CEL events, as well as user defined
  42. events.
  43. </para>
  44. <note>
  45. <para>All events listed here may not be raised, depending
  46. on the configuration in <filename>cel.conf</filename>.</para>
  47. </note>
  48. <enumlist>
  49. <enum name="CHAN_START">
  50. <para>A channel was created.</para>
  51. </enum>
  52. <enum name="CHAN_END">
  53. <para>A channel was terminated.</para>
  54. </enum>
  55. <enum name="ANSWER">
  56. <para>A channel answered.</para>
  57. </enum>
  58. <enum name="HANGUP">
  59. <para>A channel was hung up.</para>
  60. </enum>
  61. <enum name="BRIDGE_ENTER">
  62. <para>A channel entered a bridge.</para>
  63. </enum>
  64. <enum name="BRIDGE_EXIT">
  65. <para>A channel left a bridge.</para>
  66. </enum>
  67. <enum name="APP_START">
  68. <para>A channel entered into a tracked application.</para>
  69. </enum>
  70. <enum name="APP_END">
  71. <para>A channel left a tracked application.</para>
  72. </enum>
  73. <enum name="PARK_START">
  74. <para>A channel was parked.</para>
  75. </enum>
  76. <enum name="PARK_END">
  77. <para>A channel was unparked.</para>
  78. </enum>
  79. <enum name="BLINDTRANSFER">
  80. <para>A channel initiated a blind transfer.</para>
  81. </enum>
  82. <enum name="ATTENDEDTRANSFER">
  83. <para>A channel initiated an attended transfer.</para>
  84. </enum>
  85. <enum name="PICKUP">
  86. <para>A channel initated a call pickup.</para>
  87. </enum>
  88. <enum name="FORWARD">
  89. <para>A channel is being forwarded to another destination.</para>
  90. </enum>
  91. <enum name="LINKEDID_END">
  92. <para>The linked ID associated with this channel is being retired.</para>
  93. </enum>
  94. <enum name="LOCAL_OPTIMIZE">
  95. <para>A Local channel optimization has occurred.</para>
  96. </enum>
  97. <enum name="USER_DEFINED">
  98. <para>A user defined type.</para>
  99. <note>
  100. <para>
  101. This event is only present if <literal>show_user_defined</literal>
  102. in <filename>cel.conf</filename> is <literal>True</literal>. Otherwise,
  103. the user defined event will be placed directly in the
  104. <replaceable>EventName</replaceable> field.
  105. </para>
  106. </note>
  107. </enum>
  108. </enumlist>
  109. </parameter>
  110. <parameter name="AccountCode">
  111. <para>The channel's account code.</para>
  112. </parameter>
  113. <parameter name="CallerIDnum">
  114. <para>The Caller ID number.</para>
  115. </parameter>
  116. <parameter name="CallerIDname">
  117. <para>The Caller ID name.</para>
  118. </parameter>
  119. <parameter name="CallerIDani">
  120. <para>The Caller ID Automatic Number Identification.</para>
  121. </parameter>
  122. <parameter name="CallerIDrdnis">
  123. <para>The Caller ID Redirected Dialed Number Identification Service.</para>
  124. </parameter>
  125. <parameter name="CallerIDdnid">
  126. <para>The Caller ID Dialed Number Identifier.</para>
  127. </parameter>
  128. <parameter name="Exten">
  129. <para>The dialplan extension the channel is currently executing in.</para>
  130. </parameter>
  131. <parameter name="Context">
  132. <para>The dialplan context the channel is currently executing in.</para>
  133. </parameter>
  134. <parameter name="Application">
  135. <para>The dialplan application the channel is currently executing.</para>
  136. </parameter>
  137. <parameter name="AppData">
  138. <para>The arguments passed to the dialplan <replaceable>Application</replaceable>.</para>
  139. </parameter>
  140. <parameter name="EventTime">
  141. <para>The time the CEL event occurred.</para>
  142. </parameter>
  143. <parameter name="AMAFlags">
  144. <para>A flag that informs a billing system how to treat the CEL.</para>
  145. <enumlist>
  146. <enum name="OMIT">
  147. <para>This event should be ignored.</para>
  148. </enum>
  149. <enum name="BILLING">
  150. <para>This event contains valid billing data.</para>
  151. </enum>
  152. <enum name="DOCUMENTATION">
  153. <para>This event is for documentation purposes.</para>
  154. </enum>
  155. </enumlist>
  156. </parameter>
  157. <parameter name="UniqueID">
  158. <para>The unique ID of the channel.</para>
  159. </parameter>
  160. <parameter name="LinkedID">
  161. <para>The linked ID of the channel, which ties this event to other related channel's events.</para>
  162. </parameter>
  163. <parameter name="UserField">
  164. <para>
  165. A user defined field set on a channel, containing arbitrary
  166. application specific data.
  167. </para>
  168. </parameter>
  169. <parameter name="Peer">
  170. <para>
  171. If this channel is in a bridge, the channel that it is in
  172. a bridge with.
  173. </para>
  174. </parameter>
  175. <parameter name="PeerAccount">
  176. <para>
  177. If this channel is in a bridge, the accountcode of the
  178. channel it is in a bridge with.
  179. </para>
  180. </parameter>
  181. <parameter name="Extra">
  182. <para>
  183. Some events will have event specific data that accompanies the CEL record.
  184. This extra data is JSON encoded, and is dependent on the event in
  185. question.
  186. </para>
  187. </parameter>
  188. </syntax>
  189. </managerEventInstance>
  190. </managerEvent>
  191. ***/
  192. #include "asterisk.h"
  193. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  194. #include "asterisk/channel.h"
  195. #include "asterisk/cel.h"
  196. #include "asterisk/module.h"
  197. #include "asterisk/logger.h"
  198. #include "asterisk/utils.h"
  199. #include "asterisk/manager.h"
  200. #include "asterisk/config.h"
  201. static const char DATE_FORMAT[] = "%Y-%m-%d %T";
  202. static const char CONF_FILE[] = "cel.conf";
  203. /*! \brief AMI CEL is off by default */
  204. #define CEL_AMI_ENABLED_DEFAULT 0
  205. static int enablecel;
  206. /*! \brief show_user_def is off by default */
  207. #define CEL_SHOW_USERDEF_DEFAULT 0
  208. #define MANAGER_BACKEND_NAME "Manager Event Logging"
  209. /*! TRUE if we should set the EventName header to USER_DEFINED on user events. */
  210. static unsigned char cel_show_user_def;
  211. static void manager_log(struct ast_event *event)
  212. {
  213. struct ast_tm timeresult;
  214. char start_time[80] = "";
  215. char user_defined_header[160];
  216. const char *event_name;
  217. struct ast_cel_event_record record = {
  218. .version = AST_CEL_EVENT_RECORD_VERSION,
  219. };
  220. if (!enablecel) {
  221. return;
  222. }
  223. if (ast_cel_fill_record(event, &record)) {
  224. return;
  225. }
  226. ast_localtime(&record.event_time, &timeresult, NULL);
  227. ast_strftime(start_time, sizeof(start_time), DATE_FORMAT, &timeresult);
  228. event_name = record.event_name;
  229. user_defined_header[0] = '\0';
  230. if (record.event_type == AST_CEL_USER_DEFINED) {
  231. if (cel_show_user_def) {
  232. snprintf(user_defined_header, sizeof(user_defined_header),
  233. "UserDefType: %s\r\n", record.user_defined_name);
  234. } else {
  235. event_name = record.user_defined_name;
  236. }
  237. }
  238. manager_event(EVENT_FLAG_CALL, "CEL",
  239. "EventName: %s\r\n"
  240. "AccountCode: %s\r\n"
  241. "CallerIDnum: %s\r\n"
  242. "CallerIDname: %s\r\n"
  243. "CallerIDani: %s\r\n"
  244. "CallerIDrdnis: %s\r\n"
  245. "CallerIDdnid: %s\r\n"
  246. "Exten: %s\r\n"
  247. "Context: %s\r\n"
  248. "Channel: %s\r\n"
  249. "Application: %s\r\n"
  250. "AppData: %s\r\n"
  251. "EventTime: %s\r\n"
  252. "AMAFlags: %s\r\n"
  253. "UniqueID: %s\r\n"
  254. "LinkedID: %s\r\n"
  255. "Userfield: %s\r\n"
  256. "Peer: %s\r\n"
  257. "PeerAccount: %s\r\n"
  258. "%s"
  259. "Extra: %s\r\n",
  260. event_name,
  261. record.account_code,
  262. record.caller_id_num,
  263. record.caller_id_name,
  264. record.caller_id_ani,
  265. record.caller_id_rdnis,
  266. record.caller_id_dnid,
  267. record.extension,
  268. record.context,
  269. record.channel_name,
  270. record.application_name,
  271. record.application_data,
  272. start_time,
  273. ast_channel_amaflags2string(record.amaflag),
  274. record.unique_id,
  275. record.linked_id,
  276. record.user_field,
  277. record.peer,
  278. record.peer_account,
  279. user_defined_header,
  280. record.extra);
  281. }
  282. static int load_config(int reload)
  283. {
  284. const char *cat = NULL;
  285. struct ast_config *cfg;
  286. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  287. struct ast_variable *v;
  288. int newenablecel = CEL_AMI_ENABLED_DEFAULT;
  289. int new_cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
  290. cfg = ast_config_load(CONF_FILE, config_flags);
  291. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  292. return 0;
  293. }
  294. if (cfg == CONFIG_STATUS_FILEINVALID) {
  295. ast_log(LOG_WARNING, "Configuration file '%s' is invalid. CEL manager Module not activated.\n",
  296. CONF_FILE);
  297. enablecel = 0;
  298. return -1;
  299. } else if (!cfg) {
  300. ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
  301. enablecel = 0;
  302. return -1;
  303. }
  304. while ((cat = ast_category_browse(cfg, cat))) {
  305. if (strcasecmp(cat, "manager")) {
  306. continue;
  307. }
  308. for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
  309. if (!strcasecmp(v->name, "enabled")) {
  310. newenablecel = ast_true(v->value) ? 1 : 0;
  311. } else if (!strcasecmp(v->name, "show_user_defined")) {
  312. new_cel_show_user_def = ast_true(v->value) ? 1 : 0;
  313. } else {
  314. ast_log(LOG_NOTICE, "Unknown option '%s' specified "
  315. "for cel_manager.\n", v->name);
  316. }
  317. }
  318. }
  319. ast_config_destroy(cfg);
  320. cel_show_user_def = new_cel_show_user_def;
  321. if (enablecel && !newenablecel) {
  322. ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
  323. } else if (!enablecel && newenablecel) {
  324. if (ast_cel_backend_register(MANAGER_BACKEND_NAME, manager_log)) {
  325. ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
  326. }
  327. }
  328. enablecel = newenablecel;
  329. return 0;
  330. }
  331. static int unload_module(void)
  332. {
  333. ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
  334. return 0;
  335. }
  336. static int load_module(void)
  337. {
  338. if (load_config(0)) {
  339. return AST_MODULE_LOAD_DECLINE;
  340. }
  341. return AST_MODULE_LOAD_SUCCESS;
  342. }
  343. static int reload(void)
  344. {
  345. return load_config(1);
  346. }
  347. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
  348. .support_level = AST_MODULE_SUPPORT_CORE,
  349. .load = load_module,
  350. .unload = unload_module,
  351. .reload = reload,
  352. .load_pri = AST_MODPRI_CDR_DRIVER,
  353. );