1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
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>
33 using namespace icinga;
35 INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler);
37 void Notification::RegisterApplyRuleHandler(void)
39 std::vector<String> targets;
40 targets.push_back("Host");
41 targets.push_back("Service");
42 ApplyRule::RegisterType("Notification", targets);
45 void Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule)
47 DebugInfo di = rule.GetDebugInfo();
49 Log(LogDebug, "Notification")
50 << "Applying notification '" << name << "' to object '" << checkable->GetName() << "' for rule " << di;
52 ConfigItemBuilder::Ptr builder = new ConfigItemBuilder(di);
53 builder->SetType("Notification");
54 builder->SetName(name);
55 builder->SetScope(frame.Locals);
59 tie(host, service) = GetHostService(checkable);
61 builder->AddExpression(new SetExpression(MakeIndexer("host_name"), OpSetLiteral, MakeLiteral(host->GetName()), false, di));
64 builder->AddExpression(new SetExpression(MakeIndexer("service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), false, di));
66 String zone = checkable->GetZone();
69 builder->AddExpression(new SetExpression(MakeIndexer("zone"), OpSetLiteral, MakeLiteral(zone), false, di));
71 builder->AddExpression(new OwnedExpression(rule.GetExpression()));
73 ConfigItem::Ptr notificationItem = builder->Compile();
74 notificationItem->Commit();
77 bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
79 DebugInfo di = rule.GetDebugInfo();
81 std::ostringstream msgbuf;
82 msgbuf << "Evaluating 'apply' rule (" << di << ")";
83 CONTEXT(msgbuf.str());
87 tie(host, service) = GetHostService(checkable);
91 rule.GetScope()->CopyTo(frame.Locals);
92 frame.Locals->Set("host", host);
94 frame.Locals->Set("service", service);
96 if (!rule.EvaluateFilter(frame))
101 if (rule.GetFTerm()) {
102 vinstances = rule.GetFTerm()->Evaluate(frame);
104 Array::Ptr instances = new Array();
106 vinstances = instances;
109 if (vinstances.IsObjectType<Array>()) {
110 if (!rule.GetFVVar().IsEmpty())
111 BOOST_THROW_EXCEPTION(ScriptError("Array iterator requires value to be an array.", di));
113 Array::Ptr arr = vinstances;
115 ObjectLock olock(arr);
116 BOOST_FOREACH(const String& instance, arr) {
117 String name = rule.GetName();
119 if (!rule.GetFKVar().IsEmpty()) {
120 frame.Locals->Set(rule.GetFKVar(), instance);
124 EvaluateApplyRuleInstance(checkable, name, frame, rule);
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));
130 Dictionary::Ptr dict = vinstances;
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);
137 EvaluateApplyRuleInstance(checkable, rule.GetName() + kv.first, frame, rule);
144 void Notification::EvaluateApplyRules(const Host::Ptr& host)
146 CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
148 BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Notification"))
150 if (rule.GetTargetType() != "Host")
154 if (EvaluateApplyRule(host, rule))
156 } catch (const ScriptError& ex) {
157 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), ex.GetDebugInfo());
162 void Notification::EvaluateApplyRules(const Service::Ptr& service)
164 CONTEXT("Evaluating 'apply' rules for service '" + service->GetName() + "'");
166 BOOST_FOREACH(ApplyRule& rule, ApplyRule::GetRules("Notification")) {
167 if (rule.GetTargetType() != "Service")
171 if (EvaluateApplyRule(service, rule))
173 } catch (const ScriptError& ex) {
174 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), ex.GetDebugInfo());