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