2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2003-2005, Digium, Inc.
6 * Brian K. West <brian@bkw.org>
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.
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.
21 * \brief ODBC CDR Backend
23 * \author Brian K. West <brian@bkw.org>
26 * \arg http://www.unixodbc.org
27 * \arg \ref Config_cdr
28 * \ingroup cdr_drivers
31 /*! \li \ref cdr_odbc.c uses the configuration file \ref cdr_odbc.conf
32 * \addtogroup configuration_file Configuration Files
36 * \page cdr_odbc.conf cdr_odbc.conf
37 * \verbinclude cdr_odbc.conf.sample
41 <depend>res_odbc</depend>
42 <support_level>extended</support_level>
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49 #include "asterisk/config.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/cdr.h"
52 #include "asterisk/module.h"
53 #include "asterisk/res_odbc.h"
55 #define DATE_FORMAT "%Y-%m-%d %T"
57 static const char name[] = "ODBC";
58 static const char config_file[] = "cdr_odbc.conf";
59 static char *dsn = NULL, *table = NULL;
62 CONFIG_LOGUNIQUEID = 1 << 0,
63 CONFIG_USEGMTIME = 1 << 1,
64 CONFIG_DISPOSITIONSTRING = 1 << 2,
65 CONFIG_HRTIME = 1 << 3,
66 CONFIG_REGISTERED = 1 << 4,
69 static struct ast_flags config = { 0 };
71 static SQLHSTMT execute_cb(struct odbc_obj *obj, void *data)
73 struct ast_cdr *cdr = data;
75 char sqlcmd[2048] = "", timestr[128];
79 ast_localtime(&cdr->start, &tm, ast_test_flag(&config, CONFIG_USEGMTIME) ? "GMT" : NULL);
80 ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
82 if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
83 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
84 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,"
85 "lastdata,duration,billsec,disposition,amaflags,accountcode,uniqueid,userfield) "
86 "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", table, timestr);
88 snprintf(sqlcmd,sizeof(sqlcmd),"INSERT INTO %s "
89 "(calldate,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,"
90 "duration,billsec,disposition,amaflags,accountcode) "
91 "VALUES ({ts '%s'},?,?,?,?,?,?,?,?,?,?,?,?,?)", table, timestr);
94 ODBC_res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
96 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
97 ast_log(LOG_WARNING, "cdr_odbc: Failure in AllocStatement %d\n", ODBC_res);
98 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
102 SQLBindParameter(stmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->clid), 0, cdr->clid, 0, NULL);
103 SQLBindParameter(stmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->src), 0, cdr->src, 0, NULL);
104 SQLBindParameter(stmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dst), 0, cdr->dst, 0, NULL);
105 SQLBindParameter(stmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dcontext), 0, cdr->dcontext, 0, NULL);
106 SQLBindParameter(stmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->channel), 0, cdr->channel, 0, NULL);
107 SQLBindParameter(stmt, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->dstchannel), 0, cdr->dstchannel, 0, NULL);
108 SQLBindParameter(stmt, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastapp), 0, cdr->lastapp, 0, NULL);
109 SQLBindParameter(stmt, 8, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->lastdata), 0, cdr->lastdata, 0, NULL);
111 if (ast_test_flag(&config, CONFIG_HRTIME)) {
112 double hrbillsec = 0.0;
115 if (!ast_tvzero(cdr->answer)) {
116 hrbillsec = (double) ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0;
118 hrduration = (double) ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0;
120 SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrduration, 0, NULL);
121 SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_FLOAT, 0, 0, &hrbillsec, 0, NULL);
123 SQLBindParameter(stmt, 9, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->duration, 0, NULL);
124 SQLBindParameter(stmt, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->billsec, 0, NULL);
127 if (ast_test_flag(&config, CONFIG_DISPOSITIONSTRING))
128 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);
130 SQLBindParameter(stmt, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->disposition, 0, NULL);
131 SQLBindParameter(stmt, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &cdr->amaflags, 0, NULL);
132 SQLBindParameter(stmt, 13, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->accountcode), 0, cdr->accountcode, 0, NULL);
134 if (ast_test_flag(&config, CONFIG_LOGUNIQUEID)) {
135 SQLBindParameter(stmt, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->uniqueid), 0, cdr->uniqueid, 0, NULL);
136 SQLBindParameter(stmt, 15, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, sizeof(cdr->userfield), 0, cdr->userfield, 0, NULL);
139 ODBC_res = SQLExecDirect(stmt, (unsigned char *)sqlcmd, SQL_NTS);
141 if ((ODBC_res != SQL_SUCCESS) && (ODBC_res != SQL_SUCCESS_WITH_INFO)) {
142 ast_log(LOG_WARNING, "cdr_odbc: Error in ExecDirect: %d, query is: %s\n", ODBC_res, sqlcmd);
143 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
151 static int odbc_log(struct ast_cdr *cdr)
153 struct odbc_obj *obj = ast_odbc_request_obj(dsn, 0);
157 ast_log(LOG_ERROR, "Unable to retrieve database handle. CDR failed.\n");
161 stmt = ast_odbc_direct_execute(obj, execute_cb, cdr);
165 SQLRowCount(stmt, &rows);
166 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
169 ast_log(LOG_WARNING, "CDR successfully ran, but inserted 0 rows?\n");
171 ast_log(LOG_ERROR, "CDR direct execute failed\n");
172 ast_odbc_release_obj(obj);
176 static int odbc_load_module(int reload)
179 struct ast_config *cfg;
180 struct ast_variable *var;
182 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
185 cfg = ast_config_load(config_file, config_flags);
186 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
187 ast_log(LOG_WARNING, "cdr_odbc: Unable to load config for ODBC CDR's: %s\n", config_file);
188 res = AST_MODULE_LOAD_DECLINE;
190 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
193 var = ast_variable_browse(cfg, "global");
195 /* nothing configured */
199 if ((tmp = ast_variable_retrieve(cfg, "global", "dsn")) == NULL) {
200 ast_log(LOG_WARNING, "cdr_odbc: dsn not specified. Assuming asteriskdb\n");
205 dsn = ast_strdup(tmp);
211 if (((tmp = ast_variable_retrieve(cfg, "global", "dispositionstring"))) && ast_true(tmp))
212 ast_set_flag(&config, CONFIG_DISPOSITIONSTRING);
214 ast_clear_flag(&config, CONFIG_DISPOSITIONSTRING);
216 if (((tmp = ast_variable_retrieve(cfg, "global", "loguniqueid"))) && ast_true(tmp)) {
217 ast_set_flag(&config, CONFIG_LOGUNIQUEID);
218 ast_debug(1, "cdr_odbc: Logging uniqueid\n");
220 ast_clear_flag(&config, CONFIG_LOGUNIQUEID);
221 ast_debug(1, "cdr_odbc: Not logging uniqueid\n");
224 if (((tmp = ast_variable_retrieve(cfg, "global", "usegmtime"))) && ast_true(tmp)) {
225 ast_set_flag(&config, CONFIG_USEGMTIME);
226 ast_debug(1, "cdr_odbc: Logging in GMT\n");
228 ast_clear_flag(&config, CONFIG_USEGMTIME);
229 ast_debug(1, "cdr_odbc: Logging in local time\n");
232 if (((tmp = ast_variable_retrieve(cfg, "global", "hrtime"))) && ast_true(tmp)) {
233 ast_set_flag(&config, CONFIG_HRTIME);
234 ast_debug(1, "cdr_odbc: Logging billsec and duration fields as floats\n");
236 ast_clear_flag(&config, CONFIG_HRTIME);
237 ast_debug(1, "cdr_odbc: Logging billsec and duration fields as integers\n");
240 if ((tmp = ast_variable_retrieve(cfg, "global", "table")) == NULL) {
241 ast_log(LOG_WARNING, "cdr_odbc: table not specified. Assuming cdr\n");
246 table = ast_strdup(tmp);
252 ast_verb(3, "cdr_odbc: dsn is %s\n", dsn);
253 ast_verb(3, "cdr_odbc: table is %s\n", table);
255 if (!ast_test_flag(&config, CONFIG_REGISTERED)) {
256 res = ast_cdr_register(name, ast_module_info->description, odbc_log);
258 ast_log(LOG_ERROR, "cdr_odbc: Unable to register ODBC CDR handling\n");
260 ast_set_flag(&config, CONFIG_REGISTERED);
265 if (ast_test_flag(&config, CONFIG_REGISTERED) && (!cfg || dsn == NULL || table == NULL)) {
266 ast_cdr_unregister(name);
267 ast_clear_flag(&config, CONFIG_REGISTERED);
270 if (cfg && cfg != CONFIG_STATUS_FILEUNCHANGED && cfg != CONFIG_STATUS_FILEINVALID) {
271 ast_config_destroy(cfg);
276 static int load_module(void)
278 return odbc_load_module(0);
281 static int unload_module(void)
283 ast_cdr_unregister(name);
286 ast_verb(11, "cdr_odbc: free dsn\n");
290 ast_verb(11, "cdr_odbc: free table\n");
297 static int reload(void)
299 return odbc_load_module(1);
302 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "ODBC CDR Backend",
304 .unload = unload_module,
306 .load_pri = AST_MODPRI_CDR_DRIVER,