]> granicus.if.org Git - icinga2/blob - lib/livestatus/attributefilter.cpp
Livestatus: Fix case-insensitive comparison operator
[icinga2] / lib / livestatus / attributefilter.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
28
29 using namespace icinga;
30
31 AttributeFilter::AttributeFilter(const String& column, const String& op, const String& operand)
32         : m_Column(column), m_Operator(op), m_Operand(operand)
33 { }
34
35 bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
36 {
37         Column column = table->GetColumn(m_Column);
38
39         Value value = column.ExtractValue(row);
40
41         if (value.IsObjectType<Array>()) {
42                 Array::Ptr array = value;
43
44                 if (m_Operator == ">=" || m_Operator == "<") {
45                         bool negate = (m_Operator == "<");
46
47                         ObjectLock olock(array);
48                         BOOST_FOREACH(const String& item, array) {
49                                 if (item == m_Operand)
50                                         return !negate; /* Item found in list. */
51                         }
52
53                         return negate; /* Item not found in list. */
54                 } else if (m_Operator == "=") {
55                         return (array->GetLength() == 0);
56                 } else {
57                         BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid operator for column '" + m_Column + "': " + m_Operator + " (expected '>=' or '=')."));
58                 }
59         } else {
60                 if (m_Operator == "=") {
61                         if (value.GetType() == ValueNumber || value.GetType() == ValueBoolean)
62                                 return (static_cast<double>(value) == Convert::ToDouble(m_Operand));
63                         else
64                                 return (static_cast<String>(value) == m_Operand);
65                 } else if (m_Operator == "~") {
66                         bool ret;
67                         try {
68                                 boost::regex expr(m_Operand.GetData());
69                                 String operand = value;
70                                 boost::smatch what;
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.";
75                                 ret = false;
76                         }
77
78                         //Log(LogDebug, "LivestatusListener/AttributeFilter")
79                         //    << "Attribute filter '" << m_Operand + " " << m_Operator << " "
80                         //    << value << "' " << (ret ? "matches" : "doesn't match") << ".";
81
82                         return ret;
83                 } else if (m_Operator == "=~") {
84                         bool ret;
85                         try {
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.";
91                                 ret = false;
92                         }
93
94                         return ret;
95                 } else if (m_Operator == "~~") {
96                         bool ret;
97                         try {
98                                 boost::regex expr(m_Operand.GetData(), boost::regex::icase);
99                                 String operand = value;
100                                 boost::smatch what;
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.";
105                                 ret = false;
106                         }
107
108                         //Log(LogDebug, "LivestatusListener/AttributeFilter")
109                         //    << "Attribute filter '" << m_Operand << " " << m_Operator << " "
110                         //    << value << "' " << (ret ? "matches" : "doesn't match") << ".";
111
112                         return ret;
113                 } else if (m_Operator == "<") {
114                         if (value.GetType() == ValueNumber)
115                                 return (static_cast<double>(value) < Convert::ToDouble(m_Operand));
116                         else
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));
121                         else
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));
126                         else
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));
131                         else
132                                 return (static_cast<String>(value) >= m_Operand);
133                 } else {
134                         BOOST_THROW_EXCEPTION(std::invalid_argument("Unknown operator for column '" + m_Column + "': " + m_Operator));
135                 }
136         }
137
138         return false;
139 }