cdr_odbc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. * http://www.unixodbc.org
  24. * \ingroup cdr_drivers
  25. */
  26. /*! \li \ref cdr_odbc.c uses the configuration file \ref cdr_odbc.conf
  27. * \addtogroup configuration_file Configuration Files
  28. */
  29. /*!
  30. * \page cdr_odbc.conf cdr_odbc.conf
  31. * \verbinclude cdr_odbc.conf.sample
  32. */
  33. /*** MODULEINFO
  34. <depend>res_odbc</depend>
  35. <depend>generic_odbc</depend>
  36. <support_level>extended</support_level>
  37. ***/
  38. #include "asterisk.h"
  39. #include "asterisk/config.h"
  40. #include "asterisk/channel.h"
  41. #include "asterisk/cdr.h"
  42. #include "asterisk/module.h"
  43. #include "asterisk/res_odbc.h"
  44. #define DATE_FORMAT "%Y-%m-%d %T"
  45. static const char name[] = "ODBC";
  46. static const char config_file[] = "cdr_odbc.conf";
  47. static char *dsn = NULL, *table = NULL;
  48. enum {
  49. CONFIG_LOGUNIQUEID = 1 << 0,
  50. CONFIG_USEGMTIME = 1 << 1,
  51. CONFIG_DISPOSITIONSTRING = 1 << 2,
  52. CONFIG_HRTIME = 1 << 3,
  53. CONFIG_REGISTERED = 1 << 4,
  54. CONFIG_NEWCDRCOLUMNS = 1 << 5,
  55. };
  56. static struct ast_flags config = { 0 };
  57. static SQLHSTMT execute_cb(struct odbc_obj *obj, void *data)
  58. {
  59. struct ast_cdr *cdr = data;
  60. SQLRETURN ODBC_res;
  61. char sqlcmd[2048] = "", timestr[128], new_columns[120] = "", new_values[7] = "";
  62. struct ast_tm tm;
  63. SQLHSTMT stmt;
  64. int i = 0;
  65. ast_localtime(&cdr->start, &tm, ast_test_flag(&config, CONFIG_USEGMTIME) ? "GMT" : NULL);
  66. ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
  67. if (ast_test_flag(&config, CONFIG_NEWCDRCOLUMNS)) {
  68. snprintf(new_columns, sizeof(new_columns), "%s", ",peeraccount,linkedid,sequence");
  69. snprintf(new_values, sizeof(new_values), "%s", ",?,?,?");
  70. }
  71. if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
  72. snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
  73. "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
  74. "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield%s) "
  75. "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?,?,?%s)", table, new_columns, timestr, new_values);
  76. } else {
  77. snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
  78. "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
  79. "duration,billsec,disposition,amaflags,accountcode%s) "
  80. "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?%s)", table, new_columns, timestr, new_values);
  81. }
  82. ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
  83. if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
  84. ast_log(LOG_WARNING, "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
  85. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  86. return NULL;
  87. }
  88. SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL);
  89. SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL);
  90. SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL);
  91. SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL);
  92. SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL);
  93. SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL);
  94. SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL);
  95. SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL);
  96. if (ast_test_flag(&config, CONFIG_HRTIME)) {
  97. double hrbillsec = 0.0;
  98. double hrduration;
  99. if (!ast_tvzero(cdr->answer)) {
  100. hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
  101. }
  102. hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
  103. SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrduration, 0, NULL);
  104. SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrbillsec, 0, NULL);
  105. } else {
  106. SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
  107. SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
  108. }
  109. if (ast_test_flag(&config, CONFIG_DISPOSITIONSTRING)) {
  110. char *disposition;
  111. disposition = ast_strdupa(ast_cdr_disp2str(cdr->disposition));
  112. SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(disposition) + 1, 0, disposition, 0, NULL);
  113. } else {
  114. SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
  115. }
  116. SQLBindParameter(stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
  117. SQLBindParameter(stmt, 13, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL);
  118. i = 14;
  119. if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
  120. SQLBindParameter(stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL);
  121. SQLBindParameter(stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL);
  122. i = 16;
  123. }
  124. if (ast_test_flag(&config, CONFIG_NEWCDRCOLUMNS)) {
  125. SQLBindParameter(stmt, i, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->peeraccount), 0, cdr->peeraccount, 0, NULL);
  126. SQLBindParameter(stmt, i + 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->linkedid), 0, cdr->linkedid, 0, NULL);
  127. SQLBindParameter(stmt, i + 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->sequence, 0, NULL);
  128. }
  129. ODBC_res = ast_odbc_execute_sql(obj, stmt, sqlcmd);
  130. if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
  131. ast_log(LOG_WARNING, "cdr_odbc: Error in ExecDirect: %d, query is: %s\n", ODBC_res, sqlcmd);
  132. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  133. return NULL;
  134. }
  135. return stmt;
  136. }
  137. static int odbc_log(struct ast_cdr *cdr)
  138. {
  139. struct odbc_obj *obj = ast_odbc_request_obj(dsn, 0);
  140. SQLHSTMT stmt;
  141. if (!obj) {
  142. ast_log(LOG_ERROR, "Unable to retrieve database handle. CDR failed.\n");
  143. return -1;
  144. }
  145. stmt = ast_odbc_direct_execute(obj, execute_cb, cdr);
  146. if (stmt) {
  147. SQLLEN rows = 0;
  148. SQLRowCount(stmt, &rows);
  149. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  150. if (rows == 0)
  151. ast_log(LOG_WARNING, "CDR successfully ran, but inserted 0 rows?\n");
  152. } else
  153. ast_log(LOG_ERROR, "CDR direct execute failed\n");
  154. ast_odbc_release_obj(obj);
  155. return 0;
  156. }
  157. static int odbc_load_module(int reload)
  158. {
  159. int res = 0;
  160. struct ast_config *cfg;
  161. struct ast_variable *var;
  162. const char *tmp;
  163. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  164. do {
  165. cfg = ast_config_load(config_file, config_flags);
  166. if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
  167. ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config_file);
  168. res = AST_MODULE_LOAD_DECLINE;
  169. break;
  170. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
  171. break;
  172. var = ast_variable_browse(cfg, "global");
  173. if (!var) {
  174. /* nothing configured */
  175. break;
  176. }
  177. if ((tmp = ast_variable_retrieve(cfg, "global", "dsn")) == NULL) {
  178. ast_log(LOG_WARNING, "cdr_odbc: dsn not specified. Assuming asteriskdb\n");
  179. tmp = "asteriskdb";
  180. }
  181. if (dsn)
  182. ast_free(dsn);
  183. dsn = ast_strdup(tmp);
  184. if (dsn == NULL) {
  185. res = -1;
  186. break;
  187. }
  188. if (((tmp = ast_variable_retrieve(cfg, "global", "dispositionstring"))) && ast_true(tmp))
  189. ast_set_flag(&config, CONFIG_DISPOSITIONSTRING);
  190. else
  191. ast_clear_flag(&config, CONFIG_DISPOSITIONSTRING);
  192. if (((tmp = ast_variable_retrieve(cfg, "global", "loguniqueid"))) && ast_true(tmp)) {
  193. ast_set_flag(&config, CONFIG_LOGUNIQUEID);
  194. ast_debug(1, "cdr_odbc: Logging uniqueid\n");
  195. } else {
  196. ast_clear_flag(&config, CONFIG_LOGUNIQUEID);
  197. ast_debug(1, "cdr_odbc: Not logging uniqueid\n");
  198. }
  199. if (((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) && ast_true(tmp)) {
  200. ast_set_flag(&config, CONFIG_USEGMTIME);
  201. ast_debug(1, "cdr_odbc: Logging in GMT\n");
  202. } else {
  203. ast_clear_flag(&config, CONFIG_USEGMTIME);
  204. ast_debug(1, "cdr_odbc: Logging in local time\n");
  205. }
  206. if (((tmp = ast_variable_retrieve(cfg, "global", "hrtime"))) && ast_true(tmp)) {
  207. ast_set_flag(&config, CONFIG_HRTIME);
  208. ast_debug(1, "cdr_odbc: Logging billsec and duration fields as floats\n");
  209. } else {
  210. ast_clear_flag(&config, CONFIG_HRTIME);
  211. ast_debug(1, "cdr_odbc: Logging billsec and duration fields as integers\n");
  212. }
  213. if ((tmp = ast_variable_retrieve(cfg, "global", "table")) == NULL) {
  214. ast_log(LOG_WARNING, "cdr_odbc: table not specified. Assuming cdr\n");
  215. tmp = "cdr";
  216. }
  217. if (table)
  218. ast_free(table);
  219. table = ast_strdup(tmp);
  220. if (table == NULL) {
  221. res = -1;
  222. break;
  223. }
  224. if (!ast_test_flag(&config, CONFIG_REGISTERED)) {
  225. res = ast_cdr_register(name, ast_module_info->description, odbc_log);
  226. if (res) {
  227. ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
  228. } else {
  229. ast_set_flag(&config, CONFIG_REGISTERED);
  230. }
  231. }
  232. if (((tmp = ast_variable_retrieve(cfg, "global", "newcdrcolumns"))) && ast_true(tmp)) {
  233. ast_set_flag(&config, CONFIG_NEWCDRCOLUMNS);
  234. ast_debug(1, "cdr_odbc: Add new cdr columns\n");
  235. } else {
  236. ast_clear_flag(&config, CONFIG_NEWCDRCOLUMNS);
  237. ast_debug(1, "cdr_odbc: Not add new cdr columns\n");
  238. }
  239. } while (0);
  240. if (ast_test_flag(&config, CONFIG_REGISTERED) && (!cfg || dsn == NULL || table == NULL)) {
  241. ast_cdr_backend_suspend(name);
  242. ast_clear_flag(&config, CONFIG_REGISTERED);
  243. } else {
  244. ast_cdr_backend_unsuspend(name);
  245. }
  246. if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
  247. ast_config_destroy(cfg);
  248. }
  249. return res;
  250. }
  251. static int load_module(void)
  252. {
  253. return odbc_load_module(0);
  254. }
  255. static int unload_module(void)
  256. {
  257. if (ast_cdr_unregister(name)) {
  258. return -1;
  259. }
  260. if (dsn) {
  261. ast_free(dsn);
  262. }
  263. if (table) {
  264. ast_free(table);
  265. }
  266. return 0;
  267. }
  268. static int reload(void)
  269. {
  270. return odbc_load_module(1);
  271. }
  272. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ODBC CDR Backend",
  273. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  274. .load = load_module,
  275. .unload = unload_module,
  276. .reload = reload,
  277. .load_pri = AST_MODPRI_CDR_DRIVER,
  278. .requires = "cdr,res_odbc",
  279. );