cdr_csv.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 Comma Separated Value CDR records.
  23. *
  24. * \arg See also \ref AstCDR
  25. * \ingroup cdr_drivers
  26. */
  27. #include <sys/types.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <time.h>
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include "asterisk/channel.h"
  37. #include "asterisk/cdr.h"
  38. #include "asterisk/module.h"
  39. #include "asterisk/logger.h"
  40. #include "asterisk/utils.h"
  41. #define CSV_LOG_DIR "/cdr-csv"
  42. #define CSV_MASTER "/Master.csv"
  43. #define DATE_FORMAT "%Y-%m-%d %T"
  44. /* #define CSV_LOGUNIQUEID 1 */
  45. /* #define CSV_LOGUSERFIELD 1 */
  46. /*----------------------------------------------------
  47. The values are as follows:
  48. "accountcode", accountcode is the account name of detail records, Master.csv contains all records *
  49. Detail records are configured on a channel basis, IAX and SIP are determined by user *
  50. Zap is determined by channel in zaptel.conf
  51. "source",
  52. "destination",
  53. "destination context",
  54. "callerid",
  55. "channel",
  56. "destination channel", (if applicable)
  57. "last application", Last application run on the channel
  58. "last app argument", argument to the last channel
  59. "start time",
  60. "answer time",
  61. "end time",
  62. duration, Duration is the whole length that the entire call lasted. ie. call rx'd to hangup
  63. "end time" minus "start time"
  64. billable seconds, the duration that a call was up after other end answered which will be <= to duration
  65. "end time" minus "answer time"
  66. "disposition", ANSWERED, NO ANSWER, BUSY
  67. "amaflags", DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode.
  68. "uniqueid", unique call identifier
  69. "userfield" user field set via SetCDRUserField
  70. ----------------------------------------------------------*/
  71. static char *desc = "Comma Separated Values CDR Backend";
  72. static char *name = "csv";
  73. static FILE *mf = NULL;
  74. static int append_string(char *buf, char *s, size_t bufsize)
  75. {
  76. int pos = strlen(buf);
  77. int spos = 0;
  78. int error = 0;
  79. if (pos >= bufsize - 4)
  80. return -1;
  81. buf[pos++] = '\"';
  82. error = -1;
  83. while(pos < bufsize - 3) {
  84. if (!s[spos]) {
  85. error = 0;
  86. break;
  87. }
  88. if (s[spos] == '\"')
  89. buf[pos++] = '\"';
  90. buf[pos++] = s[spos];
  91. spos++;
  92. }
  93. buf[pos++] = '\"';
  94. buf[pos++] = ',';
  95. buf[pos++] = '\0';
  96. return error;
  97. }
  98. static int append_int(char *buf, int s, size_t bufsize)
  99. {
  100. char tmp[32];
  101. int pos = strlen(buf);
  102. snprintf(tmp, sizeof(tmp), "%d", s);
  103. if (pos + strlen(tmp) > bufsize - 3)
  104. return -1;
  105. strncat(buf, tmp, bufsize - strlen(buf) - 1);
  106. pos = strlen(buf);
  107. buf[pos++] = ',';
  108. buf[pos++] = '\0';
  109. return 0;
  110. }
  111. static int append_date(char *buf, struct timeval tv, size_t bufsize)
  112. {
  113. char tmp[80] = "";
  114. struct tm tm;
  115. time_t t;
  116. t = tv.tv_sec;
  117. if (strlen(buf) > bufsize - 3)
  118. return -1;
  119. if (ast_tvzero(tv)) {
  120. strncat(buf, ",", bufsize - strlen(buf) - 1);
  121. return 0;
  122. }
  123. localtime_r(&t,&tm);
  124. strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
  125. return append_string(buf, tmp, bufsize);
  126. }
  127. static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
  128. {
  129. buf[0] = '\0';
  130. /* Account code */
  131. append_string(buf, cdr->accountcode, bufsize);
  132. /* Source */
  133. append_string(buf, cdr->src, bufsize);
  134. /* Destination */
  135. append_string(buf, cdr->dst, bufsize);
  136. /* Destination context */
  137. append_string(buf, cdr->dcontext, bufsize);
  138. /* Caller*ID */
  139. append_string(buf, cdr->clid, bufsize);
  140. /* Channel */
  141. append_string(buf, cdr->channel, bufsize);
  142. /* Destination Channel */
  143. append_string(buf, cdr->dstchannel, bufsize);
  144. /* Last Application */
  145. append_string(buf, cdr->lastapp, bufsize);
  146. /* Last Data */
  147. append_string(buf, cdr->lastdata, bufsize);
  148. /* Start Time */
  149. append_date(buf, cdr->start, bufsize);
  150. /* Answer Time */
  151. append_date(buf, cdr->answer, bufsize);
  152. /* End Time */
  153. append_date(buf, cdr->end, bufsize);
  154. /* Duration */
  155. append_int(buf, cdr->duration, bufsize);
  156. /* Billable seconds */
  157. append_int(buf, cdr->billsec, bufsize);
  158. /* Disposition */
  159. append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
  160. /* AMA Flags */
  161. append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
  162. #ifdef CSV_LOGUNIQUEID
  163. /* Unique ID */
  164. append_string(buf, cdr->uniqueid, bufsize);
  165. #endif
  166. #ifdef CSV_LOGUSERFIELD
  167. /* append the user field */
  168. append_string(buf, cdr->userfield,bufsize);
  169. #endif
  170. /* If we hit the end of our buffer, log an error */
  171. if (strlen(buf) < bufsize - 5) {
  172. /* Trim off trailing comma */
  173. buf[strlen(buf) - 1] = '\0';
  174. strncat(buf, "\n", bufsize - strlen(buf) - 1);
  175. return 0;
  176. }
  177. return -1;
  178. }
  179. static int writefile(char *s, char *acc)
  180. {
  181. char tmp[AST_CONFIG_MAX_PATH];
  182. FILE *f;
  183. if (strchr(acc, '/') || (acc[0] == '.')) {
  184. ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
  185. return -1;
  186. }
  187. snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
  188. f = fopen(tmp, "a");
  189. if (!f)
  190. return -1;
  191. fputs(s, f);
  192. fflush(f);
  193. fclose(f);
  194. return 0;
  195. }
  196. static int csv_log(struct ast_cdr *cdr)
  197. {
  198. /* Make sure we have a big enough buf */
  199. char buf[1024];
  200. char csvmaster[AST_CONFIG_MAX_PATH];
  201. snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
  202. #if 0
  203. printf("[CDR] %s ('%s' -> '%s') Dur: %ds Bill: %ds Disp: %s Flags: %s Account: [%s]\n", cdr->channel, cdr->src, cdr->dst, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), ast_cdr_flags2str(cdr->amaflags), cdr->accountcode);
  204. #endif
  205. if (build_csv_record(buf, sizeof(buf), cdr)) {
  206. ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
  207. } else {
  208. /* because of the absolutely unconditional need for the
  209. highest reliability possible in writing billing records,
  210. we open write and close the log file each time */
  211. mf = fopen(csvmaster, "a");
  212. if (!mf) {
  213. ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
  214. }
  215. if (mf) {
  216. fputs(buf, mf);
  217. fflush(mf); /* be particularly anal here */
  218. fclose(mf);
  219. mf = NULL;
  220. }
  221. if (!ast_strlen_zero(cdr->accountcode)) {
  222. if (writefile(buf, cdr->accountcode))
  223. ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
  224. }
  225. }
  226. return 0;
  227. }
  228. char *description(void)
  229. {
  230. return desc;
  231. }
  232. int unload_module(void)
  233. {
  234. if (mf)
  235. fclose(mf);
  236. ast_cdr_unregister(name);
  237. return 0;
  238. }
  239. int load_module(void)
  240. {
  241. int res;
  242. res = ast_cdr_register(name, desc, csv_log);
  243. if (res) {
  244. ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
  245. if (mf)
  246. fclose(mf);
  247. }
  248. return res;
  249. }
  250. int reload(void)
  251. {
  252. return 0;
  253. }
  254. int usecount(void)
  255. {
  256. return 0;
  257. }
  258. char *key()
  259. {
  260. return ASTERISK_GPL_KEY;
  261. }