1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
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/foreach.hpp>
26 #include <boost/regex.hpp>
27 #include <boost/algorithm/string/predicate.hpp>
29 using namespace icinga;
31 AttributeFilter::AttributeFilter(const String& column, const String& op, const String& operand)
32 : m_Column(column), m_Operator(op), m_Operand(operand)
35 bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
37 Column column = table->GetColumn(m_Column);
39 Value value = column.ExtractValue(row);
41 if (value.IsObjectType<Array>()) {
42 Array::Ptr array = value;
44 if (m_Operator == ">=" || m_Operator == "<") {
45 bool negate = (m_Operator == "<");
47 ObjectLock olock(array);
48 BOOST_FOREACH(const String& item, array) {
49 if (item == m_Operand)
50 return !negate; /* Item found in list. */
53 return negate; /* Item not found in list. */
54 } else if (m_Operator == "=") {
55 return (array->GetLength() == 0);
57 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid operator for column '" + m_Column + "': " + m_Operator + " (expected '>=' or '=')."));
60 if (m_Operator == "=") {
61 if (value.GetType() == ValueNumber || value.GetType() == ValueBoolean)
62 return (static_cast<double>(value) == Convert::ToDouble(m_Operand));
64 return (static_cast<String>(value) == m_Operand);
65 } else if (m_Operator == "~") {
68 boost::regex expr(m_Operand.GetData());
69 String operand = value;
71 ret = boost::regex_search(operand.GetData(), what, expr);
72 } catch (boost::exception&) {
73 Log(LogWarning, "AttributeFilter")
74 << "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
78 //Log(LogDebug, "LivestatusListener/AttributeFilter")
79 // << "Attribute filter '" << m_Operand + " " << m_Operator << " "
80 // << value << "' " << (ret ? "matches" : "doesn't match") << ".";
83 } else if (m_Operator == "=~") {
86 String operand = value;
87 ret = boost::iequals(operand, m_Operand.GetData());
88 } catch (boost::exception&) {
89 Log(LogWarning, "AttributeFilter")
90 << "Case-insensitive equality '" << m_Operand << " " << m_Operator << " " << value << "' error.";
95 } else if (m_Operator == "~~") {
98 boost::regex expr(m_Operand.GetData(), boost::regex::icase);
99 String operand = value;
101 ret = boost::regex_search(operand.GetData(), what, expr);
102 } catch (boost::exception&) {
103 Log(LogWarning, "AttributeFilter")
104 << "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
108 //Log(LogDebug, "LivestatusListener/AttributeFilter")
109 // << "Attribute filter '" << m_Operand << " " << m_Operator << " "
110 // << value << "' " << (ret ? "matches" : "doesn't match") << ".";
113 } else if (m_Operator == "<") {
114 if (value.GetType() == ValueNumber)
115 return (static_cast<double>(value) < Convert::ToDouble(m_Operand));
117 return (static_cast<String>(value) < m_Operand);
118 } else if (m_Operator == ">") {
119 if (value.GetType() == ValueNumber)
120 return (static_cast<double>(value) > Convert::ToDouble(m_Operand));
122 return (static_cast<String>(value) > m_Operand);
123 } else if (m_Operator == "<=") {
124 if (value.GetType() == ValueNumber)
125 return (static_cast<double>(value) <= Convert::ToDouble(m_Operand));
127 return (static_cast<String>(value) <= m_Operand);
128 } else if (m_Operator == ">=") {
129 if (value.GetType() == ValueNumber)
130 return (static_cast<double>(value) >= Convert::ToDouble(m_Operand));
132 return (static_cast<String>(value) >= m_Operand);
134 BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown operator for column '" + m_Column + "': " + m_Operator));