Merge pull request #2771 from Icinga/feature/monitoring-detailviewextension-hook-2104

Feature/monitoring detailviewextension hook 2104
This commit is contained in:
Eric Lippmann 2017-03-28 09:46:28 +02:00
commit f749c19f37
6 changed files with 159 additions and 0 deletions

View File

@ -1,6 +1,7 @@
<div class="content" data-base-target="_next">
<?= $this->render('show/components/output.phtml') ?>
<?= $this->render('show/components/grapher.phtml') ?>
<?= $this->render('show/components/extensions.phtml') ?>
<h2><?= $this->translate('Problem handling') ?></h2>
<table class="name-value-table">

View File

@ -0,0 +1,4 @@
<?php
foreach ($extensionsHtml as $extensionHtml) {
echo $extensionHtml;
}

View File

@ -0,0 +1,76 @@
# <a id="detailviewextension"></a> Detail View Extension Hook
## About
This hook can be used to easily extend the detail view of monitored objects (hosts and services).
## How it works
### Directory structure
* `icingaweb2/modules/example`
* `library/Example/ProvidedHook/Monitoring/DetailviewExtension/Simple.php`
* `run.php`
### Files
#### run.php
```php
<?php
/** @var \Icinga\Application\Modules\Module $this */
$this->provideHook(
'monitoring/DetailviewExtension',
'Icinga\Module\Example\ProvidedHook\Monitoring\DetailviewExtension\Simple'
);
```
#### Simple.php
```php
<?php
namespace Icinga\Module\Example\ProvidedHook\Monitoring\DetailviewExtension;
use Icinga\Module\Monitoring\Hook\DetailviewExtensionHook;
use Icinga\Module\Monitoring\Object\MonitoredObject;
class Simple extends DetailviewExtensionHook
{
public function getHtmlForObject(MonitoredObject $object)
{
$stats = array();
foreach (str_split($object->name) as $c) {
if (isset($stats[$c])) {
++$stats[$c];
} else {
$stats[$c] = 1;
}
}
ksort($stats);
$view = $this->getView();
$thead = '';
$tbody = '';
foreach ($stats as $c => $amount) {
$thead .= '<th>' . $view->escape($c) . '</th>';
$tbody .= '<td>' . $amount . '</td>';
}
return '<h2>'
. $view->escape(sprintf($view->translate('A %s named "%s"'), $object->getType(), $object->name))
. '</h2>'
. '<h3>Character stats</h3>'
. '<table>'
. '<thead>' . $thead . '</thead>'
. '<tbody>' . $tbody . '</tbody>'
. '</table>';
}
}
```
## How it looks like
![Screenshot](res/detailviewextension-01.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -0,0 +1,71 @@
<?php
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Hook;
use Icinga\Module\Monitoring\Object\MonitoredObject;
use Icinga\Web\View;
/**
* Base class for hooks extending the detail view of monitored objects
*
* Extend this class if you want to extend the detail view of monitored objects with custom HTML.
*/
abstract class DetailviewExtensionHook
{
/**
* The view the generated HTML will be included in
*
* @var View
*/
private $view;
/**
* Create a new hook
*
* @see init() For hook initialization.
*/
final public function __construct()
{
$this->init();
}
/**
* Overwrite this function for hook initialization, e.g. loading the hook's config
*/
protected function init()
{
}
/**
* Shall return valid HTML to include in the detail view
*
* @param MonitoredObject $object The object to generate HTML for
*
* @return string
*/
abstract public function getHtmlForObject(MonitoredObject $object);
/**
* Get {@link view}
*
* @return View
*/
public function getView()
{
return $this->view;
}
/**
* Set {@link view}
*
* @param View $view
*
* @return $this
*/
public function setView($view)
{
$this->view = $view;
return $this;
}
}

View File

@ -10,6 +10,7 @@ use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\ObjectsCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\RemoveAcknowledgementCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\ToggleObjectFeaturesCommandForm;
use Icinga\Module\Monitoring\Hook\DetailviewExtensionHook;
use Icinga\Web\Hook;
use Icinga\Web\Url;
use Icinga\Web\Widget\Tabextension\DashboardAction;
@ -96,6 +97,12 @@ abstract class MonitoredObjectController extends Controller
}
$this->view->showInstance = $this->backend->select()->from('instance')->count() > 1;
$this->view->object = $this->object;
$this->view->extensionsHtml = array();
foreach (Hook::all('Monitoring\DetailviewExtension') as $hook) {
/** @var DetailviewExtensionHook $hook */
$this->view->extensionsHtml[] = $hook->setView($this->view)->getHtmlForObject($this->object);
}
}
/**