]> granicus.if.org Git - icinga2/blob - lib/icinga/dependency-apply.cpp
Refactor the stack frame handling for scripts
[icinga2] / lib / icinga / dependency-apply.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 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 "config/configcompilercontext.hpp"
25 #include "base/initialize.hpp"
26 #include "base/dynamictype.hpp"
27 #include "base/logger.hpp"
28 #include "base/context.hpp"
29 #include "base/workqueue.hpp"
30 #include "base/configerror.hpp"
31 #include <boost/foreach.hpp>
32
33 using namespace icinga;
34
35 INITIALIZE_ONCE(&Dependency::RegisterApplyRuleHandler);
36
37 void Dependency::RegisterApplyRuleHandler(void)
38 {
39         std::vector<String> targets;
40         targets.push_back("Host");
41         targets.push_back("Service");
42         ApplyRule::RegisterType("Dependency", targets);
43 }
44
45 void Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, VMFrame& frame, const ApplyRule& rule)
46 {
47         DebugInfo di = rule.GetDebugInfo();
48
49         Log(LogDebug, "Dependency")
50                 << "Applying dependency '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
51
52         ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
53         builder->SetType("Dependency");
54         builder->SetName(name);
55         builder->SetScope(frame.Locals);
56
57         Host::Ptr host;
58         Service::Ptr service;
59         tie(host, service) = GetHostService(checkable);
60
61         builder->AddExpression(new SetExpression(MakeIndexer("parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), false, di));
62         builder->AddExpression(new SetExpression(MakeIndexer("child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), false, di));
63
64         if (service)
65                 builder->AddExpression(new SetExpression(MakeIndexer("child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), false, di));
66
67         String zone = checkable->GetZone();
68
69         if (!zone.IsEmpty())
70                 builder->AddExpression(new SetExpression(MakeIndexer("zone"), OpSetLiteral, MakeLiteral(zone), false, di));
71
72         builder->AddExpression(new OwnedExpression(rule.GetExpression()));
73
74         ConfigItem::Ptr dependencyItem = builder->Compile();
75         dependencyItem->Commit();
76 }
77
78 bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
79 {
80         DebugInfo di = rule.GetDebugInfo();
81
82         std::ostringstream msgbuf;
83         msgbuf << "Evaluating 'apply' rule (" << di << ")";
84         CONTEXT(msgbuf.str());
85
86         Host::Ptr host;
87         Service::Ptr service;
88         tie(host, service) = GetHostService(checkable);
89
90         VMFrame frame;
91         if (rule.GetScope())
92                 rule.GetScope()->CopyTo(frame.Locals);
93         frame.Locals->Set("host", host);
94         if (service)
95                 frame.Locals->Set("service", service);
96
97         if (!rule.EvaluateFilter(frame))
98                 return false;
99
100         Value vinstances;
101
102         if (rule.GetFTerm()) {
103                 vinstances = rule.GetFTerm()->Evaluate(frame);
104         } else {
105                 Array::Ptr instances = new Array();
106                 instances->Add("");
107                 vinstances = instances;
108         }
109
110         if (vinstances.IsObjectType<Array>()) {
111                 if (!rule.GetFVVar().IsEmpty())
112                         BOOST_THROW_EXCEPTION(ConfigError("Array iterator requires value to be an array.") << errinfo_debuginfo(di));
113
114                 Array::Ptr arr = vinstances;
115
116                 ObjectLock olock(arr);
117                 BOOST_FOREACH(const String& instance, arr) {
118                         String name = rule.GetName();
119
120                         if (!rule.GetFKVar().IsEmpty()) {
121                                 frame.Locals->Set(rule.GetFKVar(), instance);
122                                 name += instance;
123                         }
124
125                         EvaluateApplyRuleInstance(checkable, name, frame, rule);
126                 }
127         } else if (vinstances.IsObjectType<Dictionary>()) {
128                 if (rule.GetFVVar().IsEmpty())
129                         BOOST_THROW_EXCEPTION(ConfigError("Dictionary iterator requires value to be a dictionary.") << errinfo_debuginfo(di));
130         
131                 Dictionary::Ptr dict = vinstances;
132
133                 ObjectLock olock(dict);
134                 BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
135                         frame.Locals->Set(rule.GetFKVar(), kv.first);
136                         frame.Locals->Set(rule.GetFVVar(), kv.second);
137
138                         EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule);
139                 }
140         }
141
142         return true;
143 }
144
145 void Dependency::EvaluateApplyRules(const Host::Ptr& host)
146 {
147         CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
148
149         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency"))
150         {
151                 if (rule.GetTargetType() != "Host")
152                         continue;
153
154                 try {
155                         if (EvaluateApplyRule(host, rule))
156                                 rule.AddMatch();
157                 } catch (const ConfigError& ex) {
158                         const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
159                         ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
160                 }
161         }
162 }
163
164 void Dependency::EvaluateApplyRules(const Service::Ptr& service)
165 {
166         CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
167
168         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency"))
169         {
170                 if (rule.GetTargetType() != "Service")
171                         continue;
172
173                 try {
174                         if (EvaluateApplyRule(service, rule))
175                                 rule.AddMatch();
176                 } catch (const ConfigError& ex) {
177                         const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
178                         ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
179                 }
180         }
181 }