]> granicus.if.org Git - icinga2/blob - lib/icinga/notification-apply.cpp
Change log message identifier for libicinga.
[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.hpp"
21 #include "icinga/service.hpp"
22 #include "config/configitembuilder.hpp"
23 #include "config/applyrule.hpp"
24 #include "base/initialize.hpp"
25 #include "base/dynamictype.hpp"
26 #include "base/logger_fwd.hpp"
27 #include "base/context.hpp"
28 #include "base/workqueue.hpp"
29 #include <boost/foreach.hpp>
30
31 using namespace icinga;
32
33 INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler);
34
35 void Notification::RegisterApplyRuleHandler(void)
36 {
37         std::vector<String> targets;
38         targets.push_back("Host");
39         targets.push_back("Service");
40         ApplyRule::RegisterType("Notification", targets, &Notification::EvaluateApplyRules);
41 }
42
43 bool Notification::EvaluateApplyRuleOne(const Checkable::Ptr& checkable, const ApplyRule& rule)
44 {
45         DebugInfo di = rule.GetDebugInfo();
46
47         std::ostringstream msgbuf;
48         msgbuf << "Evaluating 'apply' rule (" << di << ")";
49         CONTEXT(msgbuf.str());
50
51         Host::Ptr host;
52         Service::Ptr service;
53         tie(host, service) = GetHostService(checkable);
54
55         Dictionary::Ptr locals = make_shared<Dictionary>();
56         locals->Set("host", host);
57         if (service)
58                 locals->Set("service", service);
59
60         if (!rule.EvaluateFilter(locals))
61                 return false;
62
63         std::ostringstream msgbuf2;
64         msgbuf2 << "Applying notification '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di;
65         Log(LogDebug, "Notification", msgbuf2.str());
66
67         ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
68         builder->SetType("Notification");
69         builder->SetName(rule.GetName());
70         builder->SetScope(rule.GetScope());
71
72         builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
73             make_shared<AExpression>(&AExpression::OpLiteral, "host_name", di),
74             make_shared<AExpression>(&AExpression::OpLiteral, host->GetName(), di),
75             di));
76
77         if (service) {
78                 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
79                     make_shared<AExpression>(&AExpression::OpLiteral, "service_name", di),
80                     make_shared<AExpression>(&AExpression::OpLiteral, service->GetShortName(), di),
81                     di));
82         }
83
84         String zone = checkable->GetZone();
85
86         if (!zone.IsEmpty()) {
87                 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
88                     make_shared<AExpression>(&AExpression::OpLiteral, "zone", di),
89                     make_shared<AExpression>(&AExpression::OpLiteral, zone, di),
90                     di));
91         }
92
93         builder->AddExpression(rule.GetExpression());
94
95         ConfigItem::Ptr notificationItem = builder->Compile();
96         notificationItem->Register();
97         DynamicObject::Ptr dobj = notificationItem->Commit();
98         dobj->OnConfigLoaded();
99
100         return true;
101 }
102
103 void Notification::EvaluateApplyRule(const ApplyRule& rule)
104 {
105         int apply_count = 0;
106
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 (EvaluateApplyRuleOne(host, rule))
114                                 apply_count++;
115                 }
116
117                 if (apply_count == 0)
118                         Log(LogWarning, "Notification", "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 (EvaluateApplyRuleOne(service, rule))
127                                 apply_count++;
128                 }
129
130                 if (apply_count == 0)
131                         Log(LogWarning, "Notification", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
132
133         } else {
134                 Log(LogWarning, "Notification", "Wrong target type for apply rule '" + rule.GetName() + "'!");
135         }
136 }
137 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
138 {
139         ParallelWorkQueue upq;
140
141         BOOST_FOREACH(const ApplyRule& rule, rules) {
142                 upq.Enqueue(boost::bind(&Notification::EvaluateApplyRule, boost::cref(rule)));
143         }
144
145         upq.Join();
146 }