1 /******************************************************************************
3 * Copyright (C) 2012-2014 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_fwd.hpp"
25 #include <boost/foreach.hpp>
26 #include <boost/regex.hpp>
28 using namespace icinga;
30 AttributeFilter::AttributeFilter(const String& column, const String& op, const String& operand)
31 : m_Column(column), m_Operator(op), m_Operand(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 BOOST_FOREACH(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)
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", "Regex '" + m_Operand + " " + m_Operator + " " + Convert::ToString(value) + "' error.");
76 //Log(LogDebug, "LivestatusListener/AttributeFilter", "Attribute filter '" + m_Operand + " " + m_Operator + " " +
77 // static_cast<String>(value) + "' " + (ret ? "matches" : "doesn't match") + "." );
80 } else if (m_Operator == "=~") {
81 return string_iless()(value, m_Operand);
82 } else if (m_Operator == "~~") {
85 boost::regex expr(m_Operand.GetData(), boost::regex::icase);
86 String operand = value;
88 ret = boost::regex_search(operand.GetData(), what, expr);
89 } catch (boost::exception&) {
90 Log(LogWarning, "AttributeFilter", "Regex '" + m_Operand + " " + m_Operator + " " + Convert::ToString(value) + "' error.");
94 //Log(LogDebug, "LivestatusListener/AttributeFilter", "Attribute filter '" + m_Operand + " " + m_Operator + " " +
95 // static_cast<String>(value) + "' " + (ret ? "matches" : "doesn't match") + "." );
98 } else if (m_Operator == "<") {
99 if (value.GetType() == ValueNumber)
100 return (static_cast<double>(value) < Convert::ToDouble(m_Operand));
102 return (static_cast<String>(value) < m_Operand);
103 } else if (m_Operator == ">") {
104 if (value.GetType() == ValueNumber)
105 return (static_cast<double>(value) > Convert::ToDouble(m_Operand));
107 return (static_cast<String>(value) > m_Operand);
108 } else if (m_Operator == "<=") {
109 if (value.GetType() == ValueNumber)
110 return (static_cast<double>(value) <= Convert::ToDouble(m_Operand));
112 return (static_cast<String>(value) <= m_Operand);
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);
119 BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown operator for column '" + m_Column + "': " + m_Operator));