2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-03-18 11:44:09 +01:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "config/applyrule.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-11 17:14:35 +02:00
|
|
|
#include <set>
|
2022-10-18 16:48:58 +02:00
|
|
|
#include <unordered_set>
|
2014-03-18 11:44:09 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
ApplyRule::RuleMap ApplyRule::m_Rules;
|
2014-11-16 16:20:39 +01:00
|
|
|
ApplyRule::TypeMap ApplyRule::m_Types;
|
2014-03-18 11:44:09 +01:00
|
|
|
|
2022-10-18 12:40:09 +02:00
|
|
|
ApplyRule::ApplyRule(String name, Expression::Ptr expression,
|
2019-07-26 14:17:27 +02:00
|
|
|
Expression::Ptr filter, String package, String fkvar, String fvvar, Expression::Ptr fterm,
|
2018-01-04 08:54:18 +01:00
|
|
|
bool ignoreOnError, DebugInfo di, Dictionary::Ptr scope)
|
2022-10-18 12:40:09 +02:00
|
|
|
: m_Name(std::move(name)), m_Expression(std::move(expression)), m_Filter(std::move(filter)), m_Package(std::move(package)), m_FKVar(std::move(fkvar)),
|
2018-01-04 08:54:18 +01:00
|
|
|
m_FVVar(std::move(fvvar)), m_FTerm(std::move(fterm)), m_IgnoreOnError(ignoreOnError), m_DebugInfo(std::move(di)), m_Scope(std::move(scope)), m_HasMatches(false)
|
2014-03-18 11:44:09 +01:00
|
|
|
{ }
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String ApplyRule::GetName() const
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-03-28 13:25:40 +01:00
|
|
|
return m_Name;
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
|
|
|
|
2019-07-26 14:17:27 +02:00
|
|
|
Expression::Ptr ApplyRule::GetExpression() const
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
|
|
|
return m_Expression;
|
|
|
|
}
|
|
|
|
|
2019-07-26 14:17:27 +02:00
|
|
|
Expression::Ptr ApplyRule::GetFilter() const
|
2014-03-28 13:25:40 +01:00
|
|
|
{
|
|
|
|
return m_Filter;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String ApplyRule::GetPackage() const
|
2015-08-17 16:08:57 +02:00
|
|
|
{
|
2015-08-28 17:58:29 +02:00
|
|
|
return m_Package;
|
2015-08-17 16:08:57 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 14:17:27 +02:00
|
|
|
Expression::Ptr ApplyRule::GetFTerm() const
|
2014-11-02 07:22:00 +01:00
|
|
|
{
|
|
|
|
return m_FTerm;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool ApplyRule::GetIgnoreOnError() const
|
2015-10-05 12:44:11 +02:00
|
|
|
{
|
|
|
|
return m_IgnoreOnError;
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:33:44 +02:00
|
|
|
const DebugInfo& ApplyRule::GetDebugInfo() const
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
|
|
|
return m_DebugInfo;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Dictionary::Ptr ApplyRule::GetScope() const
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-03-24 11:23:47 +01:00
|
|
|
return m_Scope;
|
|
|
|
}
|
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
void ApplyRule::AddRule(const String& sourceType, const String& targetType, const String& name,
|
2019-07-26 14:17:27 +02:00
|
|
|
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)
|
2014-03-28 13:25:40 +01:00
|
|
|
{
|
2022-10-17 16:54:34 +02:00
|
|
|
auto actualTargetType (&targetType);
|
|
|
|
|
|
|
|
if (*actualTargetType == "") {
|
|
|
|
auto& targetTypes (GetTargetTypes(sourceType));
|
|
|
|
|
|
|
|
if (targetTypes.size() == 1u) {
|
|
|
|
actualTargetType = &targetTypes[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-27 16:54:09 +02:00
|
|
|
ApplyRule::Ptr rule = new ApplyRule(name, expression, filter, package, fkvar, fvvar, fterm, ignoreOnError, di, scope);
|
2022-10-28 12:41:21 +02:00
|
|
|
auto& rules (m_Rules[Type::GetByName(sourceType).get()]);
|
2022-10-18 16:48:58 +02:00
|
|
|
|
2022-10-28 12:41:21 +02:00
|
|
|
if (!AddTargetedRule(rule, *actualTargetType, rules)) {
|
|
|
|
rules.Regular[Type::GetByName(*actualTargetType).get()].emplace_back(std::move(rule));
|
2022-10-18 16:48:58 +02:00
|
|
|
}
|
2014-03-28 13:25:40 +01:00
|
|
|
}
|
|
|
|
|
2014-12-12 15:33:02 +01:00
|
|
|
bool ApplyRule::EvaluateFilter(ScriptFrame& frame) const
|
2014-03-24 11:23:47 +01:00
|
|
|
{
|
2015-02-19 12:57:52 +01:00
|
|
|
return Convert::ToBool(m_Filter->Evaluate(frame));
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
void ApplyRule::RegisterType(const String& sourceType, const std::vector<String>& targetTypes)
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-11-16 16:20:39 +01:00
|
|
|
m_Types[sourceType] = targetTypes;
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
bool ApplyRule::IsValidSourceType(const String& sourceType)
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
2014-11-16 16:20:39 +01:00
|
|
|
return m_Types.find(sourceType) != m_Types.end();
|
2014-03-18 11:44:09 +01:00
|
|
|
}
|
2014-04-05 12:56:56 +02:00
|
|
|
|
|
|
|
bool ApplyRule::IsValidTargetType(const String& sourceType, const String& targetType)
|
|
|
|
{
|
2016-08-27 08:33:15 +02:00
|
|
|
auto it = m_Types.find(sourceType);
|
2014-04-05 12:56:56 +02:00
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
if (it == m_Types.end())
|
2014-04-05 12:56:56 +02:00
|
|
|
return false;
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
if (it->second.size() == 1 && targetType == "")
|
2014-04-05 12:56:56 +02:00
|
|
|
return true;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& type : it->second) {
|
2014-04-05 12:56:56 +02:00
|
|
|
if (type == targetType)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:42:33 +02:00
|
|
|
const std::vector<String>& ApplyRule::GetTargetTypes(const String& sourceType)
|
2014-04-05 12:56:56 +02:00
|
|
|
{
|
2016-08-27 08:33:15 +02:00
|
|
|
auto it = m_Types.find(sourceType);
|
2014-04-05 12:56:56 +02:00
|
|
|
|
2022-10-19 13:42:33 +02:00
|
|
|
if (it == m_Types.end()) {
|
|
|
|
static const std::vector<String> noTypes;
|
|
|
|
return noTypes;
|
|
|
|
}
|
2014-04-05 12:56:56 +02:00
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ApplyRule::AddMatch()
|
2014-11-16 16:20:39 +01:00
|
|
|
{
|
2022-09-07 09:16:22 +02:00
|
|
|
m_HasMatches.store(true, std::memory_order_relaxed);
|
2014-11-16 16:20:39 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool ApplyRule::HasMatches() const
|
2014-11-16 16:20:39 +01:00
|
|
|
{
|
2022-09-07 09:16:22 +02:00
|
|
|
return m_HasMatches.load(std::memory_order_relaxed);
|
2014-11-16 16:20:39 +01:00
|
|
|
}
|
|
|
|
|
2022-10-27 16:54:09 +02:00
|
|
|
const std::vector<ApplyRule::Ptr>& ApplyRule::GetRules(const Type::Ptr& sourceType, const Type::Ptr& targetType)
|
2014-11-16 16:20:39 +01:00
|
|
|
{
|
2022-10-17 16:54:34 +02:00
|
|
|
auto perSourceType (m_Rules.find(sourceType.get()));
|
|
|
|
|
|
|
|
if (perSourceType != m_Rules.end()) {
|
2022-10-28 12:41:21 +02:00
|
|
|
auto perTargetType (perSourceType->second.Regular.find(targetType.get()));
|
2022-10-17 16:54:34 +02:00
|
|
|
|
2022-10-28 12:41:21 +02:00
|
|
|
if (perTargetType != perSourceType->second.Regular.end()) {
|
|
|
|
return perTargetType->second;
|
2022-10-17 16:54:34 +02:00
|
|
|
}
|
2014-11-16 16:20:39 +01:00
|
|
|
}
|
2022-10-17 16:54:34 +02:00
|
|
|
|
2022-10-27 16:54:09 +02:00
|
|
|
static const std::vector<ApplyRule::Ptr> noRules;
|
2022-10-17 16:54:34 +02:00
|
|
|
return noRules;
|
2014-11-16 16:20:39 +01:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:41:17 +02:00
|
|
|
void ApplyRule::CheckMatches(bool silent)
|
2014-11-16 16:20:39 +01:00
|
|
|
{
|
2022-10-17 16:54:34 +02:00
|
|
|
for (auto& perSourceType : m_Rules) {
|
2022-10-28 12:41:21 +02:00
|
|
|
for (auto& perTargetType : perSourceType.second.Regular) {
|
|
|
|
for (auto& rule : perTargetType.second) {
|
2022-10-18 16:48:58 +02:00
|
|
|
CheckMatches(rule, perSourceType.first, silent);
|
|
|
|
}
|
2022-10-28 12:41:21 +02:00
|
|
|
}
|
2022-10-18 16:48:58 +02:00
|
|
|
|
2022-10-28 12:41:21 +02:00
|
|
|
std::unordered_set<ApplyRule*> targeted;
|
2022-10-18 16:48:58 +02:00
|
|
|
|
2022-10-28 12:41:21 +02:00
|
|
|
for (auto& perHost : perSourceType.second.Targeted) {
|
|
|
|
for (auto& rule : perHost.second.ForHost) {
|
|
|
|
targeted.emplace(rule.get());
|
|
|
|
}
|
2022-10-18 16:48:58 +02:00
|
|
|
|
2022-10-28 12:41:21 +02:00
|
|
|
for (auto& perService : perHost.second.ForServices) {
|
|
|
|
for (auto& rule : perService.second) {
|
|
|
|
targeted.emplace(rule.get());
|
2022-10-17 16:54:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-28 12:41:21 +02:00
|
|
|
}
|
2022-10-18 16:48:58 +02:00
|
|
|
|
2022-10-28 12:41:21 +02:00
|
|
|
for (auto rule : targeted) {
|
|
|
|
CheckMatches(rule, perSourceType.first, silent);
|
2014-11-16 16:20:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 16:48:58 +02:00
|
|
|
|
2022-10-27 16:54:09 +02:00
|
|
|
void ApplyRule::CheckMatches(const ApplyRule::Ptr& rule, Type* sourceType, bool silent)
|
2022-10-18 16:48:58 +02:00
|
|
|
{
|
2022-10-27 16:54:09 +02:00
|
|
|
if (!rule->HasMatches() && !silent) {
|
2022-10-18 16:48:58 +02:00
|
|
|
Log(LogWarning, "ApplyRule")
|
2022-10-27 16:54:09 +02:00
|
|
|
<< "Apply rule '" << rule->GetName() << "' (" << rule->GetDebugInfo() << ") for type '"
|
2022-10-18 16:48:58 +02:00
|
|
|
<< sourceType->GetName() << "' does not match anywhere!";
|
|
|
|
}
|
|
|
|
}
|