2013-10-11 12:05:05 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
2013-10-11 12:05:05 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-10-11 12:05:05 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* @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>
|
|
|
|
*
|
2013-10-11 12:05:05 +02:00
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2013-10-17 19:08:00 +02:00
|
|
|
use \Icinga\Web\Form;
|
2013-10-18 09:59:06 +02:00
|
|
|
use \Icinga\Web\Controller\ActionController;
|
2013-10-18 11:05:51 +02:00
|
|
|
use \Icinga\Web\Widget\Tabextension\OutputFormat;
|
2013-10-15 12:39:06 +02:00
|
|
|
use \Icinga\Module\Monitoring\Backend;
|
2014-04-02 18:23:01 +02:00
|
|
|
use \Icinga\Data\BaseQuery;
|
2014-04-16 19:43:56 +02:00
|
|
|
use \Icinga\Web\Widget\Chart\InlinePie;
|
2013-10-16 18:50:19 +02:00
|
|
|
use \Icinga\Module\Monitoring\Form\Command\MultiCommandFlagForm;
|
2014-04-02 18:23:01 +02:00
|
|
|
use \Icinga\Module\Monitoring\DataView\HostStatus as HostStatusView;
|
2013-10-18 16:43:37 +02:00
|
|
|
use \Icinga\Module\Monitoring\DataView\ServiceStatus as ServiceStatusView;
|
2014-04-02 18:23:01 +02:00
|
|
|
use \Icinga\Module\Monitoring\DataView\Comment as CommentView;
|
2013-10-16 18:50:19 +02:00
|
|
|
|
2013-10-11 12:05:05 +02:00
|
|
|
/**
|
|
|
|
* Displays aggregations collections of multiple objects.
|
|
|
|
*/
|
|
|
|
class Monitoring_MultiController extends ActionController
|
|
|
|
{
|
|
|
|
public function init()
|
|
|
|
{
|
2014-04-02 18:23:01 +02:00
|
|
|
$this->view->queries = $this->getAllParamsAsArray();
|
2013-10-15 12:39:06 +02:00
|
|
|
$this->backend = Backend::createBackend($this->_getParam('backend'));
|
2013-10-18 11:05:51 +02:00
|
|
|
$this->createTabs();
|
2013-10-11 12:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hostAction()
|
|
|
|
{
|
2013-10-18 16:43:37 +02:00
|
|
|
$filters = $this->view->queries;
|
2014-04-02 18:23:01 +02:00
|
|
|
$errors = array();
|
2013-10-15 12:39:06 +02:00
|
|
|
|
2014-04-02 18:23:01 +02:00
|
|
|
// Fetch Hosts
|
|
|
|
$hostQuery = HostStatusView::fromRequest(
|
2013-10-18 16:43:37 +02:00
|
|
|
$this->_request,
|
|
|
|
array(
|
|
|
|
'host_name',
|
|
|
|
'host_in_downtime',
|
|
|
|
'host_unhandled_service_count',
|
|
|
|
'host_passive_checks_enabled',
|
|
|
|
'host_obsessing',
|
|
|
|
'host_notifications_enabled',
|
|
|
|
'host_event_handler_enabled',
|
|
|
|
'host_flap_detection_enabled',
|
|
|
|
'host_active_checks_enabled'
|
|
|
|
)
|
|
|
|
)->getQuery();
|
|
|
|
if ($this->_getParam('host') !== '*') {
|
2014-04-02 18:23:01 +02:00
|
|
|
$this->applyQueryFilter($hostQuery, $filters);
|
2013-10-15 17:52:30 +02:00
|
|
|
}
|
2014-04-02 18:23:01 +02:00
|
|
|
$hosts = $hostQuery->fetchAll();
|
2013-10-18 16:43:37 +02:00
|
|
|
|
2014-04-02 18:23:01 +02:00
|
|
|
// Fetch comments
|
|
|
|
$commentQuery = $this->applyQueryFilter(
|
|
|
|
CommentView::fromRequest($this->_request)->getQuery(),
|
|
|
|
$filters,
|
|
|
|
'comment_host'
|
|
|
|
);
|
|
|
|
$comments = array_keys($this->getUniqueValues($commentQuery->fetchAll(), 'comment_internal_id'));
|
2013-10-18 16:43:37 +02:00
|
|
|
|
2014-04-02 18:23:01 +02:00
|
|
|
// Populate view
|
|
|
|
$this->view->objects = $this->view->hosts = $hosts;
|
|
|
|
$this->view->problems = $this->getProblems($hosts);
|
|
|
|
$this->view->comments = isset($comments) ? $comments : $this->getComments($hosts);
|
2013-10-18 09:59:06 +02:00
|
|
|
$this->view->hostnames = $this->getProperties($hosts, 'host_name');
|
2013-10-17 19:08:00 +02:00
|
|
|
$this->view->downtimes = $this->getDowntimes($hosts);
|
2014-04-02 18:23:01 +02:00
|
|
|
$this->view->errors = $errors;
|
2014-04-16 19:43:56 +02:00
|
|
|
$this->view->states = $this->countStates($hosts);
|
|
|
|
$this->view->pie = $this->createPie($this->view->states);
|
2013-10-16 18:50:19 +02:00
|
|
|
|
2014-04-02 18:23:01 +02:00
|
|
|
// Handle configuration changes
|
2013-10-18 16:43:37 +02:00
|
|
|
$this->handleConfigurationForm(array(
|
|
|
|
'host_passive_checks_enabled' => 'Passive Checks',
|
2014-04-02 18:23:01 +02:00
|
|
|
'host_active_checks_enabled' => 'Active Checks',
|
|
|
|
'host_obsessing' => 'Obsessing',
|
|
|
|
'host_notifications_enabled' => 'Notifications',
|
|
|
|
'host_event_handler_enabled' => 'Event Handler',
|
2013-10-18 16:43:37 +02:00
|
|
|
'host_flap_detection_enabled' => 'Flap Detection'
|
|
|
|
));
|
2013-10-11 12:05:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-09 19:11:56 +02:00
|
|
|
|
|
|
|
public function serviceAction()
|
|
|
|
{
|
|
|
|
$filters = $this->view->queries;
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
$backendQuery = ServiceStatusView::fromRequest(
|
|
|
|
$this->_request,
|
|
|
|
array(
|
|
|
|
'host_name',
|
2014-04-16 19:43:56 +02:00
|
|
|
'host_state',
|
2014-04-09 19:11:56 +02:00
|
|
|
'service_description',
|
|
|
|
'service_handled',
|
|
|
|
'service_state',
|
|
|
|
'service_in_downtime',
|
|
|
|
'service_passive_checks_enabled',
|
|
|
|
'service_notifications_enabled',
|
|
|
|
'service_event_handler_enabled',
|
|
|
|
'service_flap_detection_enabled',
|
|
|
|
'service_active_checks_enabled'
|
|
|
|
)
|
|
|
|
)->getQuery();
|
|
|
|
if ($this->_getParam('service') !== '*' && $this->_getParam('host') !== '*') {
|
|
|
|
$this->applyQueryFilter($backendQuery, $filters);
|
|
|
|
$this->applyQueryFilter($backendQuery, $filters);
|
|
|
|
}
|
|
|
|
$services = $backendQuery->fetchAll();
|
|
|
|
|
|
|
|
// Comments
|
|
|
|
$commentQuery = $this->applyQueryFilter(
|
|
|
|
CommentView::fromRequest($this->_request)->getQuery(),
|
|
|
|
$filters,
|
|
|
|
'comment_host',
|
|
|
|
'comment_service'
|
|
|
|
);
|
|
|
|
$comments = array_keys($this->getUniqueValues($commentQuery->fetchAll(), 'comment_internal_id'));
|
|
|
|
|
2014-04-16 19:43:56 +02:00
|
|
|
$this->view->objects = $this->view->services = $services;
|
|
|
|
$this->view->problems = $this->getProblems($services);
|
|
|
|
$this->view->comments = isset($comments) ? $comments : $this->getComments($services);
|
|
|
|
$this->view->hostnames = $this->getProperties($services, 'host_name');
|
|
|
|
$this->view->servicenames = $this->getProperties($services, 'service_description');
|
|
|
|
$this->view->downtimes = $this->getDowntimes($services);
|
|
|
|
$this->view->service_states = $this->countStates($services);
|
|
|
|
$this->view->host_states = $this->countStates($services, 'host_name', 'host_state');
|
|
|
|
$this->view->service_pie = $this->createPie($this->view->service_states);
|
|
|
|
$this->view->host_pie = $this->createPie($this->view->host_states);
|
|
|
|
$this->view->errors = $errors;
|
2014-04-09 19:11:56 +02:00
|
|
|
|
|
|
|
$this->handleConfigurationForm(array(
|
|
|
|
'service_passive_checks_enabled' => 'Passive Checks',
|
|
|
|
'service_active_checks_enabled' => 'Active Checks',
|
|
|
|
'service_notifications_enabled' => 'Notifications',
|
|
|
|
'service_event_handler_enabled' => 'Event Handler',
|
|
|
|
'service_flap_detection_enabled' => 'Flap Detection'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2014-04-16 19:43:56 +02:00
|
|
|
|
2013-10-18 16:43:37 +02:00
|
|
|
/**
|
2014-04-02 18:23:01 +02:00
|
|
|
* Apply the query-filter received
|
|
|
|
*
|
|
|
|
* @param $backendQuery BaseQuery The query to apply the filter to
|
|
|
|
* @param $filter array Containing the queries of the current request, converted into an
|
|
|
|
* array-structure.
|
|
|
|
* @param $hostColumn string The name of the host-column in the BaseQuery, defaults to 'host_name'
|
|
|
|
* @param $serviceColumn string The name of the service-column in the BaseQuery, defaults to 'service-description'
|
|
|
|
*
|
|
|
|
* @return BaseQuery The given BaseQuery
|
2013-10-18 16:43:37 +02:00
|
|
|
*/
|
2014-04-02 18:23:01 +02:00
|
|
|
private function applyQueryFilter(
|
|
|
|
BaseQuery $backendQuery,
|
|
|
|
array $filter,
|
|
|
|
$hostColumn = 'host_name',
|
|
|
|
$serviceColumn = 'service_description'
|
|
|
|
) {
|
2013-10-18 16:43:37 +02:00
|
|
|
// fetch specified hosts
|
|
|
|
foreach ($filter as $index => $expr) {
|
2014-04-02 18:23:01 +02:00
|
|
|
// Every query entry must define at least the host.
|
2013-10-18 16:43:37 +02:00
|
|
|
if (!array_key_exists('host', $expr)) {
|
|
|
|
$errors[] = 'Query ' . $index . ' misses property host.';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// apply filter expressions from query
|
2014-04-02 18:23:01 +02:00
|
|
|
$backendQuery->orWhere($hostColumn, $expr['host']);
|
2013-10-18 16:43:37 +02:00
|
|
|
if (array_key_exists('service', $expr)) {
|
2014-04-02 18:23:01 +02:00
|
|
|
$backendQuery->andWhere($serviceColumn, $expr['service']);
|
2013-10-18 16:43:37 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-02 18:23:01 +02:00
|
|
|
return $backendQuery;
|
2013-10-18 16:43:37 +02:00
|
|
|
}
|
|
|
|
|
2013-10-17 19:08:00 +02:00
|
|
|
/**
|
|
|
|
* Create an array with all unique values as keys.
|
|
|
|
*
|
|
|
|
* @param array $values The array containing the objects
|
|
|
|
* @param $key The key to access
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-10-20 15:15:30 +02:00
|
|
|
private function getUniqueValues($values, $key)
|
2013-10-17 19:08:00 +02:00
|
|
|
{
|
|
|
|
$unique = array();
|
|
|
|
foreach ($values as $value)
|
|
|
|
{
|
2013-10-18 09:59:06 +02:00
|
|
|
if (is_array($value)) {
|
|
|
|
$unique[$value[$key]] = $value[$key];
|
|
|
|
} else {
|
2014-04-02 18:23:01 +02:00
|
|
|
$unique[$value->$key] = $value->$key;
|
2013-10-18 09:59:06 +02:00
|
|
|
}
|
2013-10-17 19:08:00 +02:00
|
|
|
}
|
|
|
|
return $unique;
|
|
|
|
}
|
|
|
|
|
2013-10-18 16:43:37 +02:00
|
|
|
/**
|
2014-04-02 18:23:01 +02:00
|
|
|
* Get the numbers of problems of the given objects
|
2013-10-18 16:43:37 +02:00
|
|
|
*
|
2014-04-02 18:23:01 +02:00
|
|
|
* @param $objects The objects containing the problems
|
|
|
|
*
|
|
|
|
* @return int The problem count
|
2013-10-18 16:43:37 +02:00
|
|
|
*/
|
2013-10-20 15:15:30 +02:00
|
|
|
private function getProblems($objects)
|
2013-10-18 16:43:37 +02:00
|
|
|
{
|
|
|
|
$problems = 0;
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
if (property_exists($object, 'host_unhandled_service_count')) {
|
2014-04-02 18:23:01 +02:00
|
|
|
$problems += $object->host_unhandled_service_count;
|
2013-10-18 16:43:37 +02:00
|
|
|
} else if (
|
|
|
|
property_exists($object, 'service_handled') &&
|
|
|
|
!$object->service_handled &&
|
|
|
|
$object->service_state > 0
|
|
|
|
) {
|
|
|
|
$problems++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $problems;
|
|
|
|
}
|
|
|
|
|
2014-04-16 19:43:56 +02:00
|
|
|
private function countStates($objects, $unique = null, $state = 'service_state')
|
|
|
|
{
|
|
|
|
$known = array();
|
|
|
|
$states = array(
|
|
|
|
'0' => 0,
|
|
|
|
'1' => 0,
|
|
|
|
'2' => 0,
|
|
|
|
'3' => 0,
|
|
|
|
'99' => 0
|
|
|
|
);
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
if (isset($unique)) {
|
|
|
|
if (array_key_exists($object->$unique, $known)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$known[$object->$unique] = true;
|
|
|
|
}
|
|
|
|
$states[$object->$state]++;
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'up' => $states['0'],
|
|
|
|
'down' => $states['1'],
|
|
|
|
'unreachable' => $states['2'],
|
|
|
|
'unknown' => $states['3'],
|
|
|
|
'pending' => $states['99']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createPie($states)
|
|
|
|
{
|
|
|
|
$chart = new InlinePie(
|
|
|
|
array_values($states),
|
|
|
|
array('#44bb77', '#FF5566', '#FF5566', '#E066FF', '#77AAFF')
|
|
|
|
);
|
|
|
|
$chart->setLabels(array_keys($states))
|
|
|
|
->setHeight(100)
|
|
|
|
->setWidth(100);
|
|
|
|
return $chart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-17 19:08:00 +02:00
|
|
|
private function getComments($objects)
|
|
|
|
{
|
|
|
|
$unique = array();
|
|
|
|
foreach ($objects as $object) {
|
2013-10-18 09:59:06 +02:00
|
|
|
$unique = array_merge($unique, $this->getUniqueValues($object->comments, 'comment_internal_id'));
|
2013-10-17 19:08:00 +02:00
|
|
|
}
|
|
|
|
return array_keys($unique);
|
|
|
|
}
|
|
|
|
|
2013-10-18 09:59:06 +02:00
|
|
|
private function getProperties($objects, $property)
|
2013-10-17 19:08:00 +02:00
|
|
|
{
|
|
|
|
$objectnames = array();
|
|
|
|
foreach ($objects as $object) {
|
2014-04-02 18:23:01 +02:00
|
|
|
$objectnames[] = $object->$property;
|
2013-10-17 19:08:00 +02:00
|
|
|
}
|
|
|
|
return $objectnames;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getDowntimes($objects)
|
2013-10-11 12:05:05 +02:00
|
|
|
{
|
2013-10-15 17:52:30 +02:00
|
|
|
$downtimes = array();
|
2013-10-17 19:08:00 +02:00
|
|
|
foreach ($objects as $object)
|
|
|
|
{
|
|
|
|
if (
|
2013-10-18 16:43:37 +02:00
|
|
|
(property_exists($object, 'host_in_downtime') && $object->host_in_downtime) ||
|
|
|
|
(property_exists($object, 'service_in_downtime') && $object->service_in_downtime)
|
2013-10-17 19:08:00 +02:00
|
|
|
) {
|
|
|
|
$downtimes[] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $downtimes;
|
|
|
|
}
|
2013-10-15 17:52:30 +02:00
|
|
|
|
2013-10-11 12:05:05 +02:00
|
|
|
|
2013-10-16 18:50:19 +02:00
|
|
|
/**
|
2013-10-18 16:43:37 +02:00
|
|
|
* Handle the form to edit configuration flags.
|
|
|
|
*
|
|
|
|
* @param $flags array The used flags.
|
2013-10-16 18:50:19 +02:00
|
|
|
*/
|
2013-10-18 16:43:37 +02:00
|
|
|
private function handleConfigurationForm(array $flags)
|
2013-10-16 18:50:19 +02:00
|
|
|
{
|
2013-10-18 16:43:37 +02:00
|
|
|
$this->view->form = $form = new MultiCommandFlagForm($flags);
|
2014-04-09 19:13:46 +02:00
|
|
|
$this->view->formElements = $form->buildCheckboxes();
|
2013-10-16 18:50:19 +02:00
|
|
|
$form->setRequest($this->_request);
|
|
|
|
if ($form->isSubmittedAndValid()) {
|
|
|
|
// TODO: Handle commands
|
|
|
|
$changed = $form->getChangedValues();
|
|
|
|
}
|
|
|
|
if ($this->_request->isPost() === false) {
|
|
|
|
$this->view->form->initFromItems($this->view->objects);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 12:05:05 +02:00
|
|
|
/**
|
2014-04-02 18:23:01 +02:00
|
|
|
* "Flips" the structure of the objects created by _getAllParams
|
|
|
|
*
|
|
|
|
* Regularly, _getAllParams would return queries like <b>host[0]=value1&service[0]=value2</b> as
|
|
|
|
* two entirely separate arrays. Instead, we want it as one single array, containing one single object
|
|
|
|
* for each index, containing all of its members as keys.
|
2013-10-11 12:05:05 +02:00
|
|
|
*
|
2014-04-02 18:23:01 +02:00
|
|
|
* @return array An array of all query parameters (See example above)
|
|
|
|
* <b>
|
|
|
|
* array( <br />
|
|
|
|
* 0 => array(host => value1, service => value2), <br />
|
|
|
|
* ... <br />
|
|
|
|
* )
|
|
|
|
* </b>
|
2013-10-11 12:05:05 +02:00
|
|
|
*/
|
2014-04-02 18:23:01 +02:00
|
|
|
private function getAllParamsAsArray()
|
2013-10-11 12:05:05 +02:00
|
|
|
{
|
|
|
|
$details = $this->_getAllParams();
|
2014-04-02 18:23:01 +02:00
|
|
|
$queries = array();
|
2013-10-17 19:08:00 +02:00
|
|
|
|
2013-10-11 12:05:05 +02:00
|
|
|
foreach ($details as $property => $values) {
|
|
|
|
if (!is_array($values)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($values as $index => $value) {
|
2014-04-02 18:23:01 +02:00
|
|
|
if (!array_key_exists($index, $queries)) {
|
|
|
|
$queries[$index] = array();
|
2013-10-11 12:05:05 +02:00
|
|
|
}
|
2014-04-02 18:23:01 +02:00
|
|
|
$queries[$index][$property] = $value;
|
2013-10-11 12:05:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-02 18:23:01 +02:00
|
|
|
return $queries;
|
2013-10-11 12:05:05 +02:00
|
|
|
}
|
2013-10-18 11:05:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all tabs for this controller
|
|
|
|
*
|
|
|
|
* @return Tabs
|
|
|
|
*/
|
|
|
|
private function createTabs()
|
|
|
|
{
|
|
|
|
$tabs = $this->getTabs();
|
|
|
|
$tabs->extend(new OutputFormat());
|
|
|
|
}
|
2013-10-15 12:39:06 +02:00
|
|
|
}
|