1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/attributefilter.hpp"
4 #include "base/convert.hpp"
5 #include "base/array.hpp"
6 #include "base/objectlock.hpp"
7 #include "base/logger.hpp"
8 #include <boost/regex.hpp>
9 #include <boost/algorithm/string/predicate.hpp>
11 using namespace icinga;
13 AttributeFilter::AttributeFilter(String column, String op, String operand)
14 : m_Column(std::move(column)), m_Operator(std::move(op)), m_Operand(std::move(operand))
17 bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
19 Column column = table->GetColumn(m_Column);
21 Value value = column.ExtractValue(row);
23 if (value.IsObjectType<Array>()) {
24 Array::Ptr array = value;
26 if (m_Operator == ">=" || m_Operator == "<") {
27 bool negate = (m_Operator == "<");
29 ObjectLock olock(array);
30 for (const String& item : array) {
31 if (item == m_Operand)
32 return !negate; /* Item found in list. */
35 return negate; /* Item not found in list. */
36 } else if (m_Operator == "=") {
37 return (array->GetLength() == 0);
39 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid operator for column '" + m_Column + "': " + m_Operator + " (expected '>=' or '=')."));
42 if (m_Operator == "=") {
43 if (value.GetType() == ValueNumber || value.GetType() == ValueBoolean)
44 return (static_cast<double>(value) == Convert::ToDouble(m_Operand));
46 return (static_cast<String>(value) == m_Operand);
47 } else if (m_Operator == "~") {
50 boost::regex expr(m_Operand.GetData());
51 String operand = value;
53 ret = boost::regex_search(operand.GetData(), what, expr);
54 } catch (boost::exception&) {
55 Log(LogWarning, "AttributeFilter")
56 << "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
60 //Log(LogDebug, "LivestatusListener/AttributeFilter")
61 // << "Attribute filter '" << m_Operand + " " << m_Operator << " "
62 // << value << "' " << (ret ? "matches" : "doesn't match") << ".";
65 } else if (m_Operator == "=~") {
68 String operand = value;
69 ret = boost::iequals(operand, m_Operand.GetData());
70 } catch (boost::exception&) {
71 Log(LogWarning, "AttributeFilter")
72 << "Case-insensitive equality '" << m_Operand << " " << m_Operator << " " << value << "' error.";
77 } else if (m_Operator == "~~") {
80 boost::regex expr(m_Operand.GetData(), boost::regex::icase);
81 String operand = value;
83 ret = boost::regex_search(operand.GetData(), what, expr);
84 } catch (boost::exception&) {
85 Log(LogWarning, "AttributeFilter")
86 << "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
90 //Log(LogDebug, "LivestatusListener/AttributeFilter")
91 // << "Attribute filter '" << m_Operand << " " << m_Operator << " "
92 // << value << "' " << (ret ? "matches" : "doesn't match") << ".";
95 } else if (m_Operator == "<") {
96 if (value.GetType() == ValueNumber)
97 return (static_cast<double>(value) < Convert::ToDouble(m_Operand));
99 return (static_cast<String>(value) < m_Operand);
100 } else if (m_Operator == ">") {
101 if (value.GetType() == ValueNumber)
102 return (static_cast<double>(value) > Convert::ToDouble(m_Operand));
104 return (static_cast<String>(value) > m_Operand);
105 } else if (m_Operator == "<=") {
106 if (value.GetType() == ValueNumber)
107 return (static_cast<double>(value) <= Convert::ToDouble(m_Operand));
109 return (static_cast<String>(value) <= m_Operand);
110 } else if (m_Operator == ">=") {
111 if (value.GetType() == ValueNumber)
112 return (static_cast<double>(value) >= Convert::ToDouble(m_Operand));
114 return (static_cast<String>(value) >= m_Operand);
116 BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown operator for column '" + m_Column + "': " + m_Operator));