cdr_manager.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Asterisk Call Manager CDR records.
  5. *
  6. * This program is free software, distributed under the terms of
  7. * the GNU General Public License.
  8. *
  9. */
  10. #include <sys/types.h>
  11. #include <asterisk/channel.h>
  12. #include <asterisk/cdr.h>
  13. #include <asterisk/module.h>
  14. #include <asterisk/logger.h>
  15. #include <asterisk/utils.h>
  16. #include <asterisk/manager.h>
  17. #include <asterisk/config.h>
  18. #include "../asterisk.h"
  19. #include "../astconf.h"
  20. #include <strings.h>
  21. #include <unistd.h>
  22. #include <time.h>
  23. #define DATE_FORMAT "%Y-%m-%d %T"
  24. #define CONF_FILE "cdr_manager.conf"
  25. static char *desc = "Asterisk Call Manager CDR Backend";
  26. static char *name = "cdr_as";
  27. static int enablecdr = 0;
  28. static void loadconfigurationfile(void)
  29. {
  30. char *cat;
  31. struct ast_config *cfg;
  32. struct ast_variable *v;
  33. cfg = ast_load(CONF_FILE);
  34. if (!cfg) {
  35. /* Standard configuration */
  36. enablecdr = 0;
  37. return;
  38. }
  39. cat = ast_category_browse(cfg, NULL);
  40. while (cat) {
  41. if (!strcasecmp(cat, "general")) {
  42. v = ast_variable_browse(cfg, cat);
  43. while (v) {
  44. if (!strcasecmp(v->name, "enabled")) {
  45. enablecdr = ast_true(v->value);
  46. }
  47. v = v->next;
  48. }
  49. }
  50. /* Next category */
  51. cat = ast_category_browse(cfg, cat);
  52. }
  53. ast_destroy(cfg);
  54. }
  55. static int manager_log(struct ast_cdr *cdr)
  56. {
  57. time_t t;
  58. struct tm timeresult;
  59. char strStartTime[80] = "";
  60. char strAnswerTime[80] = "";
  61. char strEndTime[80] = "";
  62. if (!enablecdr)
  63. return 0;
  64. t = cdr->start.tv_sec;
  65. localtime_r(&t, &timeresult);
  66. strftime(strStartTime, sizeof(strStartTime), DATE_FORMAT, &timeresult);
  67. if (cdr->answer.tv_sec) {
  68. t = cdr->answer.tv_sec;
  69. localtime_r(&t, &timeresult);
  70. strftime(strAnswerTime, sizeof(strAnswerTime), DATE_FORMAT, &timeresult);
  71. }
  72. t = cdr->end.tv_sec;
  73. localtime_r(&t, &timeresult);
  74. strftime(strEndTime, sizeof(strEndTime), DATE_FORMAT, &timeresult);
  75. manager_event(EVENT_FLAG_CALL, "Cdr",
  76. "AccountCode: %s\r\n"
  77. "Source: %s\r\n"
  78. "Destination: %s\r\n"
  79. "DestinationContext: %s\r\n"
  80. "CallerID: %s\r\n"
  81. "Channel: %s\r\n"
  82. "DestinationChannel: %s\r\n"
  83. "LastApplication: %s\r\n"
  84. "LastData: %s\r\n"
  85. "StartTime: %s\r\n"
  86. "AnswerTime: %s\r\n"
  87. "EndTime: %s\r\n"
  88. "Duration: %d\r\n"
  89. "BillableSeconds: %d\r\n"
  90. "Disposition: %s\r\n"
  91. "AMAFlags: %s\r\n"
  92. "UniqueID: %s\r\n"
  93. "UserField: %s\r\n",
  94. cdr->accountcode, cdr->src, cdr->dst, cdr->dcontext, cdr->clid, cdr->channel,
  95. cdr->dstchannel, cdr->lastapp, cdr->lastdata, strStartTime, strAnswerTime, strEndTime,
  96. cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition),
  97. ast_cdr_flags2str(cdr->amaflags), cdr->uniqueid, cdr->userfield);
  98. return 0;
  99. }
  100. char *description(void)
  101. {
  102. return desc;
  103. }
  104. int unload_module(void)
  105. {
  106. ast_cdr_unregister(name);
  107. return 0;
  108. }
  109. int load_module(void)
  110. {
  111. int res;
  112. /* Configuration file */
  113. loadconfigurationfile();
  114. res = ast_cdr_register(name, desc, manager_log);
  115. if (res) {
  116. ast_log(LOG_ERROR, "Unable to register Asterisk Call Manager CDR handling\n");
  117. }
  118. return res;
  119. }
  120. int reload(void)
  121. {
  122. loadconfigurationfile();
  123. return 0;
  124. }
  125. int usecount(void)
  126. {
  127. return 0;
  128. }
  129. char *key()
  130. {
  131. return ASTERISK_GPL_KEY;
  132. }