2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-03-28 23:03:53 +01:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/dependency.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "config/configitembuilder.hpp"
|
|
|
|
#include "config/applyrule.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.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-12-18 15:11:57 +01:00
|
|
|
#include "base/exception.hpp"
|
2014-03-28 23:03:53 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2016-08-27 09:35:08 +02:00
|
|
|
INITIALIZE_ONCE([]() {
|
2017-11-23 09:58:05 +01:00
|
|
|
ApplyRule::RegisterType("Dependency", { "Host", "Service" });
|
2016-08-27 09:35:08 +02:00
|
|
|
});
|
2014-03-28 23:03:53 +01:00
|
|
|
|
2022-10-27 16:15:35 +02:00
|
|
|
bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter)
|
2014-11-04 11:01:00 +01:00
|
|
|
{
|
2022-10-27 16:15:35 +02:00
|
|
|
if (!skipFilter && !rule.EvaluateFilter(frame))
|
2015-01-29 12:38:25 +01:00
|
|
|
return false;
|
|
|
|
|
2022-10-28 15:33:44 +02:00
|
|
|
auto& di (rule.GetDebugInfo());
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2016-08-20 23:46:44 +02:00
|
|
|
#ifdef _DEBUG
|
2014-11-04 11:01:00 +01:00
|
|
|
Log(LogDebug, "Dependency")
|
|
|
|
<< "Applying dependency '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
|
2016-08-20 23:46:44 +02:00
|
|
|
#endif /* _DEBUG */
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2018-01-10 17:20:33 +01:00
|
|
|
ConfigItemBuilder builder{di};
|
|
|
|
builder.SetType(Dependency::TypeInstance);
|
|
|
|
builder.SetName(name);
|
|
|
|
builder.SetScope(frame.Locals->ShallowClone());
|
|
|
|
builder.SetIgnoreOnError(rule.GetIgnoreOnError());
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2020-03-18 16:31:35 +01:00
|
|
|
builder.AddExpression(new ImportDefaultTemplatesExpression());
|
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
2018-01-10 17:20:33 +01:00
|
|
|
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
|
|
|
|
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
|
2014-11-09 19:48:28 +01:00
|
|
|
|
|
|
|
if (service)
|
2018-01-10 17:20:33 +01:00
|
|
|
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2015-02-09 08:50:17 +01:00
|
|
|
String zone = checkable->GetZoneName();
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
if (!zone.IsEmpty())
|
2018-01-10 17:20:33 +01:00
|
|
|
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2018-01-10 17:20:33 +01:00
|
|
|
builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
|
2016-08-28 10:27:43 +02:00
|
|
|
|
2018-01-10 17:20:33 +01:00
|
|
|
builder.AddExpression(new OwnedExpression(rule.GetExpression()));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2018-01-10 17:20:33 +01:00
|
|
|
ConfigItem::Ptr dependencyItem = builder.Compile();
|
2015-03-19 15:47:46 +01:00
|
|
|
dependencyItem->Register();
|
2015-01-29 12:38:25 +01:00
|
|
|
|
|
|
|
return true;
|
2014-11-04 11:01:00 +01:00
|
|
|
}
|
|
|
|
|
2022-10-27 16:15:35 +02:00
|
|
|
bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule, bool skipFilter)
|
2014-03-28 23:03:53 +01:00
|
|
|
{
|
2022-10-28 15:33:44 +02:00
|
|
|
auto& di (rule.GetDebugInfo());
|
2014-03-28 23:03:53 +01:00
|
|
|
|
2022-10-28 17:19:50 +02:00
|
|
|
CONTEXT("Evaluating 'apply' rule (" << di << ")");
|
2014-04-05 12:56:56 +02:00
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
2018-01-03 10:19:24 +01:00
|
|
|
ScriptFrame frame(true);
|
2014-11-22 12:21:28 +01:00
|
|
|
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-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()) {
|
2015-01-29 12:38:25 +01:00
|
|
|
try {
|
|
|
|
vinstances = rule.GetFTerm()->Evaluate(frame);
|
|
|
|
} catch (const std::exception&) {
|
|
|
|
/* Silently ignore errors here and assume there are no instances. */
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-02 07:22:00 +01:00
|
|
|
} else {
|
2018-01-11 11:17:38 +01:00
|
|
|
vinstances = new Array({ "" });
|
2014-04-05 12:56:56 +02:00
|
|
|
}
|
2014-03-28 23:03:53 +01:00
|
|
|
|
2015-01-29 12:38:25 +01:00
|
|
|
bool match = false;
|
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
if (vinstances.IsObjectType<Array>()) {
|
|
|
|
if (!rule.GetFVVar().IsEmpty())
|
2015-09-24 08:29:13 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", 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
|
|
|
|
2016-08-20 23:46:44 +02:00
|
|
|
ObjectLock olock(arr);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& instance : arr) {
|
2014-11-04 11:01:00 +01:00
|
|
|
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
|
|
|
|
2022-10-27 16:15:35 +02:00
|
|
|
if (EvaluateApplyRuleInstance(checkable, name, frame, rule, skipFilter))
|
2015-01-29 12:38:25 +01:00
|
|
|
match = true;
|
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())
|
2015-09-24 08:29:13 +02:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
Dictionary::Ptr dict = vinstances;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& key : dict->GetKeys()) {
|
2015-01-29 12:38:25 +01:00
|
|
|
frame.Locals->Set(rule.GetFKVar(), key);
|
|
|
|
frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
|
2014-11-04 11:01:00 +01:00
|
|
|
|
2022-10-27 16:15:35 +02:00
|
|
|
if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule, skipFilter))
|
2015-01-29 12:38:25 +01:00
|
|
|
match = true;
|
2014-11-02 07:22:00 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-07 13:23:27 +02:00
|
|
|
|
2015-01-29 12:38:25 +01:00
|
|
|
return match;
|
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
|
|
|
{
|
2022-11-24 12:40:36 +01:00
|
|
|
CONTEXT("Evaluating 'apply' rules for host '" << host->GetName() << "'");
|
2014-11-16 16:20:39 +01:00
|
|
|
|
2022-10-17 16:54:34 +02:00
|
|
|
for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Host::TypeInstance)) {
|
2022-10-27 16:54:09 +02:00
|
|
|
if (EvaluateApplyRule(host, *rule))
|
|
|
|
rule->AddMatch();
|
2014-03-28 23:03:53 +01:00
|
|
|
}
|
2022-10-18 16:48:58 +02:00
|
|
|
|
2022-10-28 12:41:21 +02:00
|
|
|
for (auto& rule : ApplyRule::GetTargetedHostRules(Dependency::TypeInstance, host->GetName())) {
|
2022-10-27 16:15:35 +02:00
|
|
|
if (EvaluateApplyRule(host, *rule, true))
|
2022-10-18 16:48:58 +02:00
|
|
|
rule->AddMatch();
|
|
|
|
}
|
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
|
|
|
{
|
2022-11-24 12:40:36 +01:00
|
|
|
CONTEXT("Evaluating 'apply' rules for service '" << service->GetName() << "'");
|
2014-11-16 16:20:39 +01:00
|
|
|
|
2022-10-17 16:54:34 +02:00
|
|
|
for (auto& rule : ApplyRule::GetRules(Dependency::TypeInstance, Service::TypeInstance)) {
|
2022-10-27 16:54:09 +02:00
|
|
|
if (EvaluateApplyRule(service, *rule))
|
|
|
|
rule->AddMatch();
|
2014-05-17 20:13:25 +02:00
|
|
|
}
|
2022-10-18 16:48:58 +02:00
|
|
|
|
2022-11-04 10:19:26 +01:00
|
|
|
for (auto& rule : ApplyRule::GetTargetedServiceRules(Dependency::TypeInstance, service->GetHost()->GetName(), service->GetShortName())) {
|
2022-10-27 16:15:35 +02:00
|
|
|
if (EvaluateApplyRule(service, *rule, true))
|
2022-10-18 16:48:58 +02:00
|
|
|
rule->AddMatch();
|
|
|
|
}
|
2014-05-17 20:13:25 +02:00
|
|
|
}
|