Move monitor script processing and launching into the core
This removes parts of the nearly identical code from all monitors. The removal of monitor type specific event checking is done based on the assumption that only the monitor that is monitoring the server can be the cause for a state change. This removes the need to actually check that the state change is relevant for each monitor and allows the event handling to be moved into the core.
This commit is contained in:
@ -27,7 +27,7 @@ typedef struct aurora_monitor
|
||||
bool shutdown; /**< True if the monitor is stopped */
|
||||
THREAD thread; /**< Monitor thread */
|
||||
char* script; /**< Launchable script */
|
||||
bool events[MAX_MONITOR_EVENT]; /**< Enabled monitor events */
|
||||
uint64_t events; /**< Enabled monitor events */
|
||||
} AURORA_MONITOR;
|
||||
|
||||
/**
|
||||
@ -101,39 +101,6 @@ void update_server_status(MONITOR *monitor, MONITOR_SERVERS *database)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if this is an event that the Aurora monitor handles
|
||||
* @param event Event to check
|
||||
* @return True if the event is monitored, false if it is not
|
||||
* */
|
||||
bool is_aurora_event(monitor_event_t event)
|
||||
{
|
||||
static monitor_event_t aurora_events[] =
|
||||
{
|
||||
MASTER_DOWN_EVENT,
|
||||
MASTER_UP_EVENT,
|
||||
SLAVE_DOWN_EVENT,
|
||||
SLAVE_UP_EVENT,
|
||||
SERVER_DOWN_EVENT,
|
||||
SERVER_UP_EVENT,
|
||||
LOST_MASTER_EVENT,
|
||||
LOST_SLAVE_EVENT,
|
||||
NEW_MASTER_EVENT,
|
||||
NEW_SLAVE_EVENT,
|
||||
MAX_MONITOR_EVENT
|
||||
};
|
||||
|
||||
for (int i = 0; aurora_events[i] != MAX_MONITOR_EVENT; i++)
|
||||
{
|
||||
if (event == aurora_events[i])
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Main monitoring loop
|
||||
*
|
||||
@ -170,25 +137,9 @@ monitorMain(void *arg)
|
||||
/**
|
||||
* After updating the status of all servers, check if monitor events
|
||||
* need to be launched.
|
||||
*
|
||||
* TODO: Move this functionality into monitor.c, it is duplicated in
|
||||
* every monitor.
|
||||
*/
|
||||
for (MONITOR_SERVERS *ptr = monitor->databases; ptr; ptr = ptr->next)
|
||||
{
|
||||
if (mon_status_changed(ptr))
|
||||
{
|
||||
monitor_event_t evtype = mon_get_event_type(ptr);
|
||||
if (is_aurora_event(evtype))
|
||||
{
|
||||
mon_log_state_change(ptr);
|
||||
if (handle->script && handle->events[evtype])
|
||||
{
|
||||
monitor_launch_script(monitor, ptr, handle->script);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
mon_process_state_changes(monitor, handle->script, handle->events);
|
||||
|
||||
servers_status_current_to_pending(monitor);
|
||||
release_monitor_servers(monitor);
|
||||
|
||||
@ -233,12 +184,12 @@ static void auroramon_free(AURORA_MONITOR *handle)
|
||||
static void *
|
||||
startMonitor(MONITOR *mon, const CONFIG_PARAMETER *params)
|
||||
{
|
||||
bool have_events = false, script_error = false;
|
||||
AURORA_MONITOR *handle = mon->handle;
|
||||
|
||||
if (handle)
|
||||
{
|
||||
handle->shutdown = false;
|
||||
MXS_FREE(handle->script);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -248,35 +199,6 @@ startMonitor(MONITOR *mon, const CONFIG_PARAMETER *params)
|
||||
}
|
||||
|
||||
handle->shutdown = false;
|
||||
handle->script = NULL;
|
||||
memset(handle->events, false, sizeof(handle->events));
|
||||
|
||||
while (params)
|
||||
{
|
||||
if (strcmp(params->name, "script") == 0)
|
||||
{
|
||||
if (externcmd_can_execute(params->value))
|
||||
{
|
||||
handle->script = MXS_STRDUP_A(params->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
script_error = true;
|
||||
}
|
||||
}
|
||||
else if (strcmp(params->name, "events") == 0)
|
||||
{
|
||||
if (mon_parse_event_string(handle->events, sizeof(handle->events), params->value) != 0)
|
||||
{
|
||||
script_error = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
have_events = true;
|
||||
}
|
||||
}
|
||||
params = params->next;
|
||||
}
|
||||
|
||||
if (!check_monitor_permissions(mon, "SELECT @@aurora_server_id, server_id FROM "
|
||||
"information_schema.replica_host_status "
|
||||
@ -286,22 +208,11 @@ startMonitor(MONITOR *mon, const CONFIG_PARAMETER *params)
|
||||
auroramon_free(handle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (script_error)
|
||||
{
|
||||
MXS_ERROR("Errors were found in the script configuration parameters "
|
||||
"for the monitor '%s'.", mon->name);
|
||||
auroramon_free(handle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** If no specific events are given, enable them all */
|
||||
if (!have_events)
|
||||
{
|
||||
memset(handle->events, true, sizeof(handle->events));
|
||||
}
|
||||
}
|
||||
|
||||
handle->script = config_copy_string(params, "script");
|
||||
handle->events = config_get_enum(params, "events", monitor_event_enum_values);
|
||||
|
||||
if (thread_start(&handle->thread, monitorMain, mon) == NULL)
|
||||
{
|
||||
MXS_ERROR("Failed to start monitor thread for monitor '%s'.", mon->name);
|
||||
@ -366,6 +277,19 @@ MXS_MODULE* MXS_CREATE_MODULE()
|
||||
NULL, /* Thread init. */
|
||||
NULL, /* Thread finish. */
|
||||
{
|
||||
{
|
||||
"script",
|
||||
MXS_MODULE_PARAM_PATH,
|
||||
NULL,
|
||||
MXS_MODULE_OPT_PATH_X_OK
|
||||
},
|
||||
{
|
||||
"events",
|
||||
MXS_MODULE_PARAM_ENUM,
|
||||
MONITOR_EVENT_DEFAULT_VALUE,
|
||||
MXS_MODULE_OPT_NONE,
|
||||
monitor_event_enum_values
|
||||
},
|
||||
{MXS_END_MODULE_PARAMS}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user