icingadb-web/application/controllers/HostsController.php

165 lines
5.1 KiB
PHP
Raw Normal View History

2019-09-09 08:46:56 -04:00
<?php
2019-11-04 19:07:30 -05:00
namespace Icinga\Module\Icingadb\Controllers;
2019-09-09 08:46:56 -04:00
2019-12-18 08:47:43 -05:00
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterExpression;
use Icinga\Module\Icingadb\Common\CommandActions;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Compat\FeatureStatus;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\HoststateSummary;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Web\Controller;
2020-01-10 03:29:15 -05:00
use Icinga\Module\Icingadb\Widget\ContinueWith;
2019-12-18 08:47:43 -05:00
use Icinga\Module\Icingadb\Widget\Detail\MultiselectQuickActions;
use Icinga\Module\Icingadb\Widget\Detail\ObjectsDetail;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Widget\HostList;
2019-12-03 05:37:29 -05:00
use Icinga\Module\Icingadb\Widget\HostStatusBar;
use Icinga\Module\Icingadb\Widget\ShowMore;
2019-12-18 08:47:43 -05:00
use ipl\Orm\Compat\FilterProcessor;
2019-09-09 08:46:56 -04:00
class HostsController extends Controller
{
2019-12-18 08:47:43 -05:00
use CommandActions;
2019-09-09 08:46:56 -04:00
public function indexAction()
{
$this->setTitle($this->translate('Hosts'));
$db = $this->getDb();
$hosts = Host::on($db)->with('state');
$summary = null;
if (! $this->view->compact) {
$summary = HoststateSummary::on($db)->with('state');
}
2019-09-09 08:46:56 -04:00
2019-10-08 18:09:18 -04:00
$limitControl = $this->createLimitControl();
2019-10-15 09:29:36 -04:00
$paginationControl = $this->createPaginationControl($hosts);
2019-12-11 07:55:31 -05:00
$sortControl = $this->createSortControl(
$hosts,
[
'host.display_name' => $this->translate('Name'),
'host.state.severity desc' => $this->translate('Severity'),
'host.state.soft_state' => $this->translate('Current State'),
'host.state.last_state_change desc' => $this->translate('Last State Change')
]
);
2019-10-15 09:29:36 -04:00
$viewModeSwitcher = $this->createViewModeSwitcher();
2019-10-18 02:44:23 -04:00
$filterControl = $this->createFilterControl($hosts);
2019-10-08 17:58:23 -04:00
2019-09-13 04:09:41 -04:00
$hostList = (new HostList($hosts))
2019-10-08 17:58:23 -04:00
->setViewMode($viewModeSwitcher->getViewMode());
2019-10-16 03:22:05 -04:00
$this->filter($hosts);
if (isset($summary)) {
$this->filter($summary);
yield $this->export($hosts, $summary);
} else {
yield $this->export($hosts);
}
2019-10-15 09:29:36 -04:00
$this->addControl($paginationControl);
2019-12-11 07:55:31 -05:00
$this->addControl($sortControl);
2019-10-08 18:09:18 -04:00
$this->addControl($limitControl);
$this->addControl($viewModeSwitcher);
2019-10-18 02:44:23 -04:00
$this->addControl($filterControl);
2020-01-10 03:29:15 -05:00
$this->addControl(new ContinueWith($this->getFilter(), Links::hostsDetails()));
2019-09-09 08:46:56 -04:00
$this->addContent($hostList);
if (isset($summary)) {
$this->addFooter(
(new HostStatusBar($summary->first()))->setBaseFilter($this->getFilter())
);
}
2019-12-03 05:37:29 -05:00
$this->setAutorefreshInterval(10);
2019-09-09 08:46:56 -04:00
}
2019-12-18 08:47:43 -05:00
public function detailsAction()
{
$this->setTitle($this->translate('Hosts'));
$db = $this->getDb();
$hosts = Host::on($db)->with('state');
$summary = HoststateSummary::on($db)->with(['state']);
$this->filter($hosts);
$this->filter($summary);
$hosts->limit(3);
$hosts->peekAhead();
2019-12-18 08:47:43 -05:00
yield $this->export($hosts, $summary);
$results = $hosts->execute();
2019-12-18 08:47:43 -05:00
$summary = $summary->first();
$downtimes = Host::on($db)->with(['downtime']);
$downtimes->getWith()['host.downtime']->setJoinType('INNER');
$this->filter($downtimes);
$summary->downtimes_total = $downtimes->count();
$comments = Host::on($db)->with(['comment']);
$comments->getWith()['host.comment']->setJoinType('INNER');
$this->filter($comments);
$summary->comments_total = $comments->count();
$this->addControl(
(new HostList($results))
2019-12-18 08:47:43 -05:00
->setViewMode('minimal')
);
$this->addControl(new ShowMore(
$results,
Links::hosts()->setQueryString($this->getFilter()->toQueryString()),
sprintf($this->translate('Show all %d hosts'), $hosts->count())
));
2019-12-18 08:47:43 -05:00
$this->addControl(
(new MultiselectQuickActions('host', $hosts, $summary))
->setBaseFilter($this->getFilter())
);
$this->addContent(
(new ObjectsDetail('host', $hosts, $summary))
->setBaseFilter($this->getFilter())
);
}
public function fetchCommandTargets()
{
$db = $this->getDb();
$hosts = Host::on($db)->with('state');
switch ($this->getRequest()->getActionName()) {
case 'acknowledge':
FilterProcessor::apply(
Filter::matchAll([
new FilterExpression('state.is_problem', '=', 'y'),
new FilterExpression('state.is_acknowledged', '=', 'n')
]),
$hosts
);
break;
}
$this->filter($hosts);
return $hosts;
}
public function getCommandTargetsUrl()
{
return Links::hostsDetails()->setQueryString($this->getFilter()->toQueryString());
}
protected function getFeatureStatus()
{
return new FeatureStatus('host', HoststateSummary::on($this->getDb())->with(['state'])->first());
}
2019-09-09 08:46:56 -04:00
}