[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaConfig: work with new database fields

git at icinga.org git at icinga.org
Sat Feb 27 22:15:26 CET 2016


Module: icingaweb2-module-director
Branch: master
Commit: 0a63c6bde85d3b3a84f709affb1b4340b9abc47e
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=0a63c6bde85d3b3a84f709affb1b4340b9abc47e

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sat Feb 27 21:06:22 2016 +0100

IcingaConfig: work with new database fields

This basically helps to detect whether someone travelled back in
time by manually reverting single checks. Hard to explain in a few
lines

---

 library/Director/IcingaConfig/IcingaConfig.php |   54 +++++++++++++++---------
 1 file changed, 33 insertions(+), 21 deletions(-)

diff --git a/library/Director/IcingaConfig/IcingaConfig.php b/library/Director/IcingaConfig/IcingaConfig.php
index 83a07b5..9383be3 100644
--- a/library/Director/IcingaConfig/IcingaConfig.php
+++ b/library/Director/IcingaConfig/IcingaConfig.php
@@ -144,17 +144,35 @@ class IcingaConfig
         $this->generateFromDb();
         $this->collectExtraFiles();
         $checksum = $this->calculateChecksum();
-        $exists = $this->db->fetchOne(
-            $this->db->select()->from(
-                self::$table,
-                'COUNT(*)'
-            )->where(
-                'checksum = ?',
-                $this->dbBin($checksum)
+        $activity = $this->getLastActivityChecksum();
+
+        $lastActivity = $this->binFromDb(
+            $this->db->fetchOne(
+                $this->db->select()->from(
+                    self::$table,
+                    'last_activity_checksum'
+                )->where(
+                    'checksum = ?',
+                    $this->dbBin($checksum)
+                )
             )
         );
 
-        return (int) $exists === 0;
+        if ($lastActivity === false || $lastActivity === null) {
+            return true;
+        }
+
+        if ($lastActivity !== $activity) {
+            $this->db->update(
+                self::$table,
+                array(
+                    'last_activity_checksum' => $this->dbBin($activity)
+                ),
+                $this->db->quoteInto('checksum = ?', $this->dbBin($checksum))
+            );
+        }
+
+        return false;
     }
 
     protected function storeIfModified()
@@ -262,12 +280,14 @@ class IcingaConfig
                 );
             }
 
+            $activity = $this->dbBin($this->getLastActivityChecksum());
             $this->db->insert(
                 self::$table,
                 array(
-                    'duration'               => $this->generationTime,
-                    'last_activity_checksum' => $this->dbBin($this->getLastActivityChecksum()),
-                    'checksum'               => $this->dbBin($this->getChecksum()),
+                    'duration'                => $this->generationTime,
+                    'first_activity_checksum' => $activity,
+                    'last_activity_checksum'  => $activity,
+                    'checksum'                => $this->dbBin($this->getChecksum()),
                 )
             );
             /** @var IcingaConfigFile $file */
@@ -338,17 +358,9 @@ class IcingaConfig
             throw new Exception(sprintf('Got no config for %s', Util::binary2hex($checksum)));
         }
 
-        $this->checksum = $result->checksum;
+        $this->checksum = $this->binFromDb($result->checksum);
         $this->duration = $result->duration;
-        $this->lastActivityChecksum = $result->last_activity_checksum;
-
-        if (is_resource($this->checksum)) {
-            $this->checksum = stream_get_contents($this->checksum);
-        }
-
-        if (is_resource($this->lastActivityChecksum)) {
-            $this->lastActivityChecksum = stream_get_contents($this->lastActivityChecksum);
-        }
+        $this->lastActivityChecksum = $this->binFromDb($result->last_activity_checksum);
 
         $query = $this->db->select()->from(
             array('cf' => 'director_generated_config_file'),



More information about the icinga-checkins mailing list