From a97782b3e5a79f5c4b4ab0c007638130d9f38431 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 28 Jul 2015 17:12:00 +0200 Subject: [PATCH] Implement support for limiting output attributes refs #9077 fixes #9614 --- lib/icinga/checkresult.cpp | 13 +++++++++++ lib/icinga/checkresult.hpp | 2 ++ lib/remote/statusqueryhandler.cpp | 37 +++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/icinga/checkresult.cpp b/lib/icinga/checkresult.cpp index d1f2933d9..eb28c58aa 100644 --- a/lib/icinga/checkresult.cpp +++ b/lib/icinga/checkresult.cpp @@ -19,7 +19,20 @@ #include "icinga/checkresult.hpp" #include "icinga/checkresult.tcpp" +#include "base/scriptglobal.hpp" using namespace icinga; REGISTER_TYPE(CheckResult); +INITIALIZE_ONCE(&CheckResult::StaticInitialize); + +void CheckResult::StaticInitialize(void) +{ + ScriptGlobal::Set("ServiceOK", ServiceOK); + ScriptGlobal::Set("ServiceWarning", ServiceWarning); + ScriptGlobal::Set("ServiceCritical", ServiceCritical); + ScriptGlobal::Set("ServiceUnknown", ServiceUnknown); + + ScriptGlobal::Set("HostUp", HostUp); + ScriptGlobal::Set("HostDown", HostDown); +} diff --git a/lib/icinga/checkresult.hpp b/lib/icinga/checkresult.hpp index a415e9206..5ea4a3d9c 100644 --- a/lib/icinga/checkresult.hpp +++ b/lib/icinga/checkresult.hpp @@ -35,6 +35,8 @@ class I2_ICINGA_API CheckResult : public ObjectImpl { public: DECLARE_OBJECT(CheckResult); + + static void StaticInitialize(void); }; } diff --git a/lib/remote/statusqueryhandler.cpp b/lib/remote/statusqueryhandler.cpp index 0651b93aa..d78dac78f 100644 --- a/lib/remote/statusqueryhandler.cpp +++ b/lib/remote/statusqueryhandler.cpp @@ -22,6 +22,7 @@ #include "remote/filterutility.hpp" #include "base/serializer.hpp" #include +#include using namespace icinga; @@ -40,6 +41,9 @@ bool StatusQueryHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& re QueryDescription qd; qd.Types.insert(type); + std::vector joinTypes; + joinTypes.push_back(type->GetName()); + Dictionary::Ptr params = HttpUtility::FetchRequestParameters(request); params->Set("type", type->GetName()); @@ -56,9 +60,38 @@ bool StatusQueryHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& re Array::Ptr results = new Array(); + std::set attrs; + Array::Ptr uattrs = params->Get("attrs"); + + if (uattrs) { + ObjectLock olock(uattrs); + BOOST_FOREACH(const String& uattr, uattrs) { + attrs.insert(uattr); + } + } + BOOST_FOREACH(const DynamicObject::Ptr& obj, objs) { - Value result1 = Serialize(obj, FAConfig | FAState); - results->Add(result1); + BOOST_FOREACH(const String& joinType, joinTypes) { + String prefix = joinType; + boost::algorithm::to_lower(prefix); + + Dictionary::Ptr result1 = new Dictionary(); + for (int fid = 0; fid < type->GetFieldCount(); fid++) { + Field field = type->GetFieldInfo(fid); + + if ((field.Attributes & (FAConfig|FAState)) == 0) + continue; + + String aname = prefix + "." + field.Name; + if (!attrs.empty() && attrs.find(aname) == attrs.end()) + continue; + + Value val = static_cast(obj)->GetField(fid); + Value sval = Serialize(val, FAConfig | FAState); + result1->Set(aname, sval); + } + results->Add(result1); + } } Dictionary::Ptr result = new Dictionary();