2013-06-27 12:45:18 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-06-27 12:45:18 +02:00
|
|
|
|
2015-08-27 16:05:57 +02:00
|
|
|
namespace Icinga\Module\Monitoring\Controllers;
|
|
|
|
|
2014-09-04 11:25:47 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2015-06-29 14:00:34 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2014-11-14 10:57:14 +01:00
|
|
|
use Icinga\Forms\ConfirmRemovalForm;
|
2014-11-14 11:17:22 +01:00
|
|
|
use Icinga\Module\Monitoring\Forms\Config\BackendConfigForm;
|
|
|
|
use Icinga\Module\Monitoring\Forms\Config\InstanceConfigForm;
|
|
|
|
use Icinga\Module\Monitoring\Forms\Config\SecurityConfigForm;
|
2015-08-27 16:04:56 +02:00
|
|
|
use Icinga\Web\Controller;
|
|
|
|
use Icinga\Web\Notification;
|
2014-03-13 15:54:27 +01:00
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
/**
|
|
|
|
* Configuration controller for editing monitoring resources
|
|
|
|
*/
|
2015-08-27 16:05:57 +02:00
|
|
|
class ConfigController extends Controller
|
2014-06-24 21:31:59 +02:00
|
|
|
{
|
2013-08-20 15:45:04 +02:00
|
|
|
/**
|
|
|
|
* Display a list of available backends and instances
|
|
|
|
*/
|
|
|
|
public function indexAction()
|
|
|
|
{
|
2014-09-04 11:25:47 +02:00
|
|
|
$this->view->backendsConfig = $this->Config('backends');
|
|
|
|
$this->view->instancesConfig = $this->Config('instances');
|
2014-06-24 21:22:38 +02:00
|
|
|
$this->view->tabs = $this->Module()->getConfigTabs()->activate('backends');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-29 14:00:34 +02:00
|
|
|
* Edit a monitoring backend
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function editbackendAction()
|
|
|
|
{
|
2015-06-29 14:00:34 +02:00
|
|
|
$backendName = $this->params->getRequired('backend');
|
|
|
|
|
2014-09-04 11:25:47 +02:00
|
|
|
$form = new BackendConfigForm();
|
2015-06-29 14:00:34 +02:00
|
|
|
$form->setRedirectUrl('monitoring/config');
|
|
|
|
$form->setTitle(sprintf($this->translate('Edit Monitoring Backend %s'), $backendName));
|
2014-09-08 13:31:25 +02:00
|
|
|
$form->setIniConfig($this->Config('backends'));
|
2014-09-04 11:25:47 +02:00
|
|
|
$form->setResourceConfig(ResourceFactory::getResourceConfigs());
|
2015-06-29 14:00:34 +02:00
|
|
|
$form->setOnSuccess(function (BackendConfigForm $form) use ($backendName) {
|
|
|
|
try {
|
|
|
|
$form->edit($backendName, array_map(
|
|
|
|
function ($v) {
|
|
|
|
return $v !== '' ? $v : null;
|
|
|
|
},
|
|
|
|
$form->getValues()
|
|
|
|
));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form->save()) {
|
|
|
|
Notification::success(sprintf(t('Monitoring backend "%s" successfully updated'), $backendName));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
$form->load($backendName);
|
|
|
|
$form->handleRequest();
|
|
|
|
} catch (NotFoundError $_) {
|
|
|
|
$this->httpNotFound(sprintf($this->translate('Monitoring backend "%s" not found'), $backendName));
|
|
|
|
}
|
2014-07-25 14:55:36 +02:00
|
|
|
|
|
|
|
$this->view->form = $form;
|
2015-06-29 14:00:34 +02:00
|
|
|
$this->render('form');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-29 14:00:34 +02:00
|
|
|
* Create a new monitoring backend
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function createbackendAction()
|
|
|
|
{
|
2014-09-04 11:25:47 +02:00
|
|
|
$form = new BackendConfigForm();
|
|
|
|
$form->setRedirectUrl('monitoring/config');
|
2015-06-29 14:00:34 +02:00
|
|
|
$form->setTitle($this->translate('Create New Monitoring Backend'));
|
|
|
|
$form->setIniConfig($this->Config('backends'));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$form->setResourceConfig(ResourceFactory::getResourceConfigs());
|
|
|
|
} catch (ConfigurationError $e) {
|
|
|
|
if ($this->hasPermission('config/application/resources')) {
|
|
|
|
Notification::error($e->getMessage());
|
|
|
|
$this->redirectNow('config/createresource');
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $e; // No permission for resource configuration, show the error
|
|
|
|
}
|
|
|
|
|
|
|
|
$form->setOnSuccess(function (BackendConfigForm $form) {
|
|
|
|
try {
|
|
|
|
$form->add(array_filter($form->getValues()));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form->save()) {
|
|
|
|
Notification::success(t('Monitoring backend successfully created'));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2014-09-04 11:25:47 +02:00
|
|
|
$form->handleRequest();
|
2014-07-25 15:58:44 +02:00
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
$this->view->form = $form;
|
2015-06-29 14:00:34 +02:00
|
|
|
$this->render('form');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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()
|
|
|
|
{
|
2015-06-29 14:00:34 +02:00
|
|
|
$backendName = $this->params->getRequired('backend');
|
|
|
|
|
|
|
|
$backendForm = new BackendConfigForm();
|
|
|
|
$backendForm->setIniConfig($this->Config('backends'));
|
|
|
|
$form = new ConfirmRemovalForm();
|
2014-09-04 11:25:47 +02:00
|
|
|
$form->setRedirectUrl('monitoring/config');
|
2015-06-29 14:00:34 +02:00
|
|
|
$form->setTitle(sprintf($this->translate('Remove Monitoring Backend %s'), $backendName));
|
|
|
|
$form->setOnSuccess(function (ConfirmRemovalForm $form) use ($backendName, $backendForm) {
|
|
|
|
try {
|
|
|
|
$backendForm->delete($backendName);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($backendForm->save()) {
|
|
|
|
Notification::success(sprintf(t('Monitoring backend "%s" successfully removed'), $backendName));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2014-09-04 11:25:47 +02:00
|
|
|
$form->handleRequest();
|
2013-08-20 15:45:04 +02:00
|
|
|
|
|
|
|
$this->view->form = $form;
|
2015-06-29 14:00:34 +02:00
|
|
|
$this->render('form');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-29 12:01:43 +02:00
|
|
|
* Remove a monitoring instance
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function removeinstanceAction()
|
2013-08-12 15:58:26 +02:00
|
|
|
{
|
2015-06-29 12:01:43 +02:00
|
|
|
$instanceName = $this->params->getRequired('instance');
|
2013-08-20 15:45:04 +02:00
|
|
|
|
2015-06-29 12:01:43 +02:00
|
|
|
$instanceForm = new InstanceConfigForm();
|
|
|
|
$instanceForm->setIniConfig($this->Config('instances'));
|
|
|
|
$form = new ConfirmRemovalForm();
|
|
|
|
$form->setRedirectUrl('monitoring/config');
|
|
|
|
$form->setTitle(sprintf($this->translate('Remove Monitoring Instance %s'), $instanceName));
|
2015-03-02 18:37:54 +01:00
|
|
|
$form->addDescription($this->translate(
|
|
|
|
'If you have still any environments or views referring to this instance, '
|
|
|
|
. 'you won\'t be able to send commands anymore after deletion.'
|
|
|
|
));
|
2015-06-29 12:01:43 +02:00
|
|
|
$form->setOnSuccess(function (ConfirmRemovalForm $form) use ($instanceName, $instanceForm) {
|
|
|
|
try {
|
|
|
|
$instanceForm->delete($instanceName);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($instanceForm->save()) {
|
|
|
|
Notification::success(sprintf(t('Monitoring instance "%s" successfully removed'), $instanceName));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2014-09-04 08:47:16 +02:00
|
|
|
$form->handleRequest();
|
2013-08-20 15:45:04 +02:00
|
|
|
|
|
|
|
$this->view->form = $form;
|
2015-06-29 12:01:43 +02:00
|
|
|
$this->render('form');
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
/**
|
2015-06-29 12:01:43 +02:00
|
|
|
* Edit a monitoring instance
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function editinstanceAction()
|
|
|
|
{
|
2015-06-29 12:01:43 +02:00
|
|
|
$instanceName = $this->params->getRequired('instance');
|
|
|
|
|
2014-09-04 08:47:16 +02:00
|
|
|
$form = new InstanceConfigForm();
|
|
|
|
$form->setRedirectUrl('monitoring/config');
|
2015-06-29 12:01:43 +02:00
|
|
|
$form->setTitle(sprintf($this->translate('Edit Monitoring Instance %s'), $instanceName));
|
|
|
|
$form->setIniConfig($this->Config('instances'));
|
|
|
|
$form->setOnSuccess(function (InstanceConfigForm $form) use ($instanceName) {
|
|
|
|
try {
|
|
|
|
$form->edit($instanceName, array_map(
|
|
|
|
function ($v) {
|
|
|
|
return $v !== '' ? $v : null;
|
|
|
|
},
|
|
|
|
$form->getValues()
|
|
|
|
));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form->save()) {
|
|
|
|
Notification::success(sprintf(t('Monitoring instance "%s" successfully updated'), $instanceName));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
$form->load($instanceName);
|
|
|
|
$form->handleRequest();
|
|
|
|
} catch (NotFoundError $_) {
|
|
|
|
$this->httpNotFound(sprintf($this->translate('Monitoring instance "%s" not found'), $instanceName));
|
|
|
|
}
|
2014-08-11 14:49:42 +02:00
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
$this->view->form = $form;
|
2015-06-29 12:01:43 +02:00
|
|
|
$this->render('form');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-29 12:01:43 +02:00
|
|
|
* Create a new monitoring instance
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function createinstanceAction()
|
|
|
|
{
|
2014-09-04 08:47:16 +02:00
|
|
|
$form = new InstanceConfigForm();
|
|
|
|
$form->setRedirectUrl('monitoring/config');
|
2015-06-29 12:01:43 +02:00
|
|
|
$form->setTitle($this->translate('Create New Monitoring Instance'));
|
|
|
|
$form->setIniConfig($this->Config('instances'));
|
|
|
|
$form->setOnSuccess(function (InstanceConfigForm $form) {
|
|
|
|
try {
|
|
|
|
$form->add(array_filter($form->getValues()));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form->save()) {
|
|
|
|
Notification::success(t('Monitoring instance successfully created'));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2014-09-04 08:47:16 +02:00
|
|
|
$form->handleRequest();
|
2014-08-11 14:49:42 +02:00
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
$this->view->form = $form;
|
2015-06-29 12:01:43 +02:00
|
|
|
$this->render('form');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 12:15:02 +02:00
|
|
|
/**
|
|
|
|
* Display a form to adjust security relevant settings
|
|
|
|
*/
|
2014-08-19 11:19:30 +02:00
|
|
|
public function securityAction()
|
|
|
|
{
|
2014-09-03 15:08:37 +02:00
|
|
|
$form = new SecurityConfigForm();
|
2014-09-08 13:31:25 +02:00
|
|
|
$form->setIniConfig($this->Config());
|
2014-09-03 15:08:37 +02:00
|
|
|
$form->handleRequest();
|
2014-08-22 12:30:13 +02:00
|
|
|
|
2014-08-19 14:04:00 +02:00
|
|
|
$this->view->form = $form;
|
2014-09-03 15:08:37 +02:00
|
|
|
$this->view->tabs = $this->Module()->getConfigTabs()->activate('security');
|
2015-06-29 14:00:34 +02:00
|
|
|
$this->render('form');
|
2014-08-19 11:19:30 +02:00
|
|
|
}
|
2013-08-14 12:42:32 +02:00
|
|
|
}
|