From d66262ccfcb5dbbda4ed77904d190e3da0cc035f Mon Sep 17 00:00:00 2001 From: Kirill Solomko Date: Tue, 24 Feb 2015 13:38:51 +0100 Subject: [PATCH] MT#11157 Add separate mysql handler for the stats db - use separate user,pass,port,db for the stats database connection - cosmetic change in the DEFAULT_STATS definition names - add db connection info logging for PROV and STATS Change-Id: I389bed46c634ac973616335581d7a7495d5866a3 --- config.c | 49 ++++++++++++++++++++++++++++++++++++++--- config.h | 4 ++++ debian/mediator.default | 4 ++++ debian/mediator.init | 4 ++++ mediator.c | 4 ++++ mediator.h | 8 +++++-- medmysql.c | 30 ++++++++++++++++++++++--- 7 files changed, 95 insertions(+), 8 deletions(-) diff --git a/config.c b/config.c index ee2b476..2050917 100644 --- a/config.c +++ b/config.c @@ -26,8 +26,12 @@ char *config_prov_pass = MEDIATOR_DEFAULT_PROVPASS; char *config_prov_db = MEDIATOR_DEFAULT_PROVDB; unsigned int config_prov_port = MEDIATOR_DEFAULT_PROVPORT; +char *config_stats_host = MEDIATOR_DEFAULT_STATSHOST; +char *config_stats_user = MEDIATOR_DEFAULT_STATSUSER; +char *config_stats_pass = MEDIATOR_DEFAULT_STATSPASS; char *config_stats_db = MEDIATOR_DEFAULT_STATSDB; -med_stats_period_t config_stats_period = MEDIATOR_DEFAULT_STATS_PERIOD; +unsigned int config_stats_port = MEDIATOR_DEFAULT_STATSPORT; +med_stats_period_t config_stats_period = MEDIATOR_DEFAULT_STATSPERIOD; static u_int8_t config_pid_path_free = 0; @@ -46,6 +50,9 @@ static u_int8_t config_prov_user_free = 0; static u_int8_t config_prov_pass_free = 0; static u_int8_t config_prov_db_free = 0; +static u_int8_t config_stats_host_free = 0; +static u_int8_t config_stats_user_free = 0; +static u_int8_t config_stats_pass_free = 0; static u_int8_t config_stats_db_free = 0; static void config_help(const char *self) @@ -71,6 +78,10 @@ static void config_help(const char *self) " -R\tThe prov db user (default = '%s').\n" \ " -A\tThe prov db pass (default = '%s').\n" \ " -N\tThe prov db name (default = '%s').\n" \ + " -Z\tThe stats db host (default = '%s').\n" \ + " -z\tThe stats db port (default = '%d').\n" \ + " -W\tThe stats db user (default = '%s').\n" \ + " -w\tThe stats db pass (default = '%s').\n" \ " -X\tThe stats db name (default = '%s').\n" \ " -x\tThe stats db period (default = '%d', 1=hour, 2=day, 3=month).\n" \ " -?\tDisplays this message.\n", @@ -78,14 +89,15 @@ static void config_help(const char *self) config_med_host, config_med_port, config_med_user, config_med_pass, config_med_db, config_cdr_host, config_cdr_port, config_cdr_user, config_cdr_pass, config_cdr_db, config_prov_host, config_prov_port, config_prov_user, config_prov_pass, config_prov_db, - config_stats_db, config_stats_period); + config_stats_host, config_stats_port, config_stats_user, config_stats_pass, config_stats_db, + config_stats_period); } int config_parse_cmdopts(int argc, char **argv) { int c; - while((c = getopt(argc, argv, "D:i:dl?h:u:p:b:o:H:U:P:B:O:S:T:R:A:N:X:x:")) != -1) + while((c = getopt(argc, argv, "D:i:dl?h:u:p:b:o:H:U:P:B:O:S:T:R:A:N:Z:z:W:w:X:x:")) != -1) { if(c == '?') { @@ -181,6 +193,25 @@ int config_parse_cmdopts(int argc, char **argv) { config_prov_port = atoi(optarg); } + else if(c == 'Z') + { + config_stats_host = (char*)strdup(optarg); + config_stats_host_free = 1; + } + else if(c == 'z') + { + config_stats_port = atoi(optarg); + } + else if(c == 'W') + { + config_stats_user = (char*)strdup(optarg); + config_stats_user_free = 1; + } + else if(c == 'w') + { + config_stats_pass = (char*)strdup(optarg); + config_stats_pass_free = 1; + } else if(c == 'X') { config_stats_db = (char*)strdup(optarg); @@ -249,6 +280,18 @@ void config_cleanup() { free(config_prov_db); } + if(config_stats_host_free) + { + free(config_stats_host); + } + if(config_stats_user_free) + { + free(config_stats_user); + } + if(config_stats_pass_free) + { + free(config_stats_pass); + } if(config_stats_db_free) { free(config_stats_db); diff --git a/config.h b/config.h index 4569826..cd5f430 100644 --- a/config.h +++ b/config.h @@ -26,6 +26,10 @@ extern char *config_prov_user; extern char *config_prov_pass; extern char *config_prov_db; +extern char *config_stats_host; +extern unsigned int config_stats_port; +extern char *config_stats_user; +extern char *config_stats_pass; extern char *config_stats_db; extern med_stats_period_t config_stats_period; diff --git a/debian/mediator.default b/debian/mediator.default index 6aa0b15..087e3b2 100644 --- a/debian/mediator.default +++ b/debian/mediator.default @@ -17,5 +17,9 @@ PROV_PORT="3306" PROV_USER="mediator" PROV_PASS='GimmeAllUr$$$' PROV_DB="provisioning" +STATS_HOST="localhost" +STATS_PORT="3306" +STATS_USER="mediator" +STATS_PASS='GimmeAllUr$$$' STATS_DB="sipstats" STATS_PERIOD=1 diff --git a/debian/mediator.init b/debian/mediator.init index 3a43075..82541c9 100755 --- a/debian/mediator.init +++ b/debian/mediator.init @@ -59,6 +59,10 @@ OPTIONS="" [ -z "$PROV_USER" ] || OPTIONS="$OPTIONS -R $PROV_USER" [ -z "$PROV_PASS" ] || OPTIONS="$OPTIONS -A $PROV_PASS" [ -z "$PROV_DB" ] || OPTIONS="$OPTIONS -N $PROV_DB" +[ -z "$STATS_HOST" ] || OPTIONS="$OPTIONS -Z $STATS_HOST" +[ -z "$STATS_PORT" ] || OPTIONS="$OPTIONS -z $STATS_PORT" +[ -z "$STATS_USER" ] || OPTIONS="$OPTIONS -W $STATS_USER" +[ -z "$STATS_PASS" ] || OPTIONS="$OPTIONS -w $STATS_PASS" [ -z "$STATS_DB" ] || OPTIONS="$OPTIONS -X $STATS_DB" [ -z "$STATS_PERIOD" ] || OPTIONS="$OPTIONS -x $STATS_PERIOD" if test "$FORK" = "yes" ; then diff --git a/mediator.c b/mediator.c index 1c7c96e..3132344 100644 --- a/mediator.c +++ b/mediator.c @@ -199,6 +199,10 @@ int main(int argc, char **argv) config_med_host, config_med_port, config_med_user, config_med_db); syslog(LOG_INFO, "CDR acc database host='%s', port='%d', user='%s', name='%s'", config_cdr_host, config_cdr_port, config_cdr_user, config_cdr_db); + syslog(LOG_INFO, "PROV database host='%s', port='%d', user='%s', name='%s'", + config_prov_host, config_prov_port, config_prov_user, config_prov_db); + syslog(LOG_INFO, "STATS database host='%s', port='%d', user='%s', name='%s'", + config_stats_host, config_stats_port, config_stats_user, config_stats_db); syslog(LOG_DEBUG, "Setting up mysql connections."); if(medmysql_init() != 0) diff --git a/mediator.h b/mediator.h index 9cb84c5..6c709ef 100644 --- a/mediator.h +++ b/mediator.h @@ -38,8 +38,12 @@ #define MEDIATOR_DEFAULT_PROVDB "provisioning" #define MEDIATOR_DEFAULT_PROVPORT 0 -#define MEDIATOR_DEFAULT_STATSDB "sipstats" -#define MEDIATOR_DEFAULT_STATS_PERIOD MED_STATS_HOUR +#define MEDIATOR_DEFAULT_STATSHOST "localhost" +#define MEDIATOR_DEFAULT_STATSUSER "mediator" +#define MEDIATOR_DEFAULT_STATSPASS "GimmeAllUr$$$" +#define MEDIATOR_DEFAULT_STATSDB "sipstats" +#define MEDIATOR_DEFAULT_STATSPORT 0 +#define MEDIATOR_DEFAULT_STATSPERIOD MED_STATS_HOUR #define MED_GW_STRING "gw" #define MED_AS_STRING "as" diff --git a/medmysql.c b/medmysql.c index 6d0d917..2b84eb0 100644 --- a/medmysql.c +++ b/medmysql.c @@ -23,6 +23,7 @@ static MYSQL *cdr_handler = NULL; static MYSQL *med_handler = NULL; static MYSQL *prov_handler = NULL; +static MYSQL *stats_handler = NULL; static int medmysql_flush_cdr(struct medmysql_batches *); static int medmysql_flush_medlist(struct medmysql_str *); @@ -97,6 +98,20 @@ int medmysql_init() goto err; } + stats_handler = mysql_init(NULL); + if(!mysql_real_connect(stats_handler, + config_stats_host, config_stats_user, config_stats_pass, + config_stats_db, config_stats_port, NULL, 0)) + { + syslog(LOG_CRIT, "Error connecting to STATS db: %s", mysql_error(stats_handler)); + goto err; + } + if(mysql_options(stats_handler, MYSQL_OPT_RECONNECT, &recon) != 0) + { + syslog(LOG_CRIT, "Error setting reconnect-option for STATS db: %s", mysql_error(stats_handler)); + goto err; + } + return 0; err: @@ -122,6 +137,11 @@ void medmysql_cleanup() mysql_close(prov_handler); prov_handler = NULL; } + if(stats_handler != NULL) + { + mysql_close(stats_handler); + stats_handler = NULL; + } } /**********************************************************************/ @@ -637,6 +657,8 @@ int medmysql_batch_start(struct medmysql_batches *batches) { return -1; if (mysql_query_wrapper(med_handler, "start transaction", 17)) return -1; + if (mysql_query_wrapper(stats_handler, "start transaction", 17)) + return -1; if (!med_call_stat_info_table) med_call_stat_info_table = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); @@ -711,7 +733,7 @@ static int medmysql_flush_medlist(struct medmysql_str *str) { } static int medmysql_flush_call_stat_info() { - if (!med_handler) + if (!stats_handler) return 0; if (!med_call_stat_info_table) return 0; @@ -742,10 +764,10 @@ static int medmysql_flush_call_stat_info() { //syslog(LOG_DEBUG, "updating call stats info: %s -- %s", period_t->call_code, period_t->period); //syslog(LOG_DEBUG, "sql: %s", query.str); - if(mysql_query_wrapper(med_handler, query.str, query.len) != 0) + if(mysql_query_wrapper(stats_handler, query.str, query.len) != 0) { syslog(LOG_CRIT, "Error executing call info stats query: %s", - mysql_error(med_handler)); + mysql_error(stats_handler)); critical("Failed to execute potentially crucial SQL query, check syslog for details"); return -1; } @@ -773,6 +795,8 @@ int medmysql_batch_end(struct medmysql_batches *batches) { return -1; if (mysql_query_wrapper(med_handler, "commit", 6)) return -1; + if (mysql_query_wrapper(stats_handler, "commit", 6)) + return -1; return 0; }