]> granicus.if.org Git - icinga2/blob - lib/icinga/dependency-apply.cpp
Refactor apply/object rules
[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, const Dictionary::Ptr& locals, 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(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()), di));
62         builder->AddExpression(new SetExpression(MakeIndexer("child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
63
64         if (service)
65                 builder->AddExpression(new SetExpression(MakeIndexer("child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
66
67         String zone = checkable->GetZone();
68
69         if (!zone.IsEmpty())
70                 builder->AddExpression(new SetExpression(MakeIndexer("zone"), OpSetLiteral, MakeLiteral(zone), di));
71
72         builder->AddExpression(new OwnedExpression(rule.GetExpression()));
73
74         ConfigItem::Ptr dependencyItem = builder->Compile();
75         DynamicObject::Ptr dobj = dependencyItem->Commit();
76         dobj->OnConfigLoaded();
77
78 }
79
80 bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
81 {
82         DebugInfo di = rule.GetDebugInfo();
83
84         std::ostringstream msgbuf;
85         msgbuf << "Evaluating 'apply' rule (" << di << ")";
86         CONTEXT(msgbuf.str());
87
88         Host::Ptr host;
89         Service::Ptr service;
90         tie(host, service) = GetHostService(checkable);
91
92         Dictionary::Ptr locals = new Dictionary();
93         locals->Set("__parent", rule.GetScope());
94         locals->Set("host", host);
95         if (service)
96                 locals->Set("service", service);
97
98         if (!rule.EvaluateFilter(locals))
99                 return false;
100
101         Value vinstances;
102
103         if (rule.GetFTerm()) {
104                 vinstances = rule.GetFTerm()->Evaluate(locals);
105         } else {
106                 Array::Ptr instances = new Array();
107                 instances->Add("");
108                 vinstances = instances;
109         }
110
111         if (vinstances.IsObjectType<Array>()) {
112                 if (!rule.GetFVVar().IsEmpty())
113                         BOOST_THROW_EXCEPTION(ConfigError("Array iterator requires value to be an array.") << errinfo_debuginfo(di));
114
115                 Array::Ptr arr = vinstances;
116
117                 ObjectLock olock(arr);
118                 BOOST_FOREACH(const String& instance, arr) {
119                         String name = rule.GetName();
120
121                         if (!rule.GetFKVar().IsEmpty()) {
122                                 locals->Set(rule.GetFKVar(), instance);
123                                 name += instance;
124                         }
125
126                         EvaluateApplyRuleInstance(checkable, name, locals, rule);
127                 }
128         } else if (vinstances.IsObjectType<Dictionary>()) {
129                 if (rule.GetFVVar().IsEmpty())
130                         BOOST_THROW_EXCEPTION(ConfigError("Dictionary iterator requires value to be a dictionary.") << errinfo_debuginfo(di));
131         
132                 Dictionary::Ptr dict = vinstances;
133
134                 ObjectLock olock(dict);
135                 BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
136                         locals->Set(rule.GetFKVar(), kv.first);
137                         locals->Set(rule.GetFVVar(), kv.second);
138
139                         EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, locals, rule);
140                 }
141         }
142
143         return true;
144 }
145
146 void Dependency::EvaluateApplyRules(const Host::Ptr& host)
147 {
148         CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
149
150         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency"))
151         {
152                 if (rule.GetTargetType() != "Host")
153                         continue;
154
155                 try {
156                         if (EvaluateApplyRule(host, rule))
157                                 rule.AddMatch();
158                 } catch (const ConfigError& ex) {
159                         const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
160                         ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
161                 }
162         }
163 }
164
165 void Dependency::EvaluateApplyRules(const Service::Ptr& service)
166 {
167         CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
168
169         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency"))
170         {
171                 if (rule.GetTargetType() != "Service")
172                         continue;
173
174                 try {
175                         if (EvaluateApplyRule(service, rule))
176                                 rule.AddMatch();
177                 } catch (const ConfigError& ex) {
178                         const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
179                         ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
180                 }
181         }
182 }