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