2014-03-28 23:03:53 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-04-23 15:57:58 +02:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2014-03-28 23:03:53 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/dependency.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "config/configitembuilder.hpp"
|
|
|
|
#include "config/applyrule.hpp"
|
2014-10-10 11:39:03 +02:00
|
|
|
#include "config/configcompilercontext.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/initialize.hpp"
|
|
|
|
#include "base/dynamictype.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/context.hpp"
|
|
|
|
#include "base/workqueue.hpp"
|
2014-10-10 11:39:03 +02:00
|
|
|
#include "base/configerror.hpp"
|
2014-03-28 23:03:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
INITIALIZE_ONCE(&Dependency::RegisterApplyRuleHandler);
|
|
|
|
|
|
|
|
void Dependency::RegisterApplyRuleHandler(void)
|
|
|
|
{
|
2014-04-05 12:56:56 +02:00
|
|
|
std::vector<String> targets;
|
|
|
|
targets.push_back("Host");
|
|
|
|
targets.push_back("Service");
|
2014-11-16 16:20:39 +01:00
|
|
|
ApplyRule::RegisterType("Dependency", targets);
|
2014-03-28 23:03:53 +01:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
void Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, VMFrame& frame, const ApplyRule& rule)
|
2014-11-04 11:01:00 +01:00
|
|
|
{
|
|
|
|
DebugInfo di = rule.GetDebugInfo();
|
|
|
|
|
|
|
|
Log(LogDebug, "Dependency")
|
|
|
|
<< "Applying dependency '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
|
2014-11-04 11:01:00 +01:00
|
|
|
builder->SetType("Dependency");
|
|
|
|
builder->SetName(name);
|
2014-11-22 12:21:28 +01:00
|
|
|
builder->SetScope(frame.Locals);
|
2014-11-04 11:01:00 +01:00
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
builder->AddExpression(new SetExpression(MakeIndexer("parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), false, di));
|
|
|
|
builder->AddExpression(new SetExpression(MakeIndexer("child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), false, di));
|
2014-11-09 19:48:28 +01:00
|
|
|
|
|
|
|
if (service)
|
2014-11-22 12:21:28 +01:00
|
|
|
builder->AddExpression(new SetExpression(MakeIndexer("child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), false, di));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
|
|
|
String zone = checkable->GetZone();
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (!zone.IsEmpty())
|
2014-11-22 12:21:28 +01:00
|
|
|
builder->AddExpression(new SetExpression(MakeIndexer("zone"), OpSetLiteral, MakeLiteral(zone), false, di));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
builder->AddExpression(new OwnedExpression(rule.GetExpression()));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
|
|
|
ConfigItem::Ptr dependencyItem = builder->Compile();
|
2014-11-21 18:31:37 +01:00
|
|
|
dependencyItem->Commit();
|
2014-11-04 11:01:00 +01:00
|
|
|
}
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
|
2014-03-28 23:03:53 +01:00
|
|
|
{
|
2014-04-05 12:56:56 +02:00
|
|
|
DebugInfo di = rule.GetDebugInfo();
|
2014-03-28 23:03:53 +01:00
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Evaluating 'apply' rule (" << di << ")";
|
|
|
|
CONTEXT(msgbuf.str());
|
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
VMFrame frame;
|
|
|
|
if (rule.GetScope())
|
|
|
|
rule.GetScope()->CopyTo(frame.Locals);
|
|
|
|
frame.Locals->Set("host", host);
|
2014-04-05 12:56:56 +02:00
|
|
|
if (service)
|
2014-11-22 12:21:28 +01:00
|
|
|
frame.Locals->Set("service", service);
|
2014-03-28 23:03:53 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
if (!rule.EvaluateFilter(frame))
|
2014-04-07 13:23:27 +02:00
|
|
|
return false;
|
2014-03-28 23:03:53 +01:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
Value vinstances;
|
2014-03-28 23:03:53 +01:00
|
|
|
|
2014-11-02 07:22:00 +01:00
|
|
|
if (rule.GetFTerm()) {
|
2014-11-22 12:21:28 +01:00
|
|
|
vinstances = rule.GetFTerm()->Evaluate(frame);
|
2014-11-02 07:22:00 +01:00
|
|
|
} else {
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr instances = new Array();
|
2014-11-02 07:22:00 +01:00
|
|
|
instances->Add("");
|
2014-11-04 11:01:00 +01:00
|
|
|
vinstances = instances;
|
2014-04-05 12:56:56 +02:00
|
|
|
}
|
2014-03-28 23:03:53 +01:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
if (vinstances.IsObjectType<Array>()) {
|
|
|
|
if (!rule.GetFVVar().IsEmpty())
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Array iterator requires value to be an array.") << errinfo_debuginfo(di));
|
2014-11-02 07:22:00 +01:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
Array::Ptr arr = vinstances;
|
2014-11-02 07:22:00 +01:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
ObjectLock olock(arr);
|
|
|
|
BOOST_FOREACH(const String& instance, arr) {
|
|
|
|
String name = rule.GetName();
|
2014-11-02 07:22:00 +01:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
if (!rule.GetFKVar().IsEmpty()) {
|
2014-11-22 12:21:28 +01:00
|
|
|
frame.Locals->Set(rule.GetFKVar(), instance);
|
2014-11-04 11:01:00 +01:00
|
|
|
name += instance;
|
|
|
|
}
|
2014-11-02 07:22:00 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
EvaluateApplyRuleInstance(checkable, name, frame, rule);
|
2014-11-02 07:22:00 +01:00
|
|
|
}
|
2014-11-04 11:01:00 +01:00
|
|
|
} else if (vinstances.IsObjectType<Dictionary>()) {
|
|
|
|
if (rule.GetFVVar().IsEmpty())
|
|
|
|
BOOST_THROW_EXCEPTION(ConfigError("Dictionary iterator requires value to be a dictionary.") << errinfo_debuginfo(di));
|
|
|
|
|
|
|
|
Dictionary::Ptr dict = vinstances;
|
|
|
|
|
|
|
|
ObjectLock olock(dict);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
|
2014-11-22 12:21:28 +01:00
|
|
|
frame.Locals->Set(rule.GetFKVar(), kv.first);
|
|
|
|
frame.Locals->Set(rule.GetFVVar(), kv.second);
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule);
|
2014-11-02 07:22:00 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-07 13:23:27 +02:00
|
|
|
|
|
|
|
return true;
|
2014-04-05 12:56:56 +02:00
|
|
|
}
|
2014-03-30 10:00:11 +02:00
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
void Dependency::EvaluateApplyRules(const Host::Ptr& host)
|
2014-04-05 12:56:56 +02:00
|
|
|
{
|
2014-11-16 16:20:39 +01:00
|
|
|
CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
|
|
|
|
|
2014-12-08 11:43:33 +01:00
|
|
|
BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency")) {
|
2014-11-16 16:20:39 +01:00
|
|
|
if (rule.GetTargetType() != "Host")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (EvaluateApplyRule(host, rule))
|
|
|
|
rule.AddMatch();
|
|
|
|
} catch (const ConfigError& ex) {
|
|
|
|
const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
|
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
|
2014-05-17 20:13:25 +02:00
|
|
|
}
|
2014-03-28 23:03:53 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-17 20:13:25 +02:00
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
void Dependency::EvaluateApplyRules(const Service::Ptr& service)
|
2014-05-17 20:13:25 +02:00
|
|
|
{
|
2014-11-16 16:20:39 +01:00
|
|
|
CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
|
|
|
|
|
2014-12-08 11:43:33 +01:00
|
|
|
BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency")) {
|
2014-11-16 16:20:39 +01:00
|
|
|
if (rule.GetTargetType() != "Service")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (EvaluateApplyRule(service, rule))
|
|
|
|
rule.AddMatch();
|
|
|
|
} catch (const ConfigError& ex) {
|
|
|
|
const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
|
|
|
|
ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
|
|
|
|
}
|
2014-05-17 20:13:25 +02:00
|
|
|
}
|
|
|
|
}
|