1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/* Icinga 2 | (c) 2022 Icinga GmbH | GPLv2+ */
#include "base/string.hpp"
#include "config/applyrule.hpp"
#include "config/expression.hpp"
#include <utility>
#include <vector>
using namespace icinga;
/**
* @returns All ApplyRules targeting only specific parent objects including the given host. (See AddTargetedRule().)
*/
const std::set<ApplyRule::Ptr>& ApplyRule::GetTargetedHostRules(const Type::Ptr& sourceType, const String& host)
{
auto perSourceType (m_Rules.find(sourceType.get()));
if (perSourceType != m_Rules.end()) {
auto perHost (perSourceType->second.Targeted.find(host));
if (perHost != perSourceType->second.Targeted.end()) {
return perHost->second.ForHost;
}
}
static const std::set<ApplyRule::Ptr> noRules;
return noRules;
}
/**
* @returns All ApplyRules targeting only specific parent objects including the given service. (See AddTargetedRule().)
*/
const std::set<ApplyRule::Ptr>& ApplyRule::GetTargetedServiceRules(const Type::Ptr& sourceType, const String& host, const String& service)
{
auto perSourceType (m_Rules.find(sourceType.get()));
if (perSourceType != m_Rules.end()) {
auto perHost (perSourceType->second.Targeted.find(host));
if (perHost != perSourceType->second.Targeted.end()) {
auto perService (perHost->second.ForServices.find(service));
if (perService != perHost->second.ForServices.end()) {
return perService->second;
}
}
}
static const std::set<ApplyRule::Ptr> noRules;
return noRules;
}
/**
* If the given ApplyRule targets only specific parent objects, add it to the respective "index".
*
* - The above means for apply T "N" to Host: assign where host.name == "H" [ || host.name == "h" ... ]
* - For apply T "N" to Service it means: assign where host.name == "H" && service.name == "S" [ || host.name == "h" && service.name == "s" ... ]
*
* The order of operands of || && == doesn't matter.
*
* @returns Whether the rule has been added to the "index".
*/
bool ApplyRule::AddTargetedRule(const ApplyRule::Ptr& rule, const String& targetType, ApplyRule::PerSourceType& rules)
{
if (targetType == "Host") {
std::vector<const String *> hosts;
if (GetTargetHosts(rule->m_Filter.get(), hosts)) {
for (auto host : hosts) {
rules.Targeted[*host].ForHost.emplace(rule);
}
return true;
}
} else if (targetType == "Service") {
std::vector<std::pair<const String *, const String *>> services;
if (GetTargetServices(rule->m_Filter.get(), services)) {
for (auto service : services) {
rules.Targeted[*service.first].ForServices[*service.second].emplace(rule);
}
return true;
}
}
return false;
}
/**
* If the given assign filter is like the following, extract the host names ("H", "h", ...) into the vector:
*
* host.name == "H" [ || host.name == "h" ... ]
*
* The order of operands of || == doesn't matter.
*
* @returns Whether the given assign filter is like above.
*/
bool ApplyRule::GetTargetHosts(Expression* assignFilter, std::vector<const String *>& hosts)
{
auto lor (dynamic_cast<LogicalOrExpression*>(assignFilter));
if (lor) {
return GetTargetHosts(lor->GetOperand1().get(), hosts)
&& GetTargetHosts(lor->GetOperand2().get(), hosts);
}
auto name (GetComparedName(assignFilter, "host"));
if (name) {
hosts.emplace_back(name);
return true;
}
return false;
}
/**
* If the given assign filter is like the following, extract the host+service names ("H"+"S", "h"+"s", ...) into the vector:
*
* host.name == "H" && service.name == "S" [ || host.name == "h" && service.name == "s" ... ]
*
* The order of operands of || && == doesn't matter.
*
* @returns Whether the given assign filter is like above.
*/
bool ApplyRule::GetTargetServices(Expression* assignFilter, std::vector<std::pair<const String *, const String *>>& services)
{
auto lor (dynamic_cast<LogicalOrExpression*>(assignFilter));
if (lor) {
return GetTargetServices(lor->GetOperand1().get(), services)
&& GetTargetServices(lor->GetOperand2().get(), services);
}
auto service (GetTargetService(assignFilter));
if (service.first) {
services.emplace_back(service);
return true;
}
return false;
}
/**
* If the given filter is like the following, extract the host+service names ("H"+"S"):
*
* host.name == "H" && service.name == "S"
*
* The order of operands of && == doesn't matter.
*
* @returns {host, service} on success and {nullptr, nullptr} on failure.
*/
std::pair<const String *, const String *> ApplyRule::GetTargetService(Expression* assignFilter)
{
auto land (dynamic_cast<LogicalAndExpression*>(assignFilter));
if (!land) {
return {nullptr, nullptr};
}
auto op1 (land->GetOperand1().get());
auto op2 (land->GetOperand2().get());
auto host (GetComparedName(op1, "host"));
if (!host) {
std::swap(op1, op2);
host = GetComparedName(op1, "host");
}
if (host) {
auto service (GetComparedName(op2, "service"));
if (service) {
return {host, service};
}
}
return {nullptr, nullptr};
}
/**
* If the given filter is like the following, extract the object name ("N"):
*
* $lcType$.name == "N"
*
* The order of operands of == doesn't matter.
*
* @returns The object name on success and nullptr on failure.
*/
const String * ApplyRule::GetComparedName(Expression* assignFilter, const char * lcType)
{
auto eq (dynamic_cast<EqualExpression*>(assignFilter));
if (!eq) {
return nullptr;
}
auto op1 (eq->GetOperand1().get());
auto op2 (eq->GetOperand2().get());
if (IsNameIndexer(op1, lcType)) {
return GetLiteralStringValue(op2);
}
if (IsNameIndexer(op2, lcType)) {
return GetLiteralStringValue(op1);
}
return nullptr;
}
/**
* @returns Whether the given expression is like $lcType$.name.
*/
bool ApplyRule::IsNameIndexer(Expression* exp, const char * lcType)
{
auto ixr (dynamic_cast<IndexerExpression*>(exp));
if (!ixr) {
return false;
}
auto var (dynamic_cast<VariableExpression*>(ixr->GetOperand1().get()));
if (!var || var->GetVariable() != lcType) {
return false;
}
auto val (GetLiteralStringValue(ixr->GetOperand2().get()));
return val && *val == "name";
}
/**
* @returns If the given expression is a string literal, the string. nullptr on failure.
*/
const String * ApplyRule::GetLiteralStringValue(Expression* exp)
{
auto lit (dynamic_cast<LiteralExpression*>(exp));
if (!lit) {
return nullptr;
}
auto& val (lit->GetValue());
if (!val.IsString()) {
return nullptr;
}
return &val.Get<String>();
}
|