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 ApplyRule::RegisterType("Notification", &Notification::EvaluateApplyRules, 2);
38 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
40 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
41 CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
43 Dictionary::Ptr locals = make_shared<Dictionary>();
44 locals->Set("host", service->GetHost());
45 locals->Set("service", service);
47 BOOST_FOREACH(const ApplyRule& rule, rules) {
48 DebugInfo di = rule.GetDebugInfo();
50 std::ostringstream msgbuf;
51 msgbuf << "Evaluating 'apply' rule (" << di << ")";
52 CONTEXT(msgbuf.str());
54 if (!rule.EvaluateFilter(locals))
57 std::ostringstream msgbuf2;
58 msgbuf2 << "Applying notification '" << rule.GetName() << "' to service '" << service->GetName() << "' for rule " << di;
59 Log(LogDebug, "icinga", msgbuf2.str());
61 std::ostringstream namebuf;
62 namebuf << service->GetName() << "!" << rule.GetName();
63 String name = namebuf.str();
65 ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
66 builder->SetType("Notification");
67 builder->SetName(name);
68 builder->SetScope(rule.GetScope());
70 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet, "host", make_shared<AExpression>(&AExpression::OpLiteral, service->GetHost()->GetName(), di), di));
71 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet, "service", make_shared<AExpression>(&AExpression::OpLiteral, service->GetShortName(), di), di));
72 builder->AddExpression(rule.GetExpression());
74 ConfigItem::Ptr serviceItem = builder->Compile();
75 serviceItem->Register();
76 DynamicObject::Ptr dobj = serviceItem->Commit();
77 dobj->OnConfigLoaded();