]> granicus.if.org Git - icinga2/blob - lib/icinga/notification-apply.cpp
Improve error messages for 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_fwd.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("host", host);
59         if (service)
60                 locals->Set("service", service);
61
62         if (!rule.EvaluateFilter(locals))
63                 return false;
64
65         std::ostringstream msgbuf2;
66         msgbuf2 << "Applying notification '" << rule.GetName() << "' to object '" << checkable->GetName() << "' for rule " << di;
67         Log(LogDebug, "Notification", msgbuf2.str());
68
69         ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
70         builder->SetType("Notification");
71         builder->SetName(rule.GetName());
72         builder->SetScope(rule.GetScope());
73
74         builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
75             make_shared<AExpression>(&AExpression::OpLiteral, "host_name", di),
76             make_shared<AExpression>(&AExpression::OpLiteral, host->GetName(), di),
77             di));
78
79         if (service) {
80                 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
81                     make_shared<AExpression>(&AExpression::OpLiteral, "service_name", di),
82                     make_shared<AExpression>(&AExpression::OpLiteral, service->GetShortName(), di),
83                     di));
84         }
85
86         String zone = checkable->GetZone();
87
88         if (!zone.IsEmpty()) {
89                 builder->AddExpression(make_shared<AExpression>(&AExpression::OpSet,
90                     make_shared<AExpression>(&AExpression::OpLiteral, "zone", di),
91                     make_shared<AExpression>(&AExpression::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", "Apply rule '" + rule.GetName() + "' for host does not match anywhere!");
126
127         } else if (rule.GetTargetType() == "Service") {
128                 apply_count = 0;
129
130                 BOOST_FOREACH(const Service::Ptr& service, DynamicType::GetObjectsByType<Service>()) {
131                         CONTEXT("Evaluating 'apply' rules for Service '" + service->GetName() + "'");
132
133                         try {
134                                 if (EvaluateApplyRuleOne(service, rule))
135                                         apply_count++;
136                         } catch (const ConfigError& ex) {
137                                 const DebugInfo *di = boost::get_error_info<errinfo_debuginfo>(ex);
138                                 ConfigCompilerContext::GetInstance()->AddMessage(true, ex.what(), di ? *di : DebugInfo());
139                         }
140                 }
141
142                 if (apply_count == 0)
143                         Log(LogWarning, "Notification", "Apply rule '" + rule.GetName() + "' for service does not match anywhere!");
144
145         } else {
146                 Log(LogWarning, "Notification", "Wrong target type for apply rule '" + rule.GetName() + "'!");
147         }
148 }
149 void Notification::EvaluateApplyRules(const std::vector<ApplyRule>& rules)
150 {
151         ParallelWorkQueue upq;
152
153         BOOST_FOREACH(const ApplyRule& rule, rules) {
154                 upq.Enqueue(boost::bind(&Notification::EvaluateApplyRule, boost::cref(rule)));
155         }
156
157         upq.Join();
158 }