2013-06-27 12:45:18 +02:00
|
|
|
<?php
|
2013-08-14 12:42:32 +02:00
|
|
|
// @codingStandardsIgnoreStart
|
2013-06-27 12:45:18 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-28 16:47:30 +02:00
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-06-28 16:47:30 +02:00
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* 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.
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* 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.
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-06-28 16:47:30 +02:00
|
|
|
* 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-08-21 11:02:53 +02:00
|
|
|
*
|
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-06-28 16:47:30 +02:00
|
|
|
*/
|
2013-06-27 12:45:18 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2013-08-21 11:02:53 +02:00
|
|
|
use \Exception;
|
|
|
|
|
|
|
|
use \Icinga\Application\Config as IcingaConfig;
|
|
|
|
use \Icinga\Config\PreservingIniWriter;
|
2013-08-12 15:58:26 +02:00
|
|
|
use \Icinga\Web\Controller\BaseConfigController;
|
|
|
|
use \Icinga\Web\Widget\Tab;
|
2013-11-20 19:10:38 +01:00
|
|
|
use \Icinga\Web\Widget\AlertMessageBox;
|
2013-08-12 15:58:26 +02:00
|
|
|
use \Icinga\Web\Url;
|
2013-06-27 12:45:18 +02:00
|
|
|
|
2013-09-03 14:35:43 +02:00
|
|
|
use Icinga\Module\Monitoring\Form\Config\ConfirmRemovalForm;
|
|
|
|
use Icinga\Module\Monitoring\Form\Config\Backend\EditBackendForm;
|
|
|
|
use Icinga\Module\Monitoring\Form\Config\Backend\CreateBackendForm;
|
2013-08-20 15:45:04 +02:00
|
|
|
|
2013-09-03 14:35:43 +02:00
|
|
|
use Icinga\Module\Monitoring\Form\Config\Instance\EditInstanceForm;
|
|
|
|
use Icinga\Module\Monitoring\Form\Config\Instance\CreateInstanceForm;
|
2013-08-20 15:45:04 +02:00
|
|
|
|
2014-03-13 15:54:27 +01:00
|
|
|
use Icinga\Exception\NotReadableError;
|
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
/**
|
|
|
|
* Configuration controller for editing monitoring resources
|
|
|
|
*/
|
2013-08-12 15:58:26 +02:00
|
|
|
class Monitoring_ConfigController extends BaseConfigController {
|
2013-06-27 16:06:38 +02:00
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
/**
|
|
|
|
* Create the tabs for being available via the applications 'Config' view
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-08-12 15:58:26 +02:00
|
|
|
static public function createProvidedTabs()
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2013-08-12 15:58:26 +02:00
|
|
|
return array(
|
2013-08-20 17:06:44 +02:00
|
|
|
'backends' => new Tab(array(
|
|
|
|
'name' => 'backends',
|
|
|
|
'title' => 'Monitoring Backends',
|
2013-08-20 18:08:56 +02:00
|
|
|
'url' => Url::fromPath('/monitoring/config')
|
2013-08-12 15:58:26 +02:00
|
|
|
))
|
2013-06-27 12:45:18 +02:00
|
|
|
);
|
2013-08-12 15:58:26 +02:00
|
|
|
}
|
2013-06-27 12:45:18 +02:00
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
/**
|
|
|
|
* Display a list of available backends and instances
|
|
|
|
*/
|
|
|
|
public function indexAction()
|
|
|
|
{
|
2014-06-20 23:22:07 +02:00
|
|
|
$this->view->tabs->activate('backends');
|
2013-11-26 10:35:46 +01:00
|
|
|
$this->view->messageBox = new AlertMessageBox(true);
|
2014-03-13 15:54:27 +01:00
|
|
|
foreach (array('backends', 'instances') as $element) {
|
|
|
|
try {
|
|
|
|
$elementConfig = IcingaConfig::module('monitoring', $element);
|
|
|
|
if ($elementConfig === null) {
|
|
|
|
$this->view->{$element} = array();
|
|
|
|
} else {
|
|
|
|
$this->view->{$element} = $elementConfig->toArray();
|
|
|
|
}
|
|
|
|
} catch (NotReadableError $e) {
|
|
|
|
$this->view->{$element} = $e;
|
|
|
|
}
|
2014-03-07 16:08:54 +01:00
|
|
|
}
|
2013-08-27 14:37:22 +02:00
|
|
|
$this->render('index');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a form to modify the backend identified by the 'backend' parameter of the request
|
|
|
|
*/
|
|
|
|
public function editbackendAction()
|
|
|
|
{
|
|
|
|
$backend = $this->getParam('backend');
|
|
|
|
if (!$this->isExistingBackend($backend)) {
|
2013-08-20 18:08:56 +02:00
|
|
|
$this->view->error = 'Unknown backend ' . $backend;
|
2013-08-20 15:45:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$backendForm = new EditBackendForm();
|
|
|
|
$backendForm->setRequest($this->getRequest());
|
|
|
|
$backendForm->setBackendConfiguration(IcingaConfig::module('monitoring', 'backends')->get($backend));
|
|
|
|
|
|
|
|
if ($backendForm->isSubmittedAndValid()) {
|
|
|
|
$newConfig = $backendForm->getConfig();
|
|
|
|
$config = IcingaConfig::module('monitoring', 'backends');
|
|
|
|
$config->$backend = $newConfig;
|
|
|
|
if ($this->writeConfiguration($config, 'backends')) {
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->addSuccessMessage('Backend ' . $backend . ' Modified.');
|
2013-08-20 15:45:04 +02:00
|
|
|
$this->redirectNow('monitoring/config');
|
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->view->name = $backend;
|
|
|
|
$this->view->form = $backendForm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-21 11:02:53 +02:00
|
|
|
* Display a form to create a new backends
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function createbackendAction()
|
|
|
|
{
|
|
|
|
$form = new CreateBackendForm();
|
|
|
|
$form->setRequest($this->getRequest());
|
|
|
|
if ($form->isSubmittedAndValid()) {
|
|
|
|
$configArray = IcingaConfig::module('monitoring', 'backends')->toArray();
|
|
|
|
$configArray[$form->getBackendName()] = $form->getConfig();
|
|
|
|
|
|
|
|
if ($this->writeConfiguration(new Zend_Config($configArray), 'backends')) {
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->addSuccessMessage('Backend Creation Succeeded');
|
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
|
|
|
}
|
2013-08-27 14:37:22 +02:00
|
|
|
return;
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
$this->view->form = $form;
|
|
|
|
$this->render('editbackend');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-21 11:02:53 +02:00
|
|
|
* Display a confirmation form to remove the backend identified by the 'backend' parameter
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function removebackendAction()
|
|
|
|
{
|
|
|
|
$backend = $this->getParam('backend');
|
|
|
|
if (!$this->isExistingBackend($backend)) {
|
2013-08-20 18:08:56 +02:00
|
|
|
$this->view->error = 'Unknown backend ' . $backend;
|
2013-08-20 15:45:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$form = new ConfirmRemovalForm();
|
|
|
|
$form->setRequest($this->getRequest());
|
|
|
|
$form->setRemoveTarget('backend', $backend);
|
|
|
|
|
|
|
|
if ($form->isSubmittedAndValid()) {
|
|
|
|
$configArray = IcingaConfig::module('monitoring', 'backends')->toArray();
|
|
|
|
unset($configArray[$backend]);
|
|
|
|
|
|
|
|
if ($this->writeConfiguration(new Zend_Config($configArray), 'backends')) {
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->addSuccessMessage('Backend "' . $backend . '" Removed');
|
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
|
|
|
}
|
2013-08-27 14:37:22 +02:00
|
|
|
return;
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->view->form = $form;
|
|
|
|
$this->view->name = $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a form to remove the instance identified by the 'instance' parameter
|
|
|
|
*/
|
|
|
|
public function removeinstanceAction()
|
2013-08-12 15:58:26 +02:00
|
|
|
{
|
2013-08-20 15:45:04 +02:00
|
|
|
$instance = $this->getParam('instance');
|
|
|
|
if (!$this->isExistingInstance($instance)) {
|
2013-08-20 18:08:56 +02:00
|
|
|
$this->view->error = 'Unknown instance ' . $instance;
|
2013-08-20 15:45:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = new ConfirmRemovalForm();
|
|
|
|
$form->setRequest($this->getRequest());
|
|
|
|
$form->setRemoveTarget('instance', $instance);
|
|
|
|
|
|
|
|
if ($form->isSubmittedAndValid()) {
|
|
|
|
$configArray = IcingaConfig::module('monitoring', 'instances')->toArray();
|
|
|
|
unset($configArray[$instance]);
|
|
|
|
|
|
|
|
if ($this->writeConfiguration(new Zend_Config($configArray), 'instances')) {
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->addSuccessMessage('Instance "' . $instance . '" Removed');
|
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
|
|
|
}
|
2013-08-27 14:37:22 +02:00
|
|
|
return;
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->view->form = $form;
|
|
|
|
$this->view->name = $instance;
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
/**
|
|
|
|
* Display a form to edit the instance identified by the 'instance' parameter of the request
|
|
|
|
*/
|
|
|
|
public function editinstanceAction()
|
|
|
|
{
|
|
|
|
$instance = $this->getParam('instance');
|
|
|
|
if (!$this->isExistingInstance($instance)) {
|
2013-08-20 18:08:56 +02:00
|
|
|
$this->view->error = 'Unknown instance ' . htmlentities($instance);
|
2013-08-20 15:45:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$form = new EditInstanceForm();
|
|
|
|
$form->setInstanceConfiguration(IcingaConfig::module('monitoring', 'instances')->get($instance));
|
|
|
|
$form->setRequest($this->getRequest());
|
|
|
|
if ($form->isSubmittedAndValid()) {
|
|
|
|
$instanceConfig = IcingaConfig::module('monitoring', 'instances')->toArray();
|
|
|
|
$instanceConfig[$instance] = $form->getConfig();
|
|
|
|
if ($this->writeConfiguration(new Zend_Config($instanceConfig), 'instances')) {
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->addSuccessMessage('Instance Modified');
|
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->view->form = $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a form to create a new instance
|
|
|
|
*/
|
|
|
|
public function createinstanceAction()
|
|
|
|
{
|
|
|
|
$form = new CreateInstanceForm();
|
|
|
|
$form->setRequest($this->getRequest());
|
|
|
|
if ($form->isSubmittedAndValid()) {
|
2014-03-07 17:27:18 +01:00
|
|
|
$instanceConfig = IcingaConfig::module('monitoring', 'instances');
|
|
|
|
if ($instanceConfig === null) {
|
|
|
|
$instanceConfig = array();
|
|
|
|
} else {
|
|
|
|
$instanceConfig = $instanceConfig->toArray();
|
|
|
|
}
|
2013-08-20 15:45:04 +02:00
|
|
|
$instanceConfig[$form->getInstanceName()] = $form->getConfig()->toArray();
|
|
|
|
if ($this->writeConfiguration(new Zend_Config($instanceConfig), 'instances')) {
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->addSuccessMessage('Instance Creation Succeeded');
|
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
|
|
|
}
|
2013-08-27 14:37:22 +02:00
|
|
|
return;
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
$this->view->form = $form;
|
|
|
|
$this->render('editinstance');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a form to remove the instance identified by the 'instance' parameter
|
|
|
|
*/
|
|
|
|
private function writeConfiguration($config, $file)
|
|
|
|
{
|
2014-04-29 11:30:34 +02:00
|
|
|
$target = IcingaConfig::module('monitoring', $file)->getConfigFile();
|
|
|
|
$writer = new PreservingIniWriter(array('filename' => $target, 'config' => $config));
|
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
try {
|
|
|
|
$writer->write();
|
|
|
|
} catch (Exception $exc) {
|
|
|
|
$this->view->exceptionMessage = $exc->getMessage();
|
|
|
|
$this->view->iniConfigurationString = $writer->render();
|
2014-04-29 11:30:34 +02:00
|
|
|
$this->view->file = $target;
|
2013-08-20 15:45:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-29 11:30:34 +02:00
|
|
|
|
|
|
|
return true;
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the backend exists in the current configuration
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param string $backend The name of the backend to check for existence
|
2013-08-20 15:45:04 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @return bool True if the backend name exists, otherwise false
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
private function isExistingBackend($backend)
|
|
|
|
{
|
|
|
|
$backendCfg = IcingaConfig::module('monitoring', 'backends');
|
|
|
|
return $backend && $backendCfg->get($backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the instance exists in the current configuration
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param string $instance The name of the instance to check for existence
|
2013-08-20 15:45:04 +02:00
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @return bool True if the instance name exists, otherwise false
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
private function isExistingInstance($instance)
|
|
|
|
{
|
|
|
|
$instanceCfg = IcingaConfig::module('monitoring', 'instances');
|
|
|
|
return $instanceCfg && $instanceCfg->get($instance);
|
|
|
|
}
|
2013-08-14 12:42:32 +02:00
|
|
|
}
|
|
|
|
// @codingStandardsIgnoreEnd
|