2013-10-11 12:05:05 +02:00
|
|
|
<?php
|
|
|
|
// {{{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-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;
|
|
|
|
use \Icinga\Module\Monitoring\Object\Host;
|
|
|
|
use \Icinga\Module\Monitoring\Object\Service;
|
2013-10-16 18:50:19 +02:00
|
|
|
use \Icinga\Module\Monitoring\Form\Command\MultiCommandFlagForm;
|
2013-10-17 19:08:00 +02:00
|
|
|
use \Icinga\Module\Monitoring\DataView\HostAndServiceStatus as HostAndServiceStatusView;
|
|
|
|
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()
|
|
|
|
{
|
2013-10-15 12:39:06 +02:00
|
|
|
$this->view->queries = $this->getDetailQueries();
|
|
|
|
$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-17 19:08:00 +02:00
|
|
|
$queries = $this->view->queries;
|
2013-10-15 12:39:06 +02:00
|
|
|
$hosts = array();
|
2013-10-15 17:52:30 +02:00
|
|
|
$errors = array();
|
2013-10-15 12:39:06 +02:00
|
|
|
|
2013-10-17 19:08:00 +02:00
|
|
|
if ($this->_getParam('host') === '*') {
|
|
|
|
// fetch all hosts
|
|
|
|
$hosts = HostAndServiceStatusView::fromRequest(
|
|
|
|
$this->_request,
|
|
|
|
array(
|
|
|
|
'host_name',
|
|
|
|
'host_in_downtime',
|
|
|
|
'host_accepts_passive_checks',
|
|
|
|
'host_does_active_checks',
|
|
|
|
'host_notifications_enabled',
|
|
|
|
|
|
|
|
// TODO: flags missing in HostAndServiceStatus:
|
|
|
|
'host_obsessing',
|
|
|
|
'host_event_handler_enabled',
|
|
|
|
'host_flap_detection_enabled'
|
|
|
|
// <<
|
|
|
|
)
|
|
|
|
)->getQuery()->fetchAll();
|
|
|
|
$comments = array_keys($this->getUniqueValues(
|
|
|
|
CommentView::fromRequest($this->_request)->getQuery()->fetchAll(),
|
|
|
|
'comment_id'
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
// fetch specified hosts
|
|
|
|
foreach ($queries as $index => $query) {
|
|
|
|
if (!array_key_exists('host', $query)) {
|
|
|
|
$errors[] = 'Query ' . $index . ' misses property host.';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$hosts[] = Host::fetch($this->backend, $query['host']);
|
2013-10-15 17:52:30 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-15 12:39:06 +02:00
|
|
|
$this->view->objects = $this->view->hosts = $hosts;
|
2013-10-17 19:08:00 +02:00
|
|
|
$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);
|
2013-10-15 17:52:30 +02:00
|
|
|
$this->view->errors = $errors;
|
2013-10-16 18:50:19 +02:00
|
|
|
|
|
|
|
$this->handleConfigurationForm();
|
|
|
|
$this->view->form->setAction('/icinga2-web/monitoring/multi/host');
|
2013-10-11 12:05:05 +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
|
|
|
|
*/
|
|
|
|
private function getUniqueValues(array $values, $key)
|
|
|
|
{
|
|
|
|
$unique = array();
|
|
|
|
foreach ($values as $value)
|
|
|
|
{
|
2013-10-18 09:59:06 +02:00
|
|
|
if (is_array($value)) {
|
|
|
|
$unique[$value[$key]] = $value[$key];
|
|
|
|
} else {
|
|
|
|
$unique[$value->{$key}] = $value->{$key};
|
|
|
|
}
|
2013-10-17 19:08:00 +02:00
|
|
|
}
|
|
|
|
return $unique;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-10-18 09:59:06 +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 (
|
|
|
|
isset($object->host_in_downtime) && $object->host_in_downtime ||
|
|
|
|
isset($object->service_in_downtime) && $object->host_in_downtime
|
|
|
|
) {
|
|
|
|
$downtimes[] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $downtimes;
|
|
|
|
}
|
2013-10-15 17:52:30 +02:00
|
|
|
|
2013-10-17 19:08:00 +02:00
|
|
|
public function serviceAction()
|
|
|
|
{
|
|
|
|
$queries = $this->view->queries;
|
|
|
|
$services = array();
|
2013-10-15 17:52:30 +02:00
|
|
|
$errors = array();
|
2013-10-15 12:39:06 +02:00
|
|
|
|
2013-10-17 19:08:00 +02:00
|
|
|
if ($this->_getParam('service') === '*' && $this->_getParam('host') === '*') {
|
|
|
|
$services = HostAndServiceStatusView::fromRequest(
|
|
|
|
$this->_request,
|
|
|
|
array(
|
|
|
|
'host_name',
|
|
|
|
'service_name',
|
|
|
|
'service_in_downtime',
|
|
|
|
'service_accepts_passive_checks',
|
|
|
|
'service_does_active_checks',
|
|
|
|
'service_notifications_enabled',
|
|
|
|
|
|
|
|
// TODO: Flag misses in HostAndServiceStatus
|
|
|
|
'service_obsessing',
|
|
|
|
'service_event_handler_enabled',
|
|
|
|
'service_flap_detection_enabled'
|
|
|
|
// <<
|
|
|
|
)
|
|
|
|
)->getQuery()->fetchAll();
|
|
|
|
$comments = array_keys($this->getUniqueValues(
|
|
|
|
CommentView::fromRequest($this->_request)->getQuery()->fetchAll(),
|
|
|
|
'comment_id'
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
// fetch specified hosts
|
|
|
|
foreach ($queries as $index => $query) {
|
|
|
|
if (!array_key_exists('host', $query)) {
|
|
|
|
$errors[] = 'Query ' . $index . ' misses property host.';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!array_key_exists('service', $query)) {
|
|
|
|
$errors[] = 'Query ' . $index . ' misses property service.';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$services[] = Service::fetch($this->backend, $query['host'], $query['service']);
|
2013-10-15 17:52:30 +02:00
|
|
|
}
|
2013-10-15 12:39:06 +02:00
|
|
|
}
|
|
|
|
$this->view->objects = $this->view->services = $services;
|
2013-10-17 19:08:00 +02:00
|
|
|
$this->view->comments = isset($comments) ? $comments : $this->getComments($services);
|
2013-10-18 09:59:06 +02:00
|
|
|
$this->view->hostnames = $this->getProperties($services, 'host_name');
|
|
|
|
$this->view->servicenames = $this->getProperties($services, 'service_description');
|
2013-10-17 19:08:00 +02:00
|
|
|
$this->view->downtimes = $this->getDowntimes($services);
|
2013-10-15 17:52:30 +02:00
|
|
|
$this->view->errors = $errors;
|
2013-10-16 18:50:19 +02:00
|
|
|
|
|
|
|
$this->handleConfigurationForm();
|
|
|
|
$this->view->form->setAction('/icinga2-web/monitoring/multi/service');
|
2013-10-11 12:05:05 +02:00
|
|
|
}
|
|
|
|
|
2013-10-16 18:50:19 +02:00
|
|
|
/**
|
|
|
|
* Handle the form to configure configuration flags.
|
|
|
|
*/
|
|
|
|
private function handleConfigurationForm()
|
|
|
|
{
|
|
|
|
$this->view->form = $form = new MultiCommandFlagForm(
|
|
|
|
array(
|
|
|
|
'passive_checks_enabled' => 'Passive Checks',
|
|
|
|
'active_checks_enabled' => 'Active Checks',
|
|
|
|
'obsessing' => 'Obsessing',
|
|
|
|
'notifications_enabled' => 'Notifications',
|
|
|
|
'event_handler_enabled' => 'Event Handler',
|
|
|
|
'flap_detection_enabled' => 'Flap Detection'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$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
|
|
|
/**
|
2013-10-15 12:39:06 +02:00
|
|
|
* Fetch all requests from the 'detail' parameter.
|
2013-10-11 12:05:05 +02:00
|
|
|
*
|
2013-10-15 12:39:06 +02:00
|
|
|
* @return array An array of request that contain
|
|
|
|
* the filter arguments as properties.
|
2013-10-11 12:05:05 +02:00
|
|
|
*/
|
|
|
|
private function getDetailQueries()
|
|
|
|
{
|
|
|
|
$details = $this->_getAllParams();
|
|
|
|
$objects = 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) {
|
|
|
|
if (!array_key_exists($index, $objects)) {
|
|
|
|
$objects[$index] = array();
|
|
|
|
}
|
|
|
|
$objects[$index][$property] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $objects;
|
|
|
|
}
|
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
|
|
|
}
|