diff --git a/include/MySQL_Monitor.hpp b/include/MySQL_Monitor.hpp index 99849e008..8954da65f 100644 --- a/include/MySQL_Monitor.hpp +++ b/include/MySQL_Monitor.hpp @@ -45,8 +45,8 @@ class MySQL_Monitor { private: - unsigned int MySQL_Monitor__thread_MySQL_Thread_Variables_version; - MySQL_Thread *mysql_thr; + //unsigned int MySQL_Monitor__thread_MySQL_Thread_Variables_version; + //MySQL_Thread *mysql_thr; std::vector *tables_defs_monitor; void insert_into_tables_defs(std::vector *tables_defs, const char *table_name, const char *table_def); void drop_tables_defs(std::vector *tables_defs); @@ -57,6 +57,8 @@ class MySQL_Monitor { MySQL_Monitor(); ~MySQL_Monitor(); void print_version(); + void * monitor_connect(); + void * monitor_ping(); void * run(); }; diff --git a/lib/MySQL_Monitor.cpp b/lib/MySQL_Monitor.cpp index 7fb97a526..14040ca0f 100644 --- a/lib/MySQL_Monitor.cpp +++ b/lib/MySQL_Monitor.cpp @@ -1,3 +1,4 @@ +#include #include "proxysql.h" #include "cpp.h" @@ -15,7 +16,6 @@ extern MySQL_Threads_Handler *GloMTH; MySQL_Monitor::MySQL_Monitor() { shutdown=false; - // create new SQLite datatabase monitordb = new SQLite3DB(); monitordb->open((char *)"file:mem_monitordb?mode=memory&cache=shared", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX); @@ -30,11 +30,6 @@ MySQL_Monitor::MySQL_Monitor() { // create monitoring tables check_and_build_standard_tables(monitordb, tables_defs_monitor); - // initialize the MySQL Thread (note: this is not a real thread, just the structures associated with it) - mysql_thr = new MySQL_Thread(); - mysql_thr->curtime=monotonic_time(); - MySQL_Monitor__thread_MySQL_Thread_Variables_version=GloMTH->get_global_version(); - mysql_thr->refresh_variables(); }; @@ -42,7 +37,7 @@ MySQL_Monitor::~MySQL_Monitor() { drop_tables_defs(tables_defs_monitor); delete tables_defs_monitor; delete monitordb; - delete mysql_thr; + //delete mysql_thr; fprintf(stderr,"MySQL_Monitor destroyed\n"); }; @@ -84,9 +79,57 @@ void MySQL_Monitor::check_and_build_standard_tables(SQLite3DB *db, std::vectorexecute("PRAGMA foreign_keys = ON"); }; -void * MySQL_Monitor::run() { + +void * MySQL_Monitor::monitor_connect() { + // initialize the MySQL Thread (note: this is not a real thread, just the structures associated with it) + unsigned int MySQL_Monitor__thread_MySQL_Thread_Variables_version; + MySQL_Thread * mysql_thr = new MySQL_Thread(); + mysql_thr->curtime=monotonic_time(); + MySQL_Monitor__thread_MySQL_Thread_Variables_version=GloMTH->get_global_version(); + mysql_thr->refresh_variables(); + while (shutdown==false) { + unsigned int glover=GloMTH->get_global_version(); + if (MySQL_Monitor__thread_MySQL_Thread_Variables_version < glover ) { + MySQL_Monitor__thread_MySQL_Thread_Variables_version=glover; + mysql_thr->refresh_variables(); + fprintf(stderr,"MySQL_Monitor - CONNECT - refreshing variables\n"); + } + fprintf(stderr,"MySQL_Monitor - CONNECT\n"); + usleep(1000000); + } + return NULL; +} + +void * MySQL_Monitor::monitor_ping() { + // initialize the MySQL Thread (note: this is not a real thread, just the structures associated with it) + unsigned int MySQL_Monitor__thread_MySQL_Thread_Variables_version; + MySQL_Thread * mysql_thr = new MySQL_Thread(); + mysql_thr->curtime=monotonic_time(); + MySQL_Monitor__thread_MySQL_Thread_Variables_version=GloMTH->get_global_version(); + mysql_thr->refresh_variables(); while (shutdown==false) { + unsigned int glover=GloMTH->get_global_version(); + if (MySQL_Monitor__thread_MySQL_Thread_Variables_version < glover ) { + MySQL_Monitor__thread_MySQL_Thread_Variables_version=glover; + mysql_thr->refresh_variables(); + fprintf(stderr,"MySQL_Monitor - PING - refreshing variables\n"); + } + fprintf(stderr,"MySQL_Monitor - PING\n"); usleep(1000000); + } + return NULL; +} + +void * MySQL_Monitor::run() { + // initialize the MySQL Thread (note: this is not a real thread, just the structures associated with it) + unsigned int MySQL_Monitor__thread_MySQL_Thread_Variables_version; + MySQL_Thread * mysql_thr = new MySQL_Thread(); + mysql_thr->curtime=monotonic_time(); + MySQL_Monitor__thread_MySQL_Thread_Variables_version=GloMTH->get_global_version(); + mysql_thr->refresh_variables(); + std::thread * monitor_connect_thread = new std::thread(&MySQL_Monitor::monitor_connect,this); + std::thread * monitor_ping_thread = new std::thread(&MySQL_Monitor::monitor_ping,this); + while (shutdown==false) { unsigned int glover=GloMTH->get_global_version(); if (MySQL_Monitor__thread_MySQL_Thread_Variables_version < glover ) { MySQL_Monitor__thread_MySQL_Thread_Variables_version=glover; @@ -94,6 +137,9 @@ void * MySQL_Monitor::run() { fprintf(stderr,"MySQL_Monitor refreshing variables\n"); } fprintf(stderr,"MySQL_Monitor\n"); + usleep(1000000); } + monitor_connect_thread->join(); + monitor_ping_thread->join(); return NULL; };