1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
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"
30 #include <boost/foreach.hpp>
32 using namespace icinga;
34 INITIALIZE_ONCE(&Dependency::RegisterApplyRuleHandler);
36 void Dependency::RegisterApplyRuleHandler(void)
38 std::vector<String> targets;
39 targets.push_back("Host");
40 targets.push_back("Service");
41 ApplyRule::RegisterType("Dependency", targets);
44 bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
46 if (!rule.EvaluateFilter(frame))
49 DebugInfo di = rule.GetDebugInfo();
51 Log(LogDebug, "Dependency")
52 << "Applying dependency '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
54 ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
55 builder->SetType("Dependency");
56 builder->SetName(name);
57 builder->SetScope(frame.Locals->ShallowClone());
58 builder->SetIgnoreOnError(rule.GetIgnoreOnError());
62 tie(host, service) = GetHostService(checkable);
64 builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
65 builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
68 builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
70 String zone = checkable->GetZoneName();
73 builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
75 builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
77 builder->AddExpression(new OwnedExpression(rule.GetExpression()));
79 ConfigItem::Ptr dependencyItem = builder->Compile();
80 dependencyItem->Register();
85 bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
87 DebugInfo di = rule.GetDebugInfo();
89 std::ostringstream msgbuf;
90 msgbuf << "Evaluating 'apply' rule (" << di << ")";
91 CONTEXT(msgbuf.str());
95 tie(host, service) = GetHostService(checkable);
99 rule.GetScope()->CopyTo(frame.Locals);
100 frame.Locals->Set("host", host);
102 frame.Locals->Set("service", service);
106 if (rule.GetFTerm()) {
108 vinstances = rule.GetFTerm()->Evaluate(frame);
109 } catch (const std::exception&) {
110 /* Silently ignore errors here and assume there are no instances. */
114 Array::Ptr instances = new Array();
116 vinstances = instances;
121 if (vinstances.IsObjectType<Array>()) {
122 if (!rule.GetFVVar().IsEmpty())
123 BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
125 Array::Ptr arr = vinstances;
126 Array::Ptr arrclone = arr->ShallowClone();
128 ObjectLock olock(arrclone);
129 BOOST_FOREACH(const Value& instance, arrclone) {
130 String name = rule.GetName();
132 if (!rule.GetFKVar().IsEmpty()) {
133 frame.Locals->Set(rule.GetFKVar(), instance);
137 if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
140 } else if (vinstances.IsObjectType<Dictionary>()) {
141 if (rule.GetFVVar().IsEmpty())
142 BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
144 Dictionary::Ptr dict = vinstances;
146 BOOST_FOREACH(const String& key, dict->GetKeys()) {
147 frame.Locals->Set(rule.GetFKVar(), key);
148 frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
150 if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
158 void Dependency::EvaluateApplyRules(const Host::Ptr& host)
160 CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
162 BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency")) {
163 if (rule.GetTargetType() != "Host")
166 if (EvaluateApplyRule(host, rule))
171 void Dependency::EvaluateApplyRules(const Service::Ptr& service)
173 CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
175 BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency")) {
176 if (rule.GetTargetType() != "Service")
179 if (EvaluateApplyRule(service, rule))