From e56056e95038e4d1fcc9c9e4ae7a76feae8588ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20R=C3=BCll?= Date: Tue, 28 Jan 2014 09:58:07 +0100 Subject: [PATCH 1/3] makro resolver add view helper "this->makroresolver()" to replace makros. refs #4490 --- .../views/helpers/MakroResolver.php | 110 ++++++++++++++++++ .../views/scripts/list/hosts.phtml | 4 + 2 files changed, 114 insertions(+) create mode 100644 modules/monitoring/application/views/helpers/MakroResolver.php diff --git a/modules/monitoring/application/views/helpers/MakroResolver.php b/modules/monitoring/application/views/helpers/MakroResolver.php new file mode 100644 index 000000000..238f1587c --- /dev/null +++ b/modules/monitoring/application/views/helpers/MakroResolver.php @@ -0,0 +1,110 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + * + */ +// {{{ICINGA_LICENSE_HEADER}}} + +class Zend_View_Helper_MakroResolver extends Zend_View_Helper_Abstract +{ + + private $mapping = array ( + 'HOSTADDRESS' => 'host_address', + 'HOSTNAME' => 'host_name', + 'SERVICEDESC' => 'service_description' + ); + + private $costumPrefix = array ( + '_HOST' => 'host_', + '_Service' => 'service_', + '_CONTACT' => 'contact_' + ); + + private $object; + private $escape; + + public function makroResolver($string, $object, $escape = true) + { + $this->object = $object; + $this->object->host_macaddress = "123.123.123.123"; + $this->escape = $escape; + $values = explode('$', $string); + + foreach ($values as $value){ + $resolved[] = $this->replace($value); + } + $string = implode('', $resolved); + + die(var_dump($object)); + + return $string."
"; + } + + + private function replace($piece) + { + if($piece == 'COSTUM'){ + $piece = $this->mapping["$piece"]; + } + if(array_key_exists($piece, $this->mapping)) + { + $var = $this->mapping["$piece"]; + $piece = $this->object->$var; + } else { + $piece = $this->checkCostumVars($piece); + } + ($this->escape) ? $piece = $this->escapeString($piece) : '' ; + + + //echo $this->object; + return $piece; + } + + private function checkCostumVars($value) + { + if($value != '') { + foreach ($this->costumPrefix as $prefix => $val) { + if(strstr($value, $prefix)){ + $costumVar = $val.strtolower(str_replace($prefix, '', $value)); + if(array_key_exists($costumVar, $this->object)) { + $value = $this->object->$costumVar; + } + } + } + } + + return $value; + } + + private function escapeString($string) + { + return htmlspecialchars($string); + } +} + + +// @codingStandardsIgnoreStop \ No newline at end of file diff --git a/modules/monitoring/application/views/scripts/list/hosts.phtml b/modules/monitoring/application/views/scripts/list/hosts.phtml index 2383db43c..91cff3b22 100644 --- a/modules/monitoring/application/views/scripts/list/hosts.phtml +++ b/modules/monitoring/application/views/scripts/list/hosts.phtml @@ -33,7 +33,10 @@ $viewHelper = $this->getHelper('MonitoringState'); + + action_url = 'http://$HOSTNAME$/$HOSTADDRESS$/BANA/$HOSTNAME$/$_HOSTMACADDRESS$' : $host->action_url = "test"; ($i == 1) ? $i++ : $i = $i + 1;?> + util()->getHostStateName($host->host_state)); ?> href('monitoring/show/host', array('host' => $host->host_name)); ?> @@ -119,6 +122,7 @@ $viewHelper = $this->getHelper('MonitoringState');
+ makroResolver($host->action_url, $host, false); ?> host_icon_image) : ?>
From de546858c2dcc9c9c584d82dbbace2cbb444e947 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 30 Jan 2014 16:34:23 +0100 Subject: [PATCH 2/3] Add missing fetch routine for customvars in AbstractObject refs #4490 --- .../views/scripts/list/hosts.phtml | 4 --- .../Backend/Ido/Query/CustomvarQuery.php | 7 ++--- .../Monitoring/Object/AbstractObject.php | 28 ++++++++++++++++--- .../library/Monitoring/Object/Host.php | 4 +-- .../library/Monitoring/Object/Service.php | 4 +-- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/modules/monitoring/application/views/scripts/list/hosts.phtml b/modules/monitoring/application/views/scripts/list/hosts.phtml index 91cff3b22..2383db43c 100644 --- a/modules/monitoring/application/views/scripts/list/hosts.phtml +++ b/modules/monitoring/application/views/scripts/list/hosts.phtml @@ -33,10 +33,7 @@ $viewHelper = $this->getHelper('MonitoringState'); - - action_url = 'http://$HOSTNAME$/$HOSTADDRESS$/BANA/$HOSTNAME$/$_HOSTMACADDRESS$' : $host->action_url = "test"; ($i == 1) ? $i++ : $i = $i + 1;?> - util()->getHostStateName($host->host_state)); ?> href('monitoring/show/host', array('host' => $host->host_name)); ?> @@ -122,7 +119,6 @@ $viewHelper = $this->getHelper('MonitoringState');
- makroResolver($host->action_url, $host, false); ?> host_icon_image) : ?>
diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CustomvarQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CustomvarQuery.php index 365fe52ae..fec118a0b 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CustomvarQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CustomvarQuery.php @@ -4,8 +4,6 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query; class CustomvarQuery extends IdoQuery { - protected $object_id = 'object_id'; - protected $columnMap = array( 'customvars' => array( 'varname' => 'cvs.varname', @@ -24,12 +22,11 @@ class CustomvarQuery extends IdoQuery protected function joinBaseTables() { $this->baseQuery = $this->db->select()->from( - array('cvs' => $this->prefix . 'customvariablestatus'), + array('cvs' => $this->prefix . 'customvariables'), array() )->join( array('cvo' => $this->prefix . 'objects'), - 'cvs.object_id = cvo.' . $this->object_id - . ' AND cvo.is_active = 1', + 'cvs.object_id = cvo.object_id AND cvo.is_active = 1', array() ); diff --git a/modules/monitoring/library/Monitoring/Object/AbstractObject.php b/modules/monitoring/library/Monitoring/Object/AbstractObject.php index 1b8d755c6..ea433c2f8 100644 --- a/modules/monitoring/library/Monitoring/Object/AbstractObject.php +++ b/modules/monitoring/library/Monitoring/Object/AbstractObject.php @@ -2,17 +2,14 @@ namespace Icinga\Module\Monitoring\Object; -use Icinga\Data\BaseQuery as Query; -use \Icinga\Module\Monitoring\Backend; use Icinga\Module\Monitoring\DataView\Contact; use Icinga\Module\Monitoring\DataView\Contactgroup; use Icinga\Module\Monitoring\DataView\Downtime; use Icinga\Module\Monitoring\DataView\EventHistory; use Icinga\Module\Monitoring\DataView\Hostgroup; -use Icinga\Module\Monitoring\DataView\HostStatus; use Icinga\Module\Monitoring\DataView\Comment; use Icinga\Module\Monitoring\DataView\Servicegroup; -use Icinga\Module\Monitoring\DataView\ServiceStatus; +use Icinga\Module\Monitoring\DataView\Customvar; use Icinga\Web\Request; abstract class AbstractObject @@ -81,6 +78,29 @@ abstract class AbstractObject return $this; } + public function fetchCustomvars() + { + $query = Customvar::fromRequest( + $this->request, + array( + 'varname', + 'varvalue' + ) + )->getQuery(); + + if ($this->type === self::TYPE_HOST) { + $query->where('host_name', $this->host_name) + ->where('object_type', 'host'); + } else { + $query->where('host_name', $this->host_name) + ->where('object_type', 'service') + ->where('service_description', $this->service_description); + } + + $this->customvars = $query->fetchPairs(); + return $this; + } + public function fetchContacts() { $this->contacts = Contact::fromRequest( diff --git a/modules/monitoring/library/Monitoring/Object/Host.php b/modules/monitoring/library/Monitoring/Object/Host.php index 4e7a5a133..e0067491b 100644 --- a/modules/monitoring/library/Monitoring/Object/Host.php +++ b/modules/monitoring/library/Monitoring/Object/Host.php @@ -2,7 +2,6 @@ namespace Icinga\Module\Monitoring\Object; -use Icinga\Data\BaseQuery as Query; use Icinga\Module\Monitoring\DataView\HostStatus; class Host extends AbstractObject @@ -19,7 +18,8 @@ class Host extends AbstractObject ->fetchDowntimes() ->fetchHostgroups() ->fetchContacts() - ->fetchContactGroups(); + ->fetchContactGroups() + ->fetchCustomvars(); } protected function getProperties() diff --git a/modules/monitoring/library/Monitoring/Object/Service.php b/modules/monitoring/library/Monitoring/Object/Service.php index d566e5260..dcd6e117d 100644 --- a/modules/monitoring/library/Monitoring/Object/Service.php +++ b/modules/monitoring/library/Monitoring/Object/Service.php @@ -2,7 +2,6 @@ namespace Icinga\Module\Monitoring\Object; -use Icinga\Data\BaseQuery as Query; use Icinga\Module\Monitoring\DataView\ServiceStatus; class Service extends AbstractObject @@ -19,7 +18,8 @@ class Service extends AbstractObject ->fetchHostgroups() ->fetchServicegroups() ->fetchContacts() - ->fetchContactGroups(); + ->fetchContactGroups() + ->fetchCustomvars(); } protected function getProperties() From 4aed7468b7de51fe52d99c8d4705459195496019 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 31 Jan 2014 15:54:44 +0100 Subject: [PATCH 3/3] Add view helper to resolve host-/servicemacros as well as customvars refs #4490 --- .../views/helpers/MakroResolver.php | 110 ------------------ .../views/helpers/ResolveMacros.php | 88 ++++++++++++++ .../views/scripts/list/hosts.phtml | 2 +- .../views/scripts/list/services.phtml | 2 +- .../scripts/show/components/status.phtml | 20 +++- .../views/helpers/ResolveMacrosTest.php | 107 +++++++++++++++++ 6 files changed, 215 insertions(+), 114 deletions(-) delete mode 100644 modules/monitoring/application/views/helpers/MakroResolver.php create mode 100644 modules/monitoring/application/views/helpers/ResolveMacros.php create mode 100644 modules/monitoring/test/php/application/views/helpers/ResolveMacrosTest.php diff --git a/modules/monitoring/application/views/helpers/MakroResolver.php b/modules/monitoring/application/views/helpers/MakroResolver.php deleted file mode 100644 index 238f1587c..000000000 --- a/modules/monitoring/application/views/helpers/MakroResolver.php +++ /dev/null @@ -1,110 +0,0 @@ - - * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 - * @author Icinga Development Team - * - */ -// {{{ICINGA_LICENSE_HEADER}}} - -class Zend_View_Helper_MakroResolver extends Zend_View_Helper_Abstract -{ - - private $mapping = array ( - 'HOSTADDRESS' => 'host_address', - 'HOSTNAME' => 'host_name', - 'SERVICEDESC' => 'service_description' - ); - - private $costumPrefix = array ( - '_HOST' => 'host_', - '_Service' => 'service_', - '_CONTACT' => 'contact_' - ); - - private $object; - private $escape; - - public function makroResolver($string, $object, $escape = true) - { - $this->object = $object; - $this->object->host_macaddress = "123.123.123.123"; - $this->escape = $escape; - $values = explode('$', $string); - - foreach ($values as $value){ - $resolved[] = $this->replace($value); - } - $string = implode('', $resolved); - - die(var_dump($object)); - - return $string."
"; - } - - - private function replace($piece) - { - if($piece == 'COSTUM'){ - $piece = $this->mapping["$piece"]; - } - if(array_key_exists($piece, $this->mapping)) - { - $var = $this->mapping["$piece"]; - $piece = $this->object->$var; - } else { - $piece = $this->checkCostumVars($piece); - } - ($this->escape) ? $piece = $this->escapeString($piece) : '' ; - - - //echo $this->object; - return $piece; - } - - private function checkCostumVars($value) - { - if($value != '') { - foreach ($this->costumPrefix as $prefix => $val) { - if(strstr($value, $prefix)){ - $costumVar = $val.strtolower(str_replace($prefix, '', $value)); - if(array_key_exists($costumVar, $this->object)) { - $value = $this->object->$costumVar; - } - } - } - } - - return $value; - } - - private function escapeString($string) - { - return htmlspecialchars($string); - } -} - - -// @codingStandardsIgnoreStop \ No newline at end of file diff --git a/modules/monitoring/application/views/helpers/ResolveMacros.php b/modules/monitoring/application/views/helpers/ResolveMacros.php new file mode 100644 index 000000000..e88a9e918 --- /dev/null +++ b/modules/monitoring/application/views/helpers/ResolveMacros.php @@ -0,0 +1,88 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + * + */ +// {{{ICINGA_LICENSE_HEADER}}} + +use \Zend_View_Helper_Abstract; +use Icinga\Module\Monitoring\Object\AbstractObject; + +class Zend_View_Helper_ResolveMacros extends Zend_View_Helper_Abstract +{ + /** + * Known icinga macros + * + * @var array + */ + private $icingaMacros = array( + 'HOSTNAME' => 'host_name', + 'HOSTADDRESS' => 'host_address', + 'SERVICEDESC' => 'service_description' + ); + + /** + * Return the given string with macros being resolved + * + * @param string $input The string in which to look for macros + * @param AbstractObject|stdClass $object The host or service used to resolve macros + * + * @return string The substituted or unchanged string + */ + public function resolveMacros($input, $object) + { + $matches = array(); + if (preg_match_all('@\$([^\$\s]+)\$@', $input, $matches)) { + foreach ($matches[1] as $key => $value) { + $newValue = $this->resolveMacro($value, $object); + if ($newValue !== $value) { + $input = str_replace($matches[0][$key], $newValue, $input); + } + } + } + + return $input; + } + + /** + * Resolve a macro based on the given object + * + * @param string $macro The macro to resolve + * @param AbstractObject|stdClass $object The object used to resolve the macro + * + * @return string The new value or the macro if it cannot be resolved + */ + public function resolveMacro($macro, $object) + { + if (array_key_exists($macro, $this->icingaMacros) && $object->{$this->icingaMacros[$macro]} !== false) { + return $object->{$this->icingaMacros[$macro]}; + } + if (array_key_exists($macro, $object->customvars)) { + return $object->customvars[$macro]; + } + + return $macro; + } +} diff --git a/modules/monitoring/application/views/scripts/list/hosts.phtml b/modules/monitoring/application/views/scripts/list/hosts.phtml index 2383db43c..98ea8d555 100644 --- a/modules/monitoring/application/views/scripts/list/hosts.phtml +++ b/modules/monitoring/application/views/scripts/list/hosts.phtml @@ -121,7 +121,7 @@ $viewHelper = $this->getHelper('MonitoringState');
host_icon_image) : ?>
- +
diff --git a/modules/monitoring/application/views/scripts/list/services.phtml b/modules/monitoring/application/views/scripts/list/services.phtml index f1c285b6f..9fcf8019b 100644 --- a/modules/monitoring/application/views/scripts/list/services.phtml +++ b/modules/monitoring/application/views/scripts/list/services.phtml @@ -121,7 +121,7 @@ $viewHelper = $this->getHelper('MonitoringState');
service_icon_image): ?>
- +
service_display_name; ?> diff --git a/modules/monitoring/application/views/scripts/show/components/status.phtml b/modules/monitoring/application/views/scripts/show/components/status.phtml index 75307ddd4..65d3823e9 100644 --- a/modules/monitoring/application/views/scripts/show/components/status.phtml +++ b/modules/monitoring/application/views/scripts/show/components/status.phtml @@ -59,10 +59,26 @@ action_url || $object->notes_url): ?>
notes_url): ?> - Notes + notes_url, "' ") === false): ?> + Notes + + notes_url) as $url): ?> + + + Notes + + action_url): ?> - Action + action_url, "' ") === false): ?> + Action + + action_url) as $url): ?> + + + Action + + diff --git a/modules/monitoring/test/php/application/views/helpers/ResolveMacrosTest.php b/modules/monitoring/test/php/application/views/helpers/ResolveMacrosTest.php new file mode 100644 index 000000000..f52202671 --- /dev/null +++ b/modules/monitoring/test/php/application/views/helpers/ResolveMacrosTest.php @@ -0,0 +1,107 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + * + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Test\Modules\Monitoring\Application\Views\Helpers; + +require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php'); + +use Icinga\Test\BaseTestCase; + +require_once 'Zend/View/Helper/Abstract.php'; +require_once BaseTestCase::$moduleDir . '/monitoring/application/views/helpers/ResolveMacros.php'; + +use \stdClass; + +class ResolveMacrosTest extends BaseTestCase +{ + public function testHostMacros() + { + $hostMock = new stdClass(); + $hostMock->host_name = 'test'; + $hostMock->host_address = '1.1.1.1'; + + $helper = new \Zend_View_Helper_ResolveMacros(); + $this->assertEquals($helper->resolveMacros('$HOSTNAME$', $hostMock), $hostMock->host_name); + $this->assertEquals($helper->resolveMacros('$HOSTADDRESS$', $hostMock), $hostMock->host_address); + } + + public function testServiceMacros() + { + $svcMock = new stdClass(); + $svcMock->host_name = 'test'; + $svcMock->host_address = '1.1.1.1'; + $svcMock->service_description = 'a service'; + + $helper = new \Zend_View_Helper_ResolveMacros(); + $this->assertEquals($helper->resolveMacros('$HOSTNAME$', $svcMock), $svcMock->host_name); + $this->assertEquals($helper->resolveMacros('$HOSTADDRESS$', $svcMock), $svcMock->host_address); + $this->assertEquals($helper->resolveMacros('$SERVICEDESC$', $svcMock), $svcMock->service_description); + } + + public function testCustomvars() + { + $objectMock = new stdClass(); + $objectMock->customvars = array( + 'CUSTOMVAR' => 'test' + ); + + $helper = new \Zend_View_Helper_ResolveMacros(); + $this->assertEquals($helper->resolveMacros('$CUSTOMVAR$', $objectMock), $objectMock->customvars['CUSTOMVAR']); + } + + public function testFaultyMacros() + { + $hostMock = new \stdClass(); + $hostMock->host_name = 'test'; + $hostMock->customvars = array( + 'HOST' => 'te', + 'NAME' => 'st' + ); + + $helper = new \Zend_View_Helper_ResolveMacros(); + $this->assertEquals( + $helper->resolveMacros('$$HOSTNAME$ $ HOSTNAME$ $HOST$NAME$', $hostMock), + '$test $ HOSTNAME$ teNAME$' + ); + } + + public function testMacrosWithSpecialCharacters() + { + $objectMock = new \stdClass(); + $objectMock->customvars = array( + 'V€RY_SP3C|@L' => 'not too special!' + ); + + $helper = new \Zend_View_Helper_ResolveMacros(); + $this->assertEquals( + $helper->resolveMacros('$V€RY_SP3C|@L$', $objectMock), + $objectMock->customvars['V€RY_SP3C|@L'] + ); + } +}