MXS-2379: Fix maxinfo HTTP interface

Added the missing HTTP request handlers and a new JSON conversion
function.
This commit is contained in:
Markus Mäkelä 2019-03-28 13:19:24 +02:00
parent 0fa7ad8580
commit 116efb2409
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19
6 changed files with 140 additions and 1 deletions

View File

@ -52,6 +52,13 @@ public:
*/
void write(DCB* dcb);
/**
* Write the result set to a DCB as JSON
*
* @param dcb DCB where the result set is written
*/
void write_as_json(DCB* dcb);
private:
std::vector<std::string> m_columns;
std::vector<std::vector<std::string>> m_rows;

View File

@ -16,6 +16,7 @@
#include <numeric>
#include <maxscale/alloc.h>
#include <maxscale/resultset.hh>
#include <maxscale/buffer.h>
#include <maxscale/dcb.h>
@ -206,3 +207,24 @@ void ResultSet::write(DCB* dcb)
mysql_send_eof(dcb, seqno);
}
void ResultSet::write_as_json(DCB* dcb)
{
json_t* arr = json_array();
for (const auto& row : m_rows)
{
json_t* obj = json_object();
for (size_t i = 0; i < row.size(); i++)
{
json_object_set_new(obj, m_columns[i].c_str(), json_string(row[i].c_str()));
}
json_array_append_new(arr, obj);
}
char* js = json_dumps(arr, JSON_INDENT(4));
dcb_printf(dcb, "%s", js);
MXS_FREE(js);
}

View File

@ -1,4 +1,4 @@
add_library(maxinfo SHARED maxinfo.cc maxinfo_parse.cc maxinfo_error.cc maxinfo_exec.cc)
add_library(maxinfo SHARED maxinfo.cc maxinfo_parse.cc maxinfo_error.cc maxinfo_exec.cc maxinfo_http.cc)
set_target_properties(maxinfo PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_RPATH}:${MAXSCALE_LIBDIR} VERSION "1.0.0" LINK_FLAGS -Wl,-z,defs)
target_link_libraries(maxinfo maxscale-common)
install_module(maxinfo core)

View File

@ -305,6 +305,7 @@ static int execute(MXS_ROUTER* rinstance, MXS_ROUTER_SESSION* router_session, GW
if (GWBUF_TYPE(queue) == GWBUF_TYPE_HTTP)
{
handle_url(instance, session, queue);
gwbuf_free(queue);
return 0;
}

View File

@ -137,5 +137,6 @@ extern void maxinfo_send_error(DCB*, int, const char*);
extern void maxinfo_send_parse_error(DCB*, char*, PARSE_ERROR);
extern std::unique_ptr<ResultSet> maxinfo_variables();
extern std::unique_ptr<ResultSet> maxinfo_status();
extern int handle_url(INFO_INSTANCE* instance, INFO_SESSION* session, GWBUF* queue);
#endif

View File

@ -0,0 +1,108 @@
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include "maxinfo.h"
#include <unordered_map>
#include <string>
#include <functional>
#include <maxscale/utils.hh>
#include "../../../core/internal/poll.hh"
#include "../../../core/internal/monitor.hh"
#include "../../../core/internal/server.hh"
#include "../../../core/internal/service.hh"
#include "../../../core/internal/modules.hh"
#include "../../../core/internal/session.hh"
void serviceGetList_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
serviceGetList()->write_as_json(dcb);
}
void serviceGetListenerList_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
serviceGetListenerList()->write_as_json(dcb);
}
void moduleGetList_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
moduleGetList()->write_as_json(dcb);
}
void monitorGetList_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
monitor_get_list()->write_as_json(dcb);
}
void maxinfoSessionsAll_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
sessionGetList()->write_as_json(dcb);
}
void maxinfoClientSessions_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
sessionGetList()->write_as_json(dcb);
}
void serverGetList_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
serverGetList()->write_as_json(dcb);
}
void eventTimesGetList_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
eventTimesGetList()->write_as_json(dcb);
}
void maxinfo_variables_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
maxinfo_variables()->write_as_json(dcb);
}
void maxinfo_status_http(INFO_INSTANCE* instance, INFO_SESSION* session, DCB* dcb)
{
maxinfo_status()->write_as_json(dcb);
}
/**
* Table that maps a URI to a function to call to
* to obtain the result set related to that URI
*/
static std::unordered_map<std::string, void (*)(INFO_INSTANCE*, INFO_SESSION*, DCB*)> supported_uri
{
{"/services", serviceGetList_http},
{"/listeners", serviceGetListenerList_http},
{"/modules", moduleGetList_http},
{"/monitors", monitorGetList_http},
{"/sessions", maxinfoSessionsAll_http},
{"/clients", maxinfoClientSessions_http},
{"/servers", serverGetList_http},
{"/variables", maxinfo_variables_http},
{"/status", maxinfo_status_http},
{"/event/times", eventTimesGetList_http}
};
int handle_url(INFO_INSTANCE* instance, INFO_SESSION* session, GWBUF* queue)
{
std::string uri((char*)GWBUF_DATA(queue));
auto it = supported_uri.find(uri);
if (it != supported_uri.end())
{
it->second(instance, session, session->dcb);
}
return 1;
}