2019-09-09 08:46:56 -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-09-09 08:46:56 -04:00
|
|
|
|
2020-11-02 08:31:00 -05:00
|
|
|
use GuzzleHttp\Psr7\ServerRequest;
|
2019-12-18 08:47:43 -05:00
|
|
|
use Icinga\Module\Icingadb\Common\CommandActions;
|
|
|
|
|
use Icinga\Module\Icingadb\Common\Links;
|
2019-11-04 19:07:30 -05:00
|
|
|
use Icinga\Module\Icingadb\Model\Host;
|
2019-11-05 06:36:38 -05:00
|
|
|
use Icinga\Module\Icingadb\Model\HoststateSummary;
|
2021-10-06 09:34:36 -04:00
|
|
|
use Icinga\Module\Icingadb\Redis\VolatileStateResults;
|
2021-02-12 10:27:13 -05:00
|
|
|
use Icinga\Module\Icingadb\Util\FeatureStatus;
|
2020-11-02 08:31:00 -05:00
|
|
|
use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions;
|
2019-11-04 19:07:30 -05:00
|
|
|
use Icinga\Module\Icingadb\Web\Controller;
|
2019-12-18 08:47:43 -05:00
|
|
|
use Icinga\Module\Icingadb\Widget\Detail\MultiselectQuickActions;
|
|
|
|
|
use Icinga\Module\Icingadb\Widget\Detail\ObjectsDetail;
|
2019-12-03 05:37:29 -05:00
|
|
|
use Icinga\Module\Icingadb\Widget\HostStatusBar;
|
2025-03-19 10:59:29 -04:00
|
|
|
use Icinga\Module\Icingadb\Widget\ItemList\ObjectList;
|
2022-05-19 09:53:02 -04:00
|
|
|
use Icinga\Module\Icingadb\Widget\ItemTable\HostItemTable;
|
2021-08-20 11:23:29 -04:00
|
|
|
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
|
2023-08-09 08:25:44 -04:00
|
|
|
use Icinga\Module\Icingadb\Widget\ShowMore;
|
2021-09-22 04:21:15 -04:00
|
|
|
use ipl\Orm\Query;
|
2020-12-03 11:05:51 -05:00
|
|
|
use ipl\Stdlib\Filter;
|
2021-03-05 07:45:36 -05:00
|
|
|
use ipl\Web\Control\LimitControl;
|
|
|
|
|
use ipl\Web\Control\SortControl;
|
2020-01-31 09:37:51 -05:00
|
|
|
use ipl\Web\Url;
|
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()
|
|
|
|
|
{
|
2022-03-16 11:06:00 -04:00
|
|
|
$this->addTitleTab(t('Hosts'));
|
2020-01-31 09:37:51 -05:00
|
|
|
$compact = $this->view->compact;
|
2019-09-09 08:46:56 -04:00
|
|
|
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
|
|
2022-05-31 08:11:49 -04:00
|
|
|
$hosts = Host::on($db)->with(['state', 'icon_image', 'state.last_comment']);
|
2021-11-12 06:59:17 -05:00
|
|
|
$hosts->getWith()['host.state']->setJoinType('INNER');
|
2021-10-06 09:34:36 -04:00
|
|
|
$hosts->setResultSetClass(VolatileStateResults::class);
|
2019-12-12 10:59:12 -05:00
|
|
|
|
2023-05-30 09:26:00 -04:00
|
|
|
$this->handleSearchRequest($hosts, ['address', 'address6']);
|
2020-11-02 08:31:00 -05:00
|
|
|
|
2019-12-12 10:59:12 -05:00
|
|
|
$summary = null;
|
2020-01-31 09:37:51 -05:00
|
|
|
if (! $compact) {
|
2022-05-19 05:24:20 -04:00
|
|
|
$summary = HoststateSummary::on($db);
|
2019-12-12 10:59:12 -05:00
|
|
|
}
|
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,
|
|
|
|
|
[
|
2023-05-19 08:10:27 -04:00
|
|
|
'host.display_name' => t('Name'),
|
|
|
|
|
'host.state.severity desc,host.state.last_state_change desc' => t('Severity'),
|
|
|
|
|
'host.state.soft_state' => t('Current State'),
|
|
|
|
|
'host.state.last_state_change desc' => t('Last State Change')
|
2025-01-09 09:47:55 -05:00
|
|
|
],
|
|
|
|
|
['host.state.severity DESC', 'host.state.last_state_change DESC']
|
2019-12-11 07:55:31 -05:00
|
|
|
);
|
2021-06-23 09:09:22 -04:00
|
|
|
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);
|
2022-05-19 09:53:02 -04:00
|
|
|
$columns = $this->createColumnControl($hosts, $viewModeSwitcher);
|
2022-05-23 07:32:45 -04:00
|
|
|
|
2020-11-02 08:31:00 -05:00
|
|
|
$searchBar = $this->createSearchBar($hosts, [
|
2020-05-19 06:26:52 -04:00
|
|
|
$limitControl->getLimitParam(),
|
|
|
|
|
$sortControl->getSortParam(),
|
2022-05-19 09:53:02 -04:00
|
|
|
$viewModeSwitcher->getViewModeParam(),
|
|
|
|
|
'columns'
|
2020-05-19 06:26:52 -04:00
|
|
|
]);
|
2019-10-08 17:58:23 -04:00
|
|
|
|
2020-11-06 09:51:00 -05:00
|
|
|
if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
|
|
|
|
|
if ($searchBar->hasBeenSubmitted()) {
|
2020-12-03 11:05:51 -05:00
|
|
|
$filter = $this->getFilter();
|
2020-11-06 09:51:00 -05:00
|
|
|
} else {
|
|
|
|
|
$this->addControl($searchBar);
|
|
|
|
|
$this->sendMultipartUpdate();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$filter = $searchBar->getFilter();
|
|
|
|
|
}
|
2020-11-02 08:31:00 -05:00
|
|
|
|
2020-01-31 09:37:51 -05:00
|
|
|
$hosts->peekAhead($compact);
|
2019-10-08 17:58:23 -04:00
|
|
|
|
2020-11-02 08:31:00 -05:00
|
|
|
$this->filter($hosts, $filter);
|
2023-08-17 05:32:27 -04:00
|
|
|
if (! $compact) {
|
2020-11-02 08:31:00 -05:00
|
|
|
$this->filter($summary, $filter);
|
2019-12-12 10:59:12 -05:00
|
|
|
yield $this->export($hosts, $summary);
|
|
|
|
|
} else {
|
|
|
|
|
yield $this->export($hosts);
|
|
|
|
|
}
|
2019-10-15 10:50:40 -04:00
|
|
|
|
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);
|
2019-11-05 04:16:21 -05:00
|
|
|
$this->addControl($viewModeSwitcher);
|
2020-11-02 08:31:00 -05:00
|
|
|
$this->addControl($searchBar);
|
2021-04-22 10:18:16 -04:00
|
|
|
$continueWith = $this->createContinueWith(Links::hostsDetails(), $searchBar);
|
2019-09-09 08:46:56 -04:00
|
|
|
|
2020-01-31 09:37:51 -05:00
|
|
|
$results = $hosts->execute();
|
2022-05-19 09:53:02 -04:00
|
|
|
|
|
|
|
|
if ($viewModeSwitcher->getViewMode() === 'tabular') {
|
|
|
|
|
$hostList = (new HostItemTable($results, HostItemTable::applyColumnMetaData($hosts, $columns)))
|
|
|
|
|
->setSort($sortControl->getSort());
|
|
|
|
|
} else {
|
2025-03-19 10:59:29 -04:00
|
|
|
$hostList = (new ObjectList($results))
|
2025-03-27 09:34:56 -04:00
|
|
|
->setViewMode($viewModeSwitcher->getViewMode());
|
2022-05-19 09:53:02 -04:00
|
|
|
}
|
2020-01-31 09:37:51 -05:00
|
|
|
|
2025-05-06 05:17:39 -04:00
|
|
|
$hostList->setEmptyStateMessage($paginationControl->getEmptyStateMessage());
|
|
|
|
|
|
2019-09-09 08:46:56 -04:00
|
|
|
$this->addContent($hostList);
|
2019-12-11 02:59:30 -05:00
|
|
|
|
2020-01-31 09:37:51 -05:00
|
|
|
if ($compact) {
|
|
|
|
|
$this->addContent(
|
2022-08-04 10:07:28 -04:00
|
|
|
(new ShowMore($results, Url::fromRequest()->without(['showCompact', 'limit', 'view'])))
|
2021-08-02 10:02:05 -04:00
|
|
|
->setBaseTarget('_next')
|
2020-01-31 09:37:51 -05:00
|
|
|
->setAttribute('title', sprintf(
|
2020-04-24 08:36:37 -04:00
|
|
|
t('Show all %d hosts'),
|
2020-01-31 09:37:51 -05:00
|
|
|
$hosts->count()
|
|
|
|
|
))
|
|
|
|
|
);
|
2020-11-02 08:31:00 -05:00
|
|
|
} else {
|
2023-08-09 08:25:44 -04:00
|
|
|
/** @var HoststateSummary $hostsSummary */
|
|
|
|
|
$hostsSummary = $summary->first();
|
|
|
|
|
$this->addFooter((new HostStatusBar($hostsSummary))->setBaseFilter($filter));
|
2019-12-12 10:59:12 -05:00
|
|
|
}
|
2019-12-03 05:37:29 -05:00
|
|
|
|
2020-11-06 09:51:00 -05:00
|
|
|
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
|
2021-05-26 05:44:50 -04:00
|
|
|
$this->sendMultipartUpdate($continueWith);
|
2020-11-02 08:31:00 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-11 02:59:30 -05:00
|
|
|
$this->setAutorefreshInterval(10);
|
2019-09-09 08:46:56 -04:00
|
|
|
}
|
2019-12-18 08:47:43 -05:00
|
|
|
|
|
|
|
|
public function detailsAction()
|
|
|
|
|
{
|
2022-03-16 11:06:00 -04:00
|
|
|
$this->addTitleTab(t('Hosts'));
|
2019-12-18 08:47:43 -05:00
|
|
|
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
|
|
2022-05-31 08:11:49 -04:00
|
|
|
$hosts = Host::on($db)->with(['state', 'icon_image']);
|
|
|
|
|
$hosts->setResultSetClass(VolatileStateResults::class);
|
|
|
|
|
$summary = HoststateSummary::on($db)->with(['state']);
|
2019-12-18 08:47:43 -05:00
|
|
|
|
|
|
|
|
$this->filter($hosts);
|
|
|
|
|
$this->filter($summary);
|
|
|
|
|
|
2020-01-15 03:54:32 -05:00
|
|
|
$hosts->limit(3);
|
|
|
|
|
$hosts->peekAhead();
|
|
|
|
|
|
2019-12-18 08:47:43 -05:00
|
|
|
yield $this->export($hosts, $summary);
|
|
|
|
|
|
2020-01-15 03:54:32 -05:00
|
|
|
$results = $hosts->execute();
|
2019-12-18 08:47:43 -05:00
|
|
|
$summary = $summary->first();
|
|
|
|
|
|
2022-05-31 08:11:49 -04:00
|
|
|
$downtimes = Host::on($db)->with(['downtime']);
|
2019-12-18 08:47:43 -05:00
|
|
|
$downtimes->getWith()['host.downtime']->setJoinType('INNER');
|
|
|
|
|
$this->filter($downtimes);
|
|
|
|
|
$summary->downtimes_total = $downtimes->count();
|
|
|
|
|
|
2022-05-31 08:11:49 -04:00
|
|
|
$comments = Host::on($db)->with(['comment']);
|
2019-12-18 08:47:43 -05:00
|
|
|
$comments->getWith()['host.comment']->setJoinType('INNER');
|
2021-01-28 06:41:04 -05:00
|
|
|
// TODO: This should be automatically done by the model/resolver and added as ON condition
|
2021-03-19 10:42:15 -04:00
|
|
|
$comments->filter(Filter::equal('comment.object_type', 'host'));
|
2019-12-18 08:47:43 -05:00
|
|
|
$this->filter($comments);
|
|
|
|
|
$summary->comments_total = $comments->count();
|
|
|
|
|
|
|
|
|
|
$this->addControl(
|
2025-03-19 10:59:29 -04:00
|
|
|
(new ObjectList($results))
|
2019-12-18 08:47:43 -05:00
|
|
|
->setViewMode('minimal')
|
2025-03-27 12:00:15 -04:00
|
|
|
->setDetailActionsDisabled()
|
2019-12-18 08:47:43 -05:00
|
|
|
);
|
2020-01-15 03:54:32 -05:00
|
|
|
$this->addControl(new ShowMore(
|
|
|
|
|
$results,
|
2023-09-01 07:53:02 -04:00
|
|
|
Links::hosts()->setFilter($this->getFilter()),
|
2020-04-24 08:36:37 -04:00
|
|
|
sprintf(t('Show all %d hosts'), $hosts->count())
|
2020-01-15 03:54:32 -05:00
|
|
|
));
|
2019-12-18 08:47:43 -05:00
|
|
|
$this->addControl(
|
2020-01-15 03:58:08 -05:00
|
|
|
(new MultiselectQuickActions('host', $summary))
|
2019-12-18 08:47:43 -05:00
|
|
|
->setBaseFilter($this->getFilter())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->addContent(
|
2021-08-11 07:11:32 -04:00
|
|
|
(new ObjectsDetail('host', $summary, $hosts))
|
2019-12-18 08:47:43 -05:00
|
|
|
->setBaseFilter($this->getFilter())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-02 08:31:00 -05:00
|
|
|
public function completeAction()
|
|
|
|
|
{
|
|
|
|
|
$suggestions = new ObjectSuggestions();
|
|
|
|
|
$suggestions->setModel(Host::class);
|
|
|
|
|
$suggestions->forRequest(ServerRequest::fromGlobals());
|
|
|
|
|
$this->getDocument()->add($suggestions);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 07:45:36 -05:00
|
|
|
public function searchEditorAction()
|
|
|
|
|
{
|
|
|
|
|
$editor = $this->createSearchEditor(Host::on($this->getDb()), [
|
|
|
|
|
LimitControl::DEFAULT_LIMIT_PARAM,
|
|
|
|
|
SortControl::DEFAULT_SORT_PARAM,
|
2022-05-19 09:53:02 -04:00
|
|
|
ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM,
|
|
|
|
|
'columns'
|
2021-03-05 07:45:36 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->getDocument()->add($editor);
|
|
|
|
|
$this->setTitle(t('Adjust Filter'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
protected function fetchCommandTargets(): Query
|
2019-12-18 08:47:43 -05:00
|
|
|
{
|
|
|
|
|
$db = $this->getDb();
|
|
|
|
|
|
|
|
|
|
$hosts = Host::on($db)->with('state');
|
2021-10-06 09:34:36 -04:00
|
|
|
$hosts->setResultSetClass(VolatileStateResults::class);
|
2019-12-18 08:47:43 -05:00
|
|
|
|
|
|
|
|
switch ($this->getRequest()->getActionName()) {
|
|
|
|
|
case 'acknowledge':
|
2021-03-19 10:42:15 -04:00
|
|
|
$hosts->filter(Filter::equal('state.is_problem', 'y'))
|
|
|
|
|
->filter(Filter::equal('state.is_acknowledged', 'n'));
|
2019-12-18 08:47:43 -05:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->filter($hosts);
|
|
|
|
|
|
|
|
|
|
return $hosts;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 02:23:28 -04:00
|
|
|
protected function getCommandTargetsUrl(): Url
|
2019-12-18 08:47:43 -05:00
|
|
|
{
|
2023-09-01 07:53:02 -04:00
|
|
|
return Links::hostsDetails()->setFilter($this->getFilter());
|
2019-12-18 08:47:43 -05:00
|
|
|
}
|
2020-01-08 07:59:48 -05:00
|
|
|
|
|
|
|
|
protected function getFeatureStatus()
|
|
|
|
|
{
|
2022-05-19 05:24:20 -04:00
|
|
|
$summary = HoststateSummary::on($this->getDb());
|
2021-01-28 06:54:57 -05:00
|
|
|
$this->filter($summary);
|
|
|
|
|
|
|
|
|
|
return new FeatureStatus('host', $summary->first());
|
2020-01-08 07:59:48 -05:00
|
|
|
}
|
2019-09-09 08:46:56 -04:00
|
|
|
}
|