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