cdr_csv.c 9.0 KB

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