Add view helper to resolve host-/servicemacros as well as customvars

refs #4490
This commit is contained in:
Johannes Meyer 2014-01-31 15:54:44 +01:00
parent de546858c2
commit 4aed7468b7
6 changed files with 215 additions and 114 deletions

View File

@ -1,110 +0,0 @@
<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{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."<br>";
}
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

View File

@ -0,0 +1,88 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2014 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2014 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{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;
}
}

View File

@ -121,7 +121,7 @@ $viewHelper = $this->getHelper('MonitoringState');
<td>
<?php if ($host->host_icon_image) : ?>
<div class="pull-left" style="margin-right: 2px;">
<i class="inline-image" style="background-image: url(<?= $host->host_icon_image; ?>);"></i>
<i class="inline-image" style="background-image: url(<?= $this->escape($this->resolveMacros($host->host_icon_image, $host)); ?>);"></i>
</div>
<?php endif; ?>
<a href="<?= $this->href('monitoring/list/services', array('host' => $host->host_name)) ?>">

View File

@ -121,7 +121,7 @@ $viewHelper = $this->getHelper('MonitoringState');
<td title="<?= $viewHelper->getStateTitle($service, 'host'); ?>">
<?php if ($service->service_icon_image): ?>
<div class="pull-left" style="margin-right: 2px;">
<i class="inline-image" style="background-image: url(<?= $service->service_icon_image; ?>);"></i>
<i class="inline-image" style="background-image: url(<?= $this->escape($this->resolveMacros($service->service_icon_image, $service)); ?>);"></i>
</div>
<?php endif; ?>
<strong> <?= $service->service_display_name; ?></strong>

View File

@ -59,10 +59,26 @@
<?php if ($object->action_url || $object->notes_url): ?>
<br />
<?php if ($object->notes_url): ?>
<a target="_new" href='<?= $object->notes_url ?>'>Notes</a>
<?php if (strpos($object->notes_url, "' ") === false): ?>
<a target="_new" href='<?= $this->escape($this->resolveMacros($object->notes_url, $object)); ?>'>Notes</a>
<?php else: ?>
<?php foreach(explode("' ", $object->notes_url) as $url): ?>
<?php $url = strpos($url, "'") === 0 ? substr($url, 1) : $url; ?>
<?php $url = strrpos($url, "'") === strlen($url) - 1 ? substr($url, 0, strlen($url) - 1) : $url; ?>
<a target="_new" href='<?= $this->escape($this->resolveMacros($url, $object)); ?>'>Notes</a>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>
<?php if ($object->action_url): ?>
<a target="_new" href='<?= $this->object->action_url ?>'>Action</a>
<?php if (strpos($object->action_url, "' ") === false): ?>
<a target="_new" href='<?= $this->escape($this->resolveMacros($this->object->action_url, $object)); ?>'>Action</a>
<?php else: ?>
<?php foreach(explode("' ", $object->action_url) as $url): ?>
<?php $url = strpos($url, "'") === 0 ? substr($url, 1) : $url; ?>
<?php $url = strrpos($url, "'") === strlen($url) - 1 ? substr($url, 0, strlen($url) - 1) : $url; ?>
<a target="_new" href='<?= $this->escape($this->resolveMacros($url, $object)); ?>'>Action</a>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
</div>

View File

@ -0,0 +1,107 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2014 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2014 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{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']
);
}
}