MXS-1220: Add monitor to JSON conversion
Monitors can now be printed in JSON format. The REST API resource `/monitors` accepts GET requests and returns a JSON representation of the monitors as a response.
This commit is contained in:

committed by
Markus Mäkelä

parent
d2e4d9cc64
commit
75b44198ba
@ -244,4 +244,20 @@ void mon_process_state_changes(MXS_MONITOR *monitor, const char *script, uint64_
|
|||||||
*/
|
*/
|
||||||
void mon_hangup_failed_servers(MXS_MONITOR *monitor);
|
void mon_hangup_failed_servers(MXS_MONITOR *monitor);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Convert monitor to JSON
|
||||||
|
*
|
||||||
|
* @param monitor Monitor to convert
|
||||||
|
*
|
||||||
|
* @return JSON representation of the monitor
|
||||||
|
*/
|
||||||
|
json_t* monitor_to_json(const MXS_MONITOR* monitor);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Convert all monitors to JSON
|
||||||
|
*
|
||||||
|
* @return JSON array containing all monitors
|
||||||
|
*/
|
||||||
|
json_t* monitor_list_to_json();
|
||||||
|
|
||||||
MXS_END_DECLS
|
MXS_END_DECLS
|
||||||
|
@ -33,6 +33,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <maxscale/alloc.h>
|
#include <maxscale/alloc.h>
|
||||||
#include <mysqld_error.h>
|
#include <mysqld_error.h>
|
||||||
@ -48,6 +49,8 @@
|
|||||||
#include "maxscale/monitor.h"
|
#include "maxscale/monitor.h"
|
||||||
#include "maxscale/modules.h"
|
#include "maxscale/modules.h"
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
|
||||||
static MXS_MONITOR *allMonitors = NULL;
|
static MXS_MONITOR *allMonitors = NULL;
|
||||||
static SPINLOCK monLock = SPINLOCK_INIT;
|
static SPINLOCK monLock = SPINLOCK_INIT;
|
||||||
|
|
||||||
@ -1508,3 +1511,84 @@ void mon_process_state_changes(MXS_MONITOR *monitor, const char *script, uint64_
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char* monitor_state_to_string(int state)
|
||||||
|
{
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case MONITOR_STATE_RUNNING:
|
||||||
|
return "Running";
|
||||||
|
|
||||||
|
case MONITOR_STATE_STOPPING:
|
||||||
|
return "Stopping";
|
||||||
|
|
||||||
|
case MONITOR_STATE_STOPPED:
|
||||||
|
return "Stopped";
|
||||||
|
|
||||||
|
case MONITOR_STATE_ALLOC:
|
||||||
|
return "Allocated";
|
||||||
|
|
||||||
|
default:
|
||||||
|
ss_dassert(false);
|
||||||
|
return "Unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
json_t* monitor_to_json(const MXS_MONITOR* monitor)
|
||||||
|
{
|
||||||
|
json_t* rval = json_object();
|
||||||
|
|
||||||
|
json_object_set_new(rval, "name", json_string(monitor->name));
|
||||||
|
json_object_set_new(rval, "state", json_string(monitor_state_to_string(monitor->state)));
|
||||||
|
|
||||||
|
json_object_set_new(rval, "monitor_interval", json_integer(monitor->interval));
|
||||||
|
|
||||||
|
json_object_set_new(rval, "connect_timeout", json_integer(monitor->connect_timeout));
|
||||||
|
json_object_set_new(rval, "read_timeout", json_integer(monitor->read_timeout));
|
||||||
|
json_object_set_new(rval, "write_timeout", json_integer(monitor->write_timeout));
|
||||||
|
json_object_set_new(rval, "connect_attempts", json_integer(monitor->connect_attempts));
|
||||||
|
|
||||||
|
if (monitor->databases)
|
||||||
|
{
|
||||||
|
json_t* arr = json_array();
|
||||||
|
|
||||||
|
for (MXS_MONITOR_SERVERS *db = monitor->databases; db; db = db->next)
|
||||||
|
{
|
||||||
|
string serv = "/servers/";
|
||||||
|
serv += db->server->unique_name;
|
||||||
|
json_array_append_new(arr, json_string(serv.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object_set_new(rval, "servers", arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (monitor->handle && monitor->module->diagnostics)
|
||||||
|
{
|
||||||
|
// TODO: Add monitor diagnostics
|
||||||
|
//monitor->module->diagnostics(dcb, monitor);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rval;
|
||||||
|
}
|
||||||
|
|
||||||
|
json_t* monitor_list_to_json()
|
||||||
|
{
|
||||||
|
json_t* rval = json_array();
|
||||||
|
|
||||||
|
spinlock_acquire(&monLock);
|
||||||
|
|
||||||
|
for (MXS_MONITOR* mon = allMonitors; mon; mon = mon->next)
|
||||||
|
{
|
||||||
|
json_t *json = monitor_to_json(mon);
|
||||||
|
|
||||||
|
if (json)
|
||||||
|
{
|
||||||
|
json_array_append_new(rval, json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
spinlock_release(&monLock);
|
||||||
|
|
||||||
|
return rval;
|
||||||
|
}
|
||||||
|
@ -139,10 +139,13 @@ class MonitorsResource: public Resource
|
|||||||
protected:
|
protected:
|
||||||
HttpResponse handle(HttpRequest& request)
|
HttpResponse handle(HttpRequest& request)
|
||||||
{
|
{
|
||||||
|
int flags = request.get_option("pretty") == "true" ? JSON_INDENT(4) : 0;
|
||||||
|
|
||||||
if (request.uri_part_count() == 1)
|
if (request.uri_part_count() == 1)
|
||||||
{
|
{
|
||||||
|
Closer<json_t*> monitors(monitor_list_to_json());
|
||||||
// Show all monitors
|
// Show all monitors
|
||||||
return HttpResponse(HTTP_200_OK);
|
return HttpResponse(HTTP_200_OK, mxs::json_dump(monitors, flags));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -150,8 +153,9 @@ protected:
|
|||||||
|
|
||||||
if (monitor)
|
if (monitor)
|
||||||
{
|
{
|
||||||
|
Closer<json_t*> monitor_js(monitor_to_json(monitor));
|
||||||
// Show one monitor
|
// Show one monitor
|
||||||
return HttpResponse(HTTP_200_OK);
|
return HttpResponse(HTTP_200_OK, mxs::json_dump(monitor_js, flags));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user