icinga2/lib/remote/statushandler.cpp

121 lines
2.9 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "remote/statushandler.hpp"
#include "remote/httputility.hpp"
2015-09-28 08:57:25 +02:00
#include "remote/filterutility.hpp"
#include "base/serializer.hpp"
#include "base/statsfunction.hpp"
2018-08-07 13:55:41 +02:00
#include "base/namespace.hpp"
using namespace icinga;
REGISTER_URLHANDLER("/v1/status", StatusHandler);
2018-01-04 06:11:04 +01:00
class StatusTargetProvider final : public TargetProvider
2015-09-28 08:57:25 +02:00
{
public:
DECLARE_PTR_TYPEDEFS(StatusTargetProvider);
void FindTargets(const String& type,
const std::function<void (const Value&)>& addTarget) const override
2015-09-28 08:57:25 +02:00
{
2018-08-07 13:55:41 +02:00
Namespace::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty);
2017-11-30 10:46:44 +01:00
if (statsFunctions) {
ObjectLock olock(statsFunctions);
2018-08-07 13:55:41 +02:00
for (const Namespace::Pair& kv : statsFunctions)
2017-11-30 10:46:44 +01:00
addTarget(GetTargetByName("Status", kv.first));
2015-09-28 08:57:25 +02:00
}
}
Value GetTargetByName(const String& type, const String& name) const override
2015-09-28 08:57:25 +02:00
{
2018-08-07 13:55:41 +02:00
Namespace::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty);
2017-11-30 10:46:44 +01:00
if (!statsFunctions)
BOOST_THROW_EXCEPTION(std::invalid_argument("No status functions are available."));
2018-08-07 13:55:41 +02:00
Value vfunc;
if (!statsFunctions->Get(name, &vfunc))
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid status function name."));
Function::Ptr func = vfunc;
2015-09-28 08:57:25 +02:00
if (!func)
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid status function name."));
2015-09-28 08:57:25 +02:00
Dictionary::Ptr status = new Dictionary();
Array::Ptr perfdata = new Array();
2017-11-30 10:46:44 +01:00
func->Invoke({ status, perfdata });
2015-09-28 08:57:25 +02:00
return new Dictionary({
{ "name", name },
{ "status", status },
{ "perfdata", Serialize(perfdata, FAState) }
});
2015-09-28 08:57:25 +02:00
}
bool IsValidType(const String& type) const override
2015-09-28 08:57:25 +02:00
{
return type == "Status";
}
String GetPluralName(const String& type) const override
2015-09-28 08:57:25 +02:00
{
return "statuses";
}
};
bool StatusHandler::HandleRequest(
2019-02-15 11:51:12 +01:00
AsioTlsStream& stream,
const ApiUser::Ptr& user,
boost::beast::http::request<boost::beast::http::string_body>& request,
const Url::Ptr& url,
boost::beast::http::response<boost::beast::http::string_body>& response,
2019-02-15 11:51:12 +01:00
const Dictionary::Ptr& params,
boost::asio::yield_context& yc,
HttpServerConnection& server
)
{
namespace http = boost::beast::http;
if (url->GetPath().size() > 3)
2015-09-28 16:08:14 +02:00
return false;
if (request.method() != http::verb::get)
2015-09-28 16:08:14 +02:00
return false;
2015-09-28 08:57:25 +02:00
QueryDescription qd;
qd.Types.insert("Status");
qd.Provider = new StatusTargetProvider();
qd.Permission = "status/query";
2015-09-28 08:57:25 +02:00
params->Set("type", "Status");
if (url->GetPath().size() >= 3)
params->Set("status", url->GetPath()[2]);
std::vector<Value> objs;
try {
objs = FilterUtility::GetFilterTargets(qd, params, user);
} catch (const std::exception& ex) {
HttpUtility::SendJsonError(response, params, 404,
"No objects found.",
DiagnosticInformation(ex));
return true;
}
Dictionary::Ptr result = new Dictionary({
{ "results", new Array(std::move(objs)) }
});
response.result(http::status::ok);
HttpUtility::SendJsonBody(response, params, result);
return true;
}