cdr_manager.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2005
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*!
  17. * \file
  18. * \brief Asterisk Call Manager CDR records.
  19. *
  20. * See also
  21. * \arg \ref AstCDR
  22. * \arg \ref AstAMI
  23. * \arg \ref Config_ami
  24. * \ingroup cdr_drivers
  25. */
  26. /*! \li \ref cdr_manager.c uses the configuration file \ref cdr_manager.conf
  27. * \addtogroup configuration_file Configuration Files
  28. */
  29. /*!
  30. * \page cdr_manager.conf cdr_manager.conf
  31. * \verbinclude cdr_manager.conf.sample
  32. */
  33. /*** MODULEINFO
  34. <support_level>core</support_level>
  35. ***/
  36. /*** DOCUMENTATION
  37. <managerEvent language="en_US" name="Cdr">
  38. <managerEventInstance class="EVENT_FLAG_CDR">
  39. <synopsis>Raised when a CDR is generated.</synopsis>
  40. <syntax>
  41. <parameter name="AccountCode">
  42. <para>The account code of the Party A channel.</para>
  43. </parameter>
  44. <parameter name="Source">
  45. <para>The Caller ID number associated with the Party A in the CDR.</para>
  46. </parameter>
  47. <parameter name="Destination">
  48. <para>The dialplan extension the Party A was executing.</para>
  49. </parameter>
  50. <parameter name="DestinationContext">
  51. <para>The dialplan context the Party A was executing.</para>
  52. </parameter>
  53. <parameter name="CallerID">
  54. <para>The Caller ID name associated with the Party A in the CDR.</para>
  55. </parameter>
  56. <parameter name="Channel">
  57. <para>The channel name of the Party A.</para>
  58. </parameter>
  59. <parameter name="DestinationChannel">
  60. <para>The channel name of the Party B.</para>
  61. </parameter>
  62. <parameter name="LastApplication">
  63. <para>The last dialplan application the Party A executed.</para>
  64. </parameter>
  65. <parameter name="LastData">
  66. <para>
  67. The parameters passed to the last dialplan application the
  68. Party A executed.
  69. </para>
  70. </parameter>
  71. <parameter name="StartTime">
  72. <para>The time the CDR was created.</para>
  73. </parameter>
  74. <parameter name="AnswerTime">
  75. <para>
  76. The earliest of either the time when Party A answered, or
  77. the start time of this CDR.
  78. </para>
  79. </parameter>
  80. <parameter name="EndTime">
  81. <para>
  82. The time when the CDR was finished. This occurs when the
  83. Party A hangs up or when the bridge between Party A and
  84. Party B is broken.
  85. </para>
  86. </parameter>
  87. <parameter name="Duration">
  88. <para>The time, in seconds, of <replaceable>EndTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
  89. </parameter>
  90. <parameter name="BillableSeconds">
  91. <para>The time, in seconds, of <replaceable>AnswerTime</replaceable> - <replaceable>StartTime</replaceable>.</para>
  92. </parameter>
  93. <parameter name="Disposition">
  94. <para>The final known disposition of the CDR.</para>
  95. <enumlist>
  96. <enum name="NO ANSWER">
  97. <para>The channel was not answered. This is the default disposition.</para>
  98. </enum>
  99. <enum name="FAILED">
  100. <para>The channel attempted to dial but the call failed.</para>
  101. <note>
  102. <para>The congestion setting in <filename>cdr.conf</filename> can result
  103. in the <literal>AST_CAUSE_CONGESTION</literal> hang up cause or the
  104. <literal>CONGESTION</literal> dial status to map to this disposition.
  105. </para>
  106. </note>
  107. </enum>
  108. <enum name="BUSY">
  109. <para>The channel attempted to dial but the remote party was busy.</para>
  110. </enum>
  111. <enum name="ANSWERED">
  112. <para>The channel was answered. The hang up cause will no longer
  113. impact the disposition of the CDR.</para>
  114. </enum>
  115. <enum name="CONGESTION">
  116. <para>The channel attempted to dial but the remote party was congested.</para>
  117. </enum>
  118. </enumlist>
  119. </parameter>
  120. <parameter name="AMAFlags">
  121. <para>A flag that informs a billing system how to treat the CDR.</para>
  122. <enumlist>
  123. <enum name="OMIT">
  124. <para>This CDR should be ignored.</para>
  125. </enum>
  126. <enum name="BILLING">
  127. <para>This CDR contains valid billing data.</para>
  128. </enum>
  129. <enum name="DOCUMENTATION">
  130. <para>This CDR is for documentation purposes.</para>
  131. </enum>
  132. </enumlist>
  133. </parameter>
  134. <parameter name="UniqueID">
  135. <para>A unique identifier for the Party A channel.</para>
  136. </parameter>
  137. <parameter name="UserField">
  138. <para>
  139. A user defined field set on the channels. If set on both the Party A
  140. and Party B channel, the userfields of both are concatenated and
  141. separated by a <literal>;</literal>.
  142. </para>
  143. </parameter>
  144. </syntax>
  145. <description>
  146. <para>
  147. The <replaceable>Cdr</replaceable> event is only raised when the
  148. <filename>cdr_manager</filename> backend is loaded and registered with
  149. the CDR engine.
  150. </para>
  151. <note>
  152. <para>
  153. This event can contain additional fields depending on the configuration
  154. provided by <filename>cdr_manager.conf</filename>.
  155. </para>
  156. </note>
  157. </description>
  158. </managerEventInstance>
  159. </managerEvent>
  160. ***/
  161. #include "asterisk.h"
  162. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  163. #include <time.h>
  164. #include "asterisk/channel.h"
  165. #include "asterisk/cdr.h"
  166. #include "asterisk/module.h"
  167. #include "asterisk/utils.h"
  168. #include "asterisk/manager.h"
  169. #include "asterisk/config.h"
  170. #include "asterisk/pbx.h"
  171. #define DATE_FORMAT "%Y-%m-%d %T"
  172. #define CONF_FILE "cdr_manager.conf"
  173. #define CUSTOM_FIELDS_BUF_SIZE 1024
  174. static const char name[] = "cdr_manager";
  175. static int enablecdr = 0;
  176. static struct ast_str *customfields;
  177. AST_RWLOCK_DEFINE_STATIC(customfields_lock);
  178. static int manager_log(struct ast_cdr *cdr);
  179. static int load_config(int reload)
  180. {
  181. char *cat = NULL;
  182. struct ast_config *cfg;
  183. struct ast_variable *v;
  184. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  185. int newenablecdr = 0;
  186. cfg = ast_config_load(CONF_FILE, config_flags);
  187. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  188. return 0;
  189. }
  190. if (cfg == CONFIG_STATUS_FILEINVALID) {
  191. ast_log(LOG_ERROR, "Config file '%s' could not be parsed\n", CONF_FILE);
  192. return -1;
  193. }
  194. if (!cfg) {
  195. /* Standard configuration */
  196. ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
  197. if (enablecdr) {
  198. ast_cdr_backend_suspend(name);
  199. }
  200. enablecdr = 0;
  201. return -1;
  202. }
  203. if (reload) {
  204. ast_rwlock_wrlock(&customfields_lock);
  205. }
  206. if (reload && customfields) {
  207. ast_free(customfields);
  208. customfields = NULL;
  209. }
  210. while ( (cat = ast_category_browse(cfg, cat)) ) {
  211. if (!strcasecmp(cat, "general")) {
  212. v = ast_variable_browse(cfg, cat);
  213. while (v) {
  214. if (!strcasecmp(v->name, "enabled"))
  215. newenablecdr = ast_true(v->value);
  216. v = v->next;
  217. }
  218. } else if (!strcasecmp(cat, "mappings")) {
  219. customfields = ast_str_create(CUSTOM_FIELDS_BUF_SIZE);
  220. v = ast_variable_browse(cfg, cat);
  221. while (v) {
  222. if (customfields && !ast_strlen_zero(v->name) && !ast_strlen_zero(v->value)) {
  223. if ((ast_str_strlen(customfields) + strlen(v->value) + strlen(v->name) + 14) < ast_str_size(customfields)) {
  224. ast_str_append(&customfields, -1, "%s: ${CDR(%s)}\r\n", v->value, v->name);
  225. ast_log(LOG_NOTICE, "Added mapping %s: ${CDR(%s)}\n", v->value, v->name);
  226. } else {
  227. ast_log(LOG_WARNING, "No more buffer space to add other custom fields\n");
  228. break;
  229. }
  230. }
  231. v = v->next;
  232. }
  233. }
  234. }
  235. if (reload) {
  236. ast_rwlock_unlock(&customfields_lock);
  237. }
  238. ast_config_destroy(cfg);
  239. if (!newenablecdr) {
  240. ast_cdr_backend_suspend(name);
  241. } else if (newenablecdr) {
  242. ast_cdr_backend_unsuspend(name);
  243. }
  244. enablecdr = newenablecdr;
  245. return 0;
  246. }
  247. static int manager_log(struct ast_cdr *cdr)
  248. {
  249. struct ast_tm timeresult;
  250. char strStartTime[80] = "";
  251. char strAnswerTime[80] = "";
  252. char strEndTime[80] = "";
  253. char buf[CUSTOM_FIELDS_BUF_SIZE];
  254. if (!enablecdr)
  255. return 0;
  256. ast_localtime(&cdr->start, &timeresult, NULL);
  257. ast_strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
  258. if (cdr->answer.tv_sec) {
  259. ast_localtime(&cdr->answer, &timeresult, NULL);
  260. ast_strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
  261. }
  262. ast_localtime(&cdr->end, &timeresult, NULL);
  263. ast_strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
  264. buf[0] = '\0';
  265. ast_rwlock_rdlock(&customfields_lock);
  266. if (customfields && ast_str_strlen(customfields)) {
  267. struct ast_channel *dummy = ast_dummy_channel_alloc();
  268. if (!dummy) {
  269. ast_log(LOG_ERROR, "Unable to allocate channel for variable substitution.\n");
  270. return 0;
  271. }
  272. ast_channel_cdr_set(dummy, ast_cdr_dup(cdr));
  273. pbx_substitute_variables_helper(dummy, ast_str_buffer(customfields), buf, sizeof(buf) - 1);
  274. ast_channel_unref(dummy);
  275. }
  276. ast_rwlock_unlock(&customfields_lock);
  277. manager_event(EVENT_FLAG_CDR, "Cdr",
  278. "AccountCode: %s\r\n"
  279. "Source: %s\r\n"
  280. "Destination: %s\r\n"
  281. "DestinationContext: %s\r\n"
  282. "CallerID: %s\r\n"
  283. "Channel: %s\r\n"
  284. "DestinationChannel: %s\r\n"
  285. "LastApplication: %s\r\n"
  286. "LastData: %s\r\n"
  287. "StartTime: %s\r\n"
  288. "AnswerTime: %s\r\n"
  289. "EndTime: %s\r\n"
  290. "Duration: %ld\r\n"
  291. "BillableSeconds: %ld\r\n"
  292. "Disposition: %s\r\n"
  293. "AMAFlags: %s\r\n"
  294. "UniqueID: %s\r\n"
  295. "UserField: %s\r\n"
  296. "%s",
  297. cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
  298. cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
  299. cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
  300. ast_channel_amaflags2string(cdr->amaflags), cdr->uniqueid, cdr->userfield,buf);
  301. return 0;
  302. }
  303. static int unload_module(void)
  304. {
  305. if (ast_cdr_unregister(name)) {
  306. return -1;
  307. }
  308. if (customfields)
  309. ast_free(customfields);
  310. return 0;
  311. }
  312. static int load_module(void)
  313. {
  314. if (ast_cdr_register(name, "Asterisk Manager Interface CDR Backend", manager_log)) {
  315. return AST_MODULE_LOAD_DECLINE;
  316. }
  317. if (load_config(0)) {
  318. ast_cdr_unregister(name);
  319. return AST_MODULE_LOAD_DECLINE;
  320. }
  321. return AST_MODULE_LOAD_SUCCESS;
  322. }
  323. static int reload(void)
  324. {
  325. return load_config(1);
  326. }
  327. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface CDR Backend",
  328. .support_level = AST_MODULE_SUPPORT_CORE,
  329. .load = load_module,
  330. .unload = unload_module,
  331. .reload = reload,
  332. .load_pri = AST_MODPRI_CDR_DRIVER,
  333. );