2013-06-27 10:14:41 +02:00
|
|
|
<?php
|
2013-08-14 12:42:32 +02:00
|
|
|
// @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}}}
|
2013-08-20 11:44:05 +02:00
|
|
|
|
2013-08-20 15:45:44 +02:00
|
|
|
use \Icinga\Application\Benchmark;
|
|
|
|
use \Icinga\Data\Db\Query;
|
|
|
|
use \Icinga\File\Csv;
|
2013-08-30 15:50:49 +02:00
|
|
|
use \Icinga\Web\Controller\ActionController;
|
2013-08-20 11:44:05 +02:00
|
|
|
use \Icinga\Web\Hook;
|
2013-08-20 15:45:44 +02:00
|
|
|
use \Icinga\Web\Widget\Tabextension\DashboardAction;
|
|
|
|
use \Icinga\Web\Widget\Tabextension\OutputFormat;
|
|
|
|
use \Icinga\Web\Widget\Tabs;
|
2013-09-11 17:19:18 +02:00
|
|
|
use \Icinga\Module\Monitoring\Backend;
|
2013-09-03 18:43:17 +02:00
|
|
|
use \Icinga\Web\Widget\SortBox;
|
2013-09-11 17:19:18 +02:00
|
|
|
use \Icinga\Application\Config as IcingaConfig;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2013-09-24 12:48:30 +02:00
|
|
|
/**
|
|
|
|
* Controller for listing views
|
|
|
|
*/
|
2013-08-30 15:50:49 +02:00
|
|
|
class Monitoring_ListController extends ActionController
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-08-14 12:42:32 +02:00
|
|
|
/**
|
|
|
|
* The backend used for this controller
|
|
|
|
*
|
2013-08-20 15:45:44 +02:00
|
|
|
* @var Backend
|
2013-08-14 12:42:32 +02:00
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
protected $backend;
|
2013-08-14 12:42:32 +02:00
|
|
|
/**
|
2013-09-24 11:21:09 +02:00
|
|
|
* Compact layout name
|
|
|
|
*
|
2013-08-14 12:42:32 +02:00
|
|
|
* Set to a string containing the compact layout name to use when
|
|
|
|
* 'compact' is set as the layout parameter, otherwise null
|
|
|
|
*
|
2013-08-20 15:45:44 +02:00
|
|
|
* @var string
|
2013-08-14 12:42:32 +02:00
|
|
|
*/
|
2013-08-20 15:45:44 +02:00
|
|
|
private $compactView;
|
2013-06-27 10:14:41 +02:00
|
|
|
|
2013-08-14 12:42:32 +02:00
|
|
|
/**
|
|
|
|
* Retrieve backend and hooks for this controller
|
|
|
|
*
|
|
|
|
* @see ActionController::init
|
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->backend = Backend::getInstance($this->_getParam('backend'));
|
|
|
|
$this->view->grapher = Hook::get('grapher');
|
2013-08-08 16:22:22 +02:00
|
|
|
$this->createTabs();
|
2013-09-11 17:19:18 +02:00
|
|
|
$this->view->activeRowHref = $this->getParam('detail');
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
|
2013-08-30 15:50:49 +02:00
|
|
|
/**
|
|
|
|
* Overwrite the backend to use (used for testing)
|
|
|
|
*
|
|
|
|
* @param Backend $backend The Backend that should be used for querying
|
|
|
|
*/
|
|
|
|
public function setBackend($backend)
|
|
|
|
{
|
|
|
|
$this->backend = $backend;
|
|
|
|
}
|
|
|
|
|
2013-08-14 12:42:32 +02:00
|
|
|
/**
|
|
|
|
* Display host list
|
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
public function hostsAction()
|
|
|
|
{
|
2013-07-12 13:53:11 +02:00
|
|
|
Benchmark::measure("hostsAction::query()");
|
2013-08-06 11:53:42 +02:00
|
|
|
$this->compactView = "hosts-compact";
|
2013-07-19 18:04:13 +02:00
|
|
|
$this->view->hosts = $this->query(
|
2013-07-12 13:53:11 +02:00
|
|
|
'status',
|
|
|
|
array(
|
|
|
|
'host_icon_image',
|
|
|
|
'host_name',
|
|
|
|
'host_state',
|
|
|
|
'host_address',
|
|
|
|
'host_acknowledged',
|
|
|
|
'host_output',
|
2013-07-19 11:29:51 +02:00
|
|
|
'host_long_output',
|
2013-07-12 13:53:11 +02:00
|
|
|
'host_in_downtime',
|
2013-07-12 15:14:55 +02:00
|
|
|
'host_is_flapping',
|
|
|
|
'host_state_type',
|
2013-07-12 13:53:11 +02:00
|
|
|
'host_handled',
|
2013-07-19 11:29:51 +02:00
|
|
|
'host_last_check',
|
2013-07-12 13:53:11 +02:00
|
|
|
'host_last_state_change',
|
2013-07-12 15:14:55 +02:00
|
|
|
'host_notifications_enabled',
|
2013-07-16 16:16:56 +02:00
|
|
|
'host_unhandled_service_count',
|
2013-07-15 13:21:48 +02:00
|
|
|
'host_action_url',
|
|
|
|
'host_notes_url',
|
|
|
|
'host_last_comment'
|
2013-07-12 13:53:11 +02:00
|
|
|
)
|
|
|
|
);
|
2013-09-03 18:43:17 +02:00
|
|
|
$this->setupSortControl(array(
|
|
|
|
'host_last_check' => 'Last Host Check',
|
|
|
|
'host_severity' => 'Host Severity',
|
|
|
|
'host_state' => 'Current Host State',
|
|
|
|
'host_name' => 'Host Name',
|
|
|
|
'host_address' => 'Address',
|
|
|
|
'host_state' => 'Hard State'
|
|
|
|
));
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 12:42:32 +02:00
|
|
|
/**
|
|
|
|
* Display service list
|
|
|
|
*/
|
2013-06-27 10:14:41 +02:00
|
|
|
public function servicesAction()
|
|
|
|
{
|
2013-08-07 17:44:18 +02:00
|
|
|
$this->compactView = "services-compact";
|
|
|
|
|
2013-07-12 14:33:17 +02:00
|
|
|
$this->view->services = $this->query('status', array(
|
2013-06-27 10:14:41 +02:00
|
|
|
'host_name',
|
2013-07-19 17:45:51 +02:00
|
|
|
'host_state',
|
|
|
|
'host_state_type',
|
|
|
|
'host_last_state_change',
|
|
|
|
'host_address',
|
|
|
|
'host_handled',
|
2013-06-27 10:14:41 +02:00
|
|
|
'service_description',
|
2013-07-19 17:45:51 +02:00
|
|
|
'service_display_name',
|
2013-09-03 18:43:17 +02:00
|
|
|
'service_state' => 'service_state',
|
2013-06-27 10:14:41 +02:00
|
|
|
'service_in_downtime',
|
|
|
|
'service_acknowledged',
|
|
|
|
'service_handled',
|
|
|
|
'service_output',
|
2013-09-03 18:43:17 +02:00
|
|
|
'service_last_state_change' => 'service_last_state_change',
|
2013-07-19 17:45:51 +02:00
|
|
|
'service_icon_image',
|
|
|
|
'service_long_output',
|
|
|
|
'service_is_flapping',
|
|
|
|
'service_state_type',
|
|
|
|
'service_handled',
|
|
|
|
'service_severity',
|
|
|
|
'service_last_check',
|
|
|
|
'service_notifications_enabled',
|
|
|
|
'service_action_url',
|
|
|
|
'service_notes_url',
|
|
|
|
'service_last_comment'
|
2013-07-12 14:33:17 +02:00
|
|
|
));
|
2013-09-03 18:43:17 +02:00
|
|
|
|
|
|
|
$this->setupSortControl(array(
|
|
|
|
'service_last_check' => 'Last Service Check',
|
|
|
|
'service_severity' => 'Severity',
|
|
|
|
'service_state' => 'Current Service State',
|
|
|
|
'service_description' => 'Service Name',
|
|
|
|
'service_state_type' => 'Hard State',
|
|
|
|
'host_severity' => 'Host Severity',
|
|
|
|
'host_state' => 'Current Host State',
|
|
|
|
'host_name' => 'Host Name',
|
|
|
|
'host_address' => 'Host Address',
|
|
|
|
'host_last_check' => 'Last Host Check'
|
2013-08-21 00:44:50 +02:00
|
|
|
));
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
|
|
|
|
2013-07-30 17:17:19 +02:00
|
|
|
/**
|
2013-08-20 11:44:05 +02:00
|
|
|
* Fetch the current downtimes and put them into the view property `downtimes`
|
2013-07-30 17:17:19 +02:00
|
|
|
*/
|
|
|
|
public function downtimesAction()
|
|
|
|
{
|
2013-09-03 18:43:17 +02:00
|
|
|
$this->setDefaultSortColumn('downtime_is_in_effect');
|
|
|
|
$this->view->downtimes = $this->query('downtime', array(
|
|
|
|
'host_name',
|
|
|
|
'object_type',
|
|
|
|
'service_description',
|
|
|
|
'downtime_entry_time',
|
|
|
|
'downtime_internal_downtime_id',
|
|
|
|
'downtime_author_name',
|
|
|
|
'downtime_comment_data',
|
|
|
|
'downtime_duration',
|
|
|
|
'downtime_scheduled_start_time',
|
|
|
|
'downtime_scheduled_end_time',
|
|
|
|
'downtime_is_fixed',
|
|
|
|
'downtime_is_in_effect',
|
|
|
|
'downtime_triggered_by_id',
|
|
|
|
'downtime_trigger_time'
|
2013-07-30 17:17:19 +02:00
|
|
|
));
|
2013-09-03 18:43:17 +02:00
|
|
|
|
|
|
|
$this->setupSortControl(array(
|
|
|
|
'downtime_is_in_effect' => 'Is In Effect',
|
|
|
|
'object_type' => 'Service/Host',
|
|
|
|
'host_name' => 'Host Name',
|
|
|
|
'service_description' => 'Service Name',
|
|
|
|
'downtime_entry_time' => 'Entry Time',
|
|
|
|
'downtime_author_name' => 'Author',
|
|
|
|
'downtime_comment_data' => 'Comment',
|
|
|
|
'downtime_scheduled_start_time' => 'Start',
|
|
|
|
'downtime_scheduled_end_time' => 'End',
|
|
|
|
'downtime_trigger_time' => 'Trigger Time',
|
|
|
|
'downtime_internal_downtime_id' => 'Downtime ID',
|
|
|
|
'downtime_duration' => 'Duration',
|
|
|
|
));
|
2013-07-30 17:17:19 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 12:42:32 +02:00
|
|
|
/**
|
2013-08-09 11:23:42 +02:00
|
|
|
* Display notification overview
|
2013-08-14 12:42:32 +02:00
|
|
|
*/
|
2013-08-09 11:23:42 +02:00
|
|
|
public function notificationsAction()
|
|
|
|
{
|
2013-09-03 18:43:17 +02:00
|
|
|
$this->setDefaultSortColumn('notification_start_time', 'DESC');
|
2013-08-12 11:09:22 +02:00
|
|
|
$this->view->notifications = $this->query(
|
|
|
|
'notification',
|
|
|
|
array(
|
2013-08-09 11:23:42 +02:00
|
|
|
'host_name',
|
|
|
|
'service_description',
|
|
|
|
'notification_type',
|
2013-08-09 15:47:34 +02:00
|
|
|
'notification_reason',
|
2013-08-09 11:23:42 +02:00
|
|
|
'notification_start_time',
|
|
|
|
'notification_contact',
|
|
|
|
'notification_information',
|
2013-08-09 15:47:34 +02:00
|
|
|
'notification_command'
|
2013-08-12 11:09:22 +02:00
|
|
|
)
|
|
|
|
);
|
2013-09-03 18:43:17 +02:00
|
|
|
$this->setupSortControl(array(
|
|
|
|
'notification_start_time' => 'Notification Start'
|
|
|
|
));
|
2013-08-09 11:23:42 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:44:05 +02:00
|
|
|
/**
|
|
|
|
* Create query
|
|
|
|
*
|
2013-08-20 15:45:44 +02:00
|
|
|
* @param string $view
|
|
|
|
* @param array $columns
|
2013-08-20 11:44:05 +02:00
|
|
|
*
|
2013-08-20 15:45:44 +02:00
|
|
|
* @return Query
|
2013-08-20 11:44:05 +02:00
|
|
|
*/
|
2013-08-20 15:45:44 +02:00
|
|
|
private function query($view, $columns)
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
|
|
|
$extra = preg_split(
|
|
|
|
'~,~',
|
|
|
|
$this->_getParam('extracolumns', ''),
|
|
|
|
-1,
|
|
|
|
PREG_SPLIT_NO_EMPTY
|
|
|
|
);
|
2013-08-21 00:44:50 +02:00
|
|
|
if (empty($extra)) {
|
|
|
|
$cols = $columns;
|
|
|
|
} else {
|
|
|
|
$cols = array_merge($columns, $extra);
|
|
|
|
}
|
2013-07-12 14:33:17 +02:00
|
|
|
$this->view->extraColumns = $extra;
|
|
|
|
$query = $this->backend->select()
|
2013-08-21 00:44:50 +02:00
|
|
|
->from($view, $cols)
|
2013-07-12 14:33:17 +02:00
|
|
|
->applyRequest($this->_request);
|
|
|
|
$this->handleFormatRequest($query);
|
2013-09-12 17:26:29 +02:00
|
|
|
return $query->paginate();
|
2013-07-12 14:33:17 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 12:42:32 +02:00
|
|
|
/**
|
|
|
|
* Handle the 'format' and 'view' parameter
|
|
|
|
*
|
2013-08-20 15:45:44 +02:00
|
|
|
* @param Query $query The current query
|
2013-08-14 12:42:32 +02:00
|
|
|
*/
|
2013-08-20 15:45:44 +02:00
|
|
|
private function handleFormatRequest($query)
|
2013-07-12 14:33:17 +02:00
|
|
|
{
|
2013-08-08 16:33:42 +02:00
|
|
|
if ($this->compactView !== null && ($this->_getParam('view', false) === 'compact')) {
|
2013-08-06 11:53:42 +02:00
|
|
|
$this->_helper->viewRenderer($this->compactView);
|
|
|
|
}
|
|
|
|
|
2013-09-11 17:19:18 +02:00
|
|
|
if ($this->_getParam('format') === 'sql'
|
|
|
|
&& IcingaConfig::app()->global->get('environment', 'production') === 'development') {
|
2013-07-12 14:33:17 +02:00
|
|
|
echo '<pre>'
|
|
|
|
. htmlspecialchars(wordwrap($query->getQuery()->dump()))
|
|
|
|
. '</pre>';
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if ($this->_getParam('format') === 'json'
|
|
|
|
|| $this->_request->getHeader('Accept') === 'application/json')
|
|
|
|
{
|
|
|
|
header('Content-type: application/json');
|
|
|
|
echo json_encode($query->fetchAll());
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
if ($this->_getParam('format') === 'csv'
|
|
|
|
|| $this->_request->getHeader('Accept') === 'text/csv') {
|
|
|
|
Csv::fromQuery($query)->dump();
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 12:42:32 +02:00
|
|
|
/**
|
2013-09-03 18:43:17 +02:00
|
|
|
* Set the default sort column for this action if none is provided
|
2013-08-14 12:42:32 +02:00
|
|
|
*
|
2013-09-03 18:43:17 +02:00
|
|
|
* @param string $column The column to use for sorting
|
|
|
|
* @param string $dir The direction ('ASC' or 'DESC')
|
2013-08-14 12:42:32 +02:00
|
|
|
*/
|
2013-09-03 18:43:17 +02:00
|
|
|
private function setDefaultSortColumn($column, $dir = 'DESC')
|
2013-06-27 10:14:41 +02:00
|
|
|
{
|
2013-08-08 16:22:22 +02:00
|
|
|
|
2013-09-03 18:43:17 +02:00
|
|
|
$this->setParam('sort', $this->getParam('sort', $column));
|
|
|
|
$this->setParam('dir', $this->getParam('dir', $dir));
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
2013-07-30 17:17:19 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-03 18:43:17 +02:00
|
|
|
* Create a sort control box at the 'sortControl' view parameter
|
|
|
|
*
|
|
|
|
* @param array $columns An array containing the sort columns, with the
|
|
|
|
* submit value as the key and the value as the label
|
2013-07-30 17:17:19 +02:00
|
|
|
*/
|
2013-09-03 18:43:17 +02:00
|
|
|
private function setupSortControl(array $columns)
|
2013-07-30 17:17:19 +02:00
|
|
|
{
|
2013-09-03 18:43:17 +02:00
|
|
|
$this->view->sortControl = new SortBox(
|
|
|
|
$this->getRequest()->getActionName(),
|
|
|
|
$columns
|
|
|
|
);
|
|
|
|
$this->view->sortControl->applyRequest($this->getRequest());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all tabs for this controller
|
|
|
|
*
|
|
|
|
* @return Tabs
|
|
|
|
*/
|
|
|
|
private function createTabs()
|
|
|
|
{
|
|
|
|
|
|
|
|
$tabs = $this->getTabs();
|
|
|
|
$tabs->extend(new OutputFormat())
|
|
|
|
->extend(new DashboardAction());
|
2013-07-30 17:17:19 +02:00
|
|
|
}
|
2013-06-27 10:14:41 +02:00
|
|
|
}
|
2013-08-14 12:42:32 +02:00
|
|
|
// @codingStandardsIgnoreEnd
|