]> granicus.if.org Git - icinga2/blob - lib/icinga/dependency-apply.cpp
Merge pull request #5753 from Icinga/fix/ringbuffer-does-not-get-updated-if-nothing...
[icinga2] / lib / icinga / dependency-apply.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 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::Ptr builder = new ConfigItemBuilder(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;
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                 Array::Ptr instances = new Array();
112                 instances->Add("");
113                 vinstances = instances;
114         }
115
116         bool match = false;
117
118         if (vinstances.IsObjectType<Array>()) {
119                 if (!rule.GetFVVar().IsEmpty())
120                         BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
121
122                 Array::Ptr arr = vinstances;
123
124                 ObjectLock olock(arr);
125                 for (const Value& instance : arr) {
126                         String name = rule.GetName();
127
128                         if (!rule.GetFKVar().IsEmpty()) {
129                                 frame.Locals->Set(rule.GetFKVar(), instance);
130                                 name += instance;
131                         }
132
133                         if (EvaluateApplyRuleInstance(checkable, name, frame, rule))
134                                 match = true;
135                 }
136         } else if (vinstances.IsObjectType<Dictionary>()) {
137                 if (rule.GetFVVar().IsEmpty())
138                         BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
139         
140                 Dictionary::Ptr dict = vinstances;
141
142                 for (const String& key : dict->GetKeys()) {
143                         frame.Locals->Set(rule.GetFKVar(), key);
144                         frame.Locals->Set(rule.GetFVVar(), dict->Get(key));
145
146                         if (EvaluateApplyRuleInstance(checkable, rule.GetName() + key, frame, rule))
147                                 match = true;
148                 }
149         }
150
151         return match;
152 }
153
154 void Dependency::EvaluateApplyRules(const Host::Ptr& host)
155 {
156         CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
157
158         for (ApplyRule& rule : ApplyRule::GetRules("Dependency")) {
159                 if (rule.GetTargetType() != "Host")
160                         continue;
161
162                 if (EvaluateApplyRule(host, rule))
163                         rule.AddMatch();
164         }
165 }
166
167 void Dependency::EvaluateApplyRules(const Service::Ptr& service)
168 {
169         CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
170
171         for (ApplyRule& rule : ApplyRule::GetRules("Dependency")) {
172                 if (rule.GetTargetType() != "Service")
173                         continue;
174
175                 if (EvaluateApplyRule(service, rule))
176                         rule.AddMatch();
177         }
178 }