icingadb-web/application/controllers/HostController.php

271 lines
8 KiB
PHP
Raw Normal View History

2019-10-09 11:26:40 -04:00
<?php
2020-03-13 03:38:01 -04:00
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
2019-11-04 19:07:30 -05:00
namespace Icinga\Module\Icingadb\Controllers;
2019-10-09 11:26:40 -04:00
use Icinga\Exception\NotFoundError;
2021-05-05 04:29:26 -04:00
use Icinga\Module\Icingadb\Command\Object\GetObjectCommand;
use Icinga\Module\Icingadb\Command\Transport\CommandTransport;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Common\CommandActions;
use Icinga\Module\Icingadb\Common\HostLinks;
use Icinga\Module\Icingadb\Common\Links;
2021-08-17 11:25:26 -04:00
use Icinga\Module\Icingadb\Hook\TabHook\HookActions;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Model\History;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
2020-02-06 09:26:51 -05:00
use Icinga\Module\Icingadb\Model\ServicestateSummary;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Web\Controller;
use Icinga\Module\Icingadb\Widget\Detail\HostDetail;
2021-05-05 04:29:26 -04:00
use Icinga\Module\Icingadb\Widget\Detail\HostInspectionDetail;
use Icinga\Module\Icingadb\Widget\Detail\HostMetaInfo;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Widget\Detail\QuickActions;
use Icinga\Module\Icingadb\Widget\ItemList\HostList;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Widget\ItemList\HistoryList;
use Icinga\Module\Icingadb\Widget\ServiceList;
use Icinga\Module\Icingadb\Widget\ShowMore;
2019-10-09 11:26:40 -04:00
class HostController extends Controller
{
use CommandActions;
2021-08-17 11:25:26 -04:00
use HookActions;
/** @var Host The host object */
protected $host;
public function init()
2019-10-09 11:26:40 -04:00
{
$name = $this->params->shiftRequired('name');
2021-08-20 09:43:12 -04:00
$query = Host::on($this->getDb())->with(['state', 'icon_image']);
2019-10-09 11:26:40 -04:00
$query->getSelectBase()
->where(['host.name = ?' => $name]);
2019-10-09 11:26:40 -04:00
$this->applyRestrictions($query);
2019-10-09 11:26:40 -04:00
/** @var Host $host */
$host = $query->first();
if ($host === null) {
throw new NotFoundError(t('Host not found'));
2019-10-09 11:26:40 -04:00
}
$this->host = $host;
2021-08-17 11:25:26 -04:00
$this->loadTabsForObject($host);
2019-11-03 17:20:33 -05:00
$this->setTitleTab($this->getRequest()->getActionName());
}
public function indexAction()
{
2020-02-06 09:26:51 -05:00
$serviceSummary = ServicestateSummary::on($this->getDb())->with('state');
$serviceSummary->getSelectBase()
->where(['host_id = ?' => $this->host->id]);
$this->applyRestrictions($serviceSummary);
if ($this->host->state->is_overdue) {
$this->controls->addAttributes(['class' => 'overdue']);
}
$this->addControl((new HostList([$this->host]))
->setViewMode('minimal')
->setDetailActionsDisabled()
->setNoSubjectLink());
$this->addControl(new HostMetaInfo($this->host));
2019-11-03 09:32:46 -05:00
$this->addControl(new QuickActions($this->host));
2020-02-06 09:26:51 -05:00
$this->addContent(new HostDetail($this->host, $serviceSummary->first()));
$this->setAutorefreshInterval(10);
2019-10-09 11:26:40 -04:00
}
2019-11-03 17:20:33 -05:00
2021-05-05 04:29:26 -04:00
public function sourceAction()
{
$this->assertPermission('icingadb/object/show-source');
$apiResult = (new CommandTransport())->send((new GetObjectCommand())->setObject($this->host));
if ($this->host->state->is_overdue) {
$this->controls->addAttributes(['class' => 'overdue']);
}
$this->addControl((new HostList([$this->host]))
->setDetailActionsDisabled()
->setNoSubjectLink());
$this->addContent(new HostInspectionDetail(
$this->host,
reset($apiResult)
));
}
2019-11-03 17:20:33 -05:00
public function historyAction()
{
$compact = $this->view->compact; // TODO: Find a less-legacy way..
if ($this->host->state->is_overdue) {
$this->controls->addAttributes(['class' => 'overdue']);
}
2019-11-03 17:20:33 -05:00
$db = $this->getDb();
$history = History::on($db)->with([
'host',
'host.state',
'comment',
'downtime',
'flapping',
2019-11-03 17:20:33 -05:00
'notification',
'acknowledgement',
2019-11-03 17:20:33 -05:00
'state'
]);
$history
->getSelectBase()
->where([
'history_host.id = ?' => $this->host->id,
'history.object_type = ?' => 'host'
]);
$url = HostLinks::history($this->host);
if (! $this->params->has('page') || ($page = (int) $this->params->shift('page')) < 1) {
$page = 1;
}
2019-11-03 17:20:33 -05:00
$limitControl = $this->createLimitControl();
$history->peekAhead();
$history->limit($limitControl->getLimit());
if ($page > 1) {
if ($compact) {
$history->offset(($page - 1) * $limitControl->getLimit());
} else {
$history->limit($page * $limitControl->getLimit());
}
}
2019-11-03 17:20:33 -05:00
yield $this->export($history);
$results = $history->execute();
$showMore = (new ShowMore(
$results,
$url->setParam('page', $page + 1)
->setAnchor('page-' . ($page + 1))
))
->setLabel(t('Load More'))
->setAttribute('data-no-icinga-ajax', true);
$this->addControl((new HostList([$this->host]))->setViewMode('minimal'));
2019-11-03 17:20:33 -05:00
$this->addControl($limitControl);
$historyList = (new HistoryList($results))
->setPageSize($limitControl->getLimit());
if ($compact) {
$historyList->setPageNumber($page);
}
// TODO: Dirty, really dirty, find a better solution (And I don't just mean `getContent()` !)
$historyList->add($showMore->setTag('li')->addAttributes(['class' => 'list-item']));
if ($compact && $page > 1) {
$this->document->add($historyList->getContent());
} else {
$this->addContent($historyList);
}
2019-11-03 17:20:33 -05:00
}
2019-11-04 17:42:42 -05:00
public function servicesAction()
{
if ($this->host->state->is_overdue) {
$this->controls->addAttributes(['class' => 'overdue']);
}
2019-11-04 17:42:42 -05:00
$db = $this->getDb();
$services = Service::on($db)->with([
'state',
2021-08-20 09:43:12 -04:00
'icon_image',
2019-11-04 17:42:42 -05:00
'host',
'host.state'
]);
$services
->getSelectBase()
->where(['service_host.id = ?' => $this->host->id]);
$this->applyRestrictions($services);
2019-11-04 17:42:42 -05:00
$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($services);
$viewModeSwitcher = $this->createViewModeSwitcher();
yield $this->export($services);
$serviceList = (new ServiceList($services))
->setViewMode($viewModeSwitcher->getViewMode());
$this->addControl((new HostList([$this->host]))->setViewMode('minimal'));
2019-11-04 17:42:42 -05:00
$this->addControl($paginationControl);
$this->addControl($viewModeSwitcher);
$this->addControl($limitControl);
$this->addContent($serviceList);
$this->setAutorefreshInterval(10);
2019-11-04 17:42:42 -05:00
}
2019-11-03 17:20:33 -05:00
protected function createTabs()
{
2021-05-05 04:29:26 -04:00
$tabs = $this->getTabs()
2019-11-03 17:20:33 -05:00
->add('index', [
'label' => t('Host'),
2019-11-03 17:20:33 -05:00
'url' => Links::host($this->host)
])
2019-11-04 17:42:42 -05:00
->add('services', [
'label' => t('Services'),
2019-11-04 17:42:42 -05:00
'url' => HostLinks::services($this->host)
])
2019-11-03 17:20:33 -05:00
->add('history', [
'label' => t('History'),
2019-11-03 17:20:33 -05:00
'url' => HostLinks::history($this->host)
]);
2021-05-05 04:29:26 -04:00
if ($this->hasPermission('icingadb/object/show-source')) {
$tabs->add('source', [
'label' => t('Source'),
'url' => Links::hostSource($this->host)
]);
}
2021-08-17 11:25:26 -04:00
foreach ($this->loadAdditionalTabs() as $name => $tab) {
$tabs->add($name, $tab + ['urlParams' => ['name' => $this->host->name]]);
}
2021-05-05 04:29:26 -04:00
return $tabs;
2019-11-03 17:20:33 -05:00
}
protected function setTitleTab($name)
{
$tab = $this->createTabs()->get($name);
if ($tab !== null) {
$tab->setActive();
$this->view->title = $tab->getLabel();
}
}
2019-12-16 10:03:06 -05:00
protected function fetchCommandTargets()
{
return [$this->host];
}
protected function getCommandTargetsUrl()
{
return Links::host($this->host);
}
2021-08-17 11:25:26 -04:00
protected function getDefaultTabControls()
{
return [(new HostList([$this->host]))->setDetailActionsDisabled()->setNoSubjectLink()];
}
2019-10-09 11:26:40 -04:00
}