1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/dependency.hpp"
21 #include "icinga/service.hpp"
22 #include "config/configitembuilder.hpp"
23 #include "config/applyrule.hpp"
24 #include "base/initialize.hpp"
25 #include "base/configtype.hpp"
26 #include "base/logger.hpp"
27 #include "base/context.hpp"
28 #include "base/workqueue.hpp"
29 #include "base/exception.hpp"
31 using namespace icinga;
33 INITIALIZE_ONCE([]() {
34 ApplyRule::RegisterType("Dependency", { "Host", "Service" });
37 bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
39 if (!rule.EvaluateFilter(frame))
42 DebugInfo di = rule.GetDebugInfo();
45 Log(LogDebug, "Dependency")
46 << "Applying dependency '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
49 ConfigItemBuilder builder{di};
50 builder.SetType(Dependency::TypeInstance);
51 builder.SetName(name);
52 builder.SetScope(frame.Locals->ShallowClone());
53 builder.SetIgnoreOnError(rule.GetIgnoreOnError());
57 tie(host, service) = GetHostService(checkable);
59 builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
60 builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
63 builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
65 String zone = checkable->GetZoneName();
68 builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
70 builder.AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
72 builder.AddExpression(new ImportDefaultTemplatesExpression());
74 builder.AddExpression(new OwnedExpression(rule.GetExpression()));
76 ConfigItem::Ptr dependencyItem = builder.Compile();
77 dependencyItem->Register();
82 bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
84 DebugInfo di = rule.GetDebugInfo();
86 std::ostringstream msgbuf;
87 msgbuf << "Evaluating 'apply' rule (" << di << ")";
88 CONTEXT(msgbuf.str());
92 tie(host, service) = GetHostService(checkable);
94 ScriptFrame frame(true);
96 rule.GetScope()->CopyTo(frame.Locals);
97 frame.Locals->Set("host", host);
99 frame.Locals->Set("service", service);
103 if (rule.GetFTerm()) {
105 vinstances = rule.GetFTerm()->Evaluate(frame);
106 } catch (const std::exception&) {
107 /* Silently ignore errors here and assume there are no instances. */
111 vinstances = new Array({ "" });
116 if (vinstances.IsObjectType<Array>()) {
117 if (!rule.GetFVVar().IsEmpty())
118 BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
120 Array::Ptr arr = vinstances;
122 ObjectLock olock(arr);
123 for (const Value& instance : arr) {
124 String name = rule.GetName();
126 if (!rule.GetFKVar().IsEmpty()) {
127 frame.Locals->Set(rule.GetFKVar(), instance);
131 if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
134 } else if (vinstances.IsObjectType<Dictionary>()) {
135 if (rule.GetFVVar().IsEmpty())
136 BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
138 Dictionary::Ptr dict = vinstances;
140 for (const String& key : dict->GetKeys()) {
141 frame.Locals->Set(rule.GetFKVar(), key);
142 frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
144 if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
152 void Dependency::EvaluateApplyRules(const Host::Ptr& host)
154 CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
156 for (ApplyRule& rule : ApplyRule::GetRules("Dependency")) {
157 if (rule.GetTargetType() != "Host")
160 if (EvaluateApplyRule(host, rule))
165 void Dependency::EvaluateApplyRules(const Service::Ptr& service)
167 CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
169 for (ApplyRule& rule : ApplyRule::GetRules("Dependency")) {
170 if (rule.GetTargetType() != "Service")
173 if (EvaluateApplyRule(service, rule))