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