cel_manager.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #include "asterisk/channel.h"
  194. #include "asterisk/cel.h"
  195. #include "asterisk/module.h"
  196. #include "asterisk/logger.h"
  197. #include "asterisk/utils.h"
  198. #include "asterisk/manager.h"
  199. #include "asterisk/config.h"
  200. static const char DATE_FORMAT[] = "%Y-%m-%d %T";
  201. static const char CONF_FILE[] = "cel.conf";
  202. /*! \brief AMI CEL is off by default */
  203. #define CEL_AMI_ENABLED_DEFAULT 0
  204. static int enablecel;
  205. /*! \brief show_user_def is off by default */
  206. #define CEL_SHOW_USERDEF_DEFAULT 0
  207. #define MANAGER_BACKEND_NAME "Manager Event Logging"
  208. /*! TRUE if we should set the EventName header to USER_DEFINED on user events. */
  209. static unsigned char cel_show_user_def;
  210. static void manager_log(struct ast_event *event)
  211. {
  212. struct ast_tm timeresult;
  213. char start_time[80] = "";
  214. char user_defined_header[160];
  215. const char *event_name;
  216. struct ast_cel_event_record record = {
  217. .version = AST_CEL_EVENT_RECORD_VERSION,
  218. };
  219. RAII_VAR(char *, tenant_id, NULL, ast_free);
  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. if (!ast_strlen_zero(record.tenant_id)) {
  239. ast_asprintf(&tenant_id, "TenantID: %s\r\n", record.tenant_id);
  240. }
  241. manager_event(EVENT_FLAG_CALL, "CEL",
  242. "EventName: %s\r\n"
  243. "AccountCode: %s\r\n"
  244. "CallerIDnum: %s\r\n"
  245. "CallerIDname: %s\r\n"
  246. "CallerIDani: %s\r\n"
  247. "CallerIDrdnis: %s\r\n"
  248. "CallerIDdnid: %s\r\n"
  249. "Exten: %s\r\n"
  250. "Context: %s\r\n"
  251. "Channel: %s\r\n"
  252. "Application: %s\r\n"
  253. "AppData: %s\r\n"
  254. "EventTime: %s\r\n"
  255. "AMAFlags: %s\r\n"
  256. "UniqueID: %s\r\n"
  257. "LinkedID: %s\r\n"
  258. "%s"
  259. "Userfield: %s\r\n"
  260. "Peer: %s\r\n"
  261. "PeerAccount: %s\r\n"
  262. "%s"
  263. "Extra: %s\r\n",
  264. event_name,
  265. record.account_code,
  266. record.caller_id_num,
  267. record.caller_id_name,
  268. record.caller_id_ani,
  269. record.caller_id_rdnis,
  270. record.caller_id_dnid,
  271. record.extension,
  272. record.context,
  273. record.channel_name,
  274. record.application_name,
  275. record.application_data,
  276. start_time,
  277. ast_channel_amaflags2string(record.amaflag),
  278. record.unique_id,
  279. record.linked_id,
  280. !ast_strlen_zero(tenant_id) ? tenant_id : "",
  281. record.user_field,
  282. record.peer,
  283. record.peer_account,
  284. user_defined_header,
  285. record.extra);
  286. }
  287. static int load_config(int reload)
  288. {
  289. const char *cat = NULL;
  290. struct ast_config *cfg;
  291. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  292. struct ast_variable *v;
  293. int newenablecel = CEL_AMI_ENABLED_DEFAULT;
  294. int new_cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
  295. cfg = ast_config_load(CONF_FILE, config_flags);
  296. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  297. return 0;
  298. }
  299. if (cfg == CONFIG_STATUS_FILEINVALID) {
  300. ast_log(LOG_WARNING, "Configuration file '%s' is invalid. CEL manager Module not activated.\n",
  301. CONF_FILE);
  302. enablecel = 0;
  303. return -1;
  304. } else if (!cfg) {
  305. ast_log(LOG_WARNING, "Failed to load configuration file. CEL manager Module not activated.\n");
  306. enablecel = 0;
  307. return -1;
  308. }
  309. while ((cat = ast_category_browse(cfg, cat))) {
  310. if (strcasecmp(cat, "manager")) {
  311. continue;
  312. }
  313. for (v = ast_variable_browse(cfg, cat); v; v = v->next) {
  314. if (!strcasecmp(v->name, "enabled")) {
  315. newenablecel = ast_true(v->value) ? 1 : 0;
  316. } else if (!strcasecmp(v->name, "show_user_defined")) {
  317. new_cel_show_user_def = ast_true(v->value) ? 1 : 0;
  318. } else {
  319. ast_log(LOG_NOTICE, "Unknown option '%s' specified "
  320. "for cel_manager.\n", v->name);
  321. }
  322. }
  323. }
  324. ast_config_destroy(cfg);
  325. cel_show_user_def = new_cel_show_user_def;
  326. if (enablecel && !newenablecel) {
  327. ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
  328. } else if (!enablecel && newenablecel) {
  329. if (ast_cel_backend_register(MANAGER_BACKEND_NAME, manager_log)) {
  330. ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CEL handling\n");
  331. }
  332. }
  333. enablecel = newenablecel;
  334. return 0;
  335. }
  336. static int unload_module(void)
  337. {
  338. ast_cel_backend_unregister(MANAGER_BACKEND_NAME);
  339. return 0;
  340. }
  341. static int load_module(void)
  342. {
  343. if (load_config(0)) {
  344. return AST_MODULE_LOAD_DECLINE;
  345. }
  346. return AST_MODULE_LOAD_SUCCESS;
  347. }
  348. static int reload(void)
  349. {
  350. return load_config(1);
  351. }
  352. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CEL Backend",
  353. .support_level = AST_MODULE_SUPPORT_CORE,
  354. .load = load_module,
  355. .unload = unload_module,
  356. .reload = reload,
  357. .load_pri = AST_MODPRI_CDR_DRIVER,
  358. .requires = "cel",
  359. );