]> granicus.if.org Git - icinga2/blob - lib/icinga/notification-apply.cpp
Revert "Add group apply (creates object only once and sets membership)."
[icinga2] / lib / icinga / notification-apply.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
28
29 using namespace icinga;
30
31 INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler);
32
33 void Notification::RegisterApplyRuleHandler(void)
34 {
35         std::vector<String> targets;
36         targets.push_back("Host");
37         targets.push_back("Service");
38         ApplyRule::RegisterType("Notification", targets, &Notification::EvaluateApplyRules);
39 }
40
41 bool Notification::EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule)
42 {
43         DebugInfo di = rule.GetDebugInfo();
44
45         std::ostringstream msgbuf;
46         msgbuf << "Evaluating 'apply' rule (" << di << ")";
47         CONTEXT(msgbuf.str());
48
49         Host::Ptr host;
50         Service::Ptr service;
51         tie(host, service) = GetHostService(checkable);
52
53         Dictionary::Ptr locals = make_shared<Dictionary>();
54         locals->Set("host", host);
55         if (service)
56                 locals->Set("service", service);
57
58         if (!rule.EvaluateFilter(locals))
59                 return false;
60
61         std::ostringstream msgbuf2;
62         msgbuf2 << "Applying notification '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di;
63         Log(LogDebug, "icinga", msgbuf2.str());
64
65         ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
66         builder->SetType("Notification");
67         builder->SetName(rule.GetName());
68         builder->SetScope(rule.GetScope());
69
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),
73             di));
74
75         if (service) {
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),
79                     di));
80         }
81
82         builder->AddExpression(rule.GetExpression());
83
84         ConfigItem::Ptr notificationItem = builder->Compile();
85         notificationItem->Register();
86         DynamicObject::Ptr dobj = notificationItem->Commit();
87         dobj->OnConfigLoaded();
88
89         return true;
90 }
91
92 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
93 {
94         int apply_count = 0;
95
96         BOOST_FOREACH(const ApplyRule& rule, rules) {
97                 if (rule.GetTargetType() == "Host") {
98                         apply_count = 0;
99
100                         BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjects<Host>()) {
101                                 CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
102
103                                 if (EvaluateApplyRule(host, rule))
104                                         apply_count++;
105                         }
106
107                         if (apply_count == 0)
108                                 Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!");
109
110                 } else if (rule.GetTargetType() == "Service") {
111                         apply_count = 0;
112
113                         BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjects<Service>()) {
114                                 CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
115
116                                 if(EvaluateApplyRule(service, rule))
117                                         apply_count++;
118                         }
119
120                         if (apply_count == 0)
121                                 Log(LogWarning, "icinga", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
122
123                 } else {
124                         Log(LogWarning, "icinga", "Wrong target type for apply rule '" + rule.GetName() + "'!");
125                 }
126         }
127 }