[icinga-checkins] icinga.org: icingaweb2-module-director/feature-ddo-playground: Ddo: handle more event types, more debugging info

git at icinga.org git at icinga.org
Tue Jul 19 10:31:53 CEST 2016


Module: icingaweb2-module-director
Branch: feature-ddo-playground
Commit: e37777d1c15084d43f8991e1f8d6e6863eae41fa
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=e37777d1c15084d43f8991e1f8d6e6863eae41fa

Author: Thomas Gelf <thomas at gelf.net>
Date:   Tue Jun 21 19:10:17 2016 +0200

Ddo: handle more event types, more debugging info

---

 application/clicommands/EventstreamCommand.php |    3 ++
 library/Director/Ddo/StateList.php             |   54 ++++++++++++++++++++++--
 library/Director/Ddo/StateObject.php           |   50 +++++++++++++++++-----
 3 files changed, 93 insertions(+), 14 deletions(-)

diff --git a/application/clicommands/EventstreamCommand.php b/application/clicommands/EventstreamCommand.php
index 5dfc4e5..d9d23dd 100644
--- a/application/clicommands/EventstreamCommand.php
+++ b/application/clicommands/EventstreamCommand.php
@@ -47,6 +47,9 @@ class EventstreamCommand extends DdoCommand
             while ($res = $redis->brpop('icinga2::events', 1)) {
                 // res = array(queuename, value)
                 $object = $list->processCheckResult(json_decode($res[1]));
+                if ($object === false) {
+                    continue;
+                }
 
                 if ($object->hasBeenModified()) {
                     printf("%s has been modified\n", $object->getUniqueName());
diff --git a/library/Director/Ddo/StateList.php b/library/Director/Ddo/StateList.php
index b237eb8..315d860 100644
--- a/library/Director/Ddo/StateList.php
+++ b/library/Director/Ddo/StateList.php
@@ -22,7 +22,38 @@ class StateList
 
     public function processCheckResult($result)
     {
-        list($host, $service) = $this->getHostServiceFromResult($result);
+        $type = $result->type;
+        $types = array(
+            'CheckResult'            => 'check_result',
+            'StateChange'            => 'check_result',
+            'Notification',
+            'AcknowledgementSet'     => null,
+            'AcknowledgementCleared' => null,
+            'CommentAdded'           => 'comment',
+            'CommentRemoved'         => 'comment',
+            'DowntimeAdded'          => 'downtime',
+            'DowntimeRemoved'        => 'downtime',
+            'DowntimeTriggered'      => 'downtime',
+        );
+
+        if (! array_key_exists($type, $types)) {
+            var_dump($type);
+            var_dump($result);
+            die('Type incomplete');
+        }
+
+        $eventProperty = $types[$type];
+        $eventData = $eventProperty === null ? $result : $result->$eventProperty;
+
+        list($host, $service) = $this->getHostServiceFromResult($result, $eventProperty);
+
+        if ($host === null) {
+            echo "Event has NO HOST\n";
+            var_dump($type);
+            var_dump($result);
+
+            return false;
+        }
 
         $key = self::createKey($host, $service);
 
@@ -34,7 +65,10 @@ class StateList
 
         $this->objects[$key] = $object;
 
-        $object->processCheckResult($result);
+        $method = 'process' . $type;
+        if (method_exists($object, $method)) {
+            $object->$method($eventData, $result->timestamp);
+        }
 
         return $object;
     }
@@ -65,16 +99,28 @@ class StateList
         return array_key_exists($key, $this->objects);
     }
 
-    protected function getHostServiceFromResult($result)
+    protected function getHostServiceFromResult($result, $eventProperty)
     {
-        $list = array($result->host);
+        if (property_exists($result, 'host')) {
+            $list = array($result->host);
+        } elseif (property_exists($result->$eventProperty, 'host_name')) {
+            $list = array($result->$eventProperty->host_name);
+        } else {
+            return array(null, null);
+        }
 
         if (property_exists($result, 'service')) {
             $list[] = $result->service;
+        } elseif (property_exists($result, 'service_name')) {
+            $list[] = $result->$eventProperty->service_name;
         } else {
             $list[] = null;
         }
 
+        if (count($list) === 2 && $list[1] === '') {
+            $list[1] = null;
+        }
+
         return $list;
     }
 
diff --git a/library/Director/Ddo/StateObject.php b/library/Director/Ddo/StateObject.php
index caf7edc..4f3334f 100644
--- a/library/Director/Ddo/StateObject.php
+++ b/library/Director/Ddo/StateObject.php
@@ -40,22 +40,22 @@ abstract class StateObject extends DdoObject
         'hard',
     );
 
-    public function processCheckResult($result)
+    public function processCheckResult($result, $timestamp)
     {
-        $checkResult = $result->check_result;
-        $vars = $checkResult->vars_after;
+        $vars = $result->vars_after;
 
-        $currentState = (int) $checkResult->state;
+        $currentState = (int) $result->state;
 
         if ($this->state === null || $currentState !== (int) $this->state) {
-            $this->last_state_change = $result->timestamp;
+            $this->last_state_change = $timestamp;
         }
 
-        $this->state      = $currentState;
-        $this->state_type = $vars->state_type;
-        $this->problem    = $currentState > 0;
-        $this->reachable  = $vars->reachable;
-        $this->attempt    = $vars->attempt;
+        $this->state        = $currentState;
+        $this->state_type   = $vars->state_type;
+        $this->problem      = $currentState > 0;
+        $this->reachable    = $vars->reachable;
+        $this->attempt      = $vars->attempt;
+        $this->check_source_checksum = sha1($result->check_source, true);
 
         // TODO: Handle those
         $this->acknowledged = false;
@@ -68,6 +68,36 @@ abstract class StateObject extends DdoObject
         }
     }
 
+    public function processDowntimeAdded($result, $timestamp)
+    {
+        echo "Got downtime\n";
+        print_r($result);
+    }
+
+    public function processDowntimeRemoved($result, $timestamp)
+    {
+        echo "Remove downtime\n";
+        print_r($result);
+    }
+
+    public function processDowntimeTriggered($result, $timestamp)
+    {
+        echo "Triggered downtime\n";
+        print_r($result);
+    }
+
+    public function processAcknowledgementSet($result, $timestamp)
+    {
+        $this->acknowledged = true;
+        $this->severity   = $this->calculateSeverity();
+    }
+
+    public function processAcknowledgementCleared($result, $timestamp)
+    {
+        $this->acknowledged = false;
+        $this->severity   = $this->calculateSeverity();
+    }
+
     public function setState_type($type)
     {
         if (ctype_digit((string) $type)) {



More information about the icinga-checkins mailing list