cdr_odbc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2003-2005, Digium, Inc.
  5. *
  6. * Brian K. West <brian@bkw.org>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief ODBC CDR Backend
  21. *
  22. * \author Brian K. West <brian@bkw.org>
  23. *
  24. * See also:
  25. * \arg http://www.unixodbc.org
  26. * \arg \ref Config_cdr
  27. * \ingroup cdr_drivers
  28. */
  29. /*** MODULEINFO
  30. <depend>res_odbc</depend>
  31. <support_level>extended</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  35. #include "asterisk/config.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/cdr.h"
  38. #include "asterisk/module.h"
  39. #include "asterisk/res_odbc.h"
  40. #define DATE_FORMAT "%Y-%m-%d %T"
  41. static const char name[] = "ODBC";
  42. static const char config_file[] = "cdr_odbc.conf";
  43. static char *dsn = NULL, *table = NULL;
  44. enum {
  45. CONFIG_LOGUNIQUEID = 1 << 0,
  46. CONFIG_USEGMTIME = 1 << 1,
  47. CONFIG_DISPOSITIONSTRING = 1 << 2,
  48. CONFIG_HRTIME = 1 << 3,
  49. CONFIG_REGISTERED = 1 << 4,
  50. };
  51. static struct ast_flags config = { 0 };
  52. static SQLHSTMT execute_cb(struct odbc_obj *obj, void *data)
  53. {
  54. struct ast_cdr *cdr = data;
  55. SQLRETURN ODBC_res;
  56. char sqlcmd[2048] = "", timestr[128];
  57. struct ast_tm tm;
  58. SQLHSTMT stmt;
  59. ast_localtime(&cdr->start, &tm, ast_test_flag(&config, CONFIG_USEGMTIME) ? "GMT" : NULL);
  60. ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
  61. if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
  62. snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
  63. "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
  64. "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
  65. "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table, timestr);
  66. } else {
  67. snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
  68. "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
  69. "duration,billsec,disposition,amaflags,accountcode) "
  70. "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?)", table, timestr);
  71. }
  72. ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
  73. if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
  74. ast_verb(11, "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
  75. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  76. return NULL;
  77. }
  78. SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL);
  79. SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL);
  80. SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL);
  81. SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL);
  82. SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL);
  83. SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL);
  84. SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL);
  85. SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL);
  86. if (ast_test_flag(&config, CONFIG_HRTIME)) {
  87. double hrbillsec = 0.0;
  88. double hrduration;
  89. if (!ast_tvzero(cdr->answer)) {
  90. hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
  91. }
  92. hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
  93. SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrduration, 0, NULL);
  94. SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrbillsec, 0, NULL);
  95. } else {
  96. SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
  97. SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
  98. }
  99. if (ast_test_flag(&config, CONFIG_DISPOSITIONSTRING))
  100. SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(ast_cdr_disp2str(cdr->disposition)) + 1, 0, ast_cdr_disp2str(cdr->disposition), 0, NULL);
  101. else
  102. SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
  103. SQLBindParameter(stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
  104. SQLBindParameter(stmt, 13, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL);
  105. if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
  106. SQLBindParameter(stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL);
  107. SQLBindParameter(stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL);
  108. }
  109. ODBC_res = SQLExecDirect(stmt, (unsigned char *)sqlcmd, SQL_NTS);
  110. if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
  111. ast_verb(11, "cdr_odbc: Error in ExecDirect: %d\n", ODBC_res);
  112. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  113. return NULL;
  114. }
  115. return stmt;
  116. }
  117. static int odbc_log(struct ast_cdr *cdr)
  118. {
  119. struct odbc_obj *obj = ast_odbc_request_obj(dsn, 0);
  120. SQLHSTMT stmt;
  121. if (!obj) {
  122. ast_log(LOG_ERROR, "Unable to retrieve database handle. CDR failed.\n");
  123. return -1;
  124. }
  125. stmt = ast_odbc_direct_execute(obj, execute_cb, cdr);
  126. if (stmt) {
  127. SQLLEN rows = 0;
  128. SQLRowCount(stmt, &rows);
  129. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  130. if (rows == 0)
  131. ast_log(LOG_WARNING, "CDR successfully ran, but inserted 0 rows?\n");
  132. } else
  133. ast_log(LOG_ERROR, "CDR direct execute failed\n");
  134. ast_odbc_release_obj(obj);
  135. return 0;
  136. }
  137. static int odbc_load_module(int reload)
  138. {
  139. int res = 0;
  140. struct ast_config *cfg;
  141. struct ast_variable *var;
  142. const char *tmp;
  143. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  144. do {
  145. cfg = ast_config_load(config_file, config_flags);
  146. if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
  147. ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config_file);
  148. res = AST_MODULE_LOAD_DECLINE;
  149. break;
  150. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
  151. break;
  152. var = ast_variable_browse(cfg, "global");
  153. if (!var) {
  154. /* nothing configured */
  155. break;
  156. }
  157. if ((tmp = ast_variable_retrieve(cfg, "global", "dsn")) == NULL) {
  158. ast_log(LOG_WARNING, "cdr_odbc: dsn not specified. Assuming asteriskdb\n");
  159. tmp = "asteriskdb";
  160. }
  161. if (dsn)
  162. ast_free(dsn);
  163. dsn = ast_strdup(tmp);
  164. if (dsn == NULL) {
  165. res = -1;
  166. break;
  167. }
  168. if (((tmp = ast_variable_retrieve(cfg, "global", "dispositionstring"))) && ast_true(tmp))
  169. ast_set_flag(&config, CONFIG_DISPOSITIONSTRING);
  170. else
  171. ast_clear_flag(&config, CONFIG_DISPOSITIONSTRING);
  172. if (((tmp = ast_variable_retrieve(cfg, "global", "loguniqueid"))) && ast_true(tmp)) {
  173. ast_set_flag(&config, CONFIG_LOGUNIQUEID);
  174. ast_debug(1, "cdr_odbc: Logging uniqueid\n");
  175. } else {
  176. ast_clear_flag(&config, CONFIG_LOGUNIQUEID);
  177. ast_debug(1, "cdr_odbc: Not logging uniqueid\n");
  178. }
  179. if (((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) && ast_true(tmp)) {
  180. ast_set_flag(&config, CONFIG_USEGMTIME);
  181. ast_debug(1, "cdr_odbc: Logging in GMT\n");
  182. } else {
  183. ast_clear_flag(&config, CONFIG_USEGMTIME);
  184. ast_debug(1, "cdr_odbc: Logging in local time\n");
  185. }
  186. if (((tmp = ast_variable_retrieve(cfg, "global", "hrtime"))) && ast_true(tmp)) {
  187. ast_set_flag(&config, CONFIG_HRTIME);
  188. ast_debug(1, "cdr_odbc: Logging billsec and duration fields as floats\n");
  189. } else {
  190. ast_clear_flag(&config, CONFIG_HRTIME);
  191. ast_debug(1, "cdr_odbc: Logging billsec and duration fields as integers\n");
  192. }
  193. if ((tmp = ast_variable_retrieve(cfg, "global", "table")) == NULL) {
  194. ast_log(LOG_WARNING, "cdr_odbc: table not specified. Assuming cdr\n");
  195. tmp = "cdr";
  196. }
  197. if (table)
  198. ast_free(table);
  199. table = ast_strdup(tmp);
  200. if (table == NULL) {
  201. res = -1;
  202. break;
  203. }
  204. ast_verb(3, "cdr_odbc: dsn is %s\n", dsn);
  205. ast_verb(3, "cdr_odbc: table is %s\n", table);
  206. if (!ast_test_flag(&config, CONFIG_REGISTERED)) {
  207. res = ast_cdr_register(name, ast_module_info->description, odbc_log);
  208. if (res) {
  209. ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
  210. } else {
  211. ast_set_flag(&config, CONFIG_REGISTERED);
  212. }
  213. }
  214. } while (0);
  215. if (ast_test_flag(&config, CONFIG_REGISTERED) && (!cfg || dsn == NULL || table == NULL)) {
  216. ast_cdr_unregister(name);
  217. ast_clear_flag(&config, CONFIG_REGISTERED);
  218. }
  219. if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
  220. ast_config_destroy(cfg);
  221. }
  222. return res;
  223. }
  224. static int load_module(void)
  225. {
  226. return odbc_load_module(0);
  227. }
  228. static int unload_module(void)
  229. {
  230. ast_cdr_unregister(name);
  231. if (dsn) {
  232. ast_verb(11, "cdr_odbc: free dsn\n");
  233. ast_free(dsn);
  234. }
  235. if (table) {
  236. ast_verb(11, "cdr_odbc: free table\n");
  237. ast_free(table);
  238. }
  239. return 0;
  240. }
  241. static int reload(void)
  242. {
  243. return odbc_load_module(1);
  244. }
  245. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ODBC CDR Backend",
  246. .load = load_module,
  247. .unload = unload_module,
  248. .reload = reload,
  249. .load_pri = AST_MODPRI_CDR_DRIVER,
  250. );