2019-10-09 11:26:52 -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:52 -04:00
|
|
|
|
|
|
|
|
use Icinga\Exception\NotFoundError;
|
2021-05-05 04:29:58 -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\Links;
|
|
|
|
|
use Icinga\Module\Icingadb\Common\ServiceLinks;
|
2021-08-17 11:25:54 -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\Service;
|
|
|
|
|
use Icinga\Module\Icingadb\Web\Controller;
|
|
|
|
|
use Icinga\Module\Icingadb\Widget\Detail\QuickActions;
|
2020-02-06 09:23:45 -05:00
|
|
|
use Icinga\Module\Icingadb\Widget\Detail\ServiceDetail;
|
2021-05-05 04:29:58 -04:00
|
|
|
use Icinga\Module\Icingadb\Widget\Detail\ServiceInspectionDetail;
|
2019-11-04 19:07:30 -05:00
|
|
|
use Icinga\Module\Icingadb\Widget\ItemList\HistoryList;
|
|
|
|
|
use Icinga\Module\Icingadb\Widget\ServiceList;
|
2019-12-13 10:15:58 -05:00
|
|
|
use Icinga\Module\Icingadb\Widget\ShowMore;
|
2019-10-09 11:26:52 -04:00
|
|
|
|
|
|
|
|
class ServiceController extends Controller
|
|
|
|
|
{
|
2019-10-10 07:41:52 -04:00
|
|
|
use CommandActions;
|
2021-08-17 11:25:54 -04:00
|
|
|
use HookActions;
|
2019-10-10 07:41:52 -04:00
|
|
|
|
2019-10-10 07:38:55 -04:00
|
|
|
/** @var Service The service object */
|
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
|
|
public function init()
|
2019-10-09 11:26:52 -04:00
|
|
|
{
|
|
|
|
|
$name = $this->params->shiftRequired('name');
|
2019-11-03 15:52:41 -05:00
|
|
|
$hostName = $this->params->shiftRequired('host.name');
|
2019-10-09 11:26:52 -04:00
|
|
|
|
|
|
|
|
$query = Service::on($this->getDb())->with([
|
|
|
|
|
'state',
|
2021-08-20 09:43:12 -04:00
|
|
|
'icon_image',
|
2019-10-14 09:13:39 -04:00
|
|
|
'host',
|
|
|
|
|
'host.state'
|
2019-10-09 11:26:52 -04:00
|
|
|
]);
|
|
|
|
|
$query->getSelectBase()
|
|
|
|
|
->where(['service.name = ?' => $name])
|
2019-10-29 11:02:44 -04:00
|
|
|
->where(['service_host.name = ?' => $hostName]);
|
2019-10-09 11:26:52 -04:00
|
|
|
|
2021-01-28 06:38:03 -05:00
|
|
|
$this->applyRestrictions($query);
|
2019-11-22 10:01:27 -05:00
|
|
|
|
2019-10-09 11:26:52 -04:00
|
|
|
/** @var Service $service */
|
|
|
|
|
$service = $query->first();
|
|
|
|
|
if ($service === null) {
|
2020-04-24 08:36:37 -04:00
|
|
|
throw new NotFoundError(t('Service not found'));
|
2019-10-09 11:26:52 -04:00
|
|
|
}
|
|
|
|
|
|
2019-10-10 07:38:55 -04:00
|
|
|
$this->service = $service;
|
2021-08-17 11:25:54 -04:00
|
|
|
$this->loadTabsForObject($service);
|
2019-11-03 17:20:46 -05:00
|
|
|
|
|
|
|
|
$this->setTitleTab($this->getRequest()->getActionName());
|
2019-10-10 07:38:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function indexAction()
|
|
|
|
|
{
|
2019-11-28 05:35:38 -05:00
|
|
|
if ($this->service->state->is_overdue) {
|
|
|
|
|
$this->controls->addAttributes(['class' => 'overdue']);
|
|
|
|
|
}
|
2021-04-28 12:53:19 -04:00
|
|
|
|
|
|
|
|
$this->addControl((new ServiceList([$this->service]))
|
|
|
|
|
->setViewMode('minimal')
|
2021-04-28 12:55:39 -04:00
|
|
|
->setDetailActionsDisabled()
|
2021-04-28 12:53:19 -04:00
|
|
|
->setNoSubjectLink());
|
2019-11-03 09:34:09 -05:00
|
|
|
$this->addControl(new QuickActions($this->service));
|
|
|
|
|
|
2020-02-06 09:23:45 -05:00
|
|
|
$this->addContent(new ServiceDetail($this->service));
|
2019-12-11 02:59:30 -05:00
|
|
|
|
|
|
|
|
$this->setAutorefreshInterval(10);
|
2019-10-09 11:26:52 -04:00
|
|
|
}
|
2019-11-03 17:20:46 -05:00
|
|
|
|
2021-05-05 04:29:58 -04:00
|
|
|
public function sourceAction()
|
|
|
|
|
{
|
|
|
|
|
$this->assertPermission('icingadb/object/show-source');
|
|
|
|
|
$apiResult = (new CommandTransport())->send((new GetObjectCommand())->setObject($this->service));
|
|
|
|
|
|
|
|
|
|
if ($this->service->state->is_overdue) {
|
|
|
|
|
$this->controls->addAttributes(['class' => 'overdue']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->addControl((new ServiceList([$this->service]))
|
|
|
|
|
->setDetailActionsDisabled()
|
|
|
|
|
->setNoSubjectLink());
|
|
|
|
|
$this->addContent(new ServiceInspectionDetail(
|
|
|
|
|
$this->service,
|
|
|
|
|
reset($apiResult)
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 17:20:46 -05:00
|
|
|
public function historyAction()
|
|
|
|
|
{
|
2020-01-15 09:50:41 -05:00
|
|
|
$compact = $this->view->compact; // TODO: Find a less-legacy way..
|
2019-12-13 10:15:58 -05:00
|
|
|
|
2019-11-28 05:35:38 -05:00
|
|
|
if ($this->service->state->is_overdue) {
|
|
|
|
|
$this->controls->addAttributes(['class' => 'overdue']);
|
|
|
|
|
}
|
2019-12-13 10:15:58 -05:00
|
|
|
|
2019-11-03 17:20:46 -05:00
|
|
|
$db = $this->getDb();
|
|
|
|
|
|
|
|
|
|
$history = History::on($db)->with([
|
|
|
|
|
'host',
|
|
|
|
|
'host.service',
|
|
|
|
|
'host.state',
|
|
|
|
|
'service',
|
|
|
|
|
'service.state',
|
|
|
|
|
'comment',
|
|
|
|
|
'downtime',
|
2020-05-04 06:42:10 -04:00
|
|
|
'flapping',
|
2019-11-03 17:20:46 -05:00
|
|
|
'notification',
|
2020-03-10 03:18:28 -04:00
|
|
|
'acknowledgement',
|
2019-11-03 17:20:46 -05:00
|
|
|
'state'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$history
|
|
|
|
|
->getSelectBase()
|
2020-03-04 01:54:00 -05:00
|
|
|
->where([
|
|
|
|
|
'history_host_service.id = ?' => $this->service->id,
|
|
|
|
|
'history_service.id = ?' => $this->service->id
|
|
|
|
|
]);
|
2019-11-03 17:20:46 -05:00
|
|
|
|
2020-01-31 09:37:00 -05:00
|
|
|
$url = ServiceLinks::history($this->service, $this->service->host);
|
2019-12-13 10:15:58 -05:00
|
|
|
if (! $this->params->has('page') || ($page = (int) $this->params->shift('page')) < 1) {
|
|
|
|
|
$page = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-03 17:20:46 -05:00
|
|
|
$limitControl = $this->createLimitControl();
|
2019-12-13 10:15:58 -05:00
|
|
|
|
2020-01-31 08:01:01 -05:00
|
|
|
$history->peekAhead();
|
2019-12-13 10:15:58 -05:00
|
|
|
$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:46 -05:00
|
|
|
|
|
|
|
|
yield $this->export($history);
|
|
|
|
|
|
2020-01-31 08:01:01 -05:00
|
|
|
$results = $history->execute();
|
|
|
|
|
|
2019-12-13 10:15:58 -05:00
|
|
|
$showMore = (new ShowMore(
|
2020-01-31 08:01:01 -05:00
|
|
|
$results,
|
2020-01-17 09:21:14 -05:00
|
|
|
$url->setParam('page', $page + 1)
|
2019-12-13 10:15:58 -05:00
|
|
|
->setAnchor('page-' . ($page + 1))
|
|
|
|
|
))
|
2020-04-24 08:36:37 -04:00
|
|
|
->setLabel(t('Load More'))
|
2019-12-13 10:15:58 -05:00
|
|
|
->setAttribute('data-no-icinga-ajax', true);
|
|
|
|
|
|
2019-12-16 10:05:16 -05:00
|
|
|
$this->addControl((new ServiceList([$this->service]))->setViewMode('minimal'));
|
2019-11-03 17:20:46 -05:00
|
|
|
$this->addControl($limitControl);
|
|
|
|
|
|
2020-01-31 08:01:01 -05:00
|
|
|
$historyList = (new HistoryList($results))
|
2019-12-13 10:15:58 -05:00
|
|
|
->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:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function createTabs()
|
|
|
|
|
{
|
2021-05-05 04:29:58 -04:00
|
|
|
$tabs = $this->getTabs()
|
2019-11-03 17:20:46 -05:00
|
|
|
->add('index', [
|
2020-04-24 08:36:37 -04:00
|
|
|
'label' => t('Service'),
|
2019-11-03 17:20:46 -05:00
|
|
|
'url' => Links::service($this->service, $this->service->host)
|
|
|
|
|
])
|
|
|
|
|
->add('history', [
|
2020-04-24 08:36:37 -04:00
|
|
|
'label' => t('History'),
|
2019-11-03 17:20:46 -05:00
|
|
|
'url' => ServiceLinks::history($this->service, $this->service->host)
|
|
|
|
|
]);
|
2021-05-05 04:29:58 -04:00
|
|
|
|
|
|
|
|
if ($this->hasPermission('icingadb/object/show-source')) {
|
|
|
|
|
$tabs->add('source', [
|
|
|
|
|
'label' => t('Source'),
|
|
|
|
|
'url' => Links::serviceSource($this->service, $this->service->host)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-17 11:25:54 -04:00
|
|
|
foreach ($this->loadAdditionalTabs() as $name => $tab) {
|
|
|
|
|
$tabs->add($name, $tab + ['urlParams' => [
|
|
|
|
|
'name' => $this->service->name,
|
|
|
|
|
'host.name' => $this->service->host->name
|
|
|
|
|
]]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 04:29:58 -04:00
|
|
|
return $tabs;
|
2019-11-03 17:20:46 -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:04:44 -05:00
|
|
|
|
|
|
|
|
public function fetchCommandTargets()
|
|
|
|
|
{
|
|
|
|
|
return [$this->service];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCommandTargetsUrl()
|
|
|
|
|
{
|
|
|
|
|
return Links::service($this->service, $this->service->host);
|
|
|
|
|
}
|
2021-08-17 11:25:54 -04:00
|
|
|
|
|
|
|
|
protected function getDefaultTabControls()
|
|
|
|
|
{
|
|
|
|
|
return [(new ServiceList([$this->service]))->setDetailActionsDisabled()->setNoSubjectLink()];
|
|
|
|
|
}
|
2019-10-09 11:26:52 -04:00
|
|
|
}
|