1 /******************************************************************************
3 * Copyright (C) 2012-present 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 void 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();
90 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
92 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
93 CONTEXT("Evaluating 'apply' rules for Host '" + host->GetName() + "'");
95 BOOST_FOREACH(const ApplyRule& rule, rules) {
96 if (rule.GetTargetType() != "Host")
99 EvaluateApplyRule(host, rule);
103 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
104 CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
106 BOOST_FOREACH(const ApplyRule& rule, rules) {
107 if (rule.GetTargetType() != "Service")
110 EvaluateApplyRule(service, rule);