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;
|
|
|
|
|
2015-08-27 16:06:26 +02:00
|
|
|
use Exception;
|
2014-09-04 11:25:47 +02:00
|
|
|
use Icinga\Data\ResourceFactory;
|
2015-06-29 14:00:34 +02:00
|
|
|
use Icinga\Exception\ConfigurationError;
|
2015-08-27 16:07:03 +02:00
|
|
|
use Icinga\Exception\NotFoundError;
|
2014-11-14 10:57:14 +01:00
|
|
|
use Icinga\Forms\ConfirmRemovalForm;
|
2015-06-29 14:00:34 +02:00
|
|
|
use Icinga\Web\Notification;
|
2015-08-31 11:04:34 +02:00
|
|
|
use Icinga\Module\Monitoring\Controller;
|
2014-11-14 11:17:22 +01:00
|
|
|
use Icinga\Module\Monitoring\Forms\Config\BackendConfigForm;
|
|
|
|
use Icinga\Module\Monitoring\Forms\Config\SecurityConfigForm;
|
2015-08-26 15:52:36 +02:00
|
|
|
use Icinga\Module\Monitoring\Forms\Config\TransportConfigForm;
|
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
|
|
|
/**
|
2015-08-26 15:43:30 +02:00
|
|
|
* Display a list of available backends and command transports
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function indexAction()
|
|
|
|
{
|
2014-09-04 11:25:47 +02:00
|
|
|
$this->view->backendsConfig = $this->Config('backends');
|
2015-08-26 15:43:30 +02:00
|
|
|
$this->view->transportConfig = $this->Config('commandtransports');
|
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-09-03 17:03:33 +02:00
|
|
|
$backendName = $this->params->getRequired('backend-name');
|
2015-06-29 14:00:34 +02:00
|
|
|
|
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-09-03 17:03:33 +02:00
|
|
|
$backendName = $this->params->getRequired('backend-name');
|
2015-06-29 14:00:34 +02:00
|
|
|
|
|
|
|
$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-08-26 15:43:30 +02:00
|
|
|
* Remove a command transport
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
2015-08-26 15:43:30 +02:00
|
|
|
public function removetransportAction()
|
2013-08-12 15:58:26 +02:00
|
|
|
{
|
2015-08-26 15:43:30 +02:00
|
|
|
$transportName = $this->params->getRequired('transport');
|
2013-08-20 15:45:04 +02:00
|
|
|
|
2015-08-26 15:52:36 +02:00
|
|
|
$transportForm = new TransportConfigForm();
|
2015-08-26 15:43:30 +02:00
|
|
|
$transportForm->setIniConfig($this->Config('commandtransports'));
|
2015-06-29 12:01:43 +02:00
|
|
|
$form = new ConfirmRemovalForm();
|
|
|
|
$form->setRedirectUrl('monitoring/config');
|
2015-08-26 15:43:30 +02:00
|
|
|
$form->setTitle(sprintf($this->translate('Remove Command Transport %s'), $transportName));
|
|
|
|
$form->info(
|
|
|
|
$this->translate(
|
|
|
|
'If you have still any environments or views referring to this transport, '
|
|
|
|
. 'you won\'t be able to send commands anymore after deletion.'
|
|
|
|
),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
$form->setOnSuccess(function (ConfirmRemovalForm $form) use ($transportName, $transportForm) {
|
2015-06-29 12:01:43 +02:00
|
|
|
try {
|
2015-08-26 15:43:30 +02:00
|
|
|
$transportForm->delete($transportName);
|
2015-06-29 12:01:43 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-26 15:43:30 +02:00
|
|
|
if ($transportForm->save()) {
|
|
|
|
Notification::success(sprintf(t('Command transport "%s" successfully removed'), $transportName));
|
2015-06-29 12:01:43 +02:00
|
|
|
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-08-26 15:43:30 +02:00
|
|
|
* Edit a command transport
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
2015-08-26 15:43:30 +02:00
|
|
|
public function edittransportAction()
|
2013-08-20 15:45:04 +02:00
|
|
|
{
|
2015-08-26 15:43:30 +02:00
|
|
|
$transportName = $this->params->getRequired('transport');
|
2015-06-29 12:01:43 +02:00
|
|
|
|
2015-08-26 15:52:36 +02:00
|
|
|
$form = new TransportConfigForm();
|
2014-09-04 08:47:16 +02:00
|
|
|
$form->setRedirectUrl('monitoring/config');
|
2015-08-26 15:43:30 +02:00
|
|
|
$form->setTitle(sprintf($this->translate('Edit Command Transport %s'), $transportName));
|
|
|
|
$form->setIniConfig($this->Config('commandtransports'));
|
2015-08-31 12:17:57 +02:00
|
|
|
$form->setInstanceNames($this->backend->select()->from('instance', array('instance_name'))->fetchColumn());
|
2015-08-26 15:52:36 +02:00
|
|
|
$form->setOnSuccess(function (TransportConfigForm $form) use ($transportName) {
|
2015-06-29 12:01:43 +02:00
|
|
|
try {
|
2015-08-26 15:43:30 +02:00
|
|
|
$form->edit($transportName, array_map(
|
2015-06-29 12:01:43 +02:00
|
|
|
function ($v) {
|
|
|
|
return $v !== '' ? $v : null;
|
|
|
|
},
|
|
|
|
$form->getValues()
|
|
|
|
));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form->save()) {
|
2015-08-26 15:43:30 +02:00
|
|
|
Notification::success(sprintf(t('Command transport "%s" successfully updated'), $transportName));
|
2015-06-29 12:01:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2015-08-26 15:43:30 +02:00
|
|
|
$form->load($transportName);
|
2015-06-29 12:01:43 +02:00
|
|
|
$form->handleRequest();
|
|
|
|
} catch (NotFoundError $_) {
|
2015-08-26 15:43:30 +02:00
|
|
|
$this->httpNotFound(sprintf($this->translate('Command transport "%s" not found'), $transportName));
|
2015-06-29 12:01:43 +02:00
|
|
|
}
|
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-08-26 15:43:30 +02:00
|
|
|
* Create a new command transport
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
2015-08-26 15:43:30 +02:00
|
|
|
public function createtransportAction()
|
2013-08-20 15:45:04 +02:00
|
|
|
{
|
2015-08-26 15:52:36 +02:00
|
|
|
$form = new TransportConfigForm();
|
2014-09-04 08:47:16 +02:00
|
|
|
$form->setRedirectUrl('monitoring/config');
|
2015-08-26 15:43:30 +02:00
|
|
|
$form->setTitle($this->translate('Create New Command Transport'));
|
|
|
|
$form->setIniConfig($this->Config('commandtransports'));
|
2015-08-31 12:17:57 +02:00
|
|
|
$form->setInstanceNames($this->backend->select()->from('instance', array('instance_name'))->fetchColumn());
|
2015-08-26 15:52:36 +02:00
|
|
|
$form->setOnSuccess(function (TransportConfigForm $form) {
|
2015-06-29 12:01:43 +02:00
|
|
|
try {
|
|
|
|
$form->add(array_filter($form->getValues()));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->error($e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form->save()) {
|
2015-08-26 15:43:30 +02:00
|
|
|
Notification::success(t('Command transport successfully created'));
|
2015-06-29 12:01:43 +02:00
|
|
|
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');
|
2014-08-19 11:19:30 +02:00
|
|
|
}
|
2013-08-14 12:42:32 +02:00
|
|
|
}
|