From be2701bc8acf75f8210b7ae885704337090d723a Mon Sep 17 00:00:00 2001 From: Mattia Codato Date: Fri, 3 Jul 2020 15:13:51 +0200 Subject: [PATCH] Add overrideMacros property to MacroResolver --- lib/icinga/apiactions.cpp | 7 +++++++ lib/icinga/macroprocessor.cpp | 2 ++ lib/icinga/macroresolver.hpp | 2 ++ lib/methods/pluginchecktask.cpp | 3 +++ 4 files changed, 14 insertions(+) diff --git a/lib/icinga/apiactions.cpp b/lib/icinga/apiactions.cpp index ff8b68a32..ef3690847 100644 --- a/lib/icinga/apiactions.cpp +++ b/lib/icinga/apiactions.cpp @@ -15,6 +15,7 @@ #include "remote/httputility.hpp" #include "base/utility.hpp" #include "base/convert.hpp" +#include "base/defer.hpp" #include "remote/actionshandler.hpp" #include @@ -641,6 +642,12 @@ Dictionary::Ptr ApiActions::ExecuteCommand(const ConfigObject::Ptr& object, /* Check if resolved_command exists and it is of type command_type */ Dictionary::Ptr execMacros = new Dictionary(); + + MacroResolver::overrideMacros = execMacros; + Defer o ([]() { + MacroResolver::overrideMacros = nullptr; + }); + if (command_type == "CheckCommand") { CheckCommand::Ptr cmd = CheckCommand::GetByName(resolved_command); if (!cmd) diff --git a/lib/icinga/macroprocessor.cpp b/lib/icinga/macroprocessor.cpp index c92ab19d4..8fb014efc 100644 --- a/lib/icinga/macroprocessor.cpp +++ b/lib/icinga/macroprocessor.cpp @@ -15,6 +15,8 @@ using namespace icinga; +thread_local Dictionary::Ptr MacroResolver::overrideMacros; + Value MacroProcessor::ResolveMacros(const Value& str, const ResolverList& resolvers, const CheckResult::Ptr& cr, String *missingMacro, const MacroProcessor::EscapeCallback& escapeFn, const Dictionary::Ptr& resolvedMacros, diff --git a/lib/icinga/macroresolver.hpp b/lib/icinga/macroresolver.hpp index ea29ce1e4..65dc69679 100644 --- a/lib/icinga/macroresolver.hpp +++ b/lib/icinga/macroresolver.hpp @@ -21,6 +21,8 @@ class MacroResolver public: DECLARE_PTR_TYPEDEFS(MacroResolver); + static thread_local Dictionary::Ptr overrideMacros; + virtual bool ResolveMacro(const String& macro, const CheckResult::Ptr& cr, Value *result) const = 0; }; diff --git a/lib/methods/pluginchecktask.cpp b/lib/methods/pluginchecktask.cpp index 212d7447c..68458d3d9 100644 --- a/lib/methods/pluginchecktask.cpp +++ b/lib/methods/pluginchecktask.cpp @@ -29,6 +29,9 @@ void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckRes tie(host, service) = GetHostService(checkable); MacroProcessor::ResolverList resolvers; + if (MacroResolver::overrideMacros) + resolvers.emplace_back("override", MacroResolver::overrideMacros); + if (service) resolvers.emplace_back("service", service); resolvers.emplace_back("host", host);