]> granicus.if.org Git - icinga2/blob - lib/icinga/notification-apply.cpp
Set host/service variable in apply rules
[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 "config/configcompilercontext.hpp"
25 #include "base/initialize.hpp"
26 #include "base/dynamictype.hpp"
27 #include "base/logger.hpp"
28 #include "base/context.hpp"
29 #include "base/workqueue.hpp"
30 #include "base/configerror.hpp"
31 #include <boost/foreach.hpp>
32
33 using namespace icinga;
34
35 INITIALIZE_ONCE(&Notification::RegisterApplyRuleHandler);
36
37 void Notification::RegisterApplyRuleHandler(void)
38 {
39         std::vector<String> targets;
40         targets.push_back("Host");
41         targets.push_back("Service");
42         ApplyRule::RegisterType("Notification", targets, &Notification::EvaluateApplyRules);
43 }
44
45 bool Notification::EvaluateApplyRuleOne(const Checkable::Ptr& checkable, const ApplyRule& rule)
46 {
47         DebugInfo di = rule.GetDebugInfo();
48
49         std::ostringstream msgbuf;
50         msgbuf << "Evaluating 'apply' rule (" << di << ")";
51         CONTEXT(msgbuf.str());
52
53         Host::Ptr host;
54         Service::Ptr service;
55         tie(host, service) = GetHostService(checkable);
56
57         Dictionary::Ptr locals = make_shared<Dictionary>();
58         locals->Set("__parent", rule.GetScope());
59         locals->Set("host", host);
60         if (service)
61                 locals->Set("service", service);
62
63         if (!rule.EvaluateFilter(locals))
64                 return false;
65
66         Log(LogDebug, "Notification")
67             << "Applying notification '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di;
68
69         ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
70         builder->SetType("Notification");
71         builder->SetName(rule.GetName());
72         builder->SetScope(locals);
73
74         builder->AddExpression(make_shared<Expression>(&Expression::OpSet,
75             make_shared<Expression>(&Expression::OpLiteral, "host_name", di),
76             make_shared<Expression>(&Expression::OpLiteral, host->GetName(), di),
77             di));
78
79         if (service) {
80                 builder->AddExpression(make_shared<Expression>(&Expression::OpSet,
81                     make_shared<Expression>(&Expression::OpLiteral, "service_name", di),
82                     make_shared<Expression>(&Expression::OpLiteral, service->GetShortName(), di),
83                     di));
84         }
85
86         String zone = checkable->GetZone();
87
88         if (!zone.IsEmpty()) {
89                 builder->AddExpression(make_shared<Expression>(&Expression::OpSet,
90                     make_shared<Expression>(&Expression::OpLiteral, "zone", di),
91                     make_shared<Expression>(&Expression::OpLiteral, zone, di),
92                     di));
93         }
94
95         builder->AddExpression(rule.GetExpression());
96
97         ConfigItem::Ptr notificationItem = builder->Compile();
98         notificationItem->Register();
99         DynamicObject::Ptr dobj = notificationItem->Commit();
100         dobj->OnConfigLoaded();
101
102         return true;
103 }
104
105 void Notification::EvaluateApplyRule(const ApplyRule& rule)
106 {
107         int apply_count = 0;
108
109         if (rule.GetTargetType() == "Host") {
110                 apply_count = 0;
111
112                 BOOST_FOREACH(const Host::Ptr& host, DynamicType::GetObjectsByType<Host>()) {
113                         CONTEXT("Evaluating 'apply' rules for host '" + host->GetName() + "'");
114
115                         try {
116                                 if (EvaluateApplyRuleOne(host, rule))
117                                         apply_count++;
118                         } catch (const ConfigError& ex) {
119                                 const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
120                                 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
121                         }
122                 }
123
124                 if (apply_count == 0)
125                         Log(LogWarning, "Notification")
126                             << "Apply rule '" << rule.GetName() << "' for host does not match anywhere!";
127
128         } else if (rule.GetTargetType() == "Service") {
129                 apply_count = 0;
130
131                 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
132                         CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
133
134                         try {
135                                 if (EvaluateApplyRuleOne(service, rule))
136                                         apply_count++;
137                         } catch (const ConfigError& ex) {
138                                 const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
139                                 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
140                         }
141                 }
142
143                 if (apply_count == 0)
144                         Log(LogWarning, "Notification")
145                             << "Apply rule '" << rule.GetName() << "' for service does not match anywhere!";
146
147         } else {
148                 Log(LogWarning, "Notification")
149                     << "Wrong target type for apply rule '" << rule.GetName() << "'!";
150         }
151 }
152 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
153 {
154         ParallelWorkQueue upq;
155
156         BOOST_FOREACH(const ApplyRule& rule, rules) {
157                 upq.Enqueue(boost::bind(&Notification::EvaluateApplyRule, boost::cref(rule)));
158         }
159
160         upq.Join();
161 }