]> granicus.if.org Git - icinga2/blob - lib/icinga/notification-apply.cpp
Move the VMFrame class to libbase
[icinga2] / lib / icinga / notification-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/notification.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/scripterror.hpp"
31 #include <boost/foreach.hpp>
32
33 using namespace icinga;
34
35 INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler);
36
37 void Notification::RegisterApplyRuleHandler(void)
38 {
39         std::vector<String> targets;
40         targets.push_back("Host");
41         targets.push_back("Service");
42         ApplyRule::RegisterType("Notification", targets);
43 }
44
45 void Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
46 {
47         DebugInfo di = rule.GetDebugInfo();
48
49         Log(LogDebug, "Notification")
50             << "Applying notification '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
51
52         ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
53         builder->SetType("Notification");
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("host_name"), OpSetLiteral, MakeLiteral(host->GetName()), false, di));
62
63         if (service)
64                 builder->AddExpression(new SetExpression(MakeIndexer("service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), false, di));
65
66         String zone = checkable->GetZone();
67
68         if (!zone.IsEmpty())
69                 builder->AddExpression(new SetExpression(MakeIndexer("zone"), OpSetLiteral, MakeLiteral(zone), false, di));
70
71         builder->AddExpression(new OwnedExpression(rule.GetExpression()));
72
73         ConfigItem::Ptr notificationItem = builder->Compile();
74         notificationItem->Commit();
75 }
76
77 bool Notification::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 Notification::EvaluateApplyRules(const Host::Ptr& host)
145 {
146         CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
147
148         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Notification"))
149         {
150                 if (rule.GetTargetType() != "Host")
151                         continue;
152
153                 try {
154                         if (EvaluateApplyRule(host, rule))
155                                 rule.AddMatch();
156                 } catch (const ScriptError& ex) {
157                         ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), ex.GetDebugInfo());
158                 }
159         }
160 }
161
162 void Notification::EvaluateApplyRules(const Service::Ptr& service)
163 {
164         CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
165
166         BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Notification")) {
167                 if (rule.GetTargetType() != "Service")
168                         continue;
169
170                 try {
171                         if (EvaluateApplyRule(service, rule))
172                                 rule.AddMatch();
173                 } catch (const ScriptError& ex) {
174                         ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), ex.GetDebugInfo());
175                 }
176         }
177 }