2014-03-18 11:44:09 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2014-03-18 11:44:09 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
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
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
ApplyRule::ApplyRule(const String& targetType, const String& name, const boost::shared_ptr<Expression>& expression,
|
2015-08-28 17:58:29 +02:00
|
|
|
const boost::shared_ptr<Expression>& filter, const String& package, const String& fkvar, const String& fvvar, const boost::shared_ptr<Expression>& fterm,
|
2015-10-05 12:44:11 +02:00
|
|
|
bool ignoreOnError, const DebugInfo& di, const Dictionary::Ptr& scope)
|
2015-08-28 17:58:29 +02:00
|
|
|
: m_TargetType(targetType), m_Name(name), m_Expression(expression), m_Filter(filter), m_Package(package), m_FKVar(fkvar),
|
2015-10-05 12:44:11 +02:00
|
|
|
m_FVVar(fvvar), m_FTerm(fterm), m_IgnoreOnError(ignoreOnError), m_DebugInfo(di), m_Scope(scope), m_HasMatches(false)
|
2014-03-18 11:44:09 +01:00
|
|
|
{ }
|
|
|
|
|
2014-04-05 12:56:56 +02:00
|
|
|
String ApplyRule::GetTargetType(void) const
|
|
|
|
{
|
|
|
|
return m_TargetType;
|
|
|
|
}
|
|
|
|
|
2014-03-28 13:25:40 +01:00
|
|
|
String ApplyRule::GetName(void) 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
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
boost::shared_ptr<Expression> ApplyRule::GetExpression(void) const
|
2014-03-18 11:44:09 +01:00
|
|
|
{
|
|
|
|
return m_Expression;
|
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
boost::shared_ptr<Expression> ApplyRule::GetFilter(void) const
|
2014-03-28 13:25:40 +01:00
|
|
|
{
|
|
|
|
return m_Filter;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
String ApplyRule::GetPackage(void) 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
|
|
|
}
|
|
|
|
|
2014-11-04 11:01:00 +01:00
|
|
|
String ApplyRule::GetFKVar(void) const
|
2014-11-02 07:22:00 +01:00
|
|
|
{
|
2014-11-04 11:01:00 +01:00
|
|
|
return m_FKVar;
|
|
|
|
}
|
|
|
|
|
|
|
|
String ApplyRule::GetFVVar(void) const
|
|
|
|
{
|
|
|
|
return m_FVVar;
|
2014-11-02 07:22:00 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 19:48:28 +01:00
|
|
|
boost::shared_ptr<Expression> ApplyRule::GetFTerm(void) const
|
2014-11-02 07:22:00 +01:00
|
|
|
{
|
|
|
|
return m_FTerm;
|
|
|
|
}
|
|
|
|
|
2015-10-05 12:44:11 +02:00
|
|
|
bool ApplyRule::GetIgnoreOnError(void) const
|
|
|
|
{
|
|
|
|
return m_IgnoreOnError;
|
|
|
|
}
|
|
|
|
|
2014-03-18 11:44:09 +01:00
|
|
|
DebugInfo ApplyRule::GetDebugInfo(void) const
|
|
|
|
{
|
|
|
|
return m_DebugInfo;
|
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:28 +01:00
|
|
|
Dictionary::Ptr ApplyRule::GetScope(void) 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,
|
2015-08-28 17:58:29 +02:00
|
|
|
const boost::shared_ptr<Expression>& expression, const boost::shared_ptr<Expression>& filter, const String& package, const String& fkvar,
|
2015-10-05 12:44:11 +02:00
|
|
|
const String& fvvar, const boost::shared_ptr<Expression>& fterm, bool ignoreOnError, const DebugInfo& di, const Dictionary::Ptr& scope)
|
2014-03-28 13:25:40 +01:00
|
|
|
{
|
2015-10-05 12:44:11 +02:00
|
|
|
m_Rules[sourceType].push_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<String> ApplyRule::GetTargetTypes(const String& sourceType)
|
|
|
|
{
|
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 std::vector<String>();
|
|
|
|
|
2014-11-16 16:20:39 +01:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyRule::AddMatch(void)
|
|
|
|
{
|
|
|
|
m_HasMatches = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ApplyRule::HasMatches(void) const
|
|
|
|
{
|
|
|
|
return m_HasMatches;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ApplyRule>& ApplyRule::GetRules(const String& type)
|
|
|
|
{
|
2016-08-27 19:56:12 +02:00
|
|
|
auto it = m_Rules.find(type);
|
2014-11-16 16:20:39 +01:00
|
|
|
if (it == m_Rules.end()) {
|
|
|
|
static std::vector<ApplyRule> emptyList;
|
|
|
|
return emptyList;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyRule::CheckMatches(void)
|
|
|
|
{
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const RuleMap::value_type& kv : m_Rules) {
|
|
|
|
for (const ApplyRule& rule : kv.second) {
|
2014-11-16 16:20:39 +01:00
|
|
|
if (!rule.HasMatches())
|
|
|
|
Log(LogWarning, "ApplyRule")
|
2015-01-30 15:50:03 +01:00
|
|
|
<< "Apply rule '" << rule.GetName() << "' (" << rule.GetDebugInfo() << ") for type '" << kv.first << "' does not match anywhere!";
|
2014-11-16 16:20:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|