icingadb-web/application/controllers/HostController.php

287 lines
9 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;
use Icinga\Module\Icingadb\Redis\VolatileStateResults;
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\ItemList\ServiceList;
use ipl\Stdlib\Filter;
use ipl\Web\Url;
use ipl\Web\Widget\Tabs;
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->getRequired('name');
2019-10-09 11:26:40 -04:00
$query = Host::on($this->getDb())
->with('state')
->with('icon_image')
->setResultSetClass(VolatileStateResults::class)
->filter(Filter::equal('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());
$this->setTitle($host->display_name);
}
public function indexAction()
{
$serviceSummary = ServicestateSummary::on($this->getDb());
$serviceSummary->filter(Filter::equal('service.host_id', $this->host->id));
2020-02-06 09:26:51 -05:00
$this->applyRestrictions($serviceSummary);
if ($this->host->state->is_overdue) {
$this->controls->addAttributes(['class' => 'overdue']);
}
$this->addControl((new HostList([$this->host]))
->setViewMode('objectHeader')
->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]))
->setViewMode('objectHeader')
2021-05-05 04:29:26 -04:00
->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')
->with('host.state')
->with('comment')
->with('downtime')
->with('flapping')
->with('notification')
->with('acknowledgement')
->with('state');
2019-11-03 17:20:33 -05:00
$history->filter(Filter::all(
Filter::equal('history.host_id', $this->host->id),
Filter::unlike('history.service_id', '*')
));
2019-11-03 17:20:33 -05:00
$before = $this->params->shift('before', time());
$url = Url::fromRequest()->setParams(clone $this->params);
2019-11-03 17:20:33 -05:00
$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($history);
$sortControl = $this->createSortControl(
$history,
[
'history.event_time desc' => t('Event Time')
]
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl, true);
$history->peekAhead();
$page = $paginationControl->getCurrentPageNumber();
if ($page > 1 && ! $compact) {
$history->limit($page * $limitControl->getLimit());
}
2019-11-03 17:20:33 -05:00
$history->filter(Filter::lessThanOrEqual('event_time', $before));
2019-11-03 17:20:33 -05:00
yield $this->export($history);
$this->addControl((new HostList([$this->host]))
->setViewMode('objectHeader')
->setDetailActionsDisabled()
->setNoSubjectLink());
$this->addControl($sortControl);
2019-11-03 17:20:33 -05:00
$this->addControl($limitControl);
$this->addControl($viewModeSwitcher);
2019-11-03 17:20:33 -05:00
$historyList = (new HistoryList($history->execute()))
->setViewMode($viewModeSwitcher->getViewMode())
->setPageSize($limitControl->getLimit())
->setLoadMoreUrl($url->setParam('before', $before));
if ($compact) {
$historyList->setPageNumber($page);
}
if ($compact && $page > 1) {
$this->document->addFrom($historyList);
} 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')
->with('state.last_comment')
->with('icon_image')
->with('host')
->with('host.state')
->setResultSetClass(VolatileStateResults::class)
->filter(Filter::equal('host.id', $this->host->id));
2019-11-04 17:42:42 -05:00
$this->applyRestrictions($services);
2019-11-04 17:42:42 -05:00
$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($services);
2021-08-23 09:23:34 -04:00
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);
$sortControl = $this->createSortControl(
$services,
[
'service.display_name' => t('Name'),
'service.state.severity desc' => t('Severity'),
'service.state.soft_state' => t('Current State'),
'service.state.last_state_change desc' => t('Last State Change')
]
);
2019-11-04 17:42:42 -05:00
yield $this->export($services);
$serviceList = (new ServiceList($services))
->setViewMode($viewModeSwitcher->getViewMode());
$this->addControl((new HostList([$this->host]))
->setViewMode('objectHeader')
->setDetailActionsDisabled()
->setNoSubjectLink());
2019-11-04 17:42:42 -05:00
$this->addControl($paginationControl);
$this->addControl($sortControl);
2019-11-04 17:42:42 -05:00
$this->addControl($limitControl);
$this->addControl($viewModeSwitcher);
2019-11-04 17:42:42 -05:00
$this->addContent($serviceList);
$this->setAutorefreshInterval(10);
2019-11-04 17:42:42 -05:00
}
protected function createTabs(): Tabs
2019-11-03 17:20:33 -05:00
{
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(string $name)
2019-11-03 17:20:33 -05:00
{
$tab = $this->createTabs()->get($name);
if ($tab !== null) {
$tab->setActive();
$this->setTitle($tab->getLabel());
2019-11-03 17:20:33 -05:00
}
}
2019-12-16 10:03:06 -05:00
protected function fetchCommandTargets(): array
2019-12-16 10:03:06 -05:00
{
return [$this->host];
}
protected function getCommandTargetsUrl(): Url
2019-12-16 10:03:06 -05:00
{
return Links::host($this->host);
}
2021-08-17 11:25:26 -04:00
protected function getDefaultTabControls(): array
2021-08-17 11:25:26 -04:00
{
return [(new HostList([$this->host]))->setDetailActionsDisabled()->setNoSubjectLink()];
}
2019-10-09 11:26:40 -04:00
}