]> granicus.if.org Git - icinga2/commitdiff
Log warning if apply rule does not match anywhere.
authorMichael Friedrich <michael.friedrich@netways.de>
Mon, 7 Apr 2014 11:23:27 +0000 (13:23 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Mon, 7 Apr 2014 13:00:14 +0000 (15:00 +0200)
Fixes #5911
Fixes #5957

lib/config/applyrule.cpp
lib/icinga/dependency-apply.cpp
lib/icinga/dependency.h
lib/icinga/notification-apply.cpp
lib/icinga/notification.h
lib/icinga/scheduleddowntime-apply.cpp
lib/icinga/scheduleddowntime.h
lib/icinga/service-apply.cpp
lib/icinga/service.h

index bc8851c3fc5237a3bb076e8887777b871d5fd916..87a250cc3fdb0437fce373469d42bafe8aa9bd22 100644 (file)
@@ -99,6 +99,9 @@ void ApplyRule::EvaluateRules(void)
                                }
                        }
 
+                       if (cont)
+                               continue;
+
                        completedTypes.insert(sourceType);
 
                        RuleMap::const_iterator it = m_Rules.find(kv.first);
index f9134cdda11814ab87b530884c603ba33d316691..d00700225350b278ed5e851639aef5075c542345 100644 (file)
@@ -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>(&AExpression::OpSet,
            make_shared<AExpression>(&AExpression::OpLiteral, "child_host_name", di),
-           make_shared<AExpression>(&AExpression::OpLiteral, host->GetName(),
-           di), di));
+           make_shared<AExpression>(&AExpression::OpLiteral, host->GetName(), di),
+           di));
 
        if (service) {
                builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
                    make_shared<AExpression>(&AExpression::OpLiteral, "child_service_name", di),
-                   make_shared<AExpression>(&AExpression::OpLiteral, service->GetShortName(), di), di));
+                   make_shared<AExpression>(&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<ApplyRule>& rules)
 {
-       BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
-               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<Host>()) {
+                               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<Service>()) {
+                               CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
 
-       BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
-               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() + "'!");
                }
        }
 }
index 395707e3c0c1fafdbf50d95a313c61fe603367fa..9a2cf3c4d241d3c5647b565ccde1d9a97e9df72e 100644 (file)
@@ -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<ApplyRule>& rules);
 };
 
index 8537191c276084075cf1b6a51121115cca4b85e1..ad501818a03944d727403478da84b9d9e1000e1a 100644 (file)
@@ -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<ApplyRule>& rules)
 {
-       BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
-               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<Host>()) {
+                               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<Service>()) {
+                               CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
 
-       BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
-               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() + "'!");
                }
        }
 }
index 641ddb1377702d96aa3305cbbac7c3426edd9aba..32f4363990b6f82d52382d0b6cff134b290ee346 100644 (file)
@@ -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>& checkable, const ApplyRule& rule);
+       static bool EvaluateApplyRule(const shared_ptr<Checkable>& checkable, const ApplyRule& rule);
        static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
 };
 
index 37aeb6bc53340d4d3438da1e06ebf318e24c6ae0..29442b72f5228808d8227df17cfe5c81984f2919 100644 (file)
@@ -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<ConfigItemBuilder>(di);
        builder->SetType("ScheduledDowntime");
-       builder->SetName(name);
+       builder->SetName(rule.GetName());
        builder->SetScope(rule.GetScope());
 
        builder->AddExpression(make_shared<AExpression>(&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<ApplyRule>& rules)
 {
-       BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
-               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<Host>()) {
+                               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<Service>()) {
+                               CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
 
-       BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
-               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() + "'!");
                }
        }
 }
index 4ee8d11496064fa6dbe4af16e5ee53c4105b1528..23741e10bd77ca0c49e0b31f63f4c38973e1eab0 100644 (file)
@@ -55,7 +55,7 @@ private:
        std::pair<double, double> 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<ApplyRule>& rules);
 };
 
index 15971a1cec38b9b67276d5723c8dab3b0146a3ea..889ea66dc61c1a6cdc0ce2d86fb3ae4b18ef6cd6 100644 (file)
@@ -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<ApplyRule>& rules)
 {
-       BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
-               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<Host>()) {
+                       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!");
        }
 }
index e5a12e1bc9faf5e12ce73e45a61b1bef70578f60..89903271d116e7872cf5e2fe60e98af5e854bd6c 100644 (file)
@@ -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<ApplyRule>& rules);
 };