From aae5f092d4aae47dc27ca9364a1d5072e10fac88 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Mon, 7 Apr 2014 13:23:27 +0200 Subject: [PATCH] Log warning if apply rule does not match anywhere. Fixes #5911 Fixes #5957 --- lib/config/applyrule.cpp | 3 ++ lib/icinga/dependency-apply.cpp | 53 +++++++++++++++++--------- lib/icinga/dependency.h | 2 +- lib/icinga/notification-apply.cpp | 46 ++++++++++++++-------- lib/icinga/notification.h | 2 +- lib/icinga/scheduleddowntime-apply.cpp | 52 +++++++++++++++---------- lib/icinga/scheduleddowntime.h | 2 +- lib/icinga/service-apply.cpp | 23 ++++++++--- lib/icinga/service.h | 2 +- 9 files changed, 119 insertions(+), 66 deletions(-) diff --git a/lib/config/applyrule.cpp b/lib/config/applyrule.cpp index bc8851c3f..87a250cc3 100644 --- a/lib/config/applyrule.cpp +++ b/lib/config/applyrule.cpp @@ -99,6 +99,9 @@ void ApplyRule::EvaluateRules(void) } } + if (cont) + continue; + completedTypes.insert(sourceType); RuleMap::const_iterator it = m_Rules.find(kv.first); diff --git a/lib/icinga/dependency-apply.cpp b/lib/icinga/dependency-apply.cpp index f9134cdda..d00700225 100644 --- a/lib/icinga/dependency-apply.cpp +++ b/lib/icinga/dependency-apply.cpp @@ -39,7 +39,7 @@ void Dependency::RegisterApplyRuleHandler(void) ApplyRule::RegisterType("Dependency", targets, &Dependency::EvaluateApplyRules); } -void Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) +bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) { DebugInfo di = rule.GetDebugInfo(); @@ -57,7 +57,7 @@ void Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR locals->Set("service", service); if (!rule.EvaluateFilter(locals)) - return; + return false; std::ostringstream msgbuf2; msgbuf2 << "Applying dependency '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di; @@ -70,13 +70,14 @@ void Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR builder->AddExpression(make_shared(&AExpression::OpSet, make_shared(&AExpression::OpLiteral, "child_host_name", di), - make_shared(&AExpression::OpLiteral, host->GetName(), - di), di)); + make_shared(&AExpression::OpLiteral, host->GetName(), di), + di)); if (service) { builder->AddExpression(make_shared(&AExpression::OpSet, make_shared(&AExpression::OpLiteral, "child_service_name", di), - make_shared(&AExpression::OpLiteral, service->GetShortName(), di), di)); + make_shared(&AExpression::OpLiteral, service->GetShortName(), di), + di)); } builder->AddExpression(rule.GetExpression()); @@ -85,29 +86,43 @@ void Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR dependencyItem->Register(); DynamicObject::Ptr dobj = dependencyItem->Commit(); dobj->OnConfigLoaded(); + + return true; } void Dependency::EvaluateApplyRules(const std::vector& rules) { - BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { - CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); + int apply_count = 0; - BOOST_FOREACH(const ApplyRule& rule, rules) { - if (rule.GetTargetType() != "Host") - continue; + BOOST_FOREACH(const ApplyRule& rule, rules) { + if (rule.GetTargetType() == "Host") { + apply_count = 0; - EvaluateApplyRule(host, rule); - } - } + BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); + + if (EvaluateApplyRule(host, rule)) + apply_count++; + } + + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!"); + + } else if (rule.GetTargetType() == "Service") { + apply_count = 0; + + BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'"); - BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects()) { - CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'"); + if(EvaluateApplyRule(service, rule)) + apply_count++; + } - BOOST_FOREACH(const ApplyRule& rule, rules) { - if (rule.GetTargetType() != "Service") - continue; + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!"); - EvaluateApplyRule(service, rule); + } else { + Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!"); } } } diff --git a/lib/icinga/dependency.h b/lib/icinga/dependency.h index 395707e3c..9a2cf3c4d 100644 --- a/lib/icinga/dependency.h +++ b/lib/icinga/dependency.h @@ -57,7 +57,7 @@ protected: virtual void Stop(void); private: - static void EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule); + static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule); static void EvaluateApplyRules(const std::vector& rules); }; diff --git a/lib/icinga/notification-apply.cpp b/lib/icinga/notification-apply.cpp index 8537191c2..ad501818a 100644 --- a/lib/icinga/notification-apply.cpp +++ b/lib/icinga/notification-apply.cpp @@ -38,7 +38,7 @@ void Notification::RegisterApplyRuleHandler(void) ApplyRule::RegisterType("Notification", targets, &Notification::EvaluateApplyRules); } -void Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) +bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) { DebugInfo di = rule.GetDebugInfo(); @@ -56,7 +56,7 @@ void Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl locals->Set("service", service); if (!rule.EvaluateFilter(locals)) - return; + return false; std::ostringstream msgbuf2; msgbuf2 << "Applying notification '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di; @@ -85,29 +85,43 @@ void Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl notificationItem->Register(); DynamicObject::Ptr dobj = notificationItem->Commit(); dobj->OnConfigLoaded(); + + return true; } void Notification::EvaluateApplyRules(const std::vector& rules) { - BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { - CONTEXT("Evaluating 'apply' rules for Host '" + host->GetName() + "'"); + int apply_count = 0; - BOOST_FOREACH(const ApplyRule& rule, rules) { - if (rule.GetTargetType() != "Host") - continue; + BOOST_FOREACH(const ApplyRule& rule, rules) { + if (rule.GetTargetType() == "Host") { + apply_count = 0; - EvaluateApplyRule(host, rule); - } - } + BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); + + if (EvaluateApplyRule(host, rule)) + apply_count++; + } + + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!"); + + } else if (rule.GetTargetType() == "Service") { + apply_count = 0; + + BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'"); - BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects()) { - CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'"); + if(EvaluateApplyRule(service, rule)) + apply_count++; + } - BOOST_FOREACH(const ApplyRule& rule, rules) { - if (rule.GetTargetType() != "Service") - continue; + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!"); - EvaluateApplyRule(service, rule); + } else { + Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!"); } } } diff --git a/lib/icinga/notification.h b/lib/icinga/notification.h index 641ddb137..32f436399 100644 --- a/lib/icinga/notification.h +++ b/lib/icinga/notification.h @@ -113,7 +113,7 @@ protected: private: void ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author = "", const String& text = ""); - static void EvaluateApplyRule(const shared_ptr& checkable, const ApplyRule& rule); + static bool EvaluateApplyRule(const shared_ptr& checkable, const ApplyRule& rule); static void EvaluateApplyRules(const std::vector& rules); }; diff --git a/lib/icinga/scheduleddowntime-apply.cpp b/lib/icinga/scheduleddowntime-apply.cpp index 37aeb6bc5..29442b72f 100644 --- a/lib/icinga/scheduleddowntime-apply.cpp +++ b/lib/icinga/scheduleddowntime-apply.cpp @@ -38,7 +38,7 @@ void ScheduledDowntime::RegisterApplyRuleHandler(void) ApplyRule::RegisterType("ScheduledDowntime", targets, &ScheduledDowntime::EvaluateApplyRules); } -void ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) +bool ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) { DebugInfo di = rule.GetDebugInfo(); @@ -56,19 +56,15 @@ void ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const locals->Set("service", service); if (!rule.EvaluateFilter(locals)) - return; + return false; std::ostringstream msgbuf2; msgbuf2 << "Applying scheduled downtime '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di; Log(LogDebug, "icinga", msgbuf2.str()); - std::ostringstream namebuf; - namebuf << checkable->GetName() << "!" << rule.GetName(); - String name = namebuf.str(); - ConfigItemBuilder::Ptr builder = make_shared(di); builder->SetType("ScheduledDowntime"); - builder->SetName(name); + builder->SetName(rule.GetName()); builder->SetScope(rule.GetScope()); builder->AddExpression(make_shared(&AExpression::OpSet, @@ -89,29 +85,43 @@ void ScheduledDowntime::EvaluateApplyRule(const Checkable::Ptr& checkable, const downtimeItem->Register(); DynamicObject::Ptr dobj = downtimeItem->Commit(); dobj->OnConfigLoaded(); + + return true; } void ScheduledDowntime::EvaluateApplyRules(const std::vector& rules) { - BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { - CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); + int apply_count = 0; - BOOST_FOREACH(const ApplyRule& rule, rules) { - if (rule.GetTargetType() != "Host") - continue; + BOOST_FOREACH(const ApplyRule& rule, rules) { + if (rule.GetTargetType() == "Host") { + apply_count = 0; - EvaluateApplyRule(host, rule); - } - } + BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); + + if (EvaluateApplyRule(host, rule)) + apply_count++; + } + + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!"); + + } else if (rule.GetTargetType() == "Service") { + apply_count = 0; + + BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'"); - BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects()) { - CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'"); + if(EvaluateApplyRule(service, rule)) + apply_count++; + } - BOOST_FOREACH(const ApplyRule& rule, rules) { - if (rule.GetTargetType() != "Service") - continue; + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!"); - EvaluateApplyRule(service, rule); + } else { + Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!"); } } } diff --git a/lib/icinga/scheduleddowntime.h b/lib/icinga/scheduleddowntime.h index 4ee8d1149..23741e10b 100644 --- a/lib/icinga/scheduleddowntime.h +++ b/lib/icinga/scheduleddowntime.h @@ -55,7 +55,7 @@ private: std::pair FindNextSegment(void); void CreateNextDowntime(void); - static void EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule); + static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule); static void EvaluateApplyRules(const std::vector& rules); }; diff --git a/lib/icinga/service-apply.cpp b/lib/icinga/service-apply.cpp index 15971a1ce..889ea66dc 100644 --- a/lib/icinga/service-apply.cpp +++ b/lib/icinga/service-apply.cpp @@ -37,7 +37,7 @@ void Service::RegisterApplyRuleHandler(void) ApplyRule::RegisterType("Service", targets, &Service::EvaluateApplyRules); } -void Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule) +bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule) { DebugInfo di = rule.GetDebugInfo(); @@ -49,7 +49,7 @@ void Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule) locals->Set("host", host); if (!rule.EvaluateFilter(locals)) - return; + return false; std::ostringstream msgbuf2; msgbuf2 << "Applying service '" << rule.GetName() << "' to host '" << host->GetName() << "' for rule " << di; @@ -76,14 +76,25 @@ void Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule) serviceItem->Register(); DynamicObject::Ptr dobj = serviceItem->Commit(); dobj->OnConfigLoaded(); + + return true; } void Service::EvaluateApplyRules(const std::vector& rules) { - BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { - CONTEXT("Evaluating 'apply' rules for Host '" + host->GetName() + "'"); + int apply_count = 0; + + BOOST_FOREACH(const ApplyRule& rule, rules) { + apply_count = 0; + + BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects()) { + CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); + + if (EvaluateApplyRule(host, rule)) + apply_count++; + } - BOOST_FOREACH(const ApplyRule& rule, rules) - EvaluateApplyRule(host, rule); + if (apply_count == 0) + Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!"); } } diff --git a/lib/icinga/service.h b/lib/icinga/service.h index e5a12e1bc..89903271d 100644 --- a/lib/icinga/service.h +++ b/lib/icinga/service.h @@ -67,7 +67,7 @@ protected: private: Host::Ptr m_Host; - static void EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule); + static bool EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule); static void EvaluateApplyRules(const std::vector& rules); }; -- 2.40.0