cdr_csv.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. /*!
  21. * \file
  22. * \brief Comma Separated Value CDR records.
  23. *
  24. * \author Mark Spencer <markster@digium.com>
  25. *
  26. * \arg See also \ref AstCDR
  27. * \ingroup cdr_drivers
  28. */
  29. /*! \li \ref cdr_csv.c uses the configuration file \ref cdr.conf
  30. * \addtogroup configuration_file Configuration Files
  31. */
  32. /*** MODULEINFO
  33. <support_level>extended</support_level>
  34. ***/
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
  38. #include "asterisk/config.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/cdr.h"
  41. #include "asterisk/module.h"
  42. #include "asterisk/utils.h"
  43. #include "asterisk/lock.h"
  44. #define CSV_LOG_DIR "/cdr-csv"
  45. #define CSV_MASTER "/Master.csv"
  46. #define DATE_FORMAT "%Y-%m-%d %T"
  47. static int usegmtime = 0;
  48. static int accountlogs = 1;
  49. static int loguniqueid = 0;
  50. static int loguserfield = 0;
  51. static int loaded = 0;
  52. static int newcdrcolumns = 0;
  53. static const char config[] = "cdr.conf";
  54. /* #define CSV_LOGUNIQUEID 1 */
  55. /* #define CSV_LOGUSERFIELD 1 */
  56. /*----------------------------------------------------
  57. The values are as follows:
  58. "accountcode", accountcode is the account name of detail records, Master.csv contains all records *
  59. Detail records are configured on a channel basis, IAX and SIP are determined by user *
  60. DAHDI is determined by channel in dahdi.conf
  61. "source",
  62. "destination",
  63. "destination context",
  64. "callerid",
  65. "channel",
  66. "destination channel", (if applicable)
  67. "last application", Last application run on the channel
  68. "last app argument", argument to the last channel
  69. "start time",
  70. "answer time",
  71. "end time",
  72. duration, Duration is the whole length that the entire call lasted. ie. call rx'd to hangup
  73. "end time" minus "start time"
  74. billable seconds, the duration that a call was up after other end answered which will be <= to duration
  75. "end time" minus "answer time"
  76. "disposition", ANSWERED, NO ANSWER, BUSY
  77. "amaflags", DOCUMENTATION, BILL, IGNORE etc, specified on a per channel basis like accountcode.
  78. "uniqueid", unique call identifier
  79. "userfield" user field set via SetCDRUserField
  80. ----------------------------------------------------------*/
  81. static char *name = "csv";
  82. AST_MUTEX_DEFINE_STATIC(mf_lock);
  83. AST_MUTEX_DEFINE_STATIC(acf_lock);
  84. static int load_config(int reload)
  85. {
  86. struct ast_config *cfg;
  87. struct ast_variable *v;
  88. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  89. if (!(cfg = ast_config_load(config, config_flags)) || cfg == CONFIG_STATUS_FILEINVALID) {
  90. ast_log(LOG_WARNING, "unable to load config: %s\n", config);
  91. return 0;
  92. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  93. return 1;
  94. }
  95. accountlogs = 1;
  96. usegmtime = 0;
  97. loguniqueid = 0;
  98. loguserfield = 0;
  99. newcdrcolumns = 0;
  100. if (!(v = ast_variable_browse(cfg, "csv"))) {
  101. ast_config_destroy(cfg);
  102. return 0;
  103. }
  104. for (; v; v = v->next) {
  105. if (!strcasecmp(v->name, "usegmtime")) {
  106. usegmtime = ast_true(v->value);
  107. } else if (!strcasecmp(v->name, "accountlogs")) {
  108. /* Turn on/off separate files per accountcode. Default is on (as before) */
  109. accountlogs = ast_true(v->value);
  110. } else if (!strcasecmp(v->name, "loguniqueid")) {
  111. loguniqueid = ast_true(v->value);
  112. } else if (!strcasecmp(v->name, "loguserfield")) {
  113. loguserfield = ast_true(v->value);
  114. } else if (!strcasecmp(v->name, "newcdrcolumns")) {
  115. newcdrcolumns = ast_true(v->value);
  116. }
  117. }
  118. ast_config_destroy(cfg);
  119. return 1;
  120. }
  121. static int append_string(char *buf, const char *s, size_t bufsize)
  122. {
  123. int pos = strlen(buf), spos = 0, error = -1;
  124. if (pos >= bufsize - 4)
  125. return -1;
  126. buf[pos++] = '\"';
  127. while(pos < bufsize - 3) {
  128. if (!s[spos]) {
  129. error = 0;
  130. break;
  131. }
  132. if (s[spos] == '\"')
  133. buf[pos++] = '\"';
  134. buf[pos++] = s[spos];
  135. spos++;
  136. }
  137. buf[pos++] = '\"';
  138. buf[pos++] = ',';
  139. buf[pos++] = '\0';
  140. return error;
  141. }
  142. static int append_int(char *buf, int s, size_t bufsize)
  143. {
  144. char tmp[32];
  145. int pos = strlen(buf);
  146. snprintf(tmp, sizeof(tmp), "%d", s);
  147. if (pos + strlen(tmp) > bufsize - 3)
  148. return -1;
  149. strncat(buf, tmp, bufsize - strlen(buf) - 1);
  150. pos = strlen(buf);
  151. buf[pos++] = ',';
  152. buf[pos++] = '\0';
  153. return 0;
  154. }
  155. static int append_date(char *buf, struct timeval when, size_t bufsize)
  156. {
  157. char tmp[80] = "";
  158. struct ast_tm tm;
  159. if (strlen(buf) > bufsize - 3)
  160. return -1;
  161. if (ast_tvzero(when)) {
  162. strncat(buf, ",", bufsize - strlen(buf) - 1);
  163. return 0;
  164. }
  165. ast_localtime(&when, &tm, usegmtime ? "GMT" : NULL);
  166. ast_strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
  167. return append_string(buf, tmp, bufsize);
  168. }
  169. static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
  170. {
  171. buf[0] = '\0';
  172. /* Account code */
  173. append_string(buf, cdr->accountcode, bufsize);
  174. /* Source */
  175. append_string(buf, cdr->src, bufsize);
  176. /* Destination */
  177. append_string(buf, cdr->dst, bufsize);
  178. /* Destination context */
  179. append_string(buf, cdr->dcontext, bufsize);
  180. /* Caller*ID */
  181. append_string(buf, cdr->clid, bufsize);
  182. /* Channel */
  183. append_string(buf, cdr->channel, bufsize);
  184. /* Destination Channel */
  185. append_string(buf, cdr->dstchannel, bufsize);
  186. /* Last Application */
  187. append_string(buf, cdr->lastapp, bufsize);
  188. /* Last Data */
  189. append_string(buf, cdr->lastdata, bufsize);
  190. /* Start Time */
  191. append_date(buf, cdr->start, bufsize);
  192. /* Answer Time */
  193. append_date(buf, cdr->answer, bufsize);
  194. /* End Time */
  195. append_date(buf, cdr->end, bufsize);
  196. /* Duration */
  197. append_int(buf, cdr->duration, bufsize);
  198. /* Billable seconds */
  199. append_int(buf, cdr->billsec, bufsize);
  200. /* Disposition */
  201. append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
  202. /* AMA Flags */
  203. append_string(buf, ast_channel_amaflags2string(cdr->amaflags), bufsize);
  204. /* Unique ID */
  205. if (loguniqueid)
  206. append_string(buf, cdr->uniqueid, bufsize);
  207. /* append the user field */
  208. if(loguserfield)
  209. append_string(buf, cdr->userfield,bufsize);
  210. if (newcdrcolumns) {
  211. append_string(buf, cdr->peeraccount, bufsize);
  212. append_string(buf, cdr->linkedid, bufsize);
  213. append_int(buf, cdr->sequence, bufsize);
  214. }
  215. /* If we hit the end of our buffer, log an error */
  216. if (strlen(buf) < bufsize - 5) {
  217. /* Trim off trailing comma */
  218. buf[strlen(buf) - 1] = '\0';
  219. strncat(buf, "\n", bufsize - strlen(buf) - 1);
  220. return 0;
  221. }
  222. return -1;
  223. }
  224. static int writefile(char *s, char *acc)
  225. {
  226. char tmp[PATH_MAX];
  227. FILE *f;
  228. if (strchr(acc, '/') || (acc[0] == '.')) {
  229. ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
  230. return -1;
  231. }
  232. snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
  233. ast_mutex_lock(&acf_lock);
  234. if (!(f = fopen(tmp, "a"))) {
  235. ast_mutex_unlock(&acf_lock);
  236. ast_log(LOG_ERROR, "Unable to open file %s : %s\n", tmp, strerror(errno));
  237. return -1;
  238. }
  239. fputs(s, f);
  240. fflush(f);
  241. fclose(f);
  242. ast_mutex_unlock(&acf_lock);
  243. return 0;
  244. }
  245. static int csv_log(struct ast_cdr *cdr)
  246. {
  247. FILE *mf = NULL;
  248. /* Make sure we have a big enough buf */
  249. char buf[1024];
  250. char csvmaster[PATH_MAX];
  251. snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
  252. if (build_csv_record(buf, sizeof(buf), cdr)) {
  253. ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
  254. return 0;
  255. }
  256. /* because of the absolutely unconditional need for the
  257. highest reliability possible in writing billing records,
  258. we open write and close the log file each time */
  259. ast_mutex_lock(&mf_lock);
  260. if ((mf = fopen(csvmaster, "a"))) {
  261. fputs(buf, mf);
  262. fflush(mf); /* be particularly anal here */
  263. fclose(mf);
  264. mf = NULL;
  265. ast_mutex_unlock(&mf_lock);
  266. } else {
  267. ast_mutex_unlock(&mf_lock);
  268. ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
  269. }
  270. if (accountlogs && !ast_strlen_zero(cdr->accountcode)) {
  271. if (writefile(buf, cdr->accountcode))
  272. ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
  273. }
  274. return 0;
  275. }
  276. static int unload_module(void)
  277. {
  278. if (ast_cdr_unregister(name)) {
  279. return -1;
  280. }
  281. loaded = 0;
  282. return 0;
  283. }
  284. static int load_module(void)
  285. {
  286. int res;
  287. if (!load_config(0)) {
  288. return AST_MODULE_LOAD_DECLINE;
  289. }
  290. if ((res = ast_cdr_register(name, ast_module_info->description, csv_log))) {
  291. ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
  292. } else {
  293. loaded = 1;
  294. }
  295. return res;
  296. }
  297. static int reload(void)
  298. {
  299. if (load_config(1)) {
  300. loaded = 1;
  301. } else {
  302. loaded = 0;
  303. ast_log(LOG_WARNING, "No [csv] section in cdr.conf. Unregistering backend.\n");
  304. ast_cdr_unregister(name);
  305. }
  306. return 0;
  307. }
  308. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Comma Separated Values CDR Backend",
  309. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  310. .load = load_module,
  311. .unload = unload_module,
  312. .reload = reload,
  313. .load_pri = AST_MODPRI_CDR_DRIVER,
  314. );