2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-09-21 05:44:58 -04:00
|
|
|
|
|
|
|
|
#include "remote/statushandler.hpp"
|
|
|
|
|
#include "remote/httputility.hpp"
|
2015-09-28 02:57:25 -04:00
|
|
|
#include "remote/filterutility.hpp"
|
2015-09-21 05:44:58 -04:00
|
|
|
#include "base/serializer.hpp"
|
|
|
|
|
#include "base/statsfunction.hpp"
|
2018-08-07 07:55:41 -04:00
|
|
|
#include "base/namespace.hpp"
|
2015-09-21 05:44:58 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
|
|
REGISTER_URLHANDLER("/v1/status", StatusHandler);
|
|
|
|
|
|
2018-01-04 00:11:04 -05:00
|
|
|
class StatusTargetProvider final : public TargetProvider
|
2015-09-28 02:57:25 -04:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
DECLARE_PTR_TYPEDEFS(StatusTargetProvider);
|
|
|
|
|
|
2026-01-23 07:31:01 -05:00
|
|
|
void FindTargets([[maybe_unused]] const String& type,
|
2017-12-19 09:50:05 -05:00
|
|
|
const std::function<void (const Value&)>& addTarget) const override
|
2015-09-28 02:57:25 -04:00
|
|
|
{
|
2018-08-07 07:55:41 -04:00
|
|
|
Namespace::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty);
|
2017-11-30 04:46:44 -05:00
|
|
|
|
|
|
|
|
if (statsFunctions) {
|
|
|
|
|
ObjectLock olock(statsFunctions);
|
|
|
|
|
|
2018-08-07 07:55:41 -04:00
|
|
|
for (const Namespace::Pair& kv : statsFunctions)
|
2017-11-30 04:46:44 -05:00
|
|
|
addTarget(GetTargetByName("Status", kv.first));
|
2015-09-28 02:57:25 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 07:31:01 -05:00
|
|
|
Value GetTargetByName([[maybe_unused]] const String& type, const String& name) const override
|
2015-09-28 02:57:25 -04:00
|
|
|
{
|
2018-08-07 07:55:41 -04:00
|
|
|
Namespace::Ptr statsFunctions = ScriptGlobal::Get("StatsFunctions", &Empty);
|
2017-11-30 04:46:44 -05:00
|
|
|
|
|
|
|
|
if (!statsFunctions)
|
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("No status functions are available."));
|
|
|
|
|
|
2018-08-07 07:55:41 -04: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 02:57:25 -04:00
|
|
|
|
2015-09-28 10:31:49 -04:00
|
|
|
if (!func)
|
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid status function name."));
|
|
|
|
|
|
2015-09-28 02:57:25 -04:00
|
|
|
Dictionary::Ptr status = new Dictionary();
|
|
|
|
|
Array::Ptr perfdata = new Array();
|
2017-11-30 04:46:44 -05:00
|
|
|
func->Invoke({ status, perfdata });
|
2015-09-28 02:57:25 -04:00
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
return new Dictionary({
|
|
|
|
|
{ "name", name },
|
|
|
|
|
{ "status", status },
|
|
|
|
|
{ "perfdata", Serialize(perfdata, FAState) }
|
|
|
|
|
});
|
2015-09-28 02:57:25 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 23:12:56 -05:00
|
|
|
bool IsValidType(const String& type) const override
|
2015-09-28 02:57:25 -04:00
|
|
|
{
|
|
|
|
|
return type == "Status";
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 07:31:01 -05:00
|
|
|
String GetPluralName([[maybe_unused]] const String& type) const override
|
2015-09-28 02:57:25 -04:00
|
|
|
{
|
|
|
|
|
return "statuses";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
bool StatusHandler::HandleRequest(
|
2025-06-11 04:28:16 -04:00
|
|
|
const WaitGroup::Ptr&,
|
2026-01-22 06:41:21 -05:00
|
|
|
const HttpApiRequest& request,
|
|
|
|
|
HttpApiResponse& response,
|
2026-01-23 07:31:01 -05:00
|
|
|
boost::asio::yield_context&
|
2019-02-15 04:33:01 -05:00
|
|
|
)
|
2015-09-21 05:44:58 -04:00
|
|
|
{
|
2019-02-15 04:33:01 -05:00
|
|
|
namespace http = boost::beast::http;
|
|
|
|
|
|
2025-07-23 03:37:05 -04:00
|
|
|
auto url = request.Url();
|
|
|
|
|
auto user = request.User();
|
|
|
|
|
auto params = request.Params();
|
|
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
if (url->GetPath().size() > 3)
|
2015-09-28 10:08:14 -04:00
|
|
|
return false;
|
2015-09-21 05:44:58 -04:00
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
if (request.method() != http::verb::get)
|
2015-09-28 10:08:14 -04:00
|
|
|
return false;
|
2015-09-21 05:44:58 -04:00
|
|
|
|
2015-09-28 02:57:25 -04:00
|
|
|
QueryDescription qd;
|
|
|
|
|
qd.Types.insert("Status");
|
|
|
|
|
qd.Provider = new StatusTargetProvider();
|
|
|
|
|
qd.Permission = "status/query";
|
2015-09-21 05:44:58 -04:00
|
|
|
|
2015-09-28 02:57:25 -04:00
|
|
|
params->Set("type", "Status");
|
2015-09-21 05:44:58 -04:00
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
if (url->GetPath().size() >= 3)
|
|
|
|
|
params->Set("status", url->GetPath()[2]);
|
2015-09-21 05:44:58 -04:00
|
|
|
|
2016-02-04 16:40:01 -05:00
|
|
|
std::vector<Value> objs;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
objs = FilterUtility::GetFilterTargets(qd, params, user);
|
|
|
|
|
} catch (const std::exception& ex) {
|
2017-12-20 09:31:05 -05:00
|
|
|
HttpUtility::SendJsonError(response, params, 404,
|
2017-12-19 09:50:05 -05:00
|
|
|
"No objects found.",
|
2018-04-06 06:47:20 -04:00
|
|
|
DiagnosticInformation(ex));
|
2016-02-04 16:40:01 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
2015-09-24 09:26:57 -04:00
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
Dictionary::Ptr result = new Dictionary({
|
|
|
|
|
{ "results", new Array(std::move(objs)) }
|
|
|
|
|
});
|
2015-09-21 05:44:58 -04:00
|
|
|
|
2019-02-15 04:33:01 -05:00
|
|
|
response.result(http::status::ok);
|
2017-12-20 09:31:05 -05:00
|
|
|
HttpUtility::SendJsonBody(response, params, result);
|
2015-09-21 05:44:58 -04:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|