cdr_csv.c 6.5 KB

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