diff --git a/include/maxscale/monitor.h b/include/maxscale/monitor.h index 02e5e2457..7370a06a1 100644 --- a/include/maxscale/monitor.h +++ b/include/maxscale/monitor.h @@ -244,4 +244,20 @@ void mon_process_state_changes(MXS_MONITOR *monitor, const char *script, uint64_ */ 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 diff --git a/server/core/monitor.cc b/server/core/monitor.cc index 03ec9cf8d..5ff6e5fb3 100644 --- a/server/core/monitor.cc +++ b/server/core/monitor.cc @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -48,6 +49,8 @@ #include "maxscale/monitor.h" #include "maxscale/modules.h" +using std::string; + static MXS_MONITOR *allMonitors = NULL; 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; +} diff --git a/server/core/resource.cc b/server/core/resource.cc index e6abc8dd4..10ec51a5a 100644 --- a/server/core/resource.cc +++ b/server/core/resource.cc @@ -139,10 +139,13 @@ class MonitorsResource: public Resource protected: HttpResponse handle(HttpRequest& request) { + int flags = request.get_option("pretty") == "true" ? JSON_INDENT(4) : 0; + if (request.uri_part_count() == 1) { + Closer monitors(monitor_list_to_json()); // Show all monitors - return HttpResponse(HTTP_200_OK); + return HttpResponse(HTTP_200_OK, mxs::json_dump(monitors, flags)); } else { @@ -150,8 +153,9 @@ protected: if (monitor) { + Closer monitor_js(monitor_to_json(monitor)); // Show one monitor - return HttpResponse(HTTP_200_OK); + return HttpResponse(HTTP_200_OK, mxs::json_dump(monitor_js, flags)); } else {