mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-22 13:24:24 +02:00
commit
fc93bc50f3
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -121,7 +121,7 @@ $viewHelper = $this->getHelper('MonitoringState');
|
|||||||
<td>
|
<td>
|
||||||
<?php if ($host->host_icon_image) : ?>
|
<?php if ($host->host_icon_image) : ?>
|
||||||
<div class="pull-left" style="margin-right: 2px;">
|
<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>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<a href="<?= $this->href('monitoring/list/services', array('host' => $host->host_name)) ?>">
|
<a href="<?= $this->href('monitoring/list/services', array('host' => $host->host_name)) ?>">
|
||||||
|
@ -121,7 +121,7 @@ $viewHelper = $this->getHelper('MonitoringState');
|
|||||||
<td title="<?= $viewHelper->getStateTitle($service, 'host'); ?>">
|
<td title="<?= $viewHelper->getStateTitle($service, 'host'); ?>">
|
||||||
<?php if ($service->service_icon_image): ?>
|
<?php if ($service->service_icon_image): ?>
|
||||||
<div class="pull-left" style="margin-right: 2px;">
|
<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>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<strong> <?= $service->service_display_name; ?></strong>
|
<strong> <?= $service->service_display_name; ?></strong>
|
||||||
|
@ -59,10 +59,26 @@
|
|||||||
<?php if ($object->action_url || $object->notes_url): ?>
|
<?php if ($object->action_url || $object->notes_url): ?>
|
||||||
<br />
|
<br />
|
||||||
<?php if ($object->notes_url): ?>
|
<?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 endif; ?>
|
||||||
<?php if ($object->action_url): ?>
|
<?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; ?>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
|
@ -4,8 +4,6 @@ namespace Icinga\Module\Monitoring\Backend\Ido\Query;
|
|||||||
|
|
||||||
class CustomvarQuery extends IdoQuery
|
class CustomvarQuery extends IdoQuery
|
||||||
{
|
{
|
||||||
protected $object_id = 'object_id';
|
|
||||||
|
|
||||||
protected $columnMap = array(
|
protected $columnMap = array(
|
||||||
'customvars' => array(
|
'customvars' => array(
|
||||||
'varname' => 'cvs.varname',
|
'varname' => 'cvs.varname',
|
||||||
@ -24,12 +22,11 @@ class CustomvarQuery extends IdoQuery
|
|||||||
protected function joinBaseTables()
|
protected function joinBaseTables()
|
||||||
{
|
{
|
||||||
$this->baseQuery = $this->db->select()->from(
|
$this->baseQuery = $this->db->select()->from(
|
||||||
array('cvs' => $this->prefix . 'customvariablestatus'),
|
array('cvs' => $this->prefix . 'customvariables'),
|
||||||
array()
|
array()
|
||||||
)->join(
|
)->join(
|
||||||
array('cvo' => $this->prefix . 'objects'),
|
array('cvo' => $this->prefix . 'objects'),
|
||||||
'cvs.object_id = cvo.' . $this->object_id
|
'cvs.object_id = cvo.object_id AND cvo.is_active = 1',
|
||||||
. ' AND cvo.is_active = 1',
|
|
||||||
array()
|
array()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -2,17 +2,14 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Monitoring\Object;
|
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\Contact;
|
||||||
use Icinga\Module\Monitoring\DataView\Contactgroup;
|
use Icinga\Module\Monitoring\DataView\Contactgroup;
|
||||||
use Icinga\Module\Monitoring\DataView\Downtime;
|
use Icinga\Module\Monitoring\DataView\Downtime;
|
||||||
use Icinga\Module\Monitoring\DataView\EventHistory;
|
use Icinga\Module\Monitoring\DataView\EventHistory;
|
||||||
use Icinga\Module\Monitoring\DataView\Hostgroup;
|
use Icinga\Module\Monitoring\DataView\Hostgroup;
|
||||||
use Icinga\Module\Monitoring\DataView\HostStatus;
|
|
||||||
use Icinga\Module\Monitoring\DataView\Comment;
|
use Icinga\Module\Monitoring\DataView\Comment;
|
||||||
use Icinga\Module\Monitoring\DataView\Servicegroup;
|
use Icinga\Module\Monitoring\DataView\Servicegroup;
|
||||||
use Icinga\Module\Monitoring\DataView\ServiceStatus;
|
use Icinga\Module\Monitoring\DataView\Customvar;
|
||||||
use Icinga\Web\Request;
|
use Icinga\Web\Request;
|
||||||
|
|
||||||
abstract class AbstractObject
|
abstract class AbstractObject
|
||||||
@ -81,6 +78,29 @@ abstract class AbstractObject
|
|||||||
return $this;
|
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()
|
public function fetchContacts()
|
||||||
{
|
{
|
||||||
$this->contacts = Contact::fromRequest(
|
$this->contacts = Contact::fromRequest(
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Monitoring\Object;
|
namespace Icinga\Module\Monitoring\Object;
|
||||||
|
|
||||||
use Icinga\Data\BaseQuery as Query;
|
|
||||||
use Icinga\Module\Monitoring\DataView\HostStatus;
|
use Icinga\Module\Monitoring\DataView\HostStatus;
|
||||||
|
|
||||||
class Host extends AbstractObject
|
class Host extends AbstractObject
|
||||||
@ -19,7 +18,8 @@ class Host extends AbstractObject
|
|||||||
->fetchDowntimes()
|
->fetchDowntimes()
|
||||||
->fetchHostgroups()
|
->fetchHostgroups()
|
||||||
->fetchContacts()
|
->fetchContacts()
|
||||||
->fetchContactGroups();
|
->fetchContactGroups()
|
||||||
|
->fetchCustomvars();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getProperties()
|
protected function getProperties()
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace Icinga\Module\Monitoring\Object;
|
namespace Icinga\Module\Monitoring\Object;
|
||||||
|
|
||||||
use Icinga\Data\BaseQuery as Query;
|
|
||||||
use Icinga\Module\Monitoring\DataView\ServiceStatus;
|
use Icinga\Module\Monitoring\DataView\ServiceStatus;
|
||||||
|
|
||||||
class Service extends AbstractObject
|
class Service extends AbstractObject
|
||||||
@ -19,7 +18,8 @@ class Service extends AbstractObject
|
|||||||
->fetchHostgroups()
|
->fetchHostgroups()
|
||||||
->fetchServicegroups()
|
->fetchServicegroups()
|
||||||
->fetchContacts()
|
->fetchContacts()
|
||||||
->fetchContactGroups();
|
->fetchContactGroups()
|
||||||
|
->fetchCustomvars();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getProperties()
|
protected function getProperties()
|
||||||
|
@ -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']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user