1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "livestatus/attributefilter.hpp"
21 #include "base/convert.hpp"
22 #include "base/array.hpp"
23 #include "base/objectlock.hpp"
24 #include "base/logger.hpp"
25 #include <boost/regex.hpp>
26 #include <boost/algorithm/string/predicate.hpp>
28 using namespace icinga;
30 AttributeFilter::AttributeFilter(String column, String op, String operand)
31 : m_Column(std::move(column)), m_Operator(std::move(op)), m_Operand(std::move(operand))
34 bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
36 Column column = table->GetColumn(m_Column);
38 Value value = column.ExtractValue(row);
40 if (value.IsObjectType<Array>()) {
41 Array::Ptr array = value;
43 if (m_Operator == ">=" || m_Operator == "<") {
44 bool negate = (m_Operator == "<");
46 ObjectLock olock(array);
47 for (const String& item : array) {
48 if (item == m_Operand)
49 return !negate; /* Item found in list. */
52 return negate; /* Item not found in list. */
53 } else if (m_Operator == "=") {
54 return (array->GetLength() == 0);
56 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid operator for column '" + m_Column + "': " + m_Operator + " (expected '>=' or '=')."));
59 if (m_Operator == "=") {
60 if (value.GetType() == ValueNumber || value.GetType() == ValueBoolean)
61 return (static_cast<double>(value) == Convert::ToDouble(m_Operand));
63 return (static_cast<String>(value) == m_Operand);
64 } else if (m_Operator == "~") {
67 boost::regex expr(m_Operand.GetData());
68 String operand = value;
70 ret = boost::regex_search(operand.GetData(), what, expr);
71 } catch (boost::exception&) {
72 Log(LogWarning, "AttributeFilter")
73 << "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
77 //Log(LogDebug, "LivestatusListener/AttributeFilter")
78 // << "Attribute filter '" << m_Operand + " " << m_Operator << " "
79 // << value << "' " << (ret ? "matches" : "doesn't match") << ".";
82 } else if (m_Operator == "=~") {
85 String operand = value;
86 ret = boost::iequals(operand, m_Operand.GetData());
87 } catch (boost::exception&) {
88 Log(LogWarning, "AttributeFilter")
89 << "Case-insensitive equality '" << m_Operand << " " << m_Operator << " " << value << "' error.";
94 } else if (m_Operator == "~~") {
97 boost::regex expr(m_Operand.GetData(), boost::regex::icase);
98 String operand = value;
100 ret = boost::regex_search(operand.GetData(), what, expr);
101 } catch (boost::exception&) {
102 Log(LogWarning, "AttributeFilter")
103 << "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
107 //Log(LogDebug, "LivestatusListener/AttributeFilter")
108 // << "Attribute filter '" << m_Operand << " " << m_Operator << " "
109 // << value << "' " << (ret ? "matches" : "doesn't match") << ".";
112 } else if (m_Operator == "<") {
113 if (value.GetType() == ValueNumber)
114 return (static_cast<double>(value) < Convert::ToDouble(m_Operand));
116 return (static_cast<String>(value) < m_Operand);
117 } else if (m_Operator == ">") {
118 if (value.GetType() == ValueNumber)
119 return (static_cast<double>(value) > Convert::ToDouble(m_Operand));
121 return (static_cast<String>(value) > m_Operand);
122 } else if (m_Operator == "<=") {
123 if (value.GetType() == ValueNumber)
124 return (static_cast<double>(value) <= Convert::ToDouble(m_Operand));
126 return (static_cast<String>(value) <= m_Operand);
127 } else if (m_Operator == ">=") {
128 if (value.GetType() == ValueNumber)
129 return (static_cast<double>(value) >= Convert::ToDouble(m_Operand));
131 return (static_cast<String>(value) >= m_Operand);
133 BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown operator for column '" + m_Column + "': " + m_Operator));