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 "base/initialize.hpp"
25 #include "base/dynamictype.hpp"
26 #include "base/logger_fwd.hpp"
27 #include "base/context.hpp"
28 #include "base/workqueue.hpp"
29 #include <boost/foreach.hpp>
31 using namespace icinga;
33 INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler);
35 void Notification::RegisterApplyRuleHandler(void)
37 std::vector<String> targets;
38 targets.push_back("Host");
39 targets.push_back("Service");
40 ApplyRule::RegisterType("Notification", targets, &Notification::EvaluateApplyRules);
43 bool Notification::EvaluateApplyRuleOne(const Checkable::Ptr& checkable, const ApplyRule& rule)
45 DebugInfo di = rule.GetDebugInfo();
47 std::ostringstream msgbuf;
48 msgbuf << "Evaluating 'apply' rule (" << di << ")";
49 CONTEXT(msgbuf.str());
53 tie(host, service) = GetHostService(checkable);
55 Dictionary::Ptr locals = make_shared<Dictionary>();
56 locals->Set("host", host);
58 locals->Set("service", service);
60 if (!rule.EvaluateFilter(locals))
63 std::ostringstream msgbuf2;
64 msgbuf2 << "Applying notification '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di;
65 Log(LogDebug, "Notification", msgbuf2.str());
67 ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
68 builder->SetType("Notification");
69 builder->SetName(rule.GetName());
70 builder->SetScope(rule.GetScope());
72 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
73 make_shared<AExpression>(&AExpression::OpLiteral, "host_name", di),
74 make_shared<AExpression>(&AExpression::OpLiteral, host->GetName(), di),
78 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
79 make_shared<AExpression>(&AExpression::OpLiteral, "service_name", di),
80 make_shared<AExpression>(&AExpression::OpLiteral, service->GetShortName(), di),
84 String zone = checkable->GetZone();
86 if (!zone.IsEmpty()) {
87 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
88 make_shared<AExpression>(&AExpression::OpLiteral, "zone", di),
89 make_shared<AExpression>(&AExpression::OpLiteral, zone, di),
93 builder->AddExpression(rule.GetExpression());
95 ConfigItem::Ptr notificationItem = builder->Compile();
96 notificationItem->Register();
97 DynamicObject::Ptr dobj = notificationItem->Commit();
98 dobj->OnConfigLoaded();
103 void Notification::EvaluateApplyRule(const ApplyRule& rule)
107 if (rule.GetTargetType() == "Host") {
110 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
111 CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
113 if (EvaluateApplyRuleOne(host, rule))
117 if (apply_count == 0)
118 Log(LogWarning, "Notification", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!");
120 } else if (rule.GetTargetType() == "Service") {
123 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
124 CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
126 if (EvaluateApplyRuleOne(service, rule))
130 if (apply_count == 0)
131 Log(LogWarning, "Notification", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
134 Log(LogWarning, "Notification", "Wrong target type for apply rule '" + rule.GetName() + "'!");
137 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
139 ParallelWorkQueue upq;
141 BOOST_FOREACH(const ApplyRule& rule, rules) {
142 upq.Enqueue(boost::bind(&Notification::EvaluateApplyRule, boost::cref(rule)));