cdr_mysql.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. ast_cdr_setvar(cdr, "calldate", timestr, 0);
  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_getvar(cdr, cdrname, &value, workspace, sizeof(workspace), 0, 1);
  248. } else {
  249. ast_cdr_getvar(cdr, cdrname, &value, workspace, sizeof(workspace), 0, 0);
  250. }
  251. if (value) {
  252. size_t valsz;
  253. if (column_count++) {
  254. ast_str_append(&sql1, 0, ",");
  255. ast_str_append(&sql2, 0, ",");
  256. }
  257. if (!strcasecmp(cdrname, "billsec") &&
  258. (strstr(entry->type, "float") ||
  259. strstr(entry->type, "double") ||
  260. strstr(entry->type, "decimal") ||
  261. strstr(entry->type, "numeric") ||
  262. strstr(entry->type, "real"))) {
  263. if (!ast_tvzero(cdr->answer)) {
  264. snprintf(workspace, sizeof(workspace), "%lf",
  265. (double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0));
  266. } else {
  267. ast_copy_string(workspace, "0", sizeof(workspace));
  268. }
  269. if (!ast_strlen_zero(workspace)) {
  270. value = workspace;
  271. }
  272. }
  273. if (!strcasecmp(cdrname, "duration") &&
  274. (strstr(entry->type, "float") ||
  275. strstr(entry->type, "double") ||
  276. strstr(entry->type, "decimal") ||
  277. strstr(entry->type, "numeric") ||
  278. strstr(entry->type, "real"))) {
  279. snprintf(workspace, sizeof(workspace), "%lf",
  280. (double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0));
  281. if (!ast_strlen_zero(workspace)) {
  282. value = workspace;
  283. }
  284. }
  285. ast_str_make_space(&escape, (valsz = strlen(value)) * 2 + 1);
  286. mysql_real_escape_string(&mysql, ast_str_buffer(escape), value, valsz);
  287. ast_str_append(&sql1, 0, "`%s`", entry->name);
  288. ast_str_append(&sql2, 0, "'%s'", ast_str_buffer(escape));
  289. }
  290. }
  291. AST_RWLIST_UNLOCK(&columns);
  292. ast_debug(1, "Inserting a CDR record.\n");
  293. ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
  294. ast_debug(1, "SQL command as follows: %s\n", ast_str_buffer(sql1));
  295. if (mysql_real_query(&mysql, ast_str_buffer(sql1), ast_str_strlen(sql1))) {
  296. ast_log(LOG_ERROR, "Failed to insert into database: (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  297. mysql_close(&mysql);
  298. connected = 0;
  299. } else {
  300. records++;
  301. totalrecords++;
  302. }
  303. }
  304. ast_mutex_unlock(&mysql_lock);
  305. return 0;
  306. }
  307. static int my_unload_module(int reload)
  308. {
  309. struct unload_string *us;
  310. struct column *entry;
  311. ast_cli_unregister_multiple(cdr_mysql_status_cli, sizeof(cdr_mysql_status_cli) / sizeof(struct ast_cli_entry));
  312. if (connected) {
  313. mysql_close(&mysql);
  314. connected = 0;
  315. records = 0;
  316. }
  317. AST_LIST_LOCK(&unload_strings);
  318. while ((us = AST_LIST_REMOVE_HEAD(&unload_strings, entry))) {
  319. ast_free(us->str);
  320. ast_free(us);
  321. }
  322. AST_LIST_UNLOCK(&unload_strings);
  323. if (!reload) {
  324. AST_RWLIST_WRLOCK(&columns);
  325. }
  326. while ((entry = AST_RWLIST_REMOVE_HEAD(&columns, list))) {
  327. ast_free(entry);
  328. }
  329. if (!reload) {
  330. AST_RWLIST_UNLOCK(&columns);
  331. }
  332. dbport = 0;
  333. ast_cdr_unregister(name);
  334. return 0;
  335. }
  336. static int my_load_config_string(struct ast_config *cfg, const char *category, const char *variable, struct ast_str **field, const char *def)
  337. {
  338. struct unload_string *us;
  339. const char *tmp;
  340. if (!(us = ast_calloc(1, sizeof(*us))))
  341. return -1;
  342. if (!(*field = ast_str_create(16))) {
  343. ast_free(us);
  344. return -1;
  345. }
  346. tmp = ast_variable_retrieve(cfg, category, variable);
  347. ast_str_set(field, 0, "%s", tmp ? tmp : def);
  348. us->str = *field;
  349. AST_LIST_LOCK(&unload_strings);
  350. AST_LIST_INSERT_HEAD(&unload_strings, us, entry);
  351. AST_LIST_UNLOCK(&unload_strings);
  352. return 0;
  353. }
  354. static int my_load_config_number(struct ast_config *cfg, const char *category, const char *variable, int *field, int def)
  355. {
  356. const char *tmp;
  357. tmp = ast_variable_retrieve(cfg, category, variable);
  358. if (!tmp || sscanf(tmp, "%30d", field) < 1)
  359. *field = def;
  360. return 0;
  361. }
  362. static int my_load_module(int reload)
  363. {
  364. int res;
  365. struct ast_config *cfg;
  366. struct ast_variable *var;
  367. /* CONFIG_STATUS_FILEUNCHANGED is impossible when config_flags is always 0,
  368. * and it has to be zero, so a reload can be sent to tell the driver to
  369. * rescan the table layout. */
  370. struct ast_flags config_flags = { 0 };
  371. struct column *entry;
  372. char *temp;
  373. struct ast_str *compat;
  374. MYSQL_ROW row;
  375. MYSQL_RES *result;
  376. char sqldesc[128];
  377. #if MYSQL_VERSION_ID >= 50013
  378. my_bool my_bool_true = 1;
  379. #endif
  380. /* Cannot use a conditionally different flag, because the table layout may
  381. * have changed, which is not detectable by config file change detection,
  382. * but should still cause the configuration to be re-parsed. */
  383. cfg = ast_config_load(config, config_flags);
  384. if (cfg == CONFIG_STATUS_FILEMISSING) {
  385. ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
  386. return AST_MODULE_LOAD_SUCCESS;
  387. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  388. ast_log(LOG_ERROR, "Unable to load configuration file '%s'\n", config);
  389. return AST_MODULE_LOAD_DECLINE;
  390. }
  391. if (reload) {
  392. AST_RWLIST_WRLOCK(&columns);
  393. my_unload_module(1);
  394. }
  395. var = ast_variable_browse(cfg, "global");
  396. if (!var) {
  397. /* nothing configured */
  398. if (reload) {
  399. AST_RWLIST_UNLOCK(&columns);
  400. }
  401. ast_config_destroy(cfg);
  402. return AST_MODULE_LOAD_SUCCESS;
  403. }
  404. res = 0;
  405. res |= my_load_config_string(cfg, "global", "hostname", &hostname, "localhost");
  406. res |= my_load_config_string(cfg, "global", "dbname", &dbname, "astriskcdrdb");
  407. res |= my_load_config_string(cfg, "global", "user", &dbuser, "root");
  408. res |= my_load_config_string(cfg, "global", "sock", &dbsock, "");
  409. res |= my_load_config_string(cfg, "global", "table", &dbtable, "cdr");
  410. res |= my_load_config_string(cfg, "global", "password", &password, "");
  411. res |= my_load_config_string(cfg, "global", "charset", &dbcharset, "");
  412. res |= my_load_config_string(cfg, "global", "ssl_ca", &ssl_ca, "");
  413. res |= my_load_config_string(cfg, "global", "ssl_cert", &ssl_cert, "");
  414. res |= my_load_config_string(cfg, "global", "ssl_key", &ssl_key, "");
  415. res |= my_load_config_number(cfg, "global", "port", &dbport, 0);
  416. res |= my_load_config_number(cfg, "global", "timeout", &timeout, 0);
  417. res |= my_load_config_string(cfg, "global", "compat", &compat, "no");
  418. res |= my_load_config_string(cfg, "global", "cdrzone", &cdrzone, "");
  419. if (ast_str_strlen(cdrzone) == 0) {
  420. for (; var; var = var->next) {
  421. if (!strcasecmp(var->name, "usegmtime") && ast_true(var->value)) {
  422. ast_str_set(&cdrzone, 0, "UTC");
  423. }
  424. }
  425. }
  426. if (ast_true(ast_str_buffer(compat))) {
  427. calldate_compat = 1;
  428. } else {
  429. calldate_compat = 0;
  430. }
  431. if (res < 0) {
  432. if (reload) {
  433. AST_RWLIST_UNLOCK(&columns);
  434. }
  435. ast_config_destroy(cfg);
  436. return AST_MODULE_LOAD_FAILURE;
  437. }
  438. /* Check for any aliases */
  439. if (!reload) {
  440. /* Lock, if not already */
  441. AST_RWLIST_WRLOCK(&columns);
  442. }
  443. while ((entry = AST_LIST_REMOVE_HEAD(&columns, list))) {
  444. ast_free(entry);
  445. }
  446. ast_debug(1, "Got hostname of %s\n", ast_str_buffer(hostname));
  447. ast_debug(1, "Got port of %d\n", dbport);
  448. ast_debug(1, "Got a timeout of %d\n", timeout);
  449. if (dbsock)
  450. ast_debug(1, "Got sock file of %s\n", ast_str_buffer(dbsock));
  451. ast_debug(1, "Got user of %s\n", ast_str_buffer(dbuser));
  452. ast_debug(1, "Got dbname of %s\n", ast_str_buffer(dbname));
  453. ast_debug(1, "Got password of %s\n", ast_str_buffer(password));
  454. ast_debug(1, "%sunning in calldate compatibility mode\n", calldate_compat ? "R" : "Not r");
  455. ast_debug(1, "Dates and times are localized to %s\n", S_OR(ast_str_buffer(cdrzone), "local timezone"));
  456. if (dbcharset) {
  457. ast_debug(1, "Got DB charset of %s\n", ast_str_buffer(dbcharset));
  458. }
  459. mysql_init(&mysql);
  460. if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout) != 0) {
  461. ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  462. }
  463. #if MYSQL_VERSION_ID >= 50013
  464. /* Add option for automatic reconnection */
  465. if (mysql_options(&mysql, MYSQL_OPT_RECONNECT, &my_bool_true) != 0) {
  466. ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  467. }
  468. #endif
  469. if ((ssl_ca && ast_str_strlen(ssl_ca)) || (ssl_cert && ast_str_strlen(ssl_cert)) || (ssl_key && ast_str_strlen(ssl_key))) {
  470. mysql_ssl_set(&mysql,
  471. ssl_key ? ast_str_buffer(ssl_key) : NULL,
  472. ssl_cert ? ast_str_buffer(ssl_cert) : NULL,
  473. ssl_ca ? ast_str_buffer(ssl_ca) : NULL,
  474. NULL, NULL);
  475. }
  476. temp = dbsock && ast_str_strlen(dbsock) ? ast_str_buffer(dbsock) : NULL;
  477. 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)) {
  478. ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", ast_str_buffer(dbname), ast_str_buffer(hostname));
  479. connected = 0;
  480. records = 0;
  481. } else {
  482. ast_debug(1, "Successfully connected to MySQL database.\n");
  483. connected = 1;
  484. records = 0;
  485. connect_time = time(NULL);
  486. if (dbcharset) {
  487. snprintf(sqldesc, sizeof(sqldesc), "SET NAMES '%s'", ast_str_buffer(dbcharset));
  488. mysql_real_query(&mysql, sqldesc, strlen(sqldesc));
  489. ast_debug(1, "SQL command as follows: %s\n", sqldesc);
  490. }
  491. /* Get table description */
  492. snprintf(sqldesc, sizeof(sqldesc), "DESC %s", dbtable ? ast_str_buffer(dbtable) : "cdr");
  493. if (mysql_query(&mysql, sqldesc)) {
  494. ast_log(LOG_ERROR, "Unable to query table description!! Logging disabled.\n");
  495. mysql_close(&mysql);
  496. connected = 0;
  497. AST_RWLIST_UNLOCK(&columns);
  498. ast_config_destroy(cfg);
  499. return AST_MODULE_LOAD_FAILURE;
  500. }
  501. if (!(result = mysql_store_result(&mysql))) {
  502. ast_log(LOG_ERROR, "Unable to query table description!! Logging disabled.\n");
  503. mysql_close(&mysql);
  504. connected = 0;
  505. AST_RWLIST_UNLOCK(&columns);
  506. ast_config_destroy(cfg);
  507. return AST_MODULE_LOAD_FAILURE;
  508. }
  509. while ((row = mysql_fetch_row(result))) {
  510. struct column *entry;
  511. char *cdrvar = "", *staticvalue = "";
  512. ast_debug(1, "Got a field '%s' of type '%s'\n", row[0], row[1]);
  513. /* Check for an alias or a static value */
  514. for (var = ast_variable_browse(cfg, "columns"); var; var = var->next) {
  515. if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, row[0]) == 0 ) {
  516. char *alias = ast_strdupa(var->name + 5);
  517. cdrvar = ast_strip(alias);
  518. ast_verb(3, "Found alias %s for column %s\n", cdrvar, row[0]);
  519. break;
  520. } else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, row[0]) == 0) {
  521. char *item = ast_strdupa(var->name + 6);
  522. item = ast_strip(item);
  523. if (item[0] == '"' && item[strlen(item) - 1] == '"') {
  524. /* Remove surrounding quotes */
  525. item[strlen(item) - 1] = '\0';
  526. item++;
  527. }
  528. staticvalue = item;
  529. }
  530. }
  531. entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(row[0]) + 1 + strlen(cdrvar) + 1 + strlen(staticvalue) + 1 + strlen(row[1]) + 1);
  532. if (!entry) {
  533. ast_log(LOG_ERROR, "Out of memory creating entry for column '%s'\n", row[0]);
  534. res = -1;
  535. break;
  536. }
  537. entry->name = (char *)entry + sizeof(*entry);
  538. strcpy(entry->name, row[0]);
  539. if (!ast_strlen_zero(cdrvar)) {
  540. entry->cdrname = entry->name + strlen(row[0]) + 1;
  541. strcpy(entry->cdrname, cdrvar);
  542. } else { /* Point to same place as the column name */
  543. entry->cdrname = (char *)entry + sizeof(*entry);
  544. }
  545. if (!ast_strlen_zero(staticvalue)) {
  546. entry->staticvalue = entry->cdrname + strlen(entry->cdrname) + 1;
  547. strcpy(entry->staticvalue, staticvalue);
  548. ast_debug(1, "staticvalue length: %d\n", (int) strlen(staticvalue) );
  549. entry->type = entry->staticvalue + strlen(entry->staticvalue) + 1;
  550. } else {
  551. entry->type = entry->cdrname + strlen(entry->cdrname) + 1;
  552. }
  553. strcpy(entry->type, row[1]);
  554. ast_debug(1, "Entry name '%s'\n", entry->name);
  555. ast_debug(1, " cdrname '%s'\n", entry->cdrname);
  556. ast_debug(1, " static '%s'\n", entry->staticvalue);
  557. ast_debug(1, " type '%s'\n", entry->type);
  558. AST_LIST_INSERT_TAIL(&columns, entry, list);
  559. }
  560. mysql_free_result(result);
  561. }
  562. AST_RWLIST_UNLOCK(&columns);
  563. ast_config_destroy(cfg);
  564. if (res < 0) {
  565. return AST_MODULE_LOAD_FAILURE;
  566. }
  567. res = ast_cdr_register(name, desc, mysql_log);
  568. if (res) {
  569. ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
  570. } else {
  571. res = ast_cli_register_multiple(cdr_mysql_status_cli, sizeof(cdr_mysql_status_cli) / sizeof(struct ast_cli_entry));
  572. }
  573. return res;
  574. }
  575. static int load_module(void)
  576. {
  577. return my_load_module(0);
  578. }
  579. static int unload_module(void)
  580. {
  581. return my_unload_module(0);
  582. }
  583. static int reload(void)
  584. {
  585. int ret;
  586. ast_mutex_lock(&mysql_lock);
  587. ret = my_load_module(1);
  588. ast_mutex_unlock(&mysql_lock);
  589. return ret;
  590. }
  591. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "MySQL CDR Backend",
  592. .load = load_module,
  593. .unload = unload_module,
  594. .reload = reload,
  595. );