[icinga-checkins] icinga.org: icingaweb2-module-director/master: IcingaConfig: add magic apply for rule

git at icinga.org git at icinga.org
Wed Jun 15 16:56:07 CEST 2016


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

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Jun 15 16:55:19 2016 +0200

IcingaConfig: add magic apply for rule

Hint: this is highly experimental, currently hidden and has no influence
on existing configs

---

 library/Director/IcingaConfig/IcingaConfig.php |   75 +++++++++++++++++++++---
 1 file changed, 66 insertions(+), 9 deletions(-)

diff --git a/library/Director/IcingaConfig/IcingaConfig.php b/library/Director/IcingaConfig/IcingaConfig.php
index 535c29c..be31ab2 100644
--- a/library/Director/IcingaConfig/IcingaConfig.php
+++ b/library/Director/IcingaConfig/IcingaConfig.php
@@ -356,16 +356,8 @@ class IcingaConfig
             );
         }
 
-        $this->configFile(
-            sprintf(
-                'zones.d/%s/001-director-basics',
-                $this->connection->getDefaultGlobalZoneName()
-            )
-        )->prepend(
-            "\nconst DirectorStageDir = dirname(dirname(current_filename))\n"
-        );
-
         $this
+            ->prepareGlobalBasics()
             ->createFileFromDb('zone')
             ->createFileFromDb('endpoint')
             ->createFileFromDb('command')
@@ -387,6 +379,71 @@ class IcingaConfig
         return $this;
     }
 
+    protected function prepareGlobalBasics()
+    {
+        $this->configFile(
+            sprintf(
+                'zones.d/%s/001-director-basics',
+                $this->connection->getDefaultGlobalZoneName()
+            )
+        )->prepend(
+            "\nconst DirectorStageDir = dirname(dirname(current_filename))\n"
+            . $this->renderMagicApplyFor()
+        );
+
+        return $this;
+    }
+
+    protected function renderMagicApplyFor()
+    {
+        if (! $this->usesMagicApplyFor()) {
+            return '';
+        }
+
+        return sprintf(
+            '
+apply Service for (title => params in host.vars["%s"]) {
+  if (typeof(params["imports"]) in [Array, String]) {
+    import params["imports"]
+  } else {
+    import title
+  }
+
+  if (typeof(params["vars"]) == Dictionary) {
+    vars += params
+  }
+
+  if (typeof(params["host_name"]) == String) {
+    host_name = params["host_name"]
+  }
+}
+',
+            $this->getMagicApplyVarName()
+        );
+    }
+
+    protected function getMagicApplyVarName()
+    {
+        return $this->connection->getSetting(
+            'magic_apply_for',
+            '_director_apply_for'
+        );
+    }
+
+    protected function usesMagicApplyFor()
+    {
+        $db = $this->db;
+        $query = $db->select()->from(
+            array('hv' => 'icinga_host_var'),
+            array('c' => 'COUNT(*)')
+        )->where(
+            'hv.varname = ?',
+            $this->getMagicApplyVarName()
+        );
+
+        return $db->fetchOne($query);
+    }
+
     protected function loadFromDb($checksum)
     {
         $query = $this->db->select()->from(



More information about the icinga-checkins mailing list