Implement attribute filters.

This commit is contained in:
Gunnar Beutner 2013-03-10 15:11:32 +01:00
parent c9178fb0f3
commit c728e85c8c
20 changed files with 478 additions and 66 deletions

View File

@ -25,17 +25,12 @@ using namespace livestatus;
AndFilter::AndFilter(void)
{ }
bool AndFilter::Apply(const Object::Ptr& object)
bool AndFilter::Apply(const Table::Ptr& table, const Object::Ptr& object)
{
BOOST_FOREACH(const Filter::Ptr& filter, m_Filters) {
if (!filter->Apply(object))
if (!filter->Apply(table, object))
return false;
}
return true;
}
void AndFilter::AddSubFilter(const Filter::Ptr& filter)
{
m_Filters.push_back(filter);
}

View File

@ -26,7 +26,7 @@ namespace livestatus
/**
* @ingroup livestatus
*/
class AndFilter : public Filter
class AndFilter : public CombinerFilter
{
public:
typedef shared_ptr<AndFilter> Ptr;
@ -34,12 +34,7 @@ public:
AndFilter(void);
virtual bool Apply(const Object::Ptr& object);
void AddSubFilter(const Filter::Ptr& filter);
private:
vector<Filter::Ptr> m_Filters;
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object);
};
}

View File

@ -0,0 +1,88 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
#include "i2-livestatus.h"
using namespace icinga;
using namespace livestatus;
AttributeFilter::AttributeFilter(const String& column, const String& op, const String& operand)
: m_Column(column), m_Operator(op), m_Operand(operand)
{ }
bool AttributeFilter::Apply(const Table::Ptr& table, const Object::Ptr& object)
{
Table::ColumnAccessor accessor = table->GetColumn(m_Column);
if (accessor.empty())
BOOST_THROW_EXCEPTION(invalid_argument("Filter expression uses unknown column '" + m_Column + "'"));
Value value = accessor(object);
if (value.IsObjectType<Array>()) {
if (m_Operator == ">=") {
Array::Ptr array = value;
BOOST_FOREACH(const String& item, array) {
if (item == m_Operand)
return true; /* Item found in list. */
}
return false; /* Item not found in list. */
} else {
BOOST_THROW_EXCEPTION(invalid_argument("Invalid operator for column '" + m_Column + "': " + m_Operator + " (expected '>=')."));
}
} else {
if (m_Operator == "=") {
if (value.GetType() == ValueNumber)
return (static_cast<double>(value) == Convert::ToDouble(m_Operand));
else
return (static_cast<String>(value) == m_Operand);
} else if (m_Operator == "~") {
} else if (m_Operator == "=~") {
return string_iless()(value, m_Operand);
} else if (m_Operator == "~~") {
} else if (m_Operator == "<") {
if (value.GetType() == ValueNumber)
return (static_cast<double>(value) < Convert::ToDouble(m_Operand));
else
return (static_cast<String>(value) < m_Operand);
} else if (m_Operator == ">") {
if (value.GetType() == ValueNumber)
return (static_cast<double>(value) > Convert::ToDouble(m_Operand));
else
return (static_cast<String>(value) > m_Operand);
} else if (m_Operator == "<=") {
if (value.GetType() == ValueNumber)
return (static_cast<double>(value) <= Convert::ToDouble(m_Operand));
else
return (static_cast<String>(value) <= m_Operand);
} else if (m_Operator == ">=") {
if (value.GetType() == ValueNumber)
return (static_cast<double>(value) >= Convert::ToDouble(m_Operand));
else
return (static_cast<String>(value) >= m_Operand);
} else {
BOOST_THROW_EXCEPTION(invalid_argument("Unknown operator for column '" + m_Column + "': " + m_Operator));
}
}
return false;
}

View File

@ -0,0 +1,47 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
#ifndef ATTRIBUTEFILTER_H
#define ATTRIBUTEFILTER_H
namespace livestatus
{
/**
* @ingroup livestatus
*/
class AttributeFilter : public Filter
{
public:
typedef shared_ptr<AttributeFilter> Ptr;
typedef weak_ptr<AttributeFilter> WeakPtr;
AttributeFilter(const String& column, const String& op, const String& operand);
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object);
protected:
String m_Column;
String m_Operator;
String m_Operand;
};
}
#endif /* FILTER_H */

View File

@ -0,0 +1,31 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
#include "i2-livestatus.h"
using namespace icinga;
using namespace livestatus;
CombinerFilter::CombinerFilter(void)
{ }
void CombinerFilter::AddSubFilter(const Filter::Ptr& filter)
{
m_Filters.push_back(filter);
}

View File

@ -0,0 +1,45 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
#ifndef COMBINERFILTER_H
#define COMBINERFILTER_H
namespace livestatus
{
/**
* @ingroup livestatus
*/
class CombinerFilter : public Filter
{
public:
typedef shared_ptr<CombinerFilter> Ptr;
typedef weak_ptr<CombinerFilter> WeakPtr;
CombinerFilter(void);
void AddSubFilter(const Filter::Ptr& filter);
protected:
vector<Filter::Ptr> m_Filters;
};
}
#endif /* COMBINERFILTER_H */

View File

@ -32,7 +32,7 @@ public:
typedef shared_ptr<Filter> Ptr;
typedef weak_ptr<Filter> WeakPtr;
virtual bool Apply(const Object::Ptr& object) = 0;
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object) = 0;
protected:
Filter(void);

View File

@ -33,11 +33,14 @@
using namespace icinga;
#include "connection.h"
#include "query.h"
#include "table.h"
#include "filter.h"
#include "combinerfilter.h"
#include "orfilter.h"
#include "andfilter.h"
#include "table.h"
#include "negatefilter.h"
#include "attributefilter.h"
#include "query.h"
#include "statustable.h"
#include "contactgroupstable.h"
#include "contactstable.h"

View File

@ -20,12 +20,15 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="andfilter.h" />
<ClInclude Include="attributefilter.h" />
<ClInclude Include="combinerfilter.h" />
<ClInclude Include="contactgroupstable.h" />
<ClInclude Include="contactstable.h" />
<ClInclude Include="filter.h" />
<ClInclude Include="i2-livestatus.h" />
<ClInclude Include="component.h" />
<ClInclude Include="connection.h" />
<ClInclude Include="negatefilter.h" />
<ClInclude Include="orfilter.h" />
<ClInclude Include="query.h" />
<ClInclude Include="statustable.h" />
@ -33,11 +36,14 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="andfilter.cpp" />
<ClCompile Include="attributefilter.cpp" />
<ClCompile Include="combinerfilter.cpp" />
<ClCompile Include="component.cpp" />
<ClCompile Include="connection.cpp" />
<ClCompile Include="contactgroupstable.cpp" />
<ClCompile Include="contactstable.cpp" />
<ClCompile Include="filter.cpp" />
<ClCompile Include="negatefilter.cpp" />
<ClCompile Include="orfilter.cpp" />
<ClCompile Include="query.cpp" />
<ClCompile Include="statustable.cpp" />

View File

@ -42,6 +42,15 @@
<ClInclude Include="andfilter.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="attributefilter.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="negatefilter.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="combinerfilter.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="component.cpp">
@ -74,5 +83,14 @@
<ClCompile Include="andfilter.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="attributefilter.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="negatefilter.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="combinerfilter.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,32 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
#include "i2-livestatus.h"
using namespace icinga;
using namespace livestatus;
NegateFilter::NegateFilter(const Filter::Ptr& inner)
: m_Inner(inner)
{ }
bool NegateFilter::Apply(const Table::Ptr& table, const Object::Ptr& object)
{
return !m_Inner->Apply(table, object);
}

View File

@ -0,0 +1,45 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
#ifndef NEGATEFILTER_H
#define NEGATEFILTER_H
namespace livestatus
{
/**
* @ingroup livestatus
*/
class NegateFilter : public Filter
{
public:
typedef shared_ptr<NegateFilter> Ptr;
typedef weak_ptr<NegateFilter> WeakPtr;
NegateFilter(const Filter::Ptr& inner);
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object);
private:
Filter::Ptr m_Inner;
};
}
#endif /* NEGATEFILTER_H */

View File

@ -25,20 +25,15 @@ using namespace livestatus;
OrFilter::OrFilter(void)
{ }
bool OrFilter::Apply(const Object::Ptr& object)
bool OrFilter::Apply(const Table::Ptr& table, const Object::Ptr& object)
{
if (m_Filters.empty())
return true;
BOOST_FOREACH(const Filter::Ptr& filter, m_Filters) {
if (filter->Apply(object))
if (filter->Apply(table, object))
return true;
}
return false;
}
void OrFilter::AddSubFilter(const Filter::Ptr& filter)
{
m_Filters.push_back(filter);
}

View File

@ -26,7 +26,7 @@ namespace livestatus
/**
* @ingroup livestatus
*/
class OrFilter : public Filter
class OrFilter : public CombinerFilter
{
public:
typedef shared_ptr<OrFilter> Ptr;
@ -34,12 +34,7 @@ public:
OrFilter(void);
virtual bool Apply(const Object::Ptr& object);
void AddSubFilter(const Filter::Ptr& filter);
private:
vector<Filter::Ptr> m_Filters;
virtual bool Apply(const Table::Ptr& table, const Object::Ptr& object);
};
}

View File

@ -42,10 +42,15 @@ Query::Query(const vector<String>& lines)
} else if (m_Verb == "GET") {
m_Table = target;
} else {
BOOST_THROW_EXCEPTION(runtime_error("Unknown livestatus verb: " + m_Verb));
m_Verb = "ERROR";
m_ErrorCode = 452;
m_ErrorMessage = "Unknown livestatus verb: " + m_Verb;
return;
}
for (int i = 1; i < lines.size(); i++) {
deque<Filter::Ptr> filters, stats;
for (unsigned int i = 1; i < lines.size(); i++) {
line = lines[i];
size_t col_index = line.FindFirstOf(":");
@ -58,12 +63,66 @@ Query::Query(const vector<String>& lines)
m_OutputFormat = params;
else if (header == "Columns")
m_Columns = params.Split(is_any_of(" "));
else if (header == "ColumnHeaders")
m_ColumnHeaders = (params == "on");
else if (header == "Filter") {
vector<String> tokens = params.Split(is_any_of(" "));
if (tokens.size() < 3) {
m_Verb = "ERROR";
m_ErrorCode = 452;
m_ErrorMessage = "Expected 3 parameters in the filter specification.";
return;
}
String op = tokens[1];
bool negate = false;
if (op == "!=") {
op = "=";
negate = true;
} else if (op == "!~") {
op = "~";
negate = true;
} else if (op == "!=~") {
op = "=~";
negate = true;
} else if (op == "!~~") {
op = "~~";
negate = true;
}
Filter::Ptr filter = boost::make_shared<AttributeFilter>(tokens[0], op, tokens[2]);
if (negate)
filter = boost::make_shared<NegateFilter>(filter);
filters.push_back(filter);
} else if (header == "Or" || header == "And") {
int num = Convert::ToLong(params);
CombinerFilter::Ptr filter;
if (header == "Or")
filter = boost::make_shared<OrFilter>();
else
filter = boost::make_shared<AndFilter>();
while (!filters.empty() && num--) {
filter->AddSubFilter(filters.back());
filters.pop_back();
}
filters.push_back(filter);
}
}
m_Filters.swap(filters);
m_Stats.swap(stats);
}
void Query::PrintResultSet(ostream& fp, const vector<String>& columns, const Array::Ptr& rs)
{
if (m_OutputFormat == "csv" && m_Columns.size() == 0) {
if (m_OutputFormat == "csv" && m_Columns.size() == 0 && m_ColumnHeaders) {
bool first = true;
BOOST_FOREACH(const String& column, columns) {
@ -113,7 +172,15 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
return;
}
vector<Object::Ptr> objects = table->FilterRows(Filter::Ptr());
if (m_Filters.size() > 1)
SendResponse(stream, 452, "There must not be more than one top-level filter expression.");
Filter::Ptr filter;
if (!m_Filters.empty())
filter = m_Filters[0];
vector<Object::Ptr> objects = table->FilterRows(filter);
vector<String> columns;
if (m_Columns.size() > 0)
@ -149,13 +216,9 @@ void Query::ExecuteGetHelper(const Stream::Ptr& stream)
void Query::ExecuteCommandHelper(const Stream::Ptr& stream)
{
try {
Logger::Write(LogInformation, "livestatus", "Executing command: " + m_Command);
ExternalCommandProcessor::Execute(m_Command);
SendResponse(stream, 200, "");
} catch (const std::exception& ex) {
SendResponse(stream, 452, diagnostic_information(ex));
}
Logger::Write(LogInformation, "livestatus", "Executing command: " + m_Command);
ExternalCommandProcessor::Execute(m_Command);
SendResponse(stream, 200, "");
}
void Query::ExecuteErrorHelper(const Stream::Ptr& stream)
@ -185,6 +248,7 @@ void Query::PrintFixed16(const Stream::Ptr& stream, int code, const String& data
void Query::Execute(const Stream::Ptr& stream)
{
try {
Logger::Write(LogInformation, "livestatus", "Executing livestatus query: " + m_Verb);
if (m_Verb == "GET")
@ -195,6 +259,9 @@ void Query::Execute(const Stream::Ptr& stream)
ExecuteErrorHelper(stream);
else
BOOST_THROW_EXCEPTION(runtime_error("Invalid livestatus query verb."));
} catch (const std::exception& ex) {
SendResponse(stream, 452, boost::diagnostic_information(ex));
}
if (!m_KeepAlive)
stream->Close();

View File

@ -45,6 +45,9 @@ private:
String m_Table;
vector<String> m_Columns;
deque<Filter::Ptr> m_Filters;
deque<Filter::Ptr> m_Stats;
String m_OutputFormat;
bool m_ColumnHeaders;
int m_Limit;

View File

@ -68,14 +68,14 @@ vector<Object::Ptr> Table::FilterRows(const Filter::Ptr& filter)
{
vector<Object::Ptr> rs;
FetchRows(boost::bind(&Table::FilteredAddRow, boost::ref(rs), filter, _1));
FetchRows(boost::bind(&Table::FilteredAddRow, this, boost::ref(rs), filter, _1));
return rs;
}
void Table::FilteredAddRow(vector<Object::Ptr>& rs, const Filter::Ptr& filter, const Object::Ptr& object)
{
if (!filter || filter->Apply(object))
if (!filter || filter->Apply(GetSelf(), object))
rs.push_back(object);
}

View File

@ -23,6 +23,8 @@
namespace livestatus
{
class Filter;
/**
* @ingroup livestatus
*/
@ -38,7 +40,7 @@ public:
virtual String GetName(void) const = 0;
vector<Object::Ptr> FilterRows(const Filter::Ptr& filter);
vector<Object::Ptr> FilterRows(const shared_ptr<Filter>& filter);
ColumnAccessor GetColumn(const String& name) const;
vector<String> GetColumnNames(void) const;
@ -59,7 +61,7 @@ protected:
private:
map<String, ColumnAccessor> m_Columns;
static void FilteredAddRow(vector<Object::Ptr>& rs, const Filter::Ptr& filter, const Object::Ptr& object);
void FilteredAddRow(vector<Object::Ptr>& rs, const shared_ptr<Filter>& filter, const Object::Ptr& object);
};
}

View File

@ -212,6 +212,61 @@ bool icinga::operator==(const char *lhs, const String& rhs)
return lhs == static_cast<std::string>(rhs);
}
bool icinga::operator<(const String& lhs, const char *rhs)
{
return static_cast<std::string>(lhs) < rhs;
}
bool icinga::operator<(const char *lhs, const String& rhs)
{
return lhs < static_cast<std::string>(rhs);
}
bool icinga::operator>(const String& lhs, const String& rhs)
{
return static_cast<std::string>(lhs) > static_cast<std::string>(rhs);
}
bool icinga::operator>(const String& lhs, const char *rhs)
{
return static_cast<std::string>(lhs) > rhs;
}
bool icinga::operator>(const char *lhs, const String& rhs)
{
return lhs > static_cast<std::string>(rhs);
}
bool icinga::operator<=(const String& lhs, const String& rhs)
{
return static_cast<std::string>(lhs) <= static_cast<std::string>(rhs);
}
bool icinga::operator<=(const String& lhs, const char *rhs)
{
return static_cast<std::string>(lhs) <= rhs;
}
bool icinga::operator<=(const char *lhs, const String& rhs)
{
return lhs <= static_cast<std::string>(rhs);
}
bool icinga::operator>=(const String& lhs, const String& rhs)
{
return static_cast<std::string>(lhs) >= static_cast<std::string>(rhs);
}
bool icinga::operator>=(const String& lhs, const char *rhs)
{
return static_cast<std::string>(lhs) >= rhs;
}
bool icinga::operator>=(const char *lhs, const String& rhs)
{
return lhs >= static_cast<std::string>(rhs);
}
bool icinga::operator!=(const String& lhs, const String& rhs)
{
return static_cast<std::string>(lhs) != static_cast<std::string>(rhs);
@ -227,26 +282,6 @@ bool icinga::operator!=(const char *lhs, const String& rhs)
return lhs != static_cast<std::string>(rhs);
}
bool icinga::operator<(const String& lhs, const char *rhs)
{
return static_cast<std::string>(lhs) < rhs;
}
bool icinga::operator<(const char *lhs, const String& rhs)
{
return lhs < static_cast<std::string>(rhs);
}
bool icinga::operator>(const String& lhs, const char *rhs)
{
return static_cast<std::string>(lhs) > rhs;
}
bool icinga::operator>(const char *lhs, const String& rhs)
{
return lhs > static_cast<std::string>(rhs);
}
String::Iterator icinga::range_begin(String& x)
{
return x.Begin();

View File

@ -122,9 +122,19 @@ I2_BASE_API bool operator!=(const char *lhs, const String& rhs);
I2_BASE_API bool operator<(const String& lhs, const char *rhs);
I2_BASE_API bool operator<(const char *lhs, const String& rhs);
I2_BASE_API bool operator>(const String& lhs, const String& rhs);
I2_BASE_API bool operator>(const String& lhs, const char *rhs);
I2_BASE_API bool operator>(const char *lhs, const String& rhs);
I2_BASE_API bool operator<=(const String& lhs, const String& rhs);
I2_BASE_API bool operator<=(const String& lhs, const char *rhs);
I2_BASE_API bool operator<=(const char *lhs, const String& rhs);
I2_BASE_API bool operator>=(const String& lhs, const String& rhs);
I2_BASE_API bool operator>=(const String& lhs, const char *rhs);
I2_BASE_API bool operator>=(const char *lhs, const String& rhs);
I2_BASE_API String::Iterator range_begin(String& x);
I2_BASE_API String::ConstIterator range_begin(const String& x);
I2_BASE_API String::Iterator range_end(String& x);