]> granicus.if.org Git - icinga2/blob - lib/icinga/dependency-apply.cpp
Remove obsolete code
[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 "base/initialize.hpp"
25 #include "base/dynamictype.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 void Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
45 {
46         DebugInfo di = rule.GetDebugInfo();
47
48         Log(LogDebug, "Dependency")
49                 << "Applying dependency '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
50
51         ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
52         builder->SetType("Dependency");
53         builder->SetName(name);
54         builder->SetScope(frame.Locals);
55
56         Host::Ptr host;
57         Service::Ptr service;
58         tie(host, service) = GetHostService(checkable);
59
60         builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "parent_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
61         builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "child_host_name"), OpSetLiteral, MakeLiteral(host->GetName()), di));
62
63         if (service)
64                 builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di));
65
66         String zone = checkable->GetZone();
67
68         if (!zone.IsEmpty())
69                 builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di));
70
71         builder->AddExpression(new OwnedExpression(rule.GetExpression()));
72
73         ConfigItem::Ptr dependencyItem = builder->Compile();
74         dependencyItem->Commit();
75 }
76
77 bool Dependency::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
78 {
79         DebugInfo di = rule.GetDebugInfo();
80
81         std::ostringstream msgbuf;
82         msgbuf << "Evaluating 'apply' rule (" << di << ")";
83         CONTEXT(msgbuf.str());
84
85         Host::Ptr host;
86         Service::Ptr service;
87         tie(host, service) = GetHostService(checkable);
88
89         ScriptFrame frame;
90         if (rule.GetScope())
91                 rule.GetScope()->CopyTo(frame.Locals);
92         frame.Locals->Set("host", host);
93         if (service)
94                 frame.Locals->Set("service", service);
95
96         if (!rule.EvaluateFilter(frame))
97                 return false;
98
99         Value vinstances;
100
101         if (rule.GetFTerm()) {
102                 vinstances = rule.GetFTerm()->Evaluate(frame);
103         } else {
104                 Array::Ptr instances = new Array();
105                 instances->Add("");
106                 vinstances = instances;
107         }
108
109         if (vinstances.IsObjectType<Array>()) {
110                 if (!rule.GetFVVar().IsEmpty())
111                         BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
112
113                 Array::Ptr arr = vinstances;
114
115                 ObjectLock olock(arr);
116                 BOOST_FOREACH(const String& instance, arr) {
117                         String name = rule.GetName();
118
119                         if (!rule.GetFKVar().IsEmpty()) {
120                                 frame.Locals->Set(rule.GetFKVar(), instance);
121                                 name += instance;
122                         }
123
124                         EvaluateApplyRuleInstance(checkable, name, frame, rule);
125                 }
126         } else if (vinstances.IsObjectType<Dictionary>()) {
127                 if (rule.GetFVVar().IsEmpty())
128                         BOOST_THROW_EXCEPTION(ScriptError("Dictionary iterator requires value to be a dictionary.", di));
129         
130                 Dictionary::Ptr dict = vinstances;
131
132                 ObjectLock olock(dict);
133                 BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
134                         frame.Locals->Set(rule.GetFKVar(), kv.first);
135                         frame.Locals->Set(rule.GetFVVar(), kv.second);
136
137                         EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule);
138                 }
139         }
140
141         return true;
142 }
143
144 void Dependency::EvaluateApplyRules(const Host::Ptr& host)
145 {
146         CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
147
148         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency")) {
149                 if (rule.GetTargetType() != "Host")
150                         continue;
151
152                 if (EvaluateApplyRule(host, rule))
153                         rule.AddMatch();
154         }
155 }
156
157 void Dependency::EvaluateApplyRules(const Service::Ptr& service)
158 {
159         CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
160
161         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Dependency")) {
162                 if (rule.GetTargetType() != "Service")
163                         continue;
164
165                 if (EvaluateApplyRule(service, rule))
166                         rule.AddMatch();
167         }
168 }