Added filtering of monitor events.

This commit is contained in:
Markus Makela
2015-05-14 12:05:58 +03:00
parent 1aba13177b
commit b94f494d80
11 changed files with 195 additions and 54 deletions

View File

@ -66,8 +66,6 @@ MONITOR *mon;
{
return NULL;
}
mon->state = MONITOR_STATE_ALLOC;
mon->name = strdup(name);
if ((mon->module = load_module(module, MODULE_MONITOR)) == NULL)
{
@ -79,10 +77,10 @@ MONITOR *mon;
free(mon);
return NULL;
}
mon->state = MONITOR_STATE_ALLOC;
mon->name = strdup(name);
mon->handle = NULL;
mon->databases = NULL;
mon->name = NULL;
mon->password = NULL;
mon->read_timeout = DEFAULT_READ_TIMEOUT;
mon->write_timeout = DEFAULT_WRITE_TIMEOUT;

View File

@ -120,46 +120,59 @@ static void *
startMonitor(void *arg,void* opt)
{
MONITOR* mon = arg;
GALERA_MONITOR *handle = mon->handle;
CONFIG_PARAMETER* params = (CONFIG_PARAMETER*)opt;
if (handle != NULL)
{
handle->shutdown = 0;
}
else
{
if ((handle = (GALERA_MONITOR *)malloc(sizeof(GALERA_MONITOR))) == NULL)
return NULL;
handle->shutdown = 0;
handle->id = MONITOR_DEFAULT_ID;
handle->disableMasterFailback = 0;
handle->availableWhenDonor = 0;
handle->disableMasterRoleSetting = 0;
handle->master = NULL;
handle->script = NULL;
spinlock_init(&handle->lock);
}
GALERA_MONITOR *handle = mon->handle;
CONFIG_PARAMETER* params = (CONFIG_PARAMETER*)opt;
bool have_events = false;
if (handle != NULL)
{
handle->shutdown = 0;
}
else
{
if ((handle = (GALERA_MONITOR *)malloc(sizeof(GALERA_MONITOR))) == NULL)
return NULL;
handle->shutdown = 0;
handle->id = MONITOR_DEFAULT_ID;
handle->disableMasterFailback = 0;
handle->availableWhenDonor = 0;
handle->disableMasterRoleSetting = 0;
handle->master = NULL;
handle->script = NULL;
memset(handle->events,false,sizeof(handle->events));
spinlock_init(&handle->lock);
}
while(params)
while(params)
{
if(!strcmp(params->name,"disable_master_failback"))
handle->disableMasterFailback = config_truth_value(params->value);
else if(!strcmp(params->name,"available_when_donor"))
handle->availableWhenDonor = config_truth_value(params->value);
else if(!strcmp(params->name,"disable_master_role_setting"))
handle->disableMasterRoleSetting = config_truth_value(params->value);
else if(!strcmp(params->name,"script"))
{
if(!strcmp(params->name,"disable_master_failback"))
handle->disableMasterFailback = config_truth_value(params->value);
else if(!strcmp(params->name,"available_when_donor"))
handle->availableWhenDonor = config_truth_value(params->value);
else if(!strcmp(params->name,"disable_master_role_setting"))
handle->disableMasterRoleSetting = config_truth_value(params->value);
else if(!strcmp(params->name,"script"))
{
if(handle->script)
free(handle->script);
handle->script = strdup(params->value);
}
params = params->next;
if(handle->script)
free(handle->script);
handle->script = strdup(params->value);
}
else if(!strcmp(params->name,"events"))
{
mon_parse_event_string(&handle->events,sizeof(handle->events),params->value);
have_events = true;
}
params = params->next;
}
handle->tid = (THREAD)thread_start(monitorMain, mon);
return handle;
/** If no specific events are given, enable them all */
if(!have_events)
{
memset(handle->events,true,sizeof(handle->events));
}
handle->tid = (THREAD)thread_start(monitorMain, mon);
return handle;
}
/**
@ -388,6 +401,7 @@ MONITOR_SERVERS *candidate_master = NULL;
int master_stickiness = handle->disableMasterFailback;
int is_cluster=0;
int log_no_members = 1;
monitor_event_t evtype;
if (mysql_thread_init())
{
@ -554,14 +568,15 @@ int log_no_members = 1;
while(ptr)
{
/** Execute monitor script if a server state has changed */
if(mon_status_changed(ptr) && mon_get_event_type(ptr) != UNDEFINED_MONITOR_EVENT)
if(mon_status_changed(ptr) && (evtype = mon_get_event_type(ptr)) != UNDEFINED_MONITOR_EVENT)
{
skygw_log_write(LOGFILE_TRACE,"Server changed state: %s[%s:%u]: %s",
ptr->server->unique_name,
ptr->server->name,ptr->server->port,
mon_get_event_name(ptr));
if(handle->script)
if(handle->script && handle->events[evtype])
{
monitor_launch_script(mon,ptr,handle->script);
}

View File

@ -61,6 +61,7 @@ typedef struct {
int disableMasterRoleSetting; /**< Monitor flag to disable setting master role */
MONITOR_SERVERS *master; /**< Master server for MySQL Master/Slave replication */
char* script;
bool events[MAX_MONITOR_EVENT]; /*< enabled events */
} GALERA_MONITOR;
#endif

View File

@ -112,6 +112,8 @@ startMonitor(void *arg,void* opt)
MONITOR* mon = (MONITOR*)arg;
MM_MONITOR *handle = mon->handle;
CONFIG_PARAMETER* params = (CONFIG_PARAMETER*)opt;
bool have_events = false;
if (handle)
{
handle->shutdown = 0;
@ -123,6 +125,7 @@ startMonitor(void *arg,void* opt)
handle->shutdown = 0;
handle->id = MONITOR_DEFAULT_ID;
handle->master = NULL;
memset(handle->events,false,sizeof(handle->events));
spinlock_init(&handle->lock);
}
@ -141,9 +144,18 @@ startMonitor(void *arg,void* opt)
handle->script = strdup(params->value);
}
else if(!strcmp(params->name,"events"))
{
mon_parse_event_string(&handle->events,sizeof(handle->events),params->value);
have_events = true;
}
params = params->next;
}
/** If no specific events are given, enable them all */
if(!have_events)
{
memset(handle->events,true,sizeof(handle->events));
}
handle->tid = (THREAD)thread_start(monitorMain, mon);
return handle;
}
@ -579,17 +591,17 @@ size_t nrounds = 0;
}
ptr = mon->databases;
monitor_event_t evtype;
while(ptr)
{
/** Execute monitor script if a server state has changed */
if(mon_status_changed(ptr) && mon_get_event_type(ptr) != UNDEFINED_MONITOR_EVENT)
if(mon_status_changed(ptr) && (evtype = mon_get_event_type(ptr)) != UNDEFINED_MONITOR_EVENT)
{
skygw_log_write(LOGFILE_TRACE,"Server changed state: %s[%s:%u]: %s",
ptr->server->unique_name,
ptr->server->name,ptr->server->port,
mon_get_event_name(ptr));
if(handle->script)
if(handle->script && handle->events[evtype])
{
monitor_launch_script(mon,ptr,handle->script);
}

View File

@ -46,6 +46,7 @@ typedef struct {
int detectStaleMaster; /**< Monitor flag for Stale Master detection */
MONITOR_SERVERS *master; /**< Master server for Master/Slave replication */
char* script; /*< Script to call when state changes occur on servers */
bool events[MAX_MONITOR_EVENT]; /*< enabled events */
} MM_MONITOR;
#endif

View File

@ -18,6 +18,8 @@
#include <monitor_common.h>
monitor_event_t mon_name_to_event(char* tok);
/**
* Set a pending status bit in the monitor server
*
@ -312,3 +314,85 @@ void monitor_launch_script(MONITOR* mon,MONITOR_SERVERS* ptr, char* script)
}
externcmd_free(cmd);
}
/**
* Parse a string of event names to an array with enabled events.
* @param events Pointer to an array of boolean values
* @param count Size of the array
* @param string String to parse
* @return 0 on success. 1 when an error has occurred or an unexpected event was
* found.
*/
int mon_parse_event_string(bool* events, size_t count,char* string)
{
char *tok,*saved;
monitor_event_t event;
tok = strtok_r(string,",| ",&saved);
if(tok == NULL)
return -1;
while(tok)
{
event = mon_name_to_event(tok);
if(event == UNDEFINED_MONITOR_EVENT)
return -1;
events[event] = true;
tok = strtok_r(NULL,",| ",&saved);
}
return 0;
}
monitor_event_t mon_name_to_event(char* tok)
{
if(!strcasecmp("master_down",tok))
return MASTER_DOWN_EVENT;
else if(!strcasecmp("master_up",tok))
return MASTER_UP_EVENT;
else if(!strcasecmp("slave_down",tok))
return SLAVE_DOWN_EVENT;
else if(!strcasecmp("slave_up",tok))
return SLAVE_UP_EVENT;
else if(!strcasecmp("server_down",tok))
return SERVER_DOWN_EVENT;
else if(!strcasecmp("server_up",tok))
return SERVER_UP_EVENT;
else if(!strcasecmp("synced_down",tok))
return SYNCED_DOWN_EVENT;
else if(!strcasecmp("synced_up",tok))
return SYNCED_UP_EVENT;
else if(!strcasecmp("donor_down",tok))
return DONOR_DOWN_EVENT;
else if(!strcasecmp("donor_up",tok))
return DONOR_UP_EVENT;
else if(!strcasecmp("ndb_down",tok))
return NDB_DOWN_EVENT;
else if(!strcasecmp("ndb_up",tok))
return NDB_UP_EVENT;
else if(!strcasecmp("lost_master",tok))
return LOST_MASTER_EVENT;
else if(!strcasecmp("lost_slave",tok))
return LOST_SLAVE_EVENT;
else if(!strcasecmp("lost_synced",tok))
return LOST_SYNCED_EVENT;
else if(!strcasecmp("lost_donor",tok))
return LOST_DONOR_EVENT;
else if(!strcasecmp("lost_ndb",tok))
return LOST_NDB_EVENT;
else if(!strcasecmp("new_master",tok))
return NEW_MASTER_EVENT;
else if(!strcasecmp("new_slave",tok))
return NEW_SLAVE_EVENT;
else if(!strcasecmp("new_synced",tok))
return NEW_SYNCED_EVENT;
else if(!strcasecmp("new_donor",tok))
return NEW_DONOR_EVENT;
else if(!strcasecmp("new_ndb",tok))
return NEW_NDB_EVENT;
else
return UNDEFINED_MONITOR_EVENT;
}

View File

@ -60,8 +60,8 @@ typedef enum {
NEW_SLAVE_EVENT,
NEW_SYNCED_EVENT,
NEW_DONOR_EVENT,
NEW_NDB_EVENT
NEW_NDB_EVENT,
MAX_MONITOR_EVENT
}monitor_event_t;
void mon_append_node_names(MONITOR_SERVERS* start,char* str, int len);
monitor_event_t mon_get_event_type(MONITOR_SERVERS* node);
@ -71,4 +71,5 @@ void monitor_set_pending_status(MONITOR_SERVERS *ptr, int bit);
bool mon_status_changed(MONITOR_SERVERS* mon_srv);
bool mon_print_fail_status(MONITOR_SERVERS* mon_srv);
void monitor_launch_script(MONITOR* mon,MONITOR_SERVERS* ptr, char* script);
int mon_parse_event_string(bool* events, size_t count,char* string);
#endif

View File

@ -140,6 +140,8 @@ startMonitor(void *arg, void* opt)
MONITOR* monitor = (MONITOR*)arg;
MYSQL_MONITOR *handle = (MYSQL_MONITOR*)monitor->handle;
CONFIG_PARAMETER* params = (CONFIG_PARAMETER*)opt;
bool have_events = false;
if (handle)
{
handle->shutdown = 0;
@ -154,6 +156,7 @@ startMonitor(void *arg, void* opt)
handle->detectStaleMaster = 0;
handle->master = NULL;
handle->script = NULL;
memset(handle->events,false,sizeof(handle->events));
spinlock_init(&handle->lock);
}
@ -169,9 +172,18 @@ startMonitor(void *arg, void* opt)
free(handle->script);
handle->script = strdup(params->value);
}
else if(!strcmp(params->name,"events"))
{
mon_parse_event_string(handle->events,sizeof(handle->events),params->value);
have_events = true;
}
params = params->next;
}
/** If no specific events are given, enable them all */
if(!have_events)
{
memset(handle->events,true,sizeof(handle->events));
}
handle->tid = (THREAD)thread_start(monitorMain, monitor);
return handle;
}
@ -677,17 +689,17 @@ int log_no_master = 1;
}
ptr = mon->databases;
monitor_event_t evtype;
while(ptr)
{
/** Execute monitor script if a server state has changed */
if(mon_status_changed(ptr) && mon_get_event_type(ptr) != UNDEFINED_MONITOR_EVENT)
if(mon_status_changed(ptr) && (evtype = mon_get_event_type(ptr)) != UNDEFINED_MONITOR_EVENT)
{
skygw_log_write(LOGFILE_TRACE,"Server changed state: %s[%s:%u]: %s",
ptr->server->unique_name,
ptr->server->name,ptr->server->port,
mon_get_event_name(ptr));
if(handle->script)
if(handle->script && handle->events[evtype])
{
monitor_launch_script(mon,ptr,handle->script);
}

View File

@ -69,6 +69,7 @@ typedef struct {
int disableMasterRoleSetting; /**< Monitor flag to disable setting master role */
MONITOR_SERVERS *master; /**< Master server for MySQL Master/Slave replication */
char* script; /*< Script to call when state changes occur on servers */
bool events[MAX_MONITOR_EVENT]; /*< enabled events */
} MYSQL_MONITOR;
#endif

View File

@ -110,6 +110,8 @@ startMonitor(void *arg,void* opt)
MONITOR* mon = (MONITOR*)arg;
MYSQL_MONITOR *handle = mon->handle;
CONFIG_PARAMETER* params = (CONFIG_PARAMETER*)opt;
bool have_events = false;
if (handle != NULL)
{
handle->shutdown = 0;
@ -120,6 +122,7 @@ startMonitor(void *arg,void* opt)
return NULL;
handle->shutdown = 0;
handle->id = MONITOR_DEFAULT_ID;
memset(handle->events,false,sizeof(handle->events));
spinlock_init(&handle->lock);
}
while(params)
@ -130,6 +133,17 @@ startMonitor(void *arg,void* opt)
free(handle->script);
handle->script = strdup(params->value);
}
else if(!strcmp(params->name,"events"))
{
mon_parse_event_string(&handle->events,sizeof(handle->events),params->value);
have_events = true;
}
params = params->next;
}
/** If no specific events are given, enable them all */
if(!have_events)
{
memset(handle->events,true,sizeof(handle->events));
}
handle->tid = (THREAD)thread_start(monitorMain, mon);
return handle;
@ -383,17 +397,18 @@ size_t nrounds = 0;
}
ptr = mon->databases;
monitor_event_t evtype;
while(ptr)
{
/** Execute monitor script if a server state has changed */
if(mon_status_changed(ptr) && mon_get_event_type(ptr) != UNDEFINED_MONITOR_EVENT)
if(mon_status_changed(ptr) && (evtype = mon_get_event_type(ptr)) != UNDEFINED_MONITOR_EVENT)
{
skygw_log_write(LOGFILE_TRACE,"Server changed state: %s[%s:%u]: %s",
ptr->server->unique_name,
ptr->server->name,ptr->server->port,
mon_get_event_name(ptr));
if(handle->script)
if(handle->script && handle->events[evtype])
{
monitor_launch_script(mon,ptr,handle->script);
}

View File

@ -50,6 +50,7 @@ typedef struct {
unsigned long id; /**< Monitor ID */
MONITOR_SERVERS *master; /**< Master server for MySQL Master/Slave replication */
char* script; /*< Script to call when state changes occur on servers */
bool events[MAX_MONITOR_EVENT]; /*< enabled events */
} MYSQL_MONITOR;
#endif