]> granicus.if.org Git - icinga2/blob - lib/icinga/dependency-apply.cpp
Merge branch 'support/2.5'
[icinga2] / lib / icinga / dependency-apply.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
31
32 using namespace icinga;
33
34 INITIALIZE_ONCE(&Dependency::RegisterApplyRuleHandler);
35
36 void Dependency::RegisterApplyRuleHandler(void)
37 {
38         std::vector<String> targets;
39         targets.push_back("Host");
40         targets.push_back("Service");
41         ApplyRule::RegisterType("Dependency", targets);
42 }
43
44 bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
45 {
46         if (!rule.EvaluateFilter(frame))
47                 return false;
48
49         DebugInfo di = rule.GetDebugInfo();
50
51 #ifdef _DEBUG
52         Log(LogDebug, "Dependency")
53                 << "Applying dependency '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
54 #endif /* _DEBUG */
55
56         ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
57         builder->SetType("Dependency");
58         builder->SetName(name);
59         builder->SetScope(frame.Locals->ShallowClone());
60         builder->SetIgnoreOnError(rule.GetIgnoreOnError());
61
62         Host::Ptr host;
63         Service::Ptr service;
64         tie(host, service) = GetHostService(checkable);
65
66         builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
67         builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
68
69         if (service)
70                 builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
71
72         String zone = checkable->GetZoneName();
73
74         if (!zone.IsEmpty())
75                 builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
76
77         builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "package"), OpSetLiteral, MakeLiteral(rule.GetPackage()), di));
78         
79         builder->AddExpression(new OwnedExpression(rule.GetExpression()));
80
81         ConfigItem::Ptr dependencyItem = builder->Compile();
82         dependencyItem->Register();
83
84         return true;
85 }
86
87 bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
88 {
89         DebugInfo di = rule.GetDebugInfo();
90
91         std::ostringstream msgbuf;
92         msgbuf << "Evaluating 'apply' rule (" << di << ")";
93         CONTEXT(msgbuf.str());
94
95         Host::Ptr host;
96         Service::Ptr service;
97         tie(host, service) = GetHostService(checkable);
98
99         ScriptFrame frame;
100         if (rule.GetScope())
101                 rule.GetScope()->CopyTo(frame.Locals);
102         frame.Locals->Set("host", host);
103         if (service)
104                 frame.Locals->Set("service", service);
105
106         Value vinstances;
107
108         if (rule.GetFTerm()) {
109                 try {
110                         vinstances = rule.GetFTerm()->Evaluate(frame);
111                 } catch (const std::exception&) {
112                         /* Silently ignore errors here and assume there are no instances. */
113                         return false;
114                 }
115         } else {
116                 Array::Ptr instances = new Array();
117                 instances->Add("");
118                 vinstances = instances;
119         }
120
121         bool match = false;
122
123         if (vinstances.IsObjectType<Array>()) {
124                 if (!rule.GetFVVar().IsEmpty())
125                         BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
126
127                 Array::Ptr arr = vinstances;
128
129                 ObjectLock olock(arr);
130                 BOOST_FOREACH(const Value& instance, arr) {
131                         String name = rule.GetName();
132
133                         if (!rule.GetFKVar().IsEmpty()) {
134                                 frame.Locals->Set(rule.GetFKVar(), instance);
135                                 name += instance;
136                         }
137
138                         if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
139                                 match = true;
140                 }
141         } else if (vinstances.IsObjectType<Dictionary>()) {
142                 if (rule.GetFVVar().IsEmpty())
143                         BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
144         
145                 Dictionary::Ptr dict = vinstances;
146
147                 BOOST_FOREACH(const String& key, dict->GetKeys()) {
148                         frame.Locals->Set(rule.GetFKVar(), key);
149                         frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
150
151                         if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
152                                 match = true;
153                 }
154         }
155
156         return match;
157 }
158
159 void Dependency::EvaluateApplyRules(const Host::Ptr& host)
160 {
161         CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
162
163         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency")) {
164                 if (rule.GetTargetType() != "Host")
165                         continue;
166
167                 if (EvaluateApplyRule(host, rule))
168                         rule.AddMatch();
169         }
170 }
171
172 void Dependency::EvaluateApplyRules(const Service::Ptr& service)
173 {
174         CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
175
176         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency")) {
177                 if (rule.GetTargetType() != "Service")
178                         continue;
179
180                 if (EvaluateApplyRule(service, rule))
181                         rule.AddMatch();
182         }
183 }