diff --git a/server/core/server.c b/server/core/server.c index 9841b96ff..0f39c0edc 100644 --- a/server/core/server.c +++ b/server/core/server.c @@ -293,6 +293,90 @@ char *stat; spinlock_release(&server_spin); } +/** + * Print all servers in Json format to a DCB + * + * Designed to be called within a debugger session in order + * to display all active servers within the gateway + */ +void +dprintAllServersJson(DCB *dcb) +{ +SERVER *ptr; +char *stat; +int len = 0; +int el = 1; + + spinlock_acquire(&server_spin); + ptr = allServers; + while (ptr) + { + ptr = ptr->next; + len++; + } + ptr = allServers; + dcb_printf(dcb, "[\n"); + while (ptr) + { + dcb_printf(dcb, " {\n \"server\": \"%s\",\n", + ptr->name); + stat = server_status(ptr); + dcb_printf(dcb, " \"status\": \"%s\",\n", + stat); + free(stat); + dcb_printf(dcb, " \"protocol\": \"%s\",\n", + ptr->protocol); + dcb_printf(dcb, " \"port\": \"%d\",\n", + ptr->port); + if (ptr->server_string) + dcb_printf(dcb, " \"version\": \"%s\",\n", + ptr->server_string); + dcb_printf(dcb, " \"nodeId\": \"%d\",\n", + ptr->node_id); + dcb_printf(dcb, " \"masterId\": \"%d\",\n", + ptr->master_id); + if (ptr->slaves) { + int i; + dcb_printf(dcb, " \"slaveIds\": [ "); + for (i = 0; ptr->slaves[i]; i++) + { + if (i == 0) + dcb_printf(dcb, "%li", ptr->slaves[i]); + else + dcb_printf(dcb, ", %li ", ptr->slaves[i]); + } + dcb_printf(dcb, "],\n"); + } + dcb_printf(dcb, " \"replDepth\": \"%d\",\n", + ptr->depth); + if (SERVER_IS_SLAVE(ptr) || SERVER_IS_RELAY_SERVER(ptr)) { + if (ptr->rlag >= 0) { + dcb_printf(dcb, " \"slaveDelay\": \"%d\",\n", ptr->rlag); + } + } + if (ptr->node_ts > 0) { + dcb_printf(dcb, " \"lastReplHeartbeat\": \"%lu\",\n", ptr->node_ts); + } + dcb_printf(dcb, " \"totalConnections\": \"%d\",\n", + ptr->stats.n_connections); + dcb_printf(dcb, " \"currentConnections\": \"%d\",\n", + ptr->stats.n_current); + dcb_printf(dcb, " \"currentOps\": \"%d\"\n", + ptr->stats.n_current_ops); + if (el < len) { + dcb_printf(dcb, " },\n"); + } + else { + dcb_printf(dcb, " }\n"); + } + ptr = ptr->next; + el++; + } + dcb_printf(dcb, "]\n"); + spinlock_release(&server_spin); +} + + /** * Print server details to a DCB * diff --git a/server/include/server.h b/server/include/server.h index 4bb514d65..1eb2d3eeb 100644 --- a/server/include/server.h +++ b/server/include/server.h @@ -176,6 +176,7 @@ extern SERVER *server_find(char *, unsigned short); extern void printServer(SERVER *); extern void printAllServers(); extern void dprintAllServers(DCB *); +extern void dprintAllServersJson(DCB *); extern void dprintServer(DCB *, SERVER *); extern void dListServers(DCB *); extern char *server_status(SERVER *); diff --git a/server/modules/routing/debugcmd.c b/server/modules/routing/debugcmd.c index cf2232816..821e38f23 100644 --- a/server/modules/routing/debugcmd.c +++ b/server/modules/routing/debugcmd.c @@ -157,6 +157,10 @@ struct subcommand showoptions[] = { "Show all configured servers", "Show all configured servers", {0, 0, 0} }, + { "serversjson", 0, dprintAllServersJson, + "Show all configured servers in JSON format", + "Show all configured servers in JSON format", + {0, 0, 0} }, { "services", 0, dprintAllServices, "Show all configured services in MaxScale", "Show all configured services in MaxScale",