cdr_odbc.c 11 KB

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