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/configerror.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, &Notification::EvaluateApplyRules);
45 void Notification::EvaluateApplyRuleOneInstance(const Checkable::Ptr& checkable, const String& name, const Dictionary::Ptr& locals, 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 = make_shared<ConfigItemBuilder>(di);
53 builder->SetType("Notification");
54 builder->SetName(name);
55 builder->SetScope(locals);
59 tie(host, service) = GetHostService(checkable);
61 builder->AddExpression(make_shared<Expression>(&Expression::OpSet,
62 MakeArray(MakeArray(MakeLiteral("host_name")), OpSetLiteral),
63 MakeLiteral(host->GetName()),
67 builder->AddExpression(make_shared<Expression>(&Expression::OpSet,
68 MakeArray(MakeArray(MakeLiteral("service_name")), OpSetLiteral),
69 MakeLiteral(service->GetShortName()),
73 String zone = checkable->GetZone();
75 if (!zone.IsEmpty()) {
76 builder->AddExpression(make_shared<Expression>(&Expression::OpSet,
77 MakeArray(MakeArray(MakeLiteral("zone")), OpSetLiteral),
82 builder->AddExpression(rule.GetExpression());
84 ConfigItem::Ptr notificationItem = builder->Compile();
85 DynamicObject::Ptr dobj = notificationItem->Commit();
86 dobj->OnConfigLoaded();
90 bool Notification::EvaluateApplyRuleOne(const Checkable::Ptr& checkable, const ApplyRule& rule)
92 DebugInfo di = rule.GetDebugInfo();
94 std::ostringstream msgbuf;
95 msgbuf << "Evaluating 'apply' rule (" << di << ")";
96 CONTEXT(msgbuf.str());
100 tie(host, service) = GetHostService(checkable);
102 Dictionary::Ptr locals = make_shared<Dictionary>();
103 locals->Set("__parent", rule.GetScope());
104 locals->Set("host", host);
106 locals->Set("service", service);
108 if (!rule.EvaluateFilter(locals))
113 if (rule.GetFTerm()) {
114 vinstances = rule.GetFTerm()->Evaluate(locals);
116 Array::Ptr instances = make_shared<Array>();
118 vinstances = instances;
121 if (vinstances.IsObjectType<Array>()) {
122 if (!rule.GetFVVar().IsEmpty())
123 BOOST_THROW_EXCEPTION(ConfigError("Array iterator requires value to be an array.") << errinfo_debuginfo(di));
125 Array::Ptr arr = vinstances;
127 ObjectLock olock(arr);
128 BOOST_FOREACH(const String& instance, arr) {
129 String name = rule.GetName();
131 if (!rule.GetFKVar().IsEmpty()) {
132 locals->Set(rule.GetFKVar(), instance);
136 EvaluateApplyRuleOneInstance(checkable, name, locals, rule);
138 } else if (vinstances.IsObjectType<Dictionary>()) {
139 if (rule.GetFVVar().IsEmpty())
140 BOOST_THROW_EXCEPTION(ConfigError("Dictionary iterator requires value to be a dictionary.") << errinfo_debuginfo(di));
142 Dictionary::Ptr dict = vinstances;
144 ObjectLock olock(dict);
145 BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
146 locals->Set(rule.GetFKVar(), kv.first);
147 locals->Set(rule.GetFVVar(), kv.second);
149 EvaluateApplyRuleOneInstance(checkable, rule.GetName() + kv.first, locals, rule);
156 void Notification::EvaluateApplyRule(const ApplyRule& rule)
160 if (rule.GetTargetType() == "Host") {
163 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjectsByType<Host>()) {
164 CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
167 if (EvaluateApplyRuleOne(host, rule))
169 } catch (const ConfigError& ex) {
170 const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
171 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
175 if (apply_count == 0)
176 Log(LogWarning, "Notification")
177 << "Apply rule '" << rule.GetName() << "' for host does not match anywhere!";
179 } else if (rule.GetTargetType() == "Service") {
182 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
183 CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
186 if (EvaluateApplyRuleOne(service, rule))
188 } catch (const ConfigError& ex) {
189 const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
190 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
194 if (apply_count == 0)
195 Log(LogWarning, "Notification")
196 << "Apply rule '" << rule.GetName() << "' for service does not match anywhere!";
199 Log(LogWarning, "Notification")
200 << "Wrong target type for apply rule '" << rule.GetName() << "'!";
203 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
205 ParallelWorkQueue upq;
207 BOOST_FOREACH(const ApplyRule& rule, rules) {
208 upq.Enqueue(boost::bind(&Notification::EvaluateApplyRule, boost::cref(rule)));