[icinga-checkins] icinga.org: icingaweb2-module-director/master: CustomVariableCache: optionally use digest()...

git at icinga.org git at icinga.org
Mon Oct 10 12:00:29 CEST 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Sun Oct  9 12:43:04 2016 +0000

CustomVariableCache: optionally use digest()...

...for PostgreSQL in case pgcrypto is installed. Also add related hint to the
installation documentation

---

 doc/02-Installation.md                            |    6 +++++-
 library/Director/Db.php                           |   11 +++++++++++
 library/Director/Db/Cache/CustomVariableCache.php |   10 +++++++++-
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/doc/02-Installation.md b/doc/02-Installation.md
index d7a5f22..a358a14 100644
--- a/doc/02-Installation.md
+++ b/doc/02-Installation.md
@@ -35,8 +35,12 @@ command.
 
     psql -q -c "CREATE DATABASE director WITH ENCODING 'UTF8';"
     psql director -q -c "CREATE USER director WITH PASSWORD 'some-password';
-    GRANT ALL PRIVILEGES ON DATABASE director TO director;"
+    GRANT ALL PRIVILEGES ON DATABASE director TO director;
+    CREATE EXTENSION pgcrypto;"
 
+Hint: pgcrypto helps to boost performance, but is currently optional. In case you
+do not have it available on your platform and/or do not know how to solve this
+just leave away the 'CREATE EXTENSION' part.
 
 Web-based Configuration
 -----------------------
diff --git a/library/Director/Db.php b/library/Director/Db.php
index c8aa9cf..abfc7cf 100644
--- a/library/Director/Db.php
+++ b/library/Director/Db.php
@@ -632,6 +632,17 @@ class Db extends DbConnection
         return $this->getDbType() === 'pgsql';
     }
 
+    public function hasPgExtension($name)
+    {
+        $db = $this->db();
+        $query = $db->select()->from(
+            array('e' => 'pg_extension'),
+            array('cnt' => 'COUNT(*)')
+        )->where('extname = ?', $name);
+
+        return (int) $db->fetchOne($query) === 1;
+    }
+
     public function dbHexFunc($column)
     {
         if ($this->isPgsql()) {
diff --git a/library/Director/Db/Cache/CustomVariableCache.php b/library/Director/Db/Cache/CustomVariableCache.php
index c430b47..517a1bf 100644
--- a/library/Director/Db/Cache/CustomVariableCache.php
+++ b/library/Director/Db/Cache/CustomVariableCache.php
@@ -27,7 +27,11 @@ class CustomVariableCache
             'checksum' => '(NULL)',
         );
 
-        if (! $connection->isPgsql()) {
+        if ($connection->isPgsql()) {
+            if ($connection->hasPgExtension('pgcrypto')) {
+                $columns['checksum'] = "DIGEST(v.varvalue || ';' || v.format, 'sha1')";
+            }
+        } else {
             $columns['checksum'] = "UNHEX(SHA1(v.varvalue || ';' || v.format))";
         }
 
@@ -41,6 +45,10 @@ class CustomVariableCache
             $id = $row->id;
             unset($row->id);
 
+            if (is_resource($row->checksum)) {
+                $row->checksum = stream_get_contents($row->checksum);
+            }
+
             if (array_key_exists($id, $this->rowsById)) {
                 $this->rowsById[$id][] = $row;
             } else {



More information about the icinga-checkins mailing list