Log warning if apply rule does not match anywhere.

Fixes #5911
Fixes #5957
This commit is contained in:
Michael Friedrich 2014-04-07 13:23:27 +02:00
parent fded50632e
commit aae5f092d4
9 changed files with 119 additions and 66 deletions

View File

@ -99,6 +99,9 @@ void ApplyRule::EvaluateRules(void)
}
}
if (cont)
continue;
completedTypes.insert(sourceType);
RuleMap::const_iterator it = m_Rules.find(kv.first);

View 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() + "'");
BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
if (EvaluateApplyRule(host, 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 host does not match anywhere!");
EvaluateApplyRule(service, rule);
} 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() + "'!");
}
}
}

View 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);
};

View 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() + "'");
BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
if (EvaluateApplyRule(host, 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 host does not match anywhere!");
EvaluateApplyRule(service, rule);
} 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() + "'!");
}
}
}

View 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);
};

View 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() + "'");
BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
if (EvaluateApplyRule(host, 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 host does not match anywhere!");
EvaluateApplyRule(service, rule);
} 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() + "'!");
}
}
}

View 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);
};

View 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)
EvaluateApplyRule(host, rule);
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++;
}
if (apply_count == 0)
Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!");
}
}

View 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);
};