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>
|
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
|
|
|
|
2019-07-26 14:17:27 +02:00
|
|
|
ApplyRule::ApplyRule(String targetType, String name, Expression::Ptr expression,
|
|
|
|
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)
|
|
|
|
: m_TargetType(std::move(targetType)), 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)),
|
|
|
|
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::GetTargetType() const
|
2014-04-05 12:56:56 +02:00
|
|
|
{
|
|
|
|
return m_TargetType;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String ApplyRule::GetFKVar() const
|
2014-11-02 07:22:00 +01:00
|
|
|
{
|
2014-11-04 11:01:00 +01:00
|
|
|
return m_FKVar;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String ApplyRule::GetFVVar() const
|
2014-11-04 11:01:00 +01:00
|
|
|
{
|
|
|
|
return m_FVVar;
|
2014-11-02 07:22:00 +01: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;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Rules[Type::GetByName(sourceType).get()][Type::GetByName(*actualTargetType).get()].emplace_back(ApplyRule(
|
|
|
|
targetType, name, expression, filter, package, fkvar, fvvar, fterm, ignoreOnError, di, scope
|
|
|
|
));
|
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
|
|
|
{
|
|
|
|
m_HasMatches = true;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool ApplyRule::HasMatches() const
|
2014-11-16 16:20:39 +01:00
|
|
|
{
|
|
|
|
return m_HasMatches;
|
|
|
|
}
|
|
|
|
|
2022-10-17 16:54:34 +02:00
|
|
|
std::vector<ApplyRule>& 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()) {
|
|
|
|
auto perTargetType (perSourceType->second.find(targetType.get()));
|
|
|
|
|
|
|
|
if (perTargetType != perSourceType->second.end()) {
|
|
|
|
return perTargetType->second;
|
|
|
|
}
|
2014-11-16 16:20:39 +01:00
|
|
|
}
|
2022-10-17 16:54:34 +02:00
|
|
|
|
|
|
|
static std::vector<ApplyRule> noRules;
|
|
|
|
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) {
|
|
|
|
for (auto& perTargetType : perSourceType.second) {
|
|
|
|
for (auto& rule : perTargetType.second) {
|
|
|
|
if (!rule.HasMatches() && !silent) {
|
|
|
|
Log(LogWarning, "ApplyRule")
|
|
|
|
<< "Apply rule '" << rule.GetName() << "' (" << rule.GetDebugInfo() << ") for type '"
|
|
|
|
<< perSourceType.first->GetName() << "' does not match anywhere!";
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 16:20:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|