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 bool Notification::EvaluateApplyRuleOne(const Checkable::Ptr& checkable, const ApplyRule& rule)
47 DebugInfo di = rule.GetDebugInfo();
49 std::ostringstream msgbuf;
50 msgbuf << "Evaluating 'apply' rule (" << di << ")";
51 CONTEXT(msgbuf.str());
55 tie(host, service) = GetHostService(checkable);
57 Dictionary::Ptr locals = make_shared<Dictionary>();
58 locals->Set("__parent", rule.GetScope());
59 locals->Set("host", host);
61 locals->Set("service", service);
63 if (!rule.EvaluateFilter(locals))
66 Log(LogDebug, "Notification")
67 << "Applying notification '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di;
69 ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
70 builder->SetType("Notification");
71 builder->SetName(rule.GetName());
72 builder->SetScope(locals);
74 builder->AddExpression(make_shared<Expression>(&Expression::OpSet,
75 make_shared<Expression>(&Expression::OpLiteral, "host_name", di),
76 make_shared<Expression>(&Expression::OpLiteral, host->GetName(), di),
80 builder->AddExpression(make_shared<Expression>(&Expression::OpSet,
81 make_shared<Expression>(&Expression::OpLiteral, "service_name", di),
82 make_shared<Expression>(&Expression::OpLiteral, service->GetShortName(), di),
86 String zone = checkable->GetZone();
88 if (!zone.IsEmpty()) {
89 builder->AddExpression(make_shared<Expression>(&Expression::OpSet,
90 make_shared<Expression>(&Expression::OpLiteral, "zone", di),
91 make_shared<Expression>(&Expression::OpLiteral, zone, di),
95 builder->AddExpression(rule.GetExpression());
97 ConfigItem::Ptr notificationItem = builder->Compile();
98 notificationItem->Register();
99 DynamicObject::Ptr dobj = notificationItem->Commit();
100 dobj->OnConfigLoaded();
105 void Notification::EvaluateApplyRule(const ApplyRule& rule)
109 if (rule.GetTargetType() == "Host") {
112 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjectsByType<Host>()) {
113 CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
116 if (EvaluateApplyRuleOne(host, rule))
118 } catch (const ConfigError& ex) {
119 const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
120 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
124 if (apply_count == 0)
125 Log(LogWarning, "Notification")
126 << "Apply rule '" << rule.GetName() << "' for host does not match anywhere!";
128 } else if (rule.GetTargetType() == "Service") {
131 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
132 CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
135 if (EvaluateApplyRuleOne(service, rule))
137 } catch (const ConfigError& ex) {
138 const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
139 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
143 if (apply_count == 0)
144 Log(LogWarning, "Notification")
145 << "Apply rule '" << rule.GetName() << "' for service does not match anywhere!";
148 Log(LogWarning, "Notification")
149 << "Wrong target type for apply rule '" << rule.GetName() << "'!";
152 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
154 ParallelWorkQueue upq;
156 BOOST_FOREACH(const ApplyRule& rule, rules) {
157 upq.Enqueue(boost::bind(&Notification::EvaluateApplyRule, boost::cref(rule)));