cdr_custom.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * Includes code and algorithms from the Zapata library.
  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 Custom Comma Separated Value CDR records.
  23. *
  24. * \arg See also \ref AstCDR
  25. *
  26. * Logs in LOG_DIR/cdr_custom
  27. * \ingroup cdr_drivers
  28. */
  29. #include <sys/types.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35. #include <time.h>
  36. #include "asterisk.h"
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. #include "asterisk/channel.h"
  39. #include "asterisk/cdr.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/config.h"
  42. #include "asterisk/pbx.h"
  43. #include "asterisk/logger.h"
  44. #include "asterisk/utils.h"
  45. #define CUSTOM_LOG_DIR "/cdr_custom"
  46. #define DATE_FORMAT "%Y-%m-%d %T"
  47. AST_MUTEX_DEFINE_STATIC(lock);
  48. static char *desc = "Customizable Comma Separated Values CDR Backend";
  49. static char *name = "cdr-custom";
  50. static FILE *mf = NULL;
  51. static char master[AST_CONFIG_MAX_PATH];
  52. static char format[1024]="";
  53. static int load_config(int reload)
  54. {
  55. struct ast_config *cfg;
  56. struct ast_variable *var;
  57. int res = -1;
  58. strcpy(format, "");
  59. strcpy(master, "");
  60. if((cfg = ast_config_load("cdr_custom.conf"))) {
  61. var = ast_variable_browse(cfg, "mappings");
  62. while(var) {
  63. ast_mutex_lock(&lock);
  64. if (!ast_strlen_zero(var->name) && !ast_strlen_zero(var->value)) {
  65. if (strlen(var->value) > (sizeof(format) - 2))
  66. ast_log(LOG_WARNING, "Format string too long, will be truncated, at line %d\n", var->lineno);
  67. strncpy(format, var->value, sizeof(format) - 2);
  68. strcat(format,"\n");
  69. snprintf(master, sizeof(master),"%s/%s/%s", ast_config_AST_LOG_DIR, name, var->name);
  70. ast_mutex_unlock(&lock);
  71. } else
  72. ast_log(LOG_NOTICE, "Mapping must have both filename and format at line %d\n", var->lineno);
  73. if (var->next)
  74. ast_log(LOG_NOTICE, "Sorry, only one mapping is supported at this time, mapping '%s' will be ignored at line %d.\n", var->next->name, var->next->lineno);
  75. var = var->next;
  76. }
  77. ast_config_destroy(cfg);
  78. res = 0;
  79. } else {
  80. if (reload)
  81. ast_log(LOG_WARNING, "Failed to reload configuration file.\n");
  82. else
  83. ast_log(LOG_WARNING, "Failed to load configuration file. Module not activated.\n");
  84. }
  85. return res;
  86. }
  87. static int custom_log(struct ast_cdr *cdr)
  88. {
  89. /* Make sure we have a big enough buf */
  90. char buf[2048];
  91. struct ast_channel dummy;
  92. /* Abort if no master file is specified */
  93. if (ast_strlen_zero(master))
  94. return 0;
  95. memset(buf, 0 , sizeof(buf));
  96. /* Quite possibly the first use of a static struct ast_channel, we need it so the var funcs will work */
  97. memset(&dummy, 0, sizeof(dummy));
  98. dummy.cdr = cdr;
  99. pbx_substitute_variables_helper(&dummy, format, buf, sizeof(buf) - 1);
  100. /* because of the absolutely unconditional need for the
  101. highest reliability possible in writing billing records,
  102. we open write and close the log file each time */
  103. mf = fopen(master, "a");
  104. if (!mf) {
  105. ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", master, strerror(errno));
  106. }
  107. if (mf) {
  108. fputs(buf, mf);
  109. fflush(mf); /* be particularly anal here */
  110. fclose(mf);
  111. mf = NULL;
  112. }
  113. return 0;
  114. }
  115. char *description(void)
  116. {
  117. return desc;
  118. }
  119. int unload_module(void)
  120. {
  121. if (mf)
  122. fclose(mf);
  123. ast_cdr_unregister(name);
  124. return 0;
  125. }
  126. int load_module(void)
  127. {
  128. int res = 0;
  129. if (!load_config(0)) {
  130. res = ast_cdr_register(name, desc, custom_log);
  131. if (res)
  132. ast_log(LOG_ERROR, "Unable to register custom CDR handling\n");
  133. if (mf)
  134. fclose(mf);
  135. }
  136. return res;
  137. }
  138. int reload(void)
  139. {
  140. return load_config(1);
  141. }
  142. int usecount(void)
  143. {
  144. return 0;
  145. }
  146. char *key()
  147. {
  148. return ASTERISK_GPL_KEY;
  149. }