Add master extraction via REST API
The master server of a monitor can now be extracted with the mon_get_external_master function. It uses the REST API to find a master server in the set of servers monitored by a monitor.
This commit is contained in:
@ -338,4 +338,18 @@ MXS_MONITOR_API MonitorApi<MonitorInstance>::s_api =
|
|||||||
&MonitorApi<MonitorInstance>::diagnostics_json,
|
&MonitorApi<MonitorInstance>::diagnostics_json,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the master server of the monitor pointed by @c url
|
||||||
|
*
|
||||||
|
* The URL must be in `http://hostname-of-maxscale:<rest-api-port>/v1/monitors/<name-of-monitor>`
|
||||||
|
* format and must point to a single monitor resource.
|
||||||
|
*
|
||||||
|
* TODO: Move the base URL and credentials into a global parameter
|
||||||
|
*
|
||||||
|
* @param url URL to a REST API endpoint for a monitor
|
||||||
|
*
|
||||||
|
* @return The host and port of the monitor or an empty string and 0 if an error occurred
|
||||||
|
*/
|
||||||
|
std::pair<std::string, int> mon_get_external_master(const std::string& url);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2866,4 +2866,59 @@ void MonitorInstance::run_one_tick()
|
|||||||
store_server_journal(m_monitor, m_master);
|
store_server_journal(m_monitor, m_master);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool remote_server_is_master(const std::string& url, std::string* host, int* port)
|
||||||
|
{
|
||||||
|
bool rval = false;
|
||||||
|
auto res = mxs::http::get(url);
|
||||||
|
json_t* state = mxs_json_pointer(res.body.get(), "data/attributes/state");
|
||||||
|
json_t* json_host = mxs_json_pointer(res.body.get(), "data/attributes/parameters/address");
|
||||||
|
json_t* json_port = mxs_json_pointer(res.body.get(), "data/attributes/parameters/port");
|
||||||
|
|
||||||
|
if (json_is_string(json_host) && json_is_integer(json_port) && json_is_string(state))
|
||||||
|
{
|
||||||
|
const std::set<std::string> master_states{ "Master, Running", "Master, Synced, Running" };
|
||||||
|
|
||||||
|
if (master_states.count(json_string_value(state)))
|
||||||
|
{
|
||||||
|
*host = json_string_value(json_host);
|
||||||
|
*port = json_integer_value(json_port);
|
||||||
|
rval = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rval;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<std::string, int> mon_get_external_master(const std::string& url)
|
||||||
|
{
|
||||||
|
std::string host;
|
||||||
|
int port = 0;
|
||||||
|
|
||||||
|
auto res = mxs::http::get(url);
|
||||||
|
json_t* remote = mxs_json_pointer(res.body.get(), "data/relationships/servers/links/self");
|
||||||
|
json_t* arr = mxs_json_pointer(res.body.get(), "data/relationships/servers/data");
|
||||||
|
|
||||||
|
if (json_is_string(remote) && json_is_array(arr))
|
||||||
|
{
|
||||||
|
std::string remote_host = json_string_value(remote);
|
||||||
|
size_t i;
|
||||||
|
json_t* value;
|
||||||
|
|
||||||
|
json_array_foreach(arr, i, value)
|
||||||
|
{
|
||||||
|
json_t* id = mxs_json_pointer(value, "id");
|
||||||
|
|
||||||
|
if (json_is_string(id))
|
||||||
|
{
|
||||||
|
if (remote_server_is_master(remote_host + json_string_value(id), &host, &port))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {host, port};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user