Merge pull request #9545 from Icinga/targeted-apply-rules

Separately handle apply rules targetting only specific parent objects
This commit is contained in:
Julian Brost 2022-11-04 14:06:15 +01:00 committed by GitHub
commit 98902b2ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 438 additions and 59 deletions

View File

@ -461,3 +461,8 @@ String::ConstIterator icinga::range_end(const String& x)
{
return x.End();
}
std::size_t std::hash<String>::operator()(const String& s) const noexcept
{
return std::hash<std::string>{}(s.GetData());
}

View File

@ -7,6 +7,7 @@
#include "base/object.hpp"
#include <boost/range/iterator.hpp>
#include <boost/utility/string_view.hpp>
#include <functional>
#include <string>
#include <iosfwd>
@ -178,6 +179,12 @@ String::ConstIterator range_end(const String& x);
}
template<>
struct std::hash<icinga::String>
{
std::size_t operator()(const icinga::String& s) const noexcept;
};
extern template class std::vector<icinga::String>;
namespace boost

View File

@ -21,7 +21,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set(config_SOURCES
i2-config.hpp
activationcontext.cpp activationcontext.hpp
applyrule.cpp applyrule.hpp
applyrule.cpp applyrule-targeted.cpp applyrule.hpp
configcompiler.cpp configcompiler.hpp
configcompilercontext.cpp configcompilercontext.hpp
configfragment.hpp

View File

@ -0,0 +1,254 @@
/* Icinga 2 | (c) 2022 Icinga GmbH | GPLv2+ */
#include "base/string.hpp"
#include "config/applyrule.hpp"
#include "config/expression.hpp"
#include <utility>
#include <vector>
using namespace icinga;
/**
* @returns All ApplyRules targeting only specific parent objects including the given host. (See AddTargetedRule().)
*/
const std::set<ApplyRule::Ptr>& ApplyRule::GetTargetedHostRules(const Type::Ptr& sourceType, const String& host)
{
auto perSourceType (m_Rules.find(sourceType.get()));
if (perSourceType != m_Rules.end()) {
auto perHost (perSourceType->second.Targeted.find(host));
if (perHost != perSourceType->second.Targeted.end()) {
return perHost->second.ForHost;
}
}
static const std::set<ApplyRule::Ptr> noRules;
return noRules;
}
/**
* @returns All ApplyRules targeting only specific parent objects including the given service. (See AddTargetedRule().)
*/
const std::set<ApplyRule::Ptr>& ApplyRule::GetTargetedServiceRules(const Type::Ptr& sourceType, const String& host, const String& service)
{
auto perSourceType (m_Rules.find(sourceType.get()));
if (perSourceType != m_Rules.end()) {
auto perHost (perSourceType->second.Targeted.find(host));
if (perHost != perSourceType->second.Targeted.end()) {
auto perService (perHost->second.ForServices.find(service));
if (perService != perHost->second.ForServices.end()) {
return perService->second;
}
}
}
static const std::set<ApplyRule::Ptr> noRules;
return noRules;
}
/**
* If the given ApplyRule targets only specific parent objects, add it to the respective "index".
*
* - The above means for apply T "N" to Host: assign where host.name == "H" [ || host.name == "h" ... ]
* - For apply T "N" to Service it means: assign where host.name == "H" && service.name == "S" [ || host.name == "h" && service.name == "s" ... ]
*
* The order of operands of || && == doesn't matter.
*
* @returns Whether the rule has been added to the "index".
*/
bool ApplyRule::AddTargetedRule(const ApplyRule::Ptr& rule, const String& targetType, ApplyRule::PerSourceType& rules)
{
if (targetType == "Host") {
std::vector<const String *> hosts;
if (GetTargetHosts(rule->m_Filter.get(), hosts)) {
for (auto host : hosts) {
rules.Targeted[*host].ForHost.emplace(rule);
}
return true;
}
} else if (targetType == "Service") {
std::vector<std::pair<const String *, const String *>> services;
if (GetTargetServices(rule->m_Filter.get(), services)) {
for (auto service : services) {
rules.Targeted[*service.first].ForServices[*service.second].emplace(rule);
}
return true;
}
}
return false;
}
/**
* If the given assign filter is like the following, extract the host names ("H", "h", ...) into the vector:
*
* host.name == "H" [ || host.name == "h" ... ]
*
* The order of operands of || == doesn't matter.
*
* @returns Whether the given assign filter is like above.
*/
bool ApplyRule::GetTargetHosts(Expression* assignFilter, std::vector<const String *>& hosts)
{
auto lor (dynamic_cast<LogicalOrExpression*>(assignFilter));
if (lor) {
return GetTargetHosts(lor->GetOperand1().get(), hosts)
&& GetTargetHosts(lor->GetOperand2().get(), hosts);
}
auto name (GetComparedName(assignFilter, "host"));
if (name) {
hosts.emplace_back(name);
return true;
}
return false;
}
/**
* If the given assign filter is like the following, extract the host+service names ("H"+"S", "h"+"s", ...) into the vector:
*
* host.name == "H" && service.name == "S" [ || host.name == "h" && service.name == "s" ... ]
*
* The order of operands of || && == doesn't matter.
*
* @returns Whether the given assign filter is like above.
*/
bool ApplyRule::GetTargetServices(Expression* assignFilter, std::vector<std::pair<const String *, const String *>>& services)
{
auto lor (dynamic_cast<LogicalOrExpression*>(assignFilter));
if (lor) {
return GetTargetServices(lor->GetOperand1().get(), services)
&& GetTargetServices(lor->GetOperand2().get(), services);
}
auto service (GetTargetService(assignFilter));
if (service.first) {
services.emplace_back(service);
return true;
}
return false;
}
/**
* If the given filter is like the following, extract the host+service names ("H"+"S"):
*
* host.name == "H" && service.name == "S"
*
* The order of operands of && == doesn't matter.
*
* @returns {host, service} on success and {nullptr, nullptr} on failure.
*/
std::pair<const String *, const String *> ApplyRule::GetTargetService(Expression* assignFilter)
{
auto land (dynamic_cast<LogicalAndExpression*>(assignFilter));
if (!land) {
return {nullptr, nullptr};
}
auto op1 (land->GetOperand1().get());
auto op2 (land->GetOperand2().get());
auto host (GetComparedName(op1, "host"));
if (!host) {
std::swap(op1, op2);
host = GetComparedName(op1, "host");
}
if (host) {
auto service (GetComparedName(op2, "service"));
if (service) {
return {host, service};
}
}
return {nullptr, nullptr};
}
/**
* If the given filter is like the following, extract the object name ("N"):
*
* $lcType$.name == "N"
*
* The order of operands of == doesn't matter.
*
* @returns The object name on success and nullptr on failure.
*/
const String * ApplyRule::GetComparedName(Expression* assignFilter, const char * lcType)
{
auto eq (dynamic_cast<EqualExpression*>(assignFilter));
if (!eq) {
return nullptr;
}
auto op1 (eq->GetOperand1().get());
auto op2 (eq->GetOperand2().get());
if (IsNameIndexer(op1, lcType)) {
return GetLiteralStringValue(op2);
}
if (IsNameIndexer(op2, lcType)) {
return GetLiteralStringValue(op1);
}
return nullptr;
}
/**
* @returns Whether the given expression is like $lcType$.name.
*/
bool ApplyRule::IsNameIndexer(Expression* exp, const char * lcType)
{
auto ixr (dynamic_cast<IndexerExpression*>(exp));
if (!ixr) {
return false;
}
auto var (dynamic_cast<VariableExpression*>(ixr->GetOperand1().get()));
if (!var || var->GetVariable() != lcType) {
return false;
}
auto val (GetLiteralStringValue(ixr->GetOperand2().get()));
return val && *val == "name";
}
/**
* @returns If the given expression is a string literal, the string. nullptr on failure.
*/
const String * ApplyRule::GetLiteralStringValue(Expression* exp)
{
auto lit (dynamic_cast<LiteralExpression*>(exp));
if (!lit) {
return nullptr;
}
auto& val (lit->GetValue());
if (!val.IsString()) {
return nullptr;
}
return &val.Get<String>();
}

View File

@ -3,6 +3,7 @@
#include "config/applyrule.hpp"
#include "base/logger.hpp"
#include <set>
#include <unordered_set>
using namespace icinga;
@ -80,9 +81,12 @@ void ApplyRule::AddRule(const String& sourceType, const String& targetType, cons
}
}
m_Rules[Type::GetByName(sourceType).get()][Type::GetByName(*actualTargetType).get()].emplace_back(ApplyRule(
name, expression, filter, package, fkvar, fvvar, fterm, ignoreOnError, di, scope
));
ApplyRule::Ptr rule = new ApplyRule(name, expression, filter, package, fkvar, fvvar, fterm, ignoreOnError, di, scope);
auto& rules (m_Rules[Type::GetByName(sourceType).get()]);
if (!AddTargetedRule(rule, *actualTargetType, rules)) {
rules.Regular[Type::GetByName(*actualTargetType).get()].emplace_back(std::move(rule));
}
}
bool ApplyRule::EvaluateFilter(ScriptFrame& frame) const
@ -140,33 +144,56 @@ bool ApplyRule::HasMatches() const
return m_HasMatches;
}
std::vector<ApplyRule>& ApplyRule::GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType)
const std::vector<ApplyRule::Ptr>& ApplyRule::GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType)
{
auto perSourceType (m_Rules.find(sourceType.get()));
if (perSourceType != m_Rules.end()) {
auto perTargetType (perSourceType->second.find(targetType.get()));
auto perTargetType (perSourceType->second.Regular.find(targetType.get()));
if (perTargetType != perSourceType->second.end()) {
if (perTargetType != perSourceType->second.Regular.end()) {
return perTargetType->second;
}
}
static std::vector<ApplyRule> noRules;
static const std::vector<ApplyRule::Ptr> noRules;
return noRules;
}
void ApplyRule::CheckMatches(bool silent)
{
for (auto& perSourceType : m_Rules) {
for (auto& perTargetType : perSourceType.second) {
for (auto& perTargetType : perSourceType.second.Regular) {
for (auto& rule : perTargetType.second) {
if (!rule.HasMatches() && !silent) {
Log(LogWarning, "ApplyRule")
<< "Apply rule '" << rule.GetName() << "' (" << rule.GetDebugInfo() << ") for type '"
<< perSourceType.first->GetName() << "' does not match anywhere!";
CheckMatches(rule, perSourceType.first, silent);
}
}
std::unordered_set<ApplyRule*> targeted;
for (auto& perHost : perSourceType.second.Targeted) {
for (auto& rule : perHost.second.ForHost) {
targeted.emplace(rule.get());
}
for (auto& perService : perHost.second.ForServices) {
for (auto& rule : perService.second) {
targeted.emplace(rule.get());
}
}
}
for (auto rule : targeted) {
CheckMatches(rule, perSourceType.first, silent);
}
}
}
void ApplyRule::CheckMatches(const ApplyRule::Ptr& rule, Type* sourceType, bool silent)
{
if (!rule->HasMatches() && !silent) {
Log(LogWarning, "ApplyRule")
<< "Apply rule '" << rule->GetName() << "' (" << rule->GetDebugInfo() << ") for type '"
<< sourceType->GetName() << "' does not match anywhere!";
}
}

View File

@ -6,6 +6,7 @@
#include "config/i2-config.hpp"
#include "config/expression.hpp"
#include "base/debuginfo.hpp"
#include "base/shared-object.hpp"
#include "base/type.hpp"
#include <unordered_map>
@ -15,11 +16,40 @@ namespace icinga
/**
* @ingroup config
*/
class ApplyRule
class ApplyRule : public SharedObject
{
public:
DECLARE_PTR_TYPEDEFS(ApplyRule);
struct PerHost
{
std::set<ApplyRule::Ptr> ForHost;
std::unordered_map<String /* service */, std::set<ApplyRule::Ptr>> ForServices;
};
struct PerSourceType
{
std::unordered_map<Type* /* target type */, std::vector<ApplyRule::Ptr>> Regular;
std::unordered_map<String /* host */, PerHost> Targeted;
};
/*
* m_Rules[T::TypeInstance.get()].Targeted["H"].ForHost
* contains all apply rules like apply T "x" to Host { ... }
* which target only specific hosts incl. "H", e.g. via
* assign where host.name == "H" || host.name == "h".
*
* m_Rules[T::TypeInstance.get()].Targeted["H"].ForServices["S"]
* contains all apply rules like apply T "x" to Service { ... }
* which target only specific services on specific hosts,
* e.g. via assign where host.name == "H" && service.name == "S".
*
* m_Rules[T::TypeInstance.get()].Regular[C::TypeInstance.get()]
* contains all other apply rules like apply T "x" to C { ... }.
*/
typedef std::unordered_map<Type* /* source type */, PerSourceType> RuleMap;
typedef std::map<String, std::vector<String> > TypeMap;
typedef std::unordered_map<Type*, std::unordered_map<Type*, std::vector<ApplyRule>>> RuleMap;
String GetName() const;
Expression::Ptr GetExpression() const;
@ -39,7 +69,9 @@ public:
static void AddRule(const String& sourceType, const String& targetType, const String& name, const Expression::Ptr& expression,
const Expression::Ptr& filter, const String& package, const String& fkvar, const String& fvvar, const Expression::Ptr& fterm,
bool ignoreOnError, const DebugInfo& di, const Dictionary::Ptr& scope);
static std::vector<ApplyRule>& GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType);
static const std::vector<ApplyRule::Ptr>& GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType);
static const std::set<ApplyRule::Ptr>& GetTargetedHostRules(const Type::Ptr& sourceType, const String& host);
static const std::set<ApplyRule::Ptr>& GetTargetedServiceRules(const Type::Ptr& sourceType, const String& host, const String& service);
static void RegisterType(const String& sourceType, const std::vector<String>& targetTypes);
static bool IsValidSourceType(const String& sourceType);
@ -47,6 +79,7 @@ public:
static const std::vector<String>& GetTargetTypes(const String& sourceType);
static void CheckMatches(bool silent);
static void CheckMatches(const ApplyRule::Ptr& rule, Type* sourceType, bool silent);
private:
String m_Name;
@ -64,6 +97,14 @@ private:
static TypeMap m_Types;
static RuleMap m_Rules;
static bool AddTargetedRule(const ApplyRule::Ptr& rule, const String& targetType, PerSourceType& rules);
static bool GetTargetHosts(Expression* assignFilter, std::vector<const String *>& hosts);
static bool GetTargetServices(Expression* assignFilter, std::vector<std::pair<const String *, const String *>>& services);
static std::pair<const String *, const String *> GetTargetService(Expression* assignFilter);
static const String * GetComparedName(Expression* assignFilter, const char * lcType);
static bool IsNameIndexer(Expression* exp, const char * lcType);
static const String * GetLiteralStringValue(Expression* exp);
ApplyRule(String name, Expression::Ptr expression,
Expression::Ptr filter, String package, String fkvar, String fvvar, Expression::Ptr fterm,
bool ignoreOnError, DebugInfo di, Dictionary::Ptr scope);

View File

@ -283,6 +283,16 @@ public:
: DebuggableExpression(debugInfo), m_Operand1(std::move(operand1)), m_Operand2(std::move(operand2))
{ }
inline const std::unique_ptr<Expression>& GetOperand1() const noexcept
{
return m_Operand1;
}
inline const std::unique_ptr<Expression>& GetOperand2() const noexcept
{
return m_Operand2;
}
protected:
std::unique_ptr<Expression> m_Operand1;
std::unique_ptr<Expression> m_Operand2;
@ -293,7 +303,7 @@ class VariableExpression final : public DebuggableExpression
public:
VariableExpression(String variable, std::vector<Expression::Ptr> imports, const DebugInfo& debugInfo = DebugInfo());
String GetVariable() const
inline const String& GetVariable() const
{
return m_Variable;
}

View File

@ -17,9 +17,9 @@ INITIALIZE_ONCE([]() {
ApplyRule::RegisterType("Dependency", { "Host", "Service" });
});
bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter)
{
if (!rule.EvaluateFilter(frame))
if (!skipFilter && !rule.EvaluateFilter(frame))
return false;
DebugInfo di = rule.GetDebugInfo();
@ -62,7 +62,7 @@ bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, cons
return true;
}
bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule, bool skipFilter)
{
DebugInfo di = rule.GetDebugInfo();
@ -111,7 +111,7 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
name += instance;
}
if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
match = true;
}
} else if (vinstances.IsObjectType<Dictionary>()) {
@ -124,7 +124,7 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
frame.Locals->Set(rule.GetFKVar(), key);
frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule, skipFilter))
match = true;
}
}
@ -137,8 +137,13 @@ void Dependency::EvaluateApplyRules(const Host::Ptr& host)
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Host::TypeInstance)) {
if (EvaluateApplyRule(host, rule))
rule.AddMatch();
if (EvaluateApplyRule(host, *rule))
rule->AddMatch();
}
for (auto& rule : ApplyRule::GetTargetedHostRules(Dependency::TypeInstance, host->GetName())) {
if (EvaluateApplyRule(host, *rule, true))
rule->AddMatch();
}
}
@ -147,7 +152,12 @@ void Dependency::EvaluateApplyRules(const Service::Ptr& service)
CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Service::TypeInstance)) {
if (EvaluateApplyRule(service, rule))
rule.AddMatch();
if (EvaluateApplyRule(service, *rule))
rule->AddMatch();
}
for (auto& rule : ApplyRule::GetTargetedServiceRules(Dependency::TypeInstance, service->GetHost()->GetName(), service->GetShortName())) {
if (EvaluateApplyRule(service, *rule, true))
rule->AddMatch();
}
}

View File

@ -50,8 +50,8 @@ private:
Checkable::Ptr m_Parent;
Checkable::Ptr m_Child;
static bool EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule);
static bool EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter);
static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule, bool skipFilter = false);
};
}

View File

@ -17,9 +17,9 @@ INITIALIZE_ONCE([]() {
ApplyRule::RegisterType("Notification", { "Host", "Service" });
});
bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter)
{
if (!rule.EvaluateFilter(frame))
if (!skipFilter && !rule.EvaluateFilter(frame))
return false;
DebugInfo di = rule.GetDebugInfo();
@ -61,7 +61,7 @@ bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, co
return true;
}
bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule, bool skipFilter)
{
DebugInfo di = rule.GetDebugInfo();
@ -110,7 +110,7 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl
name += instance;
}
if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
match = true;
}
} else if (vinstances.IsObjectType<Dictionary>()) {
@ -123,7 +123,7 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl
frame.Locals->Set(rule.GetFKVar(), key);
frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule, skipFilter))
match = true;
}
}
@ -137,8 +137,13 @@ void Notification::EvaluateApplyRules(const Host::Ptr& host)
for (auto& rule : ApplyRule::GetRules(Notification::TypeInstance, Host::TypeInstance))
{
if (EvaluateApplyRule(host, rule))
rule.AddMatch();
if (EvaluateApplyRule(host, *rule))
rule->AddMatch();
}
for (auto& rule : ApplyRule::GetTargetedHostRules(Notification::TypeInstance, host->GetName())) {
if (EvaluateApplyRule(host, *rule, true))
rule->AddMatch();
}
}
@ -147,7 +152,12 @@ void Notification::EvaluateApplyRules(const Service::Ptr& service)
CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(Notification::TypeInstance, Service::TypeInstance)) {
if (EvaluateApplyRule(service, rule))
rule.AddMatch();
if (EvaluateApplyRule(service, *rule))
rule->AddMatch();
}
for (auto& rule : ApplyRule::GetTargetedServiceRules(Notification::TypeInstance, service->GetHost()->GetName(), service->GetShortName())) {
if (EvaluateApplyRule(service, *rule, true))
rule->AddMatch();
}
}

View File

@ -118,8 +118,8 @@ private:
void ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author = "", const String& text = "");
static bool EvaluateApplyRuleInstance(const intrusive_ptr<Checkable>& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
static bool EvaluateApplyRule(const intrusive_ptr<Checkable>& checkable, const ApplyRule& rule);
static bool EvaluateApplyRuleInstance(const intrusive_ptr<Checkable>& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter);
static bool EvaluateApplyRule(const intrusive_ptr<Checkable>& checkable, const ApplyRule& rule, bool skipFilter = false);
static std::map<String, int> m_StateFilterMap;
static std::map<String, int> m_TypeFilterMap;

View File

@ -16,9 +16,9 @@ INITIALIZE_ONCE([]() {
ApplyRule::RegisterType("ScheduledDowntime", { "Host", "Service" });
});
bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter)
{
if (!rule.EvaluateFilter(frame))
if (!skipFilter && !rule.EvaluateFilter(frame))
return false;
DebugInfo di = rule.GetDebugInfo();
@ -60,7 +60,7 @@ bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkabl
return true;
}
bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule, bool skipFilter)
{
DebugInfo di = rule.GetDebugInfo();
@ -109,7 +109,7 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const
name += instance;
}
if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
match = true;
}
} else if (vinstances.IsObjectType<Dictionary>()) {
@ -122,7 +122,7 @@ bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const
frame.Locals->Set(rule.GetFKVar(), key);
frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule, skipFilter))
match = true;
}
}
@ -135,8 +135,13 @@ void ScheduledDowntime::EvaluateApplyRules(const Host::Ptr& host)
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(ScheduledDowntime::TypeInstance, Host::TypeInstance)) {
if (EvaluateApplyRule(host, rule))
rule.AddMatch();
if (EvaluateApplyRule(host, *rule))
rule->AddMatch();
}
for (auto& rule : ApplyRule::GetTargetedHostRules(ScheduledDowntime::TypeInstance, host->GetName())) {
if (EvaluateApplyRule(host, *rule, true))
rule->AddMatch();
}
}
@ -145,7 +150,12 @@ void ScheduledDowntime::EvaluateApplyRules(const Service::Ptr& service)
CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(ScheduledDowntime::TypeInstance, Service::TypeInstance)) {
if (EvaluateApplyRule(service, rule))
rule.AddMatch();
if (EvaluateApplyRule(service, *rule))
rule->AddMatch();
}
for (auto& rule : ApplyRule::GetTargetedServiceRules(ScheduledDowntime::TypeInstance, service->GetHost()->GetName(), service->GetShortName())) {
if (EvaluateApplyRule(service, *rule, true))
rule->AddMatch();
}
}

View File

@ -51,8 +51,8 @@ private:
static std::atomic<bool> m_AllConfigLoaded;
static bool EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule);
static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule);
static bool EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter);
static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule, bool skipFilter = false);
};
}

View File

@ -16,9 +16,9 @@ INITIALIZE_ONCE([]() {
ApplyRule::RegisterType("Service", { "Host" });
});
bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule)
bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter)
{
if (!rule.EvaluateFilter(frame))
if (!skipFilter && !rule.EvaluateFilter(frame))
return false;
DebugInfo di = rule.GetDebugInfo();
@ -55,7 +55,7 @@ bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& nam
return true;
}
bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule)
bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule, bool skipFilter)
{
DebugInfo di = rule.GetDebugInfo();
@ -98,7 +98,7 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule)
name += instance;
}
if (EvaluateApplyRuleInstance(host, name, frame, rule))
if (EvaluateApplyRuleInstance(host, name, frame, rule, skipFilter))
match = true;
}
} else if (vinstances.IsObjectType<Dictionary>()) {
@ -111,7 +111,7 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule)
frame.Locals->Set(rule.GetFKVar(), key);
frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
if (EvaluateApplyRuleInstance(host, rule.GetName() + key, frame, rule))
if (EvaluateApplyRuleInstance(host, rule.GetName() + key, frame, rule, skipFilter))
match = true;
}
}
@ -124,7 +124,12 @@ void Service::EvaluateApplyRules(const Host::Ptr& host)
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
for (auto& rule : ApplyRule::GetRules(Service::TypeInstance, Host::TypeInstance)) {
if (EvaluateApplyRule(host, rule))
rule.AddMatch();
if (EvaluateApplyRule(host, *rule))
rule->AddMatch();
}
for (auto& rule : ApplyRule::GetTargetedHostRules(Service::TypeInstance, host->GetName())) {
if (EvaluateApplyRule(host, *rule, true))
rule->AddMatch();
}
}

View File

@ -54,8 +54,8 @@ protected:
private:
Host::Ptr m_Host;
static bool EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule);
static bool EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule);
static bool EvaluateApplyRuleInstance(const Host::Ptr& host, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter);
static bool EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule, bool skipFilter = false);
};
std::pair<Host::Ptr, Service::Ptr> GetHostService(const Checkable::Ptr& checkable);