From df58ea0e69dc07dfe44c89627f7165ecd0b165bf Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 15 Jun 2016 16:55:19 +0200 Subject: [PATCH] IcingaConfig: add magic apply for rule Hint: this is highly experimental, currently hidden and has no influence on existing configs --- .../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 535c29c2..be31ab29 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(