cdr_manager.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /*! \file
  17. *
  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. #include <sys/types.h>
  27. #include <strings.h>
  28. #include <unistd.h>
  29. #include <time.h>
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/channel.h"
  33. #include "asterisk/cdr.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/logger.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/manager.h"
  38. #include "asterisk/config.h"
  39. #define DATE_FORMAT "%Y-%m-%d %T"
  40. #define CONF_FILE "cdr_manager.conf"
  41. static char *desc = "Asterisk Call Manager CDR Backend";
  42. static char *name = "cdr_manager";
  43. static int enablecdr = 0;
  44. static void loadconfigurationfile(void)
  45. {
  46. char *cat;
  47. struct ast_config *cfg;
  48. struct ast_variable *v;
  49. cfg = ast_config_load(CONF_FILE);
  50. if (!cfg) {
  51. /* Standard configuration */
  52. enablecdr = 0;
  53. return;
  54. }
  55. cat = ast_category_browse(cfg, NULL);
  56. while (cat) {
  57. if (!strcasecmp(cat, "general")) {
  58. v = ast_variable_browse(cfg, cat);
  59. while (v) {
  60. if (!strcasecmp(v->name, "enabled")) {
  61. enablecdr = ast_true(v->value);
  62. }
  63. v = v->next;
  64. }
  65. }
  66. /* Next category */
  67. cat = ast_category_browse(cfg, cat);
  68. }
  69. ast_config_destroy(cfg);
  70. }
  71. static int manager_log(struct ast_cdr *cdr)
  72. {
  73. time_t t;
  74. struct tm timeresult;
  75. char strStartTime[80] = "";
  76. char strAnswerTime[80] = "";
  77. char strEndTime[80] = "";
  78. if (!enablecdr)
  79. return 0;
  80. t = cdr->start.tv_sec;
  81. localtime_r(&t, &timeresult);
  82. strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
  83. if (cdr->answer.tv_sec) {
  84. t = cdr->answer.tv_sec;
  85. localtime_r(&t, &timeresult);
  86. strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
  87. }
  88. t = cdr->end.tv_sec;
  89. localtime_r(&t, &timeresult);
  90. strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
  91. manager_event(EVENT_FLAG_CALL, "Cdr",
  92. "AccountCode: %s\r\n"
  93. "Source: %s\r\n"
  94. "Destination: %s\r\n"
  95. "DestinationContext: %s\r\n"
  96. "CallerID: %s\r\n"
  97. "Channel: %s\r\n"
  98. "DestinationChannel: %s\r\n"
  99. "LastApplication: %s\r\n"
  100. "LastData: %s\r\n"
  101. "StartTime: %s\r\n"
  102. "AnswerTime: %s\r\n"
  103. "EndTime: %s\r\n"
  104. "Duration: %d\r\n"
  105. "BillableSeconds: %d\r\n"
  106. "Disposition: %s\r\n"
  107. "AMAFlags: %s\r\n"
  108. "UniqueID: %s\r\n"
  109. "UserField: %s\r\n",
  110. cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
  111. cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
  112. cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
  113. ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield);
  114. return 0;
  115. }
  116. char *description(void)
  117. {
  118. return desc;
  119. }
  120. int unload_module(void)
  121. {
  122. ast_cdr_unregister(name);
  123. return 0;
  124. }
  125. int load_module(void)
  126. {
  127. int res;
  128. /* Configuration file */
  129. loadconfigurationfile();
  130. res = ast_cdr_register(name, desc, manager_log);
  131. if (res) {
  132. ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CDR handling\n");
  133. }
  134. return res;
  135. }
  136. int reload(void)
  137. {
  138. loadconfigurationfile();
  139. return 0;
  140. }
  141. int usecount(void)
  142. {
  143. return 0;
  144. }
  145. char *key()
  146. {
  147. return ASTERISK_GPL_KEY;
  148. }