cdr_mysql.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * James Sharp <jsharp@psychoses.org>
  5. *
  6. * Modified August 2003
  7. * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
  8. *
  9. * Modified August 6, 2005
  10. * Joseph Benden <joe@thrallingpenguin.com>
  11. * Added mysql connection timeout parameter
  12. * Added an automatic reconnect as to not lose a cdr record
  13. * Cleaned up the original code to match the coding guidelines
  14. *
  15. * Modified Juli 2006
  16. * Martin Portmann <map@infinitum.ch>
  17. * Added mysql ssl support
  18. *
  19. * See http://www.asterisk.org for more information about
  20. * the Asterisk project. Please do not directly contact
  21. * any of the maintainers of this project for assistance;
  22. * the project provides a web site, mailing lists and IRC
  23. * channels for your use.
  24. *
  25. * This program is free software, distributed under the terms of
  26. * the GNU General Public License Version 2. See the LICENSE file
  27. * at the top of the source tree.
  28. */
  29. /*!
  30. * \file
  31. * \brief MySQL CDR backend
  32. * \ingroup cdr_drivers
  33. */
  34. /*** MODULEINFO
  35. <depend>mysqlclient</depend>
  36. <defaultenabled>no</defaultenabled>
  37. <support_level>deprecated</support_level>
  38. <replacement>cdr_adaptive_odbc</replacement>
  39. ***/
  40. #include "asterisk.h"
  41. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  42. #include <mysql/mysql.h>
  43. #include <mysql/errmsg.h>
  44. #include "asterisk/config.h"
  45. #include "asterisk/options.h"
  46. #include "asterisk/channel.h"
  47. #include "asterisk/cdr.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/logger.h"
  50. #include "asterisk/cli.h"
  51. #include "asterisk/strings.h"
  52. #include "asterisk/linkedlists.h"
  53. #include "asterisk/threadstorage.h"
  54. #define DATE_FORMAT "%Y-%m-%d %T"
  55. AST_THREADSTORAGE(sql1_buf);
  56. AST_THREADSTORAGE(sql2_buf);
  57. AST_THREADSTORAGE(escape_buf);
  58. static const char desc[] = "MySQL CDR Backend";
  59. static const char name[] = "mysql";
  60. static const char config[] = "cdr_mysql.conf";
  61. static struct ast_str *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL, *dbcharset = NULL, *cdrzone = NULL;
  62. static struct ast_str *ssl_ca = NULL, *ssl_cert = NULL, *ssl_key = NULL;
  63. static int dbport = 0;
  64. static int connected = 0;
  65. static time_t connect_time = 0;
  66. static int records = 0;
  67. static int totalrecords = 0;
  68. static int timeout = 0;
  69. static int calldate_compat = 0;
  70. AST_MUTEX_DEFINE_STATIC(mysql_lock);
  71. struct unload_string {
  72. AST_LIST_ENTRY(unload_string) entry;
  73. struct ast_str *str;
  74. };
  75. static AST_LIST_HEAD_STATIC(unload_strings, unload_string);
  76. struct column {
  77. char *name;
  78. char *cdrname;
  79. char *staticvalue;
  80. char *type;
  81. AST_LIST_ENTRY(column) list;
  82. };
  83. /* Protected with mysql_lock */
  84. static AST_RWLIST_HEAD_STATIC(columns, column);
  85. static MYSQL mysql = { { NULL }, };
  86. static char *handle_cli_cdr_mysql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  87. {
  88. switch (cmd) {
  89. case CLI_INIT:
  90. e->command = "cdr mysql status";
  91. e->usage =
  92. "Usage: cdr mysql status\n"
  93. " Shows current connection status for cdr_mysql\n";
  94. return NULL;
  95. case CLI_GENERATE:
  96. return NULL;
  97. }
  98. if (a->argc != 3)
  99. return CLI_SHOWUSAGE;
  100. if (connected) {
  101. char status[256], status2[100] = "";
  102. int ctime = time(NULL) - connect_time;
  103. if (dbport)
  104. snprintf(status, 255, "Connected to %s@%s, port %d", ast_str_buffer(dbname), ast_str_buffer(hostname), dbport);
  105. else if (dbsock)
  106. snprintf(status, 255, "Connected to %s on socket file %s", ast_str_buffer(dbname), S_OR(ast_str_buffer(dbsock), "default"));
  107. else
  108. snprintf(status, 255, "Connected to %s@%s", ast_str_buffer(dbname), ast_str_buffer(hostname));
  109. if (!ast_strlen_zero(ast_str_buffer(dbuser)))
  110. snprintf(status2, 99, " with username %s", ast_str_buffer(dbuser));
  111. if (ast_str_strlen(dbtable))
  112. snprintf(status2, 99, " using table %s", ast_str_buffer(dbtable));
  113. if (ctime > 31536000) {
  114. ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
  115. } else if (ctime > 86400) {
  116. ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
  117. } else if (ctime > 3600) {
  118. ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
  119. } else if (ctime > 60) {
  120. ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
  121. } else {
  122. ast_cli(a->fd, "%s%s for %d seconds.\n", status, status2, ctime);
  123. }
  124. if (records == totalrecords)
  125. ast_cli(a->fd, " Wrote %d records since last restart.\n", totalrecords);
  126. else
  127. ast_cli(a->fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
  128. } else {
  129. ast_cli(a->fd, "Not currently connected to a MySQL server.\n");
  130. }
  131. return CLI_SUCCESS;
  132. }
  133. static struct ast_cli_entry cdr_mysql_status_cli[] = {
  134. AST_CLI_DEFINE(handle_cli_cdr_mysql_status, "Show connection status of cdr_mysql"),
  135. };
  136. static int mysql_log(struct ast_cdr *cdr)
  137. {
  138. struct ast_str *sql1 = ast_str_thread_get(&sql1_buf, 1024), *sql2 = ast_str_thread_get(&sql2_buf, 1024);
  139. int retries = 5;
  140. #if MYSQL_VERSION_ID >= 50013
  141. my_bool my_bool_true = 1;
  142. #endif
  143. if (!sql1 || !sql2) {
  144. ast_log(LOG_ERROR, "Memory error\n");
  145. return -1;
  146. }
  147. ast_mutex_lock(&mysql_lock);
  148. db_reconnect:
  149. if ((!connected) && (hostname || dbsock) && dbuser && password && dbname && dbtable ) {
  150. /* Attempt to connect */
  151. mysql_init(&mysql);
  152. /* Add option to quickly timeout the connection */
  153. if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout) != 0) {
  154. ast_log(LOG_ERROR, "mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  155. }
  156. #if MYSQL_VERSION_ID >= 50013
  157. /* Add option for automatic reconnection */
  158. if (mysql_options(&mysql, MYSQL_OPT_RECONNECT, &my_bool_true) != 0) {
  159. ast_log(LOG_ERROR, "mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  160. }
  161. #endif
  162. if (ssl_ca || ssl_cert || ssl_key) {
  163. mysql_ssl_set(&mysql, ssl_key ? ast_str_buffer(ssl_key) : NULL, ssl_cert ? ast_str_buffer(ssl_cert) : NULL, ssl_ca ? ast_str_buffer(ssl_ca) : NULL, NULL, NULL);
  164. }
  165. if (mysql_real_connect(&mysql, ast_str_buffer(hostname), ast_str_buffer(dbuser), ast_str_buffer(password), ast_str_buffer(dbname), dbport, dbsock && ast_str_strlen(dbsock) ? ast_str_buffer(dbsock) : NULL, ssl_ca ? CLIENT_SSL : 0)) {
  166. connected = 1;
  167. connect_time = time(NULL);
  168. records = 0;
  169. if (dbcharset) {
  170. ast_str_set(&sql1, 0, "SET NAMES '%s'", ast_str_buffer(dbcharset));
  171. mysql_real_query(&mysql, ast_str_buffer(sql1), ast_str_strlen(sql1));
  172. ast_debug(1, "SQL command as follows: %s\n", ast_str_buffer(sql1));
  173. }
  174. } else {
  175. ast_log(LOG_ERROR, "Cannot connect to database server %s: (%d) %s\n", ast_str_buffer(hostname), mysql_errno(&mysql), mysql_error(&mysql));
  176. connected = 0;
  177. }
  178. } else {
  179. /* Long connection - ping the server */
  180. int error;
  181. if ((error = mysql_ping(&mysql))) {
  182. connected = 0;
  183. records = 0;
  184. switch (mysql_errno(&mysql)) {
  185. case CR_SERVER_GONE_ERROR:
  186. case CR_SERVER_LOST:
  187. ast_log(LOG_ERROR, "Server has gone away. Attempting to reconnect.\n");
  188. break;
  189. default:
  190. ast_log(LOG_ERROR, "Unknown connection error: (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  191. }
  192. retries--;
  193. if (retries) {
  194. goto db_reconnect;
  195. } else {
  196. ast_log(LOG_ERROR, "Retried to connect five times, giving up.\n");
  197. }
  198. }
  199. }
  200. if (connected) {
  201. int column_count = 0;
  202. char *cdrname;
  203. char workspace[2048], *value = NULL;
  204. struct column *entry;
  205. struct ast_str *escape = ast_str_thread_get(&escape_buf, 16);
  206. ast_str_set(&sql1, 0, "INSERT INTO %s (", AS_OR(dbtable, "cdr"));
  207. ast_str_set(&sql2, 0, ") VALUES (");
  208. AST_RWLIST_RDLOCK(&columns);
  209. AST_RWLIST_TRAVERSE(&columns, entry, list) {
  210. if (!strcmp(entry->name, "calldate")) {
  211. /*!\note
  212. * For some dumb reason, "calldate" used to be formulated using
  213. * the datetime the record was posted, rather than the start
  214. * time of the call. If someone really wants the old compatible
  215. * behavior, it's provided here.
  216. */
  217. if (calldate_compat) {
  218. struct timeval tv = ast_tvnow();
  219. struct ast_tm tm;
  220. char timestr[128];
  221. ast_localtime(&tv, &tm, ast_str_strlen(cdrzone) ? ast_str_buffer(cdrzone) : NULL);
  222. ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %T", &tm);
  223. value = ast_strdupa(timestr);
  224. cdrname = "calldate";
  225. } else {
  226. cdrname = "start";
  227. }
  228. } else {
  229. cdrname = entry->cdrname;
  230. }
  231. /* Construct SQL */
  232. /* Need the type and value to determine if we want the raw value or not */
  233. if (entry->staticvalue) {
  234. value = ast_strdupa(entry->staticvalue);
  235. } else if ((!strcmp(cdrname, "start") ||
  236. !strcmp(cdrname, "answer") ||
  237. !strcmp(cdrname, "end") ||
  238. !strcmp(cdrname, "disposition") ||
  239. !strcmp(cdrname, "amaflags")) &&
  240. (strstr(entry->type, "int") ||
  241. strstr(entry->type, "dec") ||
  242. strstr(entry->type, "float") ||
  243. strstr(entry->type, "double") ||
  244. strstr(entry->type, "real") ||
  245. strstr(entry->type, "numeric") ||
  246. strstr(entry->type, "fixed"))) {
  247. ast_cdr_format_var(cdr, cdrname, &value, workspace, sizeof(workspace), 1);
  248. } else if (!strcmp(cdrname, "calldate")) {
  249. /* Skip calldate - the value has already been dup'd */
  250. } else {
  251. ast_cdr_format_var(cdr, cdrname, &value, workspace, sizeof(workspace), 0);
  252. }
  253. if (value) {
  254. size_t valsz;
  255. if (column_count++) {
  256. ast_str_append(&sql1, 0, ",");
  257. ast_str_append(&sql2, 0, ",");
  258. }
  259. if (!strcasecmp(cdrname, "billsec") &&
  260. (strstr(entry->type, "float") ||
  261. strstr(entry->type, "double") ||
  262. strstr(entry->type, "decimal") ||
  263. strstr(entry->type, "numeric") ||
  264. strstr(entry->type, "real"))) {
  265. if (!ast_tvzero(cdr->answer)) {
  266. snprintf(workspace, sizeof(workspace), "%lf",
  267. (double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0));
  268. } else {
  269. ast_copy_string(workspace, "0", sizeof(workspace));
  270. }
  271. if (!ast_strlen_zero(workspace)) {
  272. value = workspace;
  273. }
  274. }
  275. if (!strcasecmp(cdrname, "duration") &&
  276. (strstr(entry->type, "float") ||
  277. strstr(entry->type, "double") ||
  278. strstr(entry->type, "decimal") ||
  279. strstr(entry->type, "numeric") ||
  280. strstr(entry->type, "real"))) {
  281. snprintf(workspace, sizeof(workspace), "%lf",
  282. (double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0));
  283. if (!ast_strlen_zero(workspace)) {
  284. value = workspace;
  285. }
  286. }
  287. ast_str_make_space(&escape, (valsz = strlen(value)) * 2 + 1);
  288. mysql_real_escape_string(&mysql, ast_str_buffer(escape), value, valsz);
  289. ast_str_append(&sql1, 0, "`%s`", entry->name);
  290. ast_str_append(&sql2, 0, "'%s'", ast_str_buffer(escape));
  291. }
  292. }
  293. AST_RWLIST_UNLOCK(&columns);
  294. ast_debug(1, "Inserting a CDR record.\n");
  295. ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
  296. ast_debug(1, "SQL command as follows: %s\n", ast_str_buffer(sql1));
  297. if (mysql_real_query(&mysql, ast_str_buffer(sql1), ast_str_strlen(sql1))) {
  298. ast_log(LOG_ERROR, "Failed to insert into database: (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  299. mysql_close(&mysql);
  300. connected = 0;
  301. } else {
  302. records++;
  303. totalrecords++;
  304. }
  305. }
  306. ast_mutex_unlock(&mysql_lock);
  307. return 0;
  308. }
  309. static int my_unload_module(int reload)
  310. {
  311. struct unload_string *us;
  312. struct column *entry;
  313. ast_cli_unregister_multiple(cdr_mysql_status_cli, sizeof(cdr_mysql_status_cli) / sizeof(struct ast_cli_entry));
  314. if (connected) {
  315. mysql_close(&mysql);
  316. connected = 0;
  317. records = 0;
  318. }
  319. AST_LIST_LOCK(&unload_strings);
  320. while ((us = AST_LIST_REMOVE_HEAD(&unload_strings, entry))) {
  321. ast_free(us->str);
  322. ast_free(us);
  323. }
  324. AST_LIST_UNLOCK(&unload_strings);
  325. if (!reload) {
  326. AST_RWLIST_WRLOCK(&columns);
  327. }
  328. while ((entry = AST_RWLIST_REMOVE_HEAD(&columns, list))) {
  329. ast_free(entry);
  330. }
  331. if (!reload) {
  332. AST_RWLIST_UNLOCK(&columns);
  333. }
  334. dbport = 0;
  335. if (reload) {
  336. return ast_cdr_backend_suspend(name);
  337. } else {
  338. return ast_cdr_unregister(name);
  339. }
  340. }
  341. static int my_load_config_string(struct ast_config *cfg, const char *category, const char *variable, struct ast_str **field, const char *def)
  342. {
  343. struct unload_string *us;
  344. const char *tmp;
  345. if (!(us = ast_calloc(1, sizeof(*us))))
  346. return -1;
  347. if (!(*field = ast_str_create(16))) {
  348. ast_free(us);
  349. return -1;
  350. }
  351. tmp = ast_variable_retrieve(cfg, category, variable);
  352. ast_str_set(field, 0, "%s", tmp ? tmp : def);
  353. us->str = *field;
  354. AST_LIST_LOCK(&unload_strings);
  355. AST_LIST_INSERT_HEAD(&unload_strings, us, entry);
  356. AST_LIST_UNLOCK(&unload_strings);
  357. return 0;
  358. }
  359. static int my_load_config_number(struct ast_config *cfg, const char *category, const char *variable, int *field, int def)
  360. {
  361. const char *tmp;
  362. tmp = ast_variable_retrieve(cfg, category, variable);
  363. if (!tmp || sscanf(tmp, "%30d", field) < 1)
  364. *field = def;
  365. return 0;
  366. }
  367. static int my_load_module(int reload)
  368. {
  369. int res;
  370. struct ast_config *cfg;
  371. struct ast_variable *var;
  372. /* CONFIG_STATUS_FILEUNCHANGED is impossible when config_flags is always 0,
  373. * and it has to be zero, so a reload can be sent to tell the driver to
  374. * rescan the table layout. */
  375. struct ast_flags config_flags = { 0 };
  376. struct column *entry;
  377. char *temp;
  378. struct ast_str *compat;
  379. MYSQL_ROW row;
  380. MYSQL_RES *result;
  381. char sqldesc[128];
  382. #if MYSQL_VERSION_ID >= 50013
  383. my_bool my_bool_true = 1;
  384. #endif
  385. /* Cannot use a conditionally different flag, because the table layout may
  386. * have changed, which is not detectable by config file change detection,
  387. * but should still cause the configuration to be re-parsed. */
  388. cfg = ast_config_load(config, config_flags);
  389. if (cfg == CONFIG_STATUS_FILEMISSING) {
  390. ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
  391. return AST_MODULE_LOAD_SUCCESS;
  392. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  393. ast_log(LOG_ERROR, "Unable to load configuration file '%s'\n", config);
  394. return AST_MODULE_LOAD_DECLINE;
  395. }
  396. if (reload) {
  397. AST_RWLIST_WRLOCK(&columns);
  398. my_unload_module(1);
  399. }
  400. var = ast_variable_browse(cfg, "global");
  401. if (!var) {
  402. /* nothing configured */
  403. if (reload) {
  404. AST_RWLIST_UNLOCK(&columns);
  405. }
  406. ast_config_destroy(cfg);
  407. return AST_MODULE_LOAD_SUCCESS;
  408. }
  409. res = 0;
  410. res |= my_load_config_string(cfg, "global", "hostname", &hostname, "localhost");
  411. res |= my_load_config_string(cfg, "global", "dbname", &dbname, "astriskcdrdb");
  412. res |= my_load_config_string(cfg, "global", "user", &dbuser, "root");
  413. res |= my_load_config_string(cfg, "global", "sock", &dbsock, "");
  414. res |= my_load_config_string(cfg, "global", "table", &dbtable, "cdr");
  415. res |= my_load_config_string(cfg, "global", "password", &password, "");
  416. res |= my_load_config_string(cfg, "global", "charset", &dbcharset, "");
  417. res |= my_load_config_string(cfg, "global", "ssl_ca", &ssl_ca, "");
  418. res |= my_load_config_string(cfg, "global", "ssl_cert", &ssl_cert, "");
  419. res |= my_load_config_string(cfg, "global", "ssl_key", &ssl_key, "");
  420. res |= my_load_config_number(cfg, "global", "port", &dbport, 0);
  421. res |= my_load_config_number(cfg, "global", "timeout", &timeout, 0);
  422. res |= my_load_config_string(cfg, "global", "compat", &compat, "no");
  423. res |= my_load_config_string(cfg, "global", "cdrzone", &cdrzone, "");
  424. if (ast_str_strlen(cdrzone) == 0) {
  425. for (; var; var = var->next) {
  426. if (!strcasecmp(var->name, "usegmtime") && ast_true(var->value)) {
  427. ast_str_set(&cdrzone, 0, "UTC");
  428. }
  429. }
  430. }
  431. if (ast_true(ast_str_buffer(compat))) {
  432. calldate_compat = 1;
  433. } else {
  434. calldate_compat = 0;
  435. }
  436. if (res < 0) {
  437. if (reload) {
  438. AST_RWLIST_UNLOCK(&columns);
  439. }
  440. ast_config_destroy(cfg);
  441. return AST_MODULE_LOAD_FAILURE;
  442. }
  443. /* Check for any aliases */
  444. if (!reload) {
  445. /* Lock, if not already */
  446. AST_RWLIST_WRLOCK(&columns);
  447. }
  448. while ((entry = AST_LIST_REMOVE_HEAD(&columns, list))) {
  449. ast_free(entry);
  450. }
  451. ast_debug(1, "Got hostname of %s\n", ast_str_buffer(hostname));
  452. ast_debug(1, "Got port of %d\n", dbport);
  453. ast_debug(1, "Got a timeout of %d\n", timeout);
  454. if (dbsock)
  455. ast_debug(1, "Got sock file of %s\n", ast_str_buffer(dbsock));
  456. ast_debug(1, "Got user of %s\n", ast_str_buffer(dbuser));
  457. ast_debug(1, "Got dbname of %s\n", ast_str_buffer(dbname));
  458. ast_debug(1, "Got password of %s\n", ast_str_buffer(password));
  459. ast_debug(1, "%sunning in calldate compatibility mode\n", calldate_compat ? "R" : "Not r");
  460. ast_debug(1, "Dates and times are localized to %s\n", S_OR(ast_str_buffer(cdrzone), "local timezone"));
  461. if (dbcharset) {
  462. ast_debug(1, "Got DB charset of %s\n", ast_str_buffer(dbcharset));
  463. }
  464. mysql_init(&mysql);
  465. if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout) != 0) {
  466. ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  467. }
  468. #if MYSQL_VERSION_ID >= 50013
  469. /* Add option for automatic reconnection */
  470. if (mysql_options(&mysql, MYSQL_OPT_RECONNECT, &my_bool_true) != 0) {
  471. ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  472. }
  473. #endif
  474. if ((ssl_ca && ast_str_strlen(ssl_ca)) || (ssl_cert && ast_str_strlen(ssl_cert)) || (ssl_key && ast_str_strlen(ssl_key))) {
  475. mysql_ssl_set(&mysql,
  476. ssl_key ? ast_str_buffer(ssl_key) : NULL,
  477. ssl_cert ? ast_str_buffer(ssl_cert) : NULL,
  478. ssl_ca ? ast_str_buffer(ssl_ca) : NULL,
  479. NULL, NULL);
  480. }
  481. temp = dbsock && ast_str_strlen(dbsock) ? ast_str_buffer(dbsock) : NULL;
  482. if (!mysql_real_connect(&mysql, ast_str_buffer(hostname), ast_str_buffer(dbuser), ast_str_buffer(password), ast_str_buffer(dbname), dbport, temp, ssl_ca && ast_str_strlen(ssl_ca) ? CLIENT_SSL : 0)) {
  483. ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", ast_str_buffer(dbname), ast_str_buffer(hostname));
  484. connected = 0;
  485. records = 0;
  486. } else {
  487. ast_debug(1, "Successfully connected to MySQL database.\n");
  488. connected = 1;
  489. records = 0;
  490. connect_time = time(NULL);
  491. if (dbcharset) {
  492. snprintf(sqldesc, sizeof(sqldesc), "SET NAMES '%s'", ast_str_buffer(dbcharset));
  493. mysql_real_query(&mysql, sqldesc, strlen(sqldesc));
  494. ast_debug(1, "SQL command as follows: %s\n", sqldesc);
  495. }
  496. /* Get table description */
  497. snprintf(sqldesc, sizeof(sqldesc), "DESC %s", dbtable ? ast_str_buffer(dbtable) : "cdr");
  498. if (mysql_query(&mysql, sqldesc)) {
  499. ast_log(LOG_ERROR, "Unable to query table description!! Logging disabled.\n");
  500. mysql_close(&mysql);
  501. connected = 0;
  502. AST_RWLIST_UNLOCK(&columns);
  503. ast_config_destroy(cfg);
  504. return AST_MODULE_LOAD_FAILURE;
  505. }
  506. if (!(result = mysql_store_result(&mysql))) {
  507. ast_log(LOG_ERROR, "Unable to query table description!! Logging disabled.\n");
  508. mysql_close(&mysql);
  509. connected = 0;
  510. AST_RWLIST_UNLOCK(&columns);
  511. ast_config_destroy(cfg);
  512. return AST_MODULE_LOAD_FAILURE;
  513. }
  514. while ((row = mysql_fetch_row(result))) {
  515. struct column *entry;
  516. char *cdrvar = "", *staticvalue = "";
  517. ast_debug(1, "Got a field '%s' of type '%s'\n", row[0], row[1]);
  518. /* Check for an alias or a static value */
  519. for (var = ast_variable_browse(cfg, "columns"); var; var = var->next) {
  520. if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, row[0]) == 0 ) {
  521. char *alias = ast_strdupa(var->name + 5);
  522. cdrvar = ast_strip(alias);
  523. ast_verb(3, "Found alias %s for column %s\n", cdrvar, row[0]);
  524. break;
  525. } else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, row[0]) == 0) {
  526. char *item = ast_strdupa(var->name + 6);
  527. item = ast_strip(item);
  528. if (item[0] == '"' && item[strlen(item) - 1] == '"') {
  529. /* Remove surrounding quotes */
  530. item[strlen(item) - 1] = '\0';
  531. item++;
  532. }
  533. staticvalue = item;
  534. }
  535. }
  536. entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(row[0]) + 1 + strlen(cdrvar) + 1 + strlen(staticvalue) + 1 + strlen(row[1]) + 1);
  537. if (!entry) {
  538. ast_log(LOG_ERROR, "Out of memory creating entry for column '%s'\n", row[0]);
  539. res = -1;
  540. break;
  541. }
  542. entry->name = (char *)entry + sizeof(*entry);
  543. strcpy(entry->name, row[0]);
  544. if (!ast_strlen_zero(cdrvar)) {
  545. entry->cdrname = entry->name + strlen(row[0]) + 1;
  546. strcpy(entry->cdrname, cdrvar);
  547. } else { /* Point to same place as the column name */
  548. entry->cdrname = (char *)entry + sizeof(*entry);
  549. }
  550. if (!ast_strlen_zero(staticvalue)) {
  551. entry->staticvalue = entry->cdrname + strlen(entry->cdrname) + 1;
  552. strcpy(entry->staticvalue, staticvalue);
  553. ast_debug(1, "staticvalue length: %d\n", (int) strlen(staticvalue) );
  554. entry->type = entry->staticvalue + strlen(entry->staticvalue) + 1;
  555. } else {
  556. entry->type = entry->cdrname + strlen(entry->cdrname) + 1;
  557. }
  558. strcpy(entry->type, row[1]);
  559. ast_debug(1, "Entry name '%s'\n", entry->name);
  560. ast_debug(1, " cdrname '%s'\n", entry->cdrname);
  561. ast_debug(1, " static '%s'\n", entry->staticvalue);
  562. ast_debug(1, " type '%s'\n", entry->type);
  563. AST_LIST_INSERT_TAIL(&columns, entry, list);
  564. }
  565. mysql_free_result(result);
  566. }
  567. AST_RWLIST_UNLOCK(&columns);
  568. ast_config_destroy(cfg);
  569. if (res < 0) {
  570. return AST_MODULE_LOAD_FAILURE;
  571. }
  572. if (!reload) {
  573. res = ast_cdr_register(name, desc, mysql_log);
  574. } else {
  575. res = ast_cdr_backend_unsuspend(name);
  576. }
  577. if (res) {
  578. ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
  579. } else {
  580. res = ast_cli_register_multiple(cdr_mysql_status_cli, sizeof(cdr_mysql_status_cli) / sizeof(struct ast_cli_entry));
  581. }
  582. return res;
  583. }
  584. static int load_module(void)
  585. {
  586. return my_load_module(0);
  587. }
  588. static int unload_module(void)
  589. {
  590. return my_unload_module(0);
  591. }
  592. static int reload(void)
  593. {
  594. int ret;
  595. ast_mutex_lock(&mysql_lock);
  596. ret = my_load_module(1);
  597. ast_mutex_unlock(&mysql_lock);
  598. return ret;
  599. }
  600. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "MySQL CDR Backend",
  601. .support_level = AST_MODULE_SUPPORT_DEPRECATED,
  602. .load = load_module,
  603. .unload = unload_module,
  604. .reload = reload,
  605. );