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.h"
21 #include "icinga/service.h"
22 #include "config/configitembuilder.h"
23 #include "config/applyrule.h"
24 #include "base/initialize.h"
25 #include "base/dynamictype.h"
26 #include "base/logger_fwd.h"
27 #include "base/context.h"
28 #include <boost/foreach.hpp>
30 using namespace icinga;
32 INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler);
34 void Notification::RegisterApplyRuleHandler(void)
36 std::vector<String> targets;
37 targets.push_back("Host");
38 targets.push_back("Service");
39 ApplyRule::RegisterType("Notification", targets, &Notification::EvaluateApplyRules);
42 bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
44 DebugInfo di = rule.GetDebugInfo();
46 std::ostringstream msgbuf;
47 msgbuf << "Evaluating 'apply' rule (" << di << ")";
48 CONTEXT(msgbuf.str());
52 tie(host, service) = GetHostService(checkable);
54 Dictionary::Ptr locals = make_shared<Dictionary>();
55 locals->Set("host", host);
57 locals->Set("service", service);
59 if (!rule.EvaluateFilter(locals))
62 std::ostringstream msgbuf2;
63 msgbuf2 << "Applying notification '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di;
64 Log(LogDebug, "icinga", msgbuf2.str());
66 ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
67 builder->SetType("Notification");
68 builder->SetName(rule.GetName());
69 builder->SetScope(rule.GetScope());
71 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
72 make_shared<AExpression>(&AExpression::OpLiteral, "host_name", di),
73 make_shared<AExpression>(&AExpression::OpLiteral, host->GetName(), di),
77 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
78 make_shared<AExpression>(&AExpression::OpLiteral, "service_name", di),
79 make_shared<AExpression>(&AExpression::OpLiteral, service->GetShortName(), di),
83 String zone = checkable->GetZone();
85 if (!zone.IsEmpty()) {
86 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
87 make_shared<AExpression>(&AExpression::OpLiteral, "zone", di),
88 make_shared<AExpression>(&AExpression::OpLiteral, zone, di),
92 builder->AddExpression(rule.GetExpression());
94 ConfigItem::Ptr notificationItem = builder->Compile();
95 notificationItem->Register();
96 DynamicObject::Ptr dobj = notificationItem->Commit();
97 dobj->OnConfigLoaded();
102 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
106 BOOST_FOREACH(const ApplyRule& rule, rules) {
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 (EvaluateApplyRule(host, rule))
117 if (apply_count == 0)
118 Log(LogWarning, "icinga", "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(EvaluateApplyRule(service, rule))
130 if (apply_count == 0)
131 Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
134 Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!");