Execute apply/object rules in parallel.

Fixes #6223
This commit is contained in:
Gunnar Beutner 2014-05-17 20:13:25 +02:00
parent bbb2fb62dd
commit f26b37e763
12 changed files with 158 additions and 95 deletions

View File

@ -25,6 +25,7 @@
#include "base/dynamictype.h" #include "base/dynamictype.h"
#include "base/logger_fwd.h" #include "base/logger_fwd.h"
#include "base/context.h" #include "base/context.h"
#include "base/workqueue.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
using namespace icinga; using namespace icinga;
@ -39,7 +40,7 @@ void Dependency::RegisterApplyRuleHandler(void)
ApplyRule::RegisterType("Dependency", targets, &Dependency::EvaluateApplyRules); ApplyRule::RegisterType("Dependency", targets, &Dependency::EvaluateApplyRules);
} }
bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) bool Dependency::EvaluateApplyRuleOne(const Checkable::Ptr& checkable, const ApplyRule& rule)
{ {
DebugInfo di = rule.GetDebugInfo(); DebugInfo di = rule.GetDebugInfo();
@ -104,39 +105,48 @@ bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyR
return true; return true;
} }
void Dependency::EvaluateApplyRules(const std::vector<ApplyRule>& rules) void Dependency::EvaluateApplyRule(const ApplyRule& rule)
{ {
int apply_count = 0; int apply_count = 0;
BOOST_FOREACH(const ApplyRule& rule, rules) { if (rule.GetTargetType() == "Host") {
if (rule.GetTargetType() == "Host") { apply_count = 0;
apply_count = 0;
BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) { BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
if (EvaluateApplyRule(host, rule)) if (EvaluateApplyRuleOne(host, rule))
apply_count++; 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() + "'");
if(EvaluateApplyRule(service, rule))
apply_count++;
}
if (apply_count == 0)
Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
} else {
Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!");
} }
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() + "'");
if (EvaluateApplyRuleOne(service, rule))
apply_count++;
}
if (apply_count == 0)
Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
} else {
Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!");
} }
} }
void Dependency::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
{
ParallelWorkQueue upq;
BOOST_FOREACH(const ApplyRule& rule, rules) {
upq.Enqueue(boost::bind(&Dependency::EvaluateApplyRule, boost::cref(rule)));
}
upq.Join();
}

View File

@ -57,7 +57,8 @@ protected:
virtual void Stop(void); virtual void Stop(void);
private: private:
static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule); static bool EvaluateApplyRuleOne(const Checkable::Ptr& checkable, const ApplyRule& rule);
static void EvaluateApplyRule(const ApplyRule& rule);
static void EvaluateApplyRules(const std::vector<ApplyRule>& rules); static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
}; };

View File

@ -23,6 +23,7 @@
#include "base/logger_fwd.h" #include "base/logger_fwd.h"
#include "base/objectlock.h" #include "base/objectlock.h"
#include "base/context.h" #include "base/context.h"
#include "base/workqueue.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
using namespace icinga; using namespace icinga;
@ -36,7 +37,7 @@ void HostGroup::RegisterObjectRuleHandler(void)
ObjectRule::RegisterType("HostGroup", &HostGroup::EvaluateObjectRules); ObjectRule::RegisterType("HostGroup", &HostGroup::EvaluateObjectRules);
} }
bool HostGroup::EvaluateObjectRule(const Host::Ptr host, const ObjectRule& rule) bool HostGroup::EvaluateObjectRuleOne(const Host::Ptr host, const ObjectRule& rule)
{ {
DebugInfo di = rule.GetDebugInfo(); DebugInfo di = rule.GetDebugInfo();
@ -71,15 +72,24 @@ bool HostGroup::EvaluateObjectRule(const Host::Ptr host, const ObjectRule& rule)
return true; return true;
} }
void HostGroup::EvaluateObjectRule(const ObjectRule& rule)
{
BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
CONTEXT("Evaluating group membership in '" + rule.GetName() + "' for host '" + host->GetName() + "'");
EvaluateObjectRuleOne(host, rule);
}
}
void HostGroup::EvaluateObjectRules(const std::vector<ObjectRule>& rules) void HostGroup::EvaluateObjectRules(const std::vector<ObjectRule>& rules)
{ {
BOOST_FOREACH(const ObjectRule& rule, rules) { ParallelWorkQueue upq;
BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
CONTEXT("Evaluating group membership in '" + rule.GetName() + "' for host '" + host->GetName() + "'");
EvaluateObjectRule(host, rule); BOOST_FOREACH(const ObjectRule& rule, rules) {
} upq.Enqueue(boost::bind(HostGroup::EvaluateObjectRule, boost::cref(rule)));
} }
upq.Join();
} }
std::set<Host::Ptr> HostGroup::GetMembers(void) const std::set<Host::Ptr> HostGroup::GetMembers(void) const

View File

@ -52,7 +52,8 @@ private:
mutable boost::mutex m_HostGroupMutex; mutable boost::mutex m_HostGroupMutex;
std::set<Host::Ptr> m_Members; std::set<Host::Ptr> m_Members;
static bool EvaluateObjectRule(const Host::Ptr host, const ObjectRule& rule); static bool EvaluateObjectRuleOne(const Host::Ptr host, const ObjectRule& rule);
static void EvaluateObjectRule(const ObjectRule& rule);
static void EvaluateObjectRules(const std::vector<ObjectRule>& rules); static void EvaluateObjectRules(const std::vector<ObjectRule>& rules);
}; };

View File

@ -25,6 +25,7 @@
#include "base/dynamictype.h" #include "base/dynamictype.h"
#include "base/logger_fwd.h" #include "base/logger_fwd.h"
#include "base/context.h" #include "base/context.h"
#include "base/workqueue.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
using namespace icinga; using namespace icinga;
@ -39,7 +40,7 @@ void Notification::RegisterApplyRuleHandler(void)
ApplyRule::RegisterType("Notification", targets, &Notification::EvaluateApplyRules); ApplyRule::RegisterType("Notification", targets, &Notification::EvaluateApplyRules);
} }
bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule) bool Notification::EvaluateApplyRuleOne(const Checkable::Ptr& checkable, const ApplyRule& rule)
{ {
DebugInfo di = rule.GetDebugInfo(); DebugInfo di = rule.GetDebugInfo();
@ -99,39 +100,47 @@ bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const Appl
return true; return true;
} }
void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules) void Notification::EvaluateApplyRule(const ApplyRule& rule)
{ {
int apply_count = 0; int apply_count = 0;
BOOST_FOREACH(const ApplyRule& rule, rules) { if (rule.GetTargetType() == "Host") {
if (rule.GetTargetType() == "Host") { apply_count = 0;
apply_count = 0;
BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) { BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
if (EvaluateApplyRule(host, rule)) if (EvaluateApplyRuleOne(host, rule))
apply_count++; 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() + "'");
if(EvaluateApplyRule(service, rule))
apply_count++;
}
if (apply_count == 0)
Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
} else {
Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!");
} }
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() + "'");
if (EvaluateApplyRuleOne(service, rule))
apply_count++;
}
if (apply_count == 0)
Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
} else {
Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!");
} }
} }
void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
{
ParallelWorkQueue upq;
BOOST_FOREACH(const ApplyRule& rule, rules) {
upq.Enqueue(boost::bind(&Notification::EvaluateApplyRule, boost::cref(rule)));
}
upq.Join();
}

View File

@ -113,7 +113,8 @@ protected:
private: private:
void ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author = "", const String& text = ""); void ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author = "", const String& text = "");
static bool EvaluateApplyRule(const shared_ptr<Checkable>& checkable, const ApplyRule& rule); static bool EvaluateApplyRuleOne(const shared_ptr<Checkable>& checkable, const ApplyRule& rule);
static void EvaluateApplyRule(const ApplyRule& rule);
static void EvaluateApplyRules(const std::vector<ApplyRule>& rules); static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
}; };

View File

@ -24,6 +24,7 @@
#include "base/dynamictype.h" #include "base/dynamictype.h"
#include "base/logger_fwd.h" #include "base/logger_fwd.h"
#include "base/context.h" #include "base/context.h"
#include "base/workqueue.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
using namespace icinga; using namespace icinga;
@ -37,7 +38,7 @@ void Service::RegisterApplyRuleHandler(void)
ApplyRule::RegisterType("Service", targets, &Service::EvaluateApplyRules); ApplyRule::RegisterType("Service", targets, &Service::EvaluateApplyRules);
} }
bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule) bool Service::EvaluateApplyRuleOne(const Host::Ptr& host, const ApplyRule& rule)
{ {
DebugInfo di = rule.GetDebugInfo(); DebugInfo di = rule.GetDebugInfo();
@ -89,21 +90,28 @@ bool Service::EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule)
return true; return true;
} }
void Service::EvaluateApplyRules(const std::vector<ApplyRule>& rules) void Service::EvaluateApplyRule(const ApplyRule& rule)
{ {
int apply_count = 0; int apply_count = 0;
BOOST_FOREACH(const ApplyRule& rule, rules) { BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
apply_count = 0; CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) { if (EvaluateApplyRuleOne(host, rule))
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'"); apply_count++;
if (EvaluateApplyRule(host, rule))
apply_count++;
}
if (apply_count == 0)
Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!");
} }
if (apply_count == 0)
Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!");
}
void Service::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
{
ParallelWorkQueue upq;
BOOST_FOREACH(const ApplyRule& rule, rules) {
upq.Enqueue(boost::bind(&Service::EvaluateApplyRule, boost::cref(rule)));
}
upq.Join();
} }

View File

@ -59,7 +59,8 @@ protected:
private: private:
Host::Ptr m_Host; Host::Ptr m_Host;
static bool EvaluateApplyRule(const Host::Ptr& host, const ApplyRule& rule); static bool EvaluateApplyRuleOne(const Host::Ptr& host, const ApplyRule& rule);
static void EvaluateApplyRule(const ApplyRule& rule);
static void EvaluateApplyRules(const std::vector<ApplyRule>& rules); static void EvaluateApplyRules(const std::vector<ApplyRule>& rules);
}; };

View File

@ -23,6 +23,7 @@
#include "base/objectlock.h" #include "base/objectlock.h"
#include "base/logger_fwd.h" #include "base/logger_fwd.h"
#include "base/context.h" #include "base/context.h"
#include "base/workqueue.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
using namespace icinga; using namespace icinga;
@ -36,7 +37,7 @@ void ServiceGroup::RegisterObjectRuleHandler(void)
ObjectRule::RegisterType("ServiceGroup", &ServiceGroup::EvaluateObjectRules); ObjectRule::RegisterType("ServiceGroup", &ServiceGroup::EvaluateObjectRules);
} }
bool ServiceGroup::EvaluateObjectRule(const Service::Ptr service, const ObjectRule& rule) bool ServiceGroup::EvaluateObjectRuleOne(const Service::Ptr service, const ObjectRule& rule)
{ {
DebugInfo di = rule.GetDebugInfo(); DebugInfo di = rule.GetDebugInfo();
@ -74,15 +75,24 @@ bool ServiceGroup::EvaluateObjectRule(const Service::Ptr service, const ObjectRu
return true; return true;
} }
void ServiceGroup::EvaluateObjectRule(const ObjectRule& rule)
{
BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
CONTEXT("Evaluating group membership in '" + rule.GetName() + "' for service '" + service->GetName() + "'");
EvaluateObjectRuleOne(service, rule);
}
}
void ServiceGroup::EvaluateObjectRules(const std::vector<ObjectRule>& rules) void ServiceGroup::EvaluateObjectRules(const std::vector<ObjectRule>& rules)
{ {
BOOST_FOREACH(const ObjectRule& rule, rules) { ParallelWorkQueue upq;
BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
CONTEXT("Evaluating group membership in '" + rule.GetName() + "' for service '" + service->GetName() + "'");
EvaluateObjectRule(service, rule); BOOST_FOREACH(const ObjectRule& rule, rules) {
} upq.Enqueue(boost::bind(ServiceGroup::EvaluateObjectRule, boost::cref(rule)));
} }
upq.Join();
} }
std::set<Service::Ptr> ServiceGroup::GetMembers(void) const std::set<Service::Ptr> ServiceGroup::GetMembers(void) const

View File

@ -52,7 +52,8 @@ private:
mutable boost::mutex m_ServiceGroupMutex; mutable boost::mutex m_ServiceGroupMutex;
std::set<Service::Ptr> m_Members; std::set<Service::Ptr> m_Members;
static bool EvaluateObjectRule(const Service::Ptr service, const ObjectRule& rule); static bool EvaluateObjectRuleOne(const Service::Ptr service, const ObjectRule& rule);
static void EvaluateObjectRule(const ObjectRule& rule);
static void EvaluateObjectRules(const std::vector<ObjectRule>& rules); static void EvaluateObjectRules(const std::vector<ObjectRule>& rules);
}; };

View File

@ -23,6 +23,7 @@
#include "base/objectlock.h" #include "base/objectlock.h"
#include "base/logger_fwd.h" #include "base/logger_fwd.h"
#include "base/context.h" #include "base/context.h"
#include "base/workqueue.h"
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
using namespace icinga; using namespace icinga;
@ -36,7 +37,7 @@ void UserGroup::RegisterObjectRuleHandler(void)
ObjectRule::RegisterType("UserGroup", &UserGroup::EvaluateObjectRules); ObjectRule::RegisterType("UserGroup", &UserGroup::EvaluateObjectRules);
} }
bool UserGroup::EvaluateObjectRule(const User::Ptr user, const ObjectRule& rule) bool UserGroup::EvaluateObjectRuleOne(const User::Ptr user, const ObjectRule& rule)
{ {
DebugInfo di = rule.GetDebugInfo(); DebugInfo di = rule.GetDebugInfo();
@ -71,15 +72,24 @@ bool UserGroup::EvaluateObjectRule(const User::Ptr user, const ObjectRule& rule)
return true; return true;
} }
void UserGroup::EvaluateObjectRule(const ObjectRule& rule)
{
BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
CONTEXT("Evaluating group membership in '" + rule.GetName() + "' for user '" + user->GetName() + "'");
EvaluateObjectRuleOne(user, rule);
}
}
void UserGroup::EvaluateObjectRules(const std::vector<ObjectRule>& rules) void UserGroup::EvaluateObjectRules(const std::vector<ObjectRule>& rules)
{ {
BOOST_FOREACH(const ObjectRule& rule, rules) { ParallelWorkQueue upq;
BOOST_FOREACH(const User::Ptr& user, DynamicType::GetObjects<User>()) {
CONTEXT("Evaluating group membership in '" + rule.GetName() + "' for user '" + user->GetName() + "'");
EvaluateObjectRule(user, rule); BOOST_FOREACH(const ObjectRule& rule, rules) {
} upq.Enqueue(boost::bind(UserGroup::EvaluateObjectRule, boost::cref(rule)));
} }
upq.Join();
} }
std::set<User::Ptr> UserGroup::GetMembers(void) const std::set<User::Ptr> UserGroup::GetMembers(void) const

View File

@ -52,7 +52,8 @@ private:
mutable boost::mutex m_UserGroupMutex; mutable boost::mutex m_UserGroupMutex;
std::set<User::Ptr> m_Members; std::set<User::Ptr> m_Members;
static bool EvaluateObjectRule(const User::Ptr user, const ObjectRule& rule); static bool EvaluateObjectRuleOne(const User::Ptr user, const ObjectRule& rule);
static void EvaluateObjectRule(const ObjectRule& rule);
static void EvaluateObjectRules(const std::vector<ObjectRule>& rules); static void EvaluateObjectRules(const std::vector<ObjectRule>& rules);
}; };