[icinga-checkins] icinga.org: icingaweb2-module-logstash/master: Better config and elasticsearch URL handling

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


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

Author: Markus Frosch <markus at lazyfrosch.de>
Date:   Fri Jun 19 10:00:40 2015 +0200

Better config and elasticsearch URL handling

---

 application/controllers/EventController.php  |   21 +++------------------
 application/views/scripts/error-config.phtml |   13 +++++++++++++
 application/views/scripts/event/search.phtml |   17 -----------------
 library/Logstash/Controller.php              |   25 +++++++++++++++++++++++++
 library/Logstash/Search.php                  |   11 +++++------
 5 files changed, 46 insertions(+), 41 deletions(-)

diff --git a/application/controllers/EventController.php b/application/controllers/EventController.php
index 14f43cd..3fd57a2 100644
--- a/application/controllers/EventController.php
+++ b/application/controllers/EventController.php
@@ -1,12 +1,12 @@
 <?php
 
 use Icinga\Module\Logstash\Search;
-use Icinga\Web\Controller\ModuleActionController;
+use Icinga\Module\Logstash\Controller;
 
 use Icinga\Web\Widget\Limiter;
 use Icinga\Web\Widget\Paginator;
 
-class Logstash_EventController extends ModuleActionController
+class Logstash_EventController extends Controller
 {
     public function init()
     {
@@ -45,21 +45,7 @@ class Logstash_EventController extends ModuleActionController
             $this->view->detaillist = count($split) > 1 ? preg_split('/\s*[,]\s*/', $split[1]) : [];
         }
 
-        $this->view->configFile = $this->Config()->getConfigFile();
-        $hostname = $this->Config()->get('elk', 'hostname');
-        $protocol = $this->Config()->get('elk', 'protocol', 'http');
-        $port = (int) $this->Config()->get('elk', 'port', 9200);
-        $index_pattern = $this->Config()->get('elk', 'index_pattern', 'logstash-*');
-
-        $url = sprintf(
-            '%s://%s:%d/%s/',
-            $protocol,
-            $hostname,
-            $port,
-            $index_pattern
-        );
-
-        $search = new Search($url);
+        $search = new Search($this->elasticsearch_url);
         if ($this->view->query) {
             $search->setQueryString($this->view->query);
 
@@ -85,6 +71,5 @@ class Logstash_EventController extends ModuleActionController
 
             $this->view->search = $search;
         }
-        $this->view->base_url = $url;
     }
 }
diff --git a/application/views/scripts/error-config.phtml b/application/views/scripts/error-config.phtml
new file mode 100644
index 0000000..62d4540
--- /dev/null
+++ b/application/views/scripts/error-config.phtml
@@ -0,0 +1,13 @@
+<div class="content">
+
+    <p>There is no valid config in <?= $this->escape($this->configFile) ?>.</p>
+
+    <p>Here is an example:</p>
+
+    <pre>
+    [elasticsearch]
+    url = http://elasticsearch.example.org:9200
+    index_pattern = logstash-*
+    </pre>
+
+</div>
diff --git a/application/views/scripts/event/search.phtml b/application/views/scripts/event/search.phtml
index e03b2a3..2732679 100644
--- a/application/views/scripts/event/search.phtml
+++ b/application/views/scripts/event/search.phtml
@@ -5,23 +5,6 @@
     <?= $this->paginator; ?>
 </div>
 <?php endif ?>
-    <?php /* TODO: move - if (! $this->base_url): ?>
-    There is no valid config in <?= $this->escape($this->configFile) ?>. You need at least a hostname definition:
-    <pre>
-    [elk]
-    hostname = elk.example.com
-    </pre>
-
-    A full blown configuration could look like this:
-    <pre>
-    [elk]
-    hostname      = elk.example.com
-    port          = 9200
-    protocol      = http
-    index_pattern = "logstash-*"
-    </pre>
-    </div>
-    <?php return; endif */ ?>
 <div class="filter">
     <form action="<?= $this->url()->without('query')->without('filter')->without('fields') ?>" class="inline" method="get">
         <label for="query">Query:</label><input type="text" name="query" value="<?= $this->escape($this->query) ?>" /><br/>
diff --git a/library/Logstash/Controller.php b/library/Logstash/Controller.php
new file mode 100644
index 0000000..b131653
--- /dev/null
+++ b/library/Logstash/Controller.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Icinga\Module\Logstash;
+
+use Icinga\Web\Controller as IcingaWebController;
+use Exception;
+
+class Controller extends IcingaWebController
+{
+    protected $elasticsearch_url;
+
+    public function moduleInit() {
+        $this->elasticsearch_url = $this->Config()->get('elasticsearch', 'url');
+        if ($index_pattern = $this->Config()->get('elasticsearch', 'index_pattern', null)) {
+            $this->elasticsearch_url .= "/".$index_pattern;
+        }
+
+        if (!$this->elasticsearch_url) {
+            $this->view->configFile = $this->Config()->getConfigFile();
+            $this->render('error-config', null, true);
+            throw new Exception("No elasticsearch URL configured!");
+        }
+    }
+
+}
diff --git a/library/Logstash/Search.php b/library/Logstash/Search.php
index 38bcdaf..0ad4e1d 100644
--- a/library/Logstash/Search.php
+++ b/library/Logstash/Search.php
@@ -25,9 +25,6 @@ class Search implements QueryInterface
     protected $size;
     protected $from;
 
-
-    protected $elasticsearch;
-
     public $took;
     public $hits;
     public $total;
@@ -38,13 +35,16 @@ class Search implements QueryInterface
         $this->curl = new Curl();
 
         if ($elasticsearch) {
-            $this->elasticsearch = $elasticsearch;
             $this->curl->setBaseURL($elasticsearch);
         }
     }
 
     public function search()
     {
+        if (!$this->getElasticsearch()) {
+            throw new Exception("Elasticsearch URL has not be configured!");
+        }
+
         $post = array(
             'query' => $this->query
         );
@@ -150,7 +150,7 @@ class Search implements QueryInterface
      */
     public function getElasticsearch()
     {
-        return $this->elasticsearch;
+        return $this->curl->getBaseURL();
     }
 
     /**
@@ -158,7 +158,6 @@ class Search implements QueryInterface
      */
     public function setElasticsearch($elasticsearch)
     {
-        $this->elasticsearch = $elasticsearch;
         $this->curl->setBaseURL($elasticsearch);
     }
 



More information about the icinga-checkins mailing list