]> granicus.if.org Git - icinga2/blob - lib/icinga/dependency-apply.cpp
Rename DynamicObject/DynamicType to ConfigObject/ConfigType
[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
59         Host::Ptr host;
60         Service::Ptr service;
61         tie(host, service) = GetHostService(checkable);
62
63         builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
64         builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
65
66         if (service)
67                 builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
68
69         String zone = checkable->GetZoneName();
70
71         if (!zone.IsEmpty())
72                 builder->AddExpression(new SetExpression(MakeIndexer(ScopeThis, "zone"), OpSetLiteral, MakeLiteral(zone), di));
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("Array iterator requires value to be an array.", di));
121
122                 Array::Ptr arr = vinstances;
123                 Array::Ptr arrclone = arr->ShallowClone();
124
125                 ObjectLock olock(arrclone);
126                 BOOST_FOREACH(const Value& instance, arrclone) {
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("Dictionary iterator requires value to be a dictionary.", di));
140         
141                 Dictionary::Ptr dict = vinstances;
142
143                 BOOST_FOREACH(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         BOOST_FOREACH(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         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency")) {
173                 if (rule.GetTargetType() != "Service")
174                         continue;
175
176                 if (EvaluateApplyRule(service, rule))
177                         rule.AddMatch();
178         }
179 }