]> granicus.if.org Git - icinga2/commitdiff
Implement regex match attribute filters (~, ~~)
authorMichael Friedrich <michael.friedrich@netways.de>
Tue, 5 Nov 2013 16:05:41 +0000 (17:05 +0100)
committerMichael Friedrich <michael.friedrich@netways.de>
Tue, 5 Nov 2013 16:13:40 +0000 (17:13 +0100)
Requires the boost regex library.

Refs #5007

Signed-off-by: Michael Friedrich <michael.friedrich@netways.de>
CMakeLists.txt
components/livestatus/attributefilter.cpp
icinga2.spec
test/livestatus/queries/log/alerts [new file with mode: 0644]

index 6403fca18afd7ea61de06a6b6ef23bd8c3b2cea0..e6829a81bce70ade56f29367aab73c03373b4ee3 100644 (file)
@@ -48,7 +48,7 @@ if(WIN32)
   add_definitions(-DBOOST_ALL_NO_LIB)
 endif()
 
-find_package(Boost 1.41.0 COMPONENTS thread system program_options REQUIRED)
+find_package(Boost 1.41.0 COMPONENTS thread system program_options regex REQUIRED)
 link_directories(${Boost_LIBRARY_DIRS})
 include_directories(${Boost_INCLUDE_DIRS})
 
index ad1cee34b2e34510f2a87942fbbb8870af6d3ab3..76dcf508e7786b14b6bc5580c8c3e43d96898321 100644 (file)
@@ -21,7 +21,9 @@
 #include "base/convert.h"
 #include "base/array.h"
 #include "base/objectlock.h"
+#include "base/logger_fwd.h"
 #include <boost/foreach.hpp>
+#include <boost/regex.hpp>
 
 using namespace icinga;
 using namespace livestatus;
@@ -59,11 +61,33 @@ bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
                        else
                                return (static_cast<String>(value) == m_Operand);
                } else if (m_Operator == "~") {
+                       boost::regex expr(static_cast<std::string>(m_Operand));
+                       boost::smatch what;
+                       String val = static_cast<String>(value);
+                       std::string::const_iterator begin = val.Begin();
+                       std::string::const_iterator end = val.End();
 
+                       bool ret = boost::regex_search(begin, end, what, expr);
+
+                       //Log(LogDebug, "livestatus", "Attribute filter '" + m_Operand + " " + m_Operator + " " +
+                       //    static_cast<String>(value) + "' " + (ret ? "matches" : "doesn't match") + "." );
+
+                       return ret;
                } else if (m_Operator == "=~") {
                        return string_iless()(value, m_Operand);
                } else if (m_Operator == "~~") {
+                       boost::regex expr(static_cast<std::string>(m_Operand), boost::regex::icase);
+                       boost::smatch what;
+                       String val = static_cast<String>(value);
+                       std::string::const_iterator begin = val.Begin();
+                       std::string::const_iterator end = val.End();
+
+                       bool ret = boost::regex_search(begin, end, what, expr);
+
+                       //Log(LogDebug, "livestatus", "Attribute filter '" + m_Operand + " " + m_Operator + " " +
+                       //    static_cast<String>(value) + "' " + (ret ? "matches" : "doesn't match") + "." );
 
+                       return ret;
                } else if (m_Operator == "<") {
                        if (value.GetType() == ValueNumber)
                                return (static_cast<double>(value) < Convert::ToDouble(m_Operand));
index 8de8ccfaab745989e88aab97fa8fb86ba78125c8..c311807c6f84e38e4b2e3e361cca81e2ae557a29 100644 (file)
@@ -73,12 +73,14 @@ Requires: boost%{el5_boost_version}-program-options
 Requires: boost%{el5_boost_version}-system
 Requires: boost%{el5_boost_version}-test
 Requires: boost%{el5_boost_version}-thread
+Requires: boost%{el5_boost_version}-regex
 %else
 BuildRequires: boost-devel >= 1.41
 Requires: boost-program-options >= 1.41
 Requires: boost-system >= 1.41
 Requires: boost-test >= 1.41
 Requires: boost-thread >= 1.41
+Requires: boost-regex >= 1.41
 %endif
 %endif
 
@@ -98,6 +100,7 @@ Requires: libboost_program_options%{opensuse_boost_version}
 Requires: libboost_system%{opensuse_boost_version}
 Requires: libboost_test%{opensuse_boost_version}
 Requires: libboost_thread%{opensuse_boost_version}
+Requires: libboost_regex%{opensuse_boost_version}
 %endif
 %endif
 
diff --git a/test/livestatus/queries/log/alerts b/test/livestatus/queries/log/alerts
new file mode 100644 (file)
index 0000000..8c4740a
--- /dev/null
@@ -0,0 +1,6 @@
+GET log
+Columns: host_name service_description time lineno class type options plugin_output state state_type comment contact_name command_name
+Filter: time >= 1348657741
+Filter: message ~ ALERT
+ResponseHeader: fixed16
+