icingaweb2/modules/monitoring/application/controllers/ShowController.php
Jannis Moßhammer b3e0d5e8ce Remove AbstractWidget and make Widget an interface
As Widget's already denote an abstract concept, the name
'AbstractWidget' is redundant. Also this class didn't do anything except
fetching a view via a singleton (which is now injected into the render method)
and bypassing the PHP class properties by creating a 'properties' array which is
filled with magic getters and setters (which now are simply php class properties)

Further changes:
- toString is removed, as this incorporated a lot of
   application logic which would cause unrecoverable
   errors when throwing exceptions
- renderToHtml is now just render and the view dependency must be
   passed, as a widget shouldn't be responsible for getting
   view instance (this means that <?= $this->tabs ?> is now
   <?= $this->tabs->render($this); ?> in the templates
- Controllers don't have $this->widget anymore as Widgets are
   directly instanciated with their class, allowing better code completion
   and avoiding hidden dependencies, also Widget::create is now removed
   in favor of direct instanciation.

refs #4192
2013-08-07 17:41:43 +02:00

513 lines
16 KiB
PHP

<?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
use Monitoring\Backend;
use Icinga\Web\ModuleActionController;
use Icinga\Web\Hook;
use Monitoring\Object\Host;
use Monitoring\Object\Service;
use Icinga\Application\Benchmark;
use Icinga\Web\Widget\Tabs;
/**
* Class Monitoring_ShowController
*
* Actions for show context
*/
class Monitoring_ShowController extends ModuleActionController
{
/**
* @var Backend
*/
protected $backend;
/**
* Initialize the controller
*/
public function init()
{
$host = $this->_getParam('host');
$service = $this->_getParam('service');
$this->backend = Backend::getInstance($this->_getParam('backend'));
$object = null;
// TODO: Do not allow wildcards in names!
if ($host !== null) {
// TODO: $this->assertPermission('host/read', $host);
if ($this->action_name !== 'host' && $service !== null && $service !== '*') {
// TODO: $this->assertPermission('service/read', $service);
$object = Service::fetch($this->backend, $host, $service);
} else {
$object = Host::fetch($this->backend, $host);
}
}
$this->backend = Backend::getInstance($this->_getParam('backend'));
if ($service !== null && $service !== '*') {
$this->view->service = $this->backend->fetchService($host, $service, true);
}
if ($host !== null) {
$this->view->host = $this->backend->fetchHost($host, true);
}
$this->view->compact = $this->_getParam('view') === 'compact';
if ($object === null) {
// TODO: Notification, not found
$this->redirectNow('monitoring/list/services');
return;
}
$this->view->object = $object;
$this->view->tabs = $this->createTabs();
$this->prepareTicketHook();
}
/**
* Service overview
*/
public function serviceAction()
{
Benchmark::measure('Entered service action');
$this->view->active = 'service';
$this->view->tabs->activate('service')->enableSpecialActions();
if ($grapher = Hook::get('grapher')) {
if ($grapher->hasGraph(
$this->view->service->host_name,
$this->view->service->service_description
)
) {
$this->view->preview_image = $grapher->getPreviewImage(
$this->view->service->host_name,
$this->view->service->service_description
);
}
}
$this->view->servicegroups = $this->backend->select()
->from(
'servicegroup',
array(
'servicegroup_name',
'servicegroup_alias'
)
)
->where('host_name', $this->view->host->host_name)
->where('service_description', $this->view->service->service_description)
->fetchPairs();
$this->view->contacts = $this->backend->select()
->from(
'contact',
array(
'contact_name',
'contact_alias',
'contact_email',
'contact_pager',
)
)
->where('service_host_name', $this->view->host->host_name)
->where('service_description', $this->view->service->service_description)
->fetchAll();
$this->view->contactgroups = $this->backend->select()
->from(
'contactgroup',
array(
'contactgroup_name',
'contactgroup_alias',
)
)
->where('service_host_name', $this->view->host->host_name)
->where('service_description', $this->view->service->service_description)
->fetchAll();
$this->view->comments = $this->backend->select()
->from(
'comment',
array(
'comment_timestamp',
'comment_author',
'comment_data',
'comment_type',
)
)
->where('service_host_name', $this->view->host->host_name)
->where('service_description', $this->view->service->service_description)
->fetchAll();
$this->view->downtimes = $this->backend->select()
->from(
'downtime',
array(
'host_name',
'service_description',
'downtime_type',
'downtime_author_name',
'downtime_comment_data',
'downtime_is_fixed',
'downtime_duration',
'downtime_scheduled_start_time',
'downtime_scheduled_end_time',
'downtime_actual_start_time',
'downtime_was_started',
'downtime_is_in_effect',
'downtime_internal_downtime_id'
)
)
->where('host_name', $this->view->host->host_name)
->where('service_description', $this->view->service->service_description)
->where('object_type','service')
->fetchAll();
$this->view->customvars = $this->backend->select()
->from(
'customvar',
array(
'varname',
'varvalue'
)
)
->where('varname', '-*PW*,-*PASS*')
->where('host_name', $this->view->host->host_name)
->where('service_description', $this->view->service->service_description)
->where('object_type', 'service')
->fetchPairs();
Benchmark::measure('Service action done');
$object = $this->view->object->prefetch();
$this->prepareGrapherHook();
}
/**
* Host overview
*/
public function hostAction()
{
$this->view->active = 'host';
$this->view->tabs->activate('host')->enableSpecialActions();
if ($grapher = Hook::get('grapher')) {
if ($grapher->hasGraph($this->view->host->host_name)) {
$this->view->preview_image = $grapher->getPreviewImage(
$this->view->host->host_name
);
}
}
$this->view->hostgroups = $this->backend->select()
->from(
'hostgroup',
array(
'hostgroup_name',
'hostgroup_alias'
)
)
->where('host_name', $this->view->host->host_name)
->fetchPairs();
$this->view->contacts = $this->backend->select()
->from(
'contact',
array(
'contact_name',
'contact_alias',
'contact_email',
'contact_pager',
)
)
->where('host_name', $this->view->host->host_name)
->fetchAll();
$this->view->contactgroups = $this->backend->select()
->from(
'contactgroup',
array(
'contactgroup_name',
'contactgroup_alias',
)
)
->where('host_name', $this->view->host->host_name)
->fetchAll();
$this->view->comments = $this->backend->select()
->from(
'comment',
array(
'comment_timestamp',
'comment_author',
'comment_data',
'comment_type',
)
)
->where('host_name', $this->view->host->host_name)
->fetchAll();
$this->view->downtimes = $this->backend->select()
->from(
'downtime',
array(
'host_name',
'downtime_type',
'downtime_author_name',
'downtime_comment_data',
'downtime_is_fixed',
'downtime_duration',
'downtime_scheduled_start_time',
'downtime_scheduled_end_time',
'downtime_actual_start_time',
'downtime_was_started',
'downtime_is_in_effect',
'downtime_internal_downtime_id'
)
)
->where('host_name', $this->view->host->host_name)
->where('object_type','host')
->fetchAll();
$this->view->customvars = $this->backend->select()
->from(
'customvar',
array(
'varname',
'varvalue'
)
)
->where('varname', '-*PW*,-*PASS*')
->where('host_name', $this->view->host->host_name)
->where('object_type', 'host')
->fetchPairs();
$this->view->object->prefetch();
$this->prepareGrapherHook();
}
/**
* History entries for objects
*/
public function historyAction()
{
$this->view->history = $this->backend->select()
->from(
'eventHistory',
array(
'object_type',
'host_name',
'service_description',
'timestamp',
'state',
'attempt',
'max_attempts',
'output',
'type'
)
)->applyRequest($this->_request);
$this->view->preserve = $this->view->history->getAppliedFilter()->toParams();
if ($this->_getParam('dump') === 'sql') {
echo '<pre>' . htmlspecialchars($this->view->history->getQuery()->dump()) . '</pre>';
exit;
}
if ($this->_getParam('sort')) {
$this->view->preserve['sort'] = $this->_getParam('sort');
}
$this->view->preserve = $this->view->history->getAppliedFilter()->toParams();
}
/**
* Service overview
*/
public function servicesAction()
{
$this->_setParam('service', null);
// Ugly and slow:
$this->view->services = $this->view->action(
'services',
'list',
'monitoring',
array(
'host_name' => $this->view->host->host_name,
//'sort', 'service_description'
)
);
$this->view->services = $this->view->action('services', 'list', 'monitoring', array(
'view' => 'compact'
));
}
/**
* Ticets actions
*/
public function ticketAction()
{
if (Hook::has('ticket')) {
// TODO: Still hardcoded, should ask for URL:
$id = $this->_getParam('ticket');
$ticketModule = 'rt';
$this->render();
$this->redirect(
'ticket',
'show',
$ticketModule,
array(
'id' => $id
)
);
}
}
protected function prepareTicketHook()
{
if (Hook::has('ticket')) {
$object = $this->view->object;
$params = array(
'host' => $object->host_name
);
if ($object instanceof Service) {
$params['service'] = $object->service_description;
}
$params['ticket'] = '__ID__';
$this->view->ticket_link = preg_replace(
'~__ID__~',
'\$1',
$this->view->qlink('#__ID__',
'monitoring/show/ticket',
$params
)
);
// TODO: Global ticket pattern config (or per environment)
$this->view->ticket_pattern = '~#(\d{4,6})~';
}
}
protected function prepareGrapherHook()
{
if ($grapher = Hook::get('grapher')) {
$object = $this->view->object;
if ($grapher->hasGraph(
$object->host_name,
$object->service_description
)) {
$this->view->preview_image = $grapher->getPreviewImage(
$object->host_name,
$object->service_description
);
}
}
}
/**
* Creating tabs for this controller
* @return \Icinga\Web\Widget\AbstractWidget
*/
protected function createTabs()
{
$object = $this->view->object;
$tabs = new Tabs();
if (!$this->view->host) {
return $tabs;
}
$params = array(
'host' => $this->view->host->host_name,
);
if ($backend = $this->_getParam('backend')) {
$params['backend'] = $backend;
}
if ($object instanceof Service) {
$params['service'] = $object->service_description;
} elseif ($service = $this->_getParam('service')) {
$params['service'] = $service;
}
$tabs->add(
'host',
array(
'title' => 'Host',
'icon' => 'img/classic/server.png',
'url' => 'monitoring/show/host',
'urlParams' => $params,
)
);
if (!isset($this->view->service)) {
$tabs->add(
'services',
array(
'title' => 'Services',
'icon' => 'img/classic/service.png',
'url' => 'monitoring/show/services',
'urlParams' => $params,
)
);
}
if (isset($params['service'])) {
$tabs->add(
'service',
array(
'title' => 'Service',
'icon' => 'img/classic/service.png',
'url' => 'monitoring/show/service',
'urlParams' => $params,
)
);
}
$tabs->add(
'history',
array(
'title' => 'History',
'icon' => 'img/classic/history.gif',
'url' => 'monitoring/show/history',
'urlParams' => $params,
)
);
if ($this->action_name === 'ticket') {
$tabs->add(
'ticket',
array(
'title' => 'Ticket',
'icon' => 'img/classic/ticket.gif',
'url' => 'monitoring/show/ticket',
'urlParams' => $params + array('ticket' => $this->_getParam('ticket')),
)
);
}
$tabs->activate($this->action_name)->enableSpecialActions();
/**
$tabs->add('contacts', array(
'title' => 'Contacts',
'icon' => 'img/classic/customer.png',
'url' => 'monitoring/detail/contacts',
'urlParams' => $params,
));
*/
// TODO: Inventory 'img/classic/facts.gif'
// Ticket 'img/classic/ticket.gif'
// Customer 'img/classic/customer.png'
return $tabs;
}
}
// @codingStandardsIgnoreEnd