2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-09-21 11:44:58 +02:00
|
|
|
|
|
|
|
#include "remote/statushandler.hpp"
|
|
|
|
#include "remote/httputility.hpp"
|
2015-09-28 08:57:25 +02:00
|
|
|
#include "remote/filterutility.hpp"
|
2015-09-21 11:44:58 +02:00
|
|
|
#include "base/serializer.hpp"
|
|
|
|
#include "base/statsfunction.hpp"
|
2018-08-07 13:55:41 +02:00
|
|
|
#include "base/namespace.hpp"
|
2015-09-21 11:44:58 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
void FindTargets(const String& type,
|
2017-12-19 15:50:05 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01: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
|
|
|
|
2015-09-28 16:31:49 +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
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Dictionary({
|
|
|
|
{ "name", name },
|
|
|
|
{ "status", status },
|
|
|
|
{ "perfdata", Serialize(perfdata, FAState) }
|
|
|
|
});
|
2015-09-28 08:57:25 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
bool IsValidType(const String& type) const override
|
2015-09-28 08:57:25 +02:00
|
|
|
{
|
|
|
|
return type == "Status";
|
|
|
|
}
|
|
|
|
|
2018-01-04 05:12:56 +01:00
|
|
|
String GetPluralName(const String& type) const override
|
2015-09-28 08:57:25 +02:00
|
|
|
{
|
|
|
|
return "statuses";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
bool StatusHandler::HandleRequest(
|
2019-02-15 11:51:12 +01:00
|
|
|
AsioTlsStream& stream,
|
2019-02-15 10:33:01 +01:00
|
|
|
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,
|
2019-04-02 17:37:29 +02:00
|
|
|
HttpServerConnection& server
|
2019-02-15 10:33:01 +01:00
|
|
|
)
|
2015-09-21 11:44:58 +02:00
|
|
|
{
|
2019-02-15 10:33:01 +01:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
|
|
|
if (url->GetPath().size() > 3)
|
2015-09-28 16:08:14 +02:00
|
|
|
return false;
|
2015-09-21 11:44:58 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (request.method() != http::verb::get)
|
2015-09-28 16:08:14 +02:00
|
|
|
return false;
|
2015-09-21 11:44:58 +02:00
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
QueryDescription qd;
|
|
|
|
qd.Types.insert("Status");
|
|
|
|
qd.Provider = new StatusTargetProvider();
|
|
|
|
qd.Permission = "status/query";
|
2015-09-21 11:44:58 +02:00
|
|
|
|
2015-09-28 08:57:25 +02:00
|
|
|
params->Set("type", "Status");
|
2015-09-21 11:44:58 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
if (url->GetPath().size() >= 3)
|
|
|
|
params->Set("status", url->GetPath()[2]);
|
2015-09-21 11:44:58 +02:00
|
|
|
|
2016-02-04 22:40:01 +01:00
|
|
|
std::vector<Value> objs;
|
|
|
|
|
|
|
|
try {
|
|
|
|
objs = FilterUtility::GetFilterTargets(qd, params, user);
|
|
|
|
} catch (const std::exception& ex) {
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonError(response, params, 404,
|
2017-12-19 15:50:05 +01:00
|
|
|
"No objects found.",
|
2018-04-06 12:47:20 +02:00
|
|
|
DiagnosticInformation(ex));
|
2016-02-04 22:40:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-09-24 15:26:57 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
{ "results", new Array(std::move(objs)) }
|
|
|
|
});
|
2015-09-21 11:44:58 +02:00
|
|
|
|
2019-02-15 10:33:01 +01:00
|
|
|
response.result(http::status::ok);
|
2017-12-20 15:31:05 +01:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-09-21 11:44:58 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|