endpoint/inspect: add inspection for our peers

This commit is contained in:
Thomas Gelf 2015-12-17 10:49:10 +01:00
parent b2cb3183c8
commit a93bcee68a
5 changed files with 82 additions and 12 deletions

View File

@ -6,4 +6,32 @@ use Icinga\Module\Director\Web\Controller\ObjectController;
class EndpointController extends ObjectController
{
public function init()
{
parent::init();
if ($this->isPeer()) {
$params['endpoint'] = $this->object->object_name;
$this->getTabs()->add('inspect', array(
'url' => 'director/inspect/types',
'urlParams' => $params,
'label' => $this->translate('Inspect')
));
}
}
protected function isPeer()
{
if (! $this->object) return false;
$apiconfig = $this->Config()->getSection('api');
$host = $apiconfig->get('address');
$port = $apiconfig->get('port');
if ($host === $this->object->host
&& $port === $this->object->port
) {
return true;
}
}
}

View File

@ -2,13 +2,19 @@
namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Core\CoreApi;
use Icinga\Module\Director\Core\RestApiClient;
use Icinga\Module\Director\Objects\IcingaEndpoint;
use Icinga\Module\Director\Web\Controller\ActionController;
class InspectController extends ActionController
{
public function typesAction()
{
$this->view->title = $this->translate('Icinga2 object types');
$this->view->title = sprintf(
$this->translate('Icinga2 Objects: %s'),
$this->view->endpoint
);
$api = $this->api();
$types = $api->getTypes();
$rootNodes = array();
@ -30,6 +36,10 @@ class InspectController extends ActionController
public function typeAction()
{
$typeName = $this->params->get('name');
$this->view->title = sprintf(
$this->translate('Object type "%s"'),
$typeName
);
$this->view->type = $type = $this->api()->getType($typeName);
if ($type->abstract) {
return;
@ -84,4 +94,15 @@ class InspectController extends ActionController
$this->view->status = $status = $this->api()->getStatus();
print_r($status); exit;
}
protected function api()
{
$this->view->endpoint = $this->params->get('endpoint');
$endpoint = IcingaEndpoint::load($this->view->endpoint, $this->db());
$apiconfig = $this->Config()->getSection('api');
$client = new RestApiClient($endpoint->host, $endpoint->port);
$client->setCredentials($apiconfig->get('username'), $apiconfig->get('password'));
$api = new CoreApi($client);
return $api;
}
}

View File

@ -4,6 +4,6 @@
<div class="content">
<pre>
<?= $this->escape(print_r($this->object, 1)) ?>
<?= $this->renderPlainObject($this->object) ?>
</pre>
</div>

View File

@ -4,6 +4,7 @@
</div>
<div class="content">
<?php if ($this->objects): ?>
<table class="common-table table-row-selectable" data-base-target="_next">
<thead><tr><th><?= $this->translate('Object name') ?></th></tr></thead>
<tbody>
@ -11,10 +12,16 @@
<tr><td><?= $this->qlink(
str_replace('!', ': ', $object),
'director/inspect/object',
array('type' => $this->type->name, 'plural' => $this->type->plural_name, 'name' => $object)
array(
'endpoint' => $this->endpoint,
'type' => $this->type->name,
'plural' => $this->type->plural_name,
'name' => $object
)
) ?></td></tr>
<?php endforeach ?>
</tbody>
</table>
<pre><?= $this->escape(print_r($this->type, 1)) ?></pre>
<?php endif ?>
<pre><?= $this->renderPlainObject($this->type) ?></pre>
</div>

View File

@ -1,20 +1,34 @@
<?php
function dumpTree($tree, $self, $level = 0)
function dumpTree($tree, $self, $showLinks = false, $level = 0)
{
$html = '';
foreach ($tree as $name => $node) {
$html .= '<li>';
$link = $self->qlink(
$name,
'director/inspect/type',
array('name' => $name),
array('class' => $node->abstract ? 'abstract' : 'object')
);
if ($showLinks || $name === 'ConfigObject') {
$link = $self->qlink(
$name,
'director/inspect/type',
array('endpoint' => $self->endpoint, 'name' => $name),
array('class' => $node->abstract ? 'abstract' : 'object')
);
} else {
$link = sprintf(
'<a class="%s" href="#">%s</a>',
$node->abstract ? 'abstract' : 'object',
$name
);
}
if (property_exists($node, 'children')) {
$html .= '<span class="handle"> </span>';
$html .= $link;
$html .= '<ul>' . dumpTree($node->children, $self, $level + 1) . '</ul>';
$html .= '<ul>' . dumpTree(
$node->children,
$self,
$showLinks || $name === 'ConfigObject',
$level + 1
) . '</ul>';
} else {
$html .= $link;
}