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
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#ifndef APPLYRULE_H
#define APPLYRULE_H
#include "config/i2-config.hpp"
#include "config/expression.hpp"
#include "base/debuginfo.hpp"
#include "base/shared-object.hpp"
#include "base/type.hpp"
#include <unordered_map>
#include <atomic>
namespace icinga
{
/**
* @ingroup config
*/
class ApplyRule : public SharedObject
{
public:
DECLARE_PTR_TYPEDEFS(ApplyRule);
struct PerHost
{
std::set<ApplyRule::Ptr> ForHost;
std::unordered_map<String /* service */, std::set<ApplyRule::Ptr>> ForServices;
};
struct PerSourceType
{
std::unordered_map<Type* /* target type */, std::vector<ApplyRule::Ptr>> Regular;
std::unordered_map<String /* host */, PerHost> Targeted;
};
/*
* m_Rules[T::TypeInstance.get()].Targeted["H"].ForHost
* contains all apply rules like apply T "x" to Host { ... }
* which target only specific hosts incl. "H", e.g. via
* assign where host.name == "H" || host.name == "h".
*
* m_Rules[T::TypeInstance.get()].Targeted["H"].ForServices["S"]
* contains all apply rules like apply T "x" to Service { ... }
* which target only specific services on specific hosts,
* e.g. via assign where host.name == "H" && service.name == "S".
*
* m_Rules[T::TypeInstance.get()].Regular[C::TypeInstance.get()]
* contains all other apply rules like apply T "x" to C { ... }.
*/
typedef std::unordered_map<Type* /* source type */, PerSourceType> RuleMap;
typedef std::map<String, std::vector<String> > TypeMap;
String GetName() const;
Expression::Ptr GetExpression() const;
Expression::Ptr GetFilter() const;
String GetPackage() const;
inline const String& GetFKVar() const noexcept
{
return m_FKVar;
}
inline const String& GetFVVar() const noexcept
{
return m_FVVar;
}
Expression::Ptr GetFTerm() const;
bool GetIgnoreOnError() const;
const DebugInfo& GetDebugInfo() const;
Dictionary::Ptr GetScope() const;
void AddMatch();
bool HasMatches() const;
bool EvaluateFilter(ScriptFrame& frame) const;
static void AddRule(const String& sourceType, const String& targetType, const String& name, const Expression::Ptr& expression,
const Expression::Ptr& filter, const String& package, const String& fkvar, const String& fvvar, const Expression::Ptr& fterm,
bool ignoreOnError, const DebugInfo& di, const Dictionary::Ptr& scope);
static const std::vector<ApplyRule::Ptr>& GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType);
static const std::set<ApplyRule::Ptr>& GetTargetedHostRules(const Type::Ptr& sourceType, const String& host);
static const std::set<ApplyRule::Ptr>& GetTargetedServiceRules(const Type::Ptr& sourceType, const String& host, const String& service);
static bool GetTargetHosts(Expression* assignFilter, std::vector<const String *>& hosts, const Dictionary::Ptr& constants = nullptr);
static bool GetTargetServices(Expression* assignFilter, std::vector<std::pair<const String *, const String *>>& services, const Dictionary::Ptr& constants = nullptr);
static void RegisterType(const String& sourceType, const std::vector<String>& targetTypes);
static bool IsValidSourceType(const String& sourceType);
static bool IsValidTargetType(const String& sourceType, const String& targetType);
static const std::vector<String>& GetTargetTypes(const String& sourceType);
static void CheckMatches(bool silent);
static void CheckMatches(const ApplyRule::Ptr& rule, Type* sourceType, bool silent);
private:
String m_Name;
Expression::Ptr m_Expression;
Expression::Ptr m_Filter;
String m_Package;
String m_FKVar;
String m_FVVar;
Expression::Ptr m_FTerm;
bool m_IgnoreOnError;
DebugInfo m_DebugInfo;
Dictionary::Ptr m_Scope;
std::atomic<bool> m_HasMatches;
static TypeMap m_Types;
static RuleMap m_Rules;
static bool AddTargetedRule(const ApplyRule::Ptr& rule, const String& targetType, PerSourceType& rules);
static std::pair<const String *, const String *> GetTargetService(Expression* assignFilter, const Dictionary::Ptr& constants);
static const String * GetComparedName(Expression* assignFilter, const char * lcType, const Dictionary::Ptr& constants);
static bool IsNameIndexer(Expression* exp, const char * lcType, const Dictionary::Ptr& constants);
static const String * GetConstString(Expression* exp, const Dictionary::Ptr& constants);
static const Value * GetConst(Expression* exp, const Dictionary::Ptr& constants);
ApplyRule(String name, Expression::Ptr expression,
Expression::Ptr filter, String package, String fkvar, String fvvar, Expression::Ptr fterm,
bool ignoreOnError, DebugInfo di, Dictionary::Ptr scope);
};
}
#endif /* APPLYRULE_H */
|