From 926558b003819017ca4c792780719de3b924f34a Mon Sep 17 00:00:00 2001 From: Tzafrir Cohen Date: Thu, 3 May 2018 16:21:12 +0300 Subject: cdr_mysql: run a full reconnect on log if not connected If the MySQL CDR backend is not connected at startup, it will try to reconnect (with a reloaded configuration) each time it needs to log a record. However it does not run the full connection startup sequence and thus fails to set up the columns to log. As a result all subsequent records will be empty. This commit makes the mysql backend run the same connection sequence in that case as it does on module load. ASTERISK-27572 #close Change-Id: I7603c7501dae7070fac35081cf35161579c47590 --- addons/cdr_mysql.c | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/addons/cdr_mysql.c b/addons/cdr_mysql.c index 766c1fc0a..3a0e95788 100644 --- a/addons/cdr_mysql.c +++ b/addons/cdr_mysql.c @@ -169,13 +169,12 @@ static void configure_connection_charset(void) } } +static int my_connect_db(struct ast_config *cfg); + static int mysql_log(struct ast_cdr *cdr) { struct ast_str *sql1 = ast_str_thread_get(&sql1_buf, 1024), *sql2 = ast_str_thread_get(&sql2_buf, 1024); int retries = 5; -#if MYSQL_VERSION_ID >= 50013 - my_bool my_bool_true = 1; -#endif if (!sql1 || !sql2) { ast_log(LOG_ERROR, "Memory error\n"); @@ -186,31 +185,30 @@ static int mysql_log(struct ast_cdr *cdr) db_reconnect: if (!connected) { - /* Attempt to connect */ - mysql_init(&mysql); - /* Add option to quickly timeout the connection */ - if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout) != 0) { - ast_log(LOG_ERROR, "mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql)); - } -#if MYSQL_VERSION_ID >= 50013 - /* Add option for automatic reconnection */ - if (mysql_options(&mysql, MYSQL_OPT_RECONNECT, &my_bool_true) != 0) { - ast_log(LOG_ERROR, "mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql)); - } -#endif - if (ssl_ca || ssl_cert || ssl_key) { - 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); - } + struct ast_flags config_flags = { 0 }; + struct ast_config *cfg = ast_config_load(config, config_flags); + ast_verb(3, "Not connected. Attemptint to re-establish connection to MySQL CDR database.\n"); + if (cfg == CONFIG_STATUS_FILEMISSING) { + ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config); + } else if (cfg == CONFIG_STATUS_FILEINVALID) { + ast_log(LOG_ERROR, "Unable to load configuration file '%s'\n", config); + } else { + int res; - configure_connection_charset(); + AST_RWLIST_WRLOCK(&columns); + res = my_connect_db(cfg); + AST_RWLIST_UNLOCK(&columns); + ast_config_destroy(cfg); - 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)) { - connected = 1; - connect_time = time(NULL); - records = 0; - } else { - ast_log(LOG_ERROR, "Cannot connect to database server %s: (%d) %s\n", ast_str_buffer(hostname), mysql_errno(&mysql), mysql_error(&mysql)); - connected = 0; + if (res == AST_MODULE_LOAD_SUCCESS) { + connected = 1; + connect_time = time(NULL); + records = 0; + ast_verb(1, "Re-established connection to MySQL CDR database.\n"); + } else { + ast_verb(3, "Failed to re-establish connection to MySQL CDR database.\n"); + connected = 0; + } } } else { /* Long connection - ping the server */ -- cgit v1.2.3