[icinga-checkins] icinga.org: icingaweb2-module-logstash/master: Allow filtering by the Icinga queries

git at icinga.org git at icinga.org
Fri Feb 5 14:05:49 CET 2016


Module: icingaweb2-module-logstash
Branch: master
Commit: 2bd5afbfc2dbef2afddfc246736e4ae357d3a92b
URL:    https://git.icinga.org/?p=icingaweb2-module-logstash.git;a=commit;h=2bd5afbfc2dbef2afddfc246736e4ae357d3a92b

Author: Markus Frosch <markus at lazyfrosch.de>
Date:   Tue Jun 23 18:10:34 2015 +0200

Allow filtering by the Icinga queries

---

 library/Logstash/Helpers/QueryString.php |   14 +++++++--
 library/Logstash/IcingaStatus.php        |   16 ++++++++++
 library/Logstash/Search.php              |   48 ++++++++++++++++++++++++++++--
 3 files changed, 73 insertions(+), 5 deletions(-)

diff --git a/library/Logstash/Helpers/QueryString.php b/library/Logstash/Helpers/QueryString.php
index 36a5924..bfdb8ad 100644
--- a/library/Logstash/Helpers/QueryString.php
+++ b/library/Logstash/Helpers/QueryString.php
@@ -12,6 +12,8 @@ class QueryString {
     protected $parsed = false;
     protected $matchedTree = array();
 
+    protected $query_string;
+
     protected function debug($value, $op) {
         $this->debug[] = array(
             'value' => $value,
@@ -137,10 +139,10 @@ class QueryString {
     public function parse($query_string, $default_operator = 'OR') {
         $tree = array();
 
-        $query_string = trim($query_string);
+        $this->query_string = trim($query_string);
 
         // split by parts
-        $parts = preg_split("#([:\s\(\)\"/])#", $query_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
+        $parts = preg_split("#([:\s\(\)\"/])#", $this->query_string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
 
         $this->tree = $this->parse_parts($parts, $default_operator);
         $this->parsed = true;
@@ -258,4 +260,12 @@ class QueryString {
         return $this->matchedTree;
     }
 
+    /**
+     * @return mixed
+     */
+    public function getQueryString()
+    {
+        return $this->query_string;
+    }
+
 }
\ No newline at end of file
diff --git a/library/Logstash/IcingaStatus.php b/library/Logstash/IcingaStatus.php
index 53aecc6..47c9e9b 100644
--- a/library/Logstash/IcingaStatus.php
+++ b/library/Logstash/IcingaStatus.php
@@ -55,6 +55,22 @@ trait IcingaStatus
     }
 
     /**
+     * @return QueryString
+     */
+    public function getIcingaWarningQuery()
+    {
+        return $this->icinga_warning_query;
+    }
+
+    /**
+     * @return QueryString
+     */
+    public function getIcingaCriticalQuery()
+    {
+        return $this->icinga_critical_query;
+    }
+
+    /**
      * @param Array $document
      */
     protected function evalIcingaStatus(Array &$document)
diff --git a/library/Logstash/Search.php b/library/Logstash/Search.php
index d77c465..0842c72 100644
--- a/library/Logstash/Search.php
+++ b/library/Logstash/Search.php
@@ -21,6 +21,8 @@ class Search extends ElasticsearchBackend implements QueryInterface
     protected $fields = array();
 
     protected $without_ack = false;
+    protected $filtered_by_icinga_queries = false;
+    protected $filter_icinga;
 
     protected $sort_field;
     protected $sort_direction;
@@ -55,6 +57,9 @@ class Search extends ElasticsearchBackend implements QueryInterface
         if ($this->without_ack === true)
             $post['filter']['and'][] = $this->buildFilterQueryString('NOT icinga_acknowledge:1');
 
+        if ($this->filter_icinga)
+            $post['filter']['and'][] = $this->filter_icinga;
+
         if ($this->sort_field) {
             $post['sort'] = array(
                 array(
@@ -150,12 +155,14 @@ class Search extends ElasticsearchBackend implements QueryInterface
         $this->filter = array();
     }
 
-    /* TODO old
-    public function addFilter($object) {
+    /**
+     * add custom Elasticsearch filter -- beware!
+     * @param Array $object
+     */
+    public function addFilterCustom($object) {
         if (!$this->filter) $this->filter = array();
         $this->filter[] = $object;
     }
-    */
 
     public function addFilterTimeRange($from, $to, $field='@timestamp') {
         $this->addFilter(array(
@@ -413,4 +420,39 @@ class Search extends ElasticsearchBackend implements QueryInterface
     {
         $this->without_ack = $without_ack;
     }
+
+    /**
+     * @return boolean
+     */
+    public function isFilteredByIcingaQueries()
+    {
+        return $this->filtered_by_icinga_queries;
+    }
+
+    /**
+     * @param boolean $filtered_by_icinga_queries
+     */
+    public function setFilteredByIcingaQueries($filtered_by_icinga_queries)
+    {
+        $this->filtered_by_icinga_queries = $filtered_by_icinga_queries;
+
+        if ($filtered_by_icinga_queries) {
+            $filter = array();
+            if ($qs = $this->getIcingaCriticalQuery()->getQueryString()) {
+                $filter[] = $this->buildFilterQueryString($qs, 'or');
+            }
+            if ($qs = $this->getIcingaCriticalQuery()->getQueryString()) {
+                $filter[] = $this->buildFilterQueryString($qs, 'or');
+            }
+            if (count($filter) == 0)
+                throw new Exception("There are no Icinga queries to be filtered with!");
+
+            $this->filter_icinga = array('or' => $filter);
+        }
+        else {
+            $this->filter_icinga = null;
+        }
+
+
+    }
 }



More information about the icinga-checkins mailing list