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/service.h"
21 #include "config/configitembuilder.h"
22 #include "base/initialize.h"
23 #include "base/dynamictype.h"
24 #include "base/convert.h"
25 #include "base/logger_fwd.h"
26 #include "base/context.h"
27 #include <boost/foreach.hpp>
29 using namespace icinga;
31 INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler);
33 void Notification::RegisterApplyRuleHandler(void)
35 std::vector<String> targets;
36 targets.push_back("Host");
37 targets.push_back("Service");
38 ApplyRule::RegisterType("Notification", targets, &Notification::EvaluateApplyRules);
41 bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
43 DebugInfo di = rule.GetDebugInfo();
45 std::ostringstream msgbuf;
46 msgbuf << "Evaluating 'apply' rule (" << di << ")";
47 CONTEXT(msgbuf.str());
51 tie(host, service) = GetHostService(checkable);
53 Dictionary::Ptr locals = make_shared<Dictionary>();
54 locals->Set("host", host);
56 locals->Set("service", service);
58 if (!rule.EvaluateFilter(locals))
61 std::ostringstream msgbuf2;
62 msgbuf2 << "Applying notification '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di;
63 Log(LogDebug, "icinga", msgbuf2.str());
65 ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
66 builder->SetType("Notification");
67 builder->SetName(rule.GetName());
68 builder->SetScope(rule.GetScope());
70 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
71 make_shared<AExpression>(&AExpression::OpLiteral, "host_name", di),
72 make_shared<AExpression>(&AExpression::OpLiteral, host->GetName(), di),
76 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
77 make_shared<AExpression>(&AExpression::OpLiteral, "service_name", di),
78 make_shared<AExpression>(&AExpression::OpLiteral, service->GetShortName(), di),
82 builder->AddExpression(rule.GetExpression());
84 ConfigItem::Ptr notificationItem = builder->Compile();
85 notificationItem->Register();
86 DynamicObject::Ptr dobj = notificationItem->Commit();
87 dobj->OnConfigLoaded();
92 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
96 BOOST_FOREACH(const ApplyRule& rule, rules) {
97 if (rule.GetTargetType() == "Host") {
100 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
101 CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
103 if (EvaluateApplyRule(host, rule))
107 if (apply_count == 0)
108 Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!");
110 } else if (rule.GetTargetType() == "Service") {
113 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
114 CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
116 if(EvaluateApplyRule(service, rule))
120 if (apply_count == 0)
121 Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
124 Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!");