2013-06-27 12:45:18 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
2014-07-25 09:12:12 +02:00
|
|
|
use \Exception;
|
2014-07-25 14:55:36 +02:00
|
|
|
use \Zend_Config;
|
2014-06-24 21:31:59 +02:00
|
|
|
use Icinga\Config\PreservingIniWriter;
|
|
|
|
use Icinga\Web\Controller\ModuleActionController;
|
2014-06-24 21:28:01 +02:00
|
|
|
use Icinga\Web\Notification;
|
2014-07-25 16:00:38 +02:00
|
|
|
use Icinga\Form\Config\ConfirmRemovalForm;
|
2014-07-25 14:55:36 +02:00
|
|
|
use Icinga\Module\Monitoring\Form\Config\BackendForm;
|
2014-08-11 14:49:42 +02:00
|
|
|
use Icinga\Module\Monitoring\Form\Config\InstanceForm;
|
2014-08-19 14:04:00 +02:00
|
|
|
use Icinga\Module\Monitoring\Form\Config\SecurityForm;
|
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
|
|
|
|
*/
|
2014-06-24 21:31:59 +02:00
|
|
|
class Monitoring_ConfigController extends ModuleActionController
|
|
|
|
{
|
2013-08-20 15:45:04 +02:00
|
|
|
/**
|
|
|
|
* Display a list of available backends and instances
|
|
|
|
*/
|
|
|
|
public function indexAction()
|
|
|
|
{
|
2014-06-24 21:22:38 +02:00
|
|
|
$this->view->tabs = $this->Module()->getConfigTabs()->activate('backends');
|
2014-03-13 15:54:27 +01:00
|
|
|
foreach (array('backends', 'instances') as $element) {
|
|
|
|
try {
|
2014-06-24 21:22:38 +02:00
|
|
|
$elementConfig = $this->Config($element);
|
2014-03-13 15:54:27 +01:00
|
|
|
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-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a form to modify the backend identified by the 'backend' parameter of the request
|
|
|
|
*/
|
|
|
|
public function editbackendAction()
|
|
|
|
{
|
2014-07-25 14:55:36 +02:00
|
|
|
// Fetch the backend to be edited
|
2013-08-20 15:45:04 +02:00
|
|
|
$backend = $this->getParam('backend');
|
2014-07-25 14:55:36 +02:00
|
|
|
$backendsConfig = $this->Config('backends')->toArray();
|
|
|
|
if (false === array_key_exists($backend, $backendsConfig)) {
|
|
|
|
// TODO: Should behave as in the app's config controller (Specific redirect to an error action)
|
|
|
|
Notification::error(sprintf($this->translate('Cannot edit "%s". Backend not found.'), $backend));
|
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
2014-07-25 14:55:36 +02:00
|
|
|
|
|
|
|
$form = new BackendForm();
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isPost()) {
|
|
|
|
if ($form->isValid($request->getPost())) {
|
|
|
|
list($newName, $config) = $form->getBackendConfig();
|
|
|
|
|
|
|
|
if ($newName !== $backend) {
|
|
|
|
// Backend name has changed
|
|
|
|
unset($backendsConfig[$backend]); // We can safely use unset as all values are part of the form
|
|
|
|
}
|
|
|
|
|
|
|
|
$backendsConfig[$newName] = $config;
|
|
|
|
if ($this->writeConfiguration($backendsConfig, 'backends')) {
|
|
|
|
Notification::success(sprintf($this->translate('Backend "%s" successfully modified.'), $backend));
|
|
|
|
$this->redirectNow('monitoring/config');
|
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
|
|
|
return;
|
|
|
|
}
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
2014-07-25 14:55:36 +02:00
|
|
|
} else {
|
|
|
|
$form->setBackendConfig($backend, $backendsConfig[$backend]);
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
2014-07-25 14:55:36 +02:00
|
|
|
|
|
|
|
$this->view->form = $form;
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-25 08:39:27 +02:00
|
|
|
* Display a form to create a new backend
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function createbackendAction()
|
|
|
|
{
|
2014-07-25 15:58:44 +02:00
|
|
|
$form = new BackendForm();
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isPost() && $form->isValid($request->getPost())) {
|
|
|
|
list($name, $config) = $form->getBackendConfig();
|
|
|
|
$backendsConfig = $this->Config('backends')->toArray();
|
|
|
|
$backendsConfig[$name] = $config;
|
|
|
|
if ($this->writeConfiguration($backendsConfig, 'backends')) {
|
|
|
|
Notification::success(sprintf($this->translate('Backend "%s" created successfully.'), $name));
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
2014-08-11 14:49:42 +02:00
|
|
|
return;
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-25 15:58:44 +02:00
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
$this->view->form = $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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');
|
2014-07-25 16:00:38 +02:00
|
|
|
$backendsConfig = $this->Config('backends')->toArray();
|
|
|
|
if (false === array_key_exists($backend, $backendsConfig)) {
|
|
|
|
// TODO: Should behave as in the app's config controller (Specific redirect to an error action)
|
|
|
|
Notification::error(sprintf($this->translate('Cannot remove "%s". Backend not found.'), $backend));
|
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
2014-07-25 16:00:38 +02:00
|
|
|
$form = new ConfirmRemovalForm();
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isPost() && $form->isValid($request->getPost())) {
|
|
|
|
unset($backendsConfig[$backend]);
|
|
|
|
if ($this->writeConfiguration($backendsConfig, 'backends')) {
|
|
|
|
Notification::success(sprintf($this->translate('Backend "%s" successfully removed.'), $backend));
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
2014-08-11 14:49:42 +02:00
|
|
|
return;
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->view->form = $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-25 08:39:27 +02:00
|
|
|
* Display a confirmation form to remove the instance identified by the 'instance' parameter
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
|
|
|
public function removeinstanceAction()
|
2013-08-12 15:58:26 +02:00
|
|
|
{
|
2013-08-20 15:45:04 +02:00
|
|
|
$instance = $this->getParam('instance');
|
2014-08-11 14:49:42 +02:00
|
|
|
$instancesConfig = $this->Config('instances')->toArray();
|
|
|
|
if (false === array_key_exists($instance, $instancesConfig)) {
|
|
|
|
// TODO: Should behave as in the app's config controller (Specific redirect to an error action)
|
|
|
|
Notification::error(sprintf($this->translate('Cannot remove "%s". Instance not found.'), $instance));
|
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$form = new ConfirmRemovalForm();
|
2014-08-11 14:49:42 +02:00
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isPost() && $form->isValid($request->getPost())) {
|
|
|
|
unset($instancesConfig[$instance]);
|
|
|
|
if ($this->writeConfiguration($instancesConfig, 'instances')) {
|
|
|
|
Notification::success(sprintf($this->translate('Instance "%s" successfully removed.'), $instance));
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
2014-08-11 14:49:42 +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');
|
2014-08-11 14:49:42 +02:00
|
|
|
$instancesConfig = $this->Config('instances')->toArray();
|
|
|
|
if (false === array_key_exists($instance, $instancesConfig)) {
|
|
|
|
// TODO: Should behave as in the app's config controller (Specific redirect to an error action)
|
|
|
|
Notification::error(sprintf($this->translate('Cannot edit "%s". Instance not found.'), $instance));
|
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
2014-08-11 14:49:42 +02:00
|
|
|
|
|
|
|
$form = new InstanceForm();
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isPost()) {
|
|
|
|
if ($form->isValid($request->getPost())) {
|
|
|
|
list($newName, $config) = $form->getInstanceConfig();
|
|
|
|
|
|
|
|
if ($newName !== $instance) {
|
|
|
|
unset($instancesConfig[$instance]); // We can safely use unset as all values are part of the form
|
|
|
|
}
|
|
|
|
|
|
|
|
$instancesConfig[$newName] = $config;
|
|
|
|
if ($this->writeConfiguration($instancesConfig, 'instances')) {
|
|
|
|
Notification::success(sprintf($this->translate('Instance "%s" successfully modified.'), $instance));
|
|
|
|
$this->redirectNow('monitoring/config');
|
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
|
|
|
return;
|
|
|
|
}
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
2014-08-11 14:49:42 +02:00
|
|
|
} else {
|
|
|
|
$form->setInstanceConfig($instance, $instancesConfig[$instance]);
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
2014-08-11 14:49:42 +02:00
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
$this->view->form = $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a form to create a new instance
|
|
|
|
*/
|
|
|
|
public function createinstanceAction()
|
|
|
|
{
|
2014-08-11 14:49:42 +02:00
|
|
|
$form = new InstanceForm();
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isPost() && $form->isValid($request->getPost())) {
|
|
|
|
list($name, $config) = $form->getInstanceConfig();
|
|
|
|
$instancesConfig = $this->Config('instances')->toArray();
|
|
|
|
$instancesConfig[$name] = $config;
|
|
|
|
if ($this->writeConfiguration($instancesConfig, 'instances')) {
|
|
|
|
Notification::success(sprintf($this->translate('Instance "%s" created successfully.'), $name));
|
2013-11-20 19:10:38 +01:00
|
|
|
$this->redirectNow('monitoring/config');
|
2013-08-20 15:45:04 +02:00
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
2014-08-11 14:49:42 +02:00
|
|
|
return;
|
2013-08-20 15:45:04 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 14:49:42 +02:00
|
|
|
|
2013-08-20 15:45:04 +02:00
|
|
|
$this->view->form = $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-25 08:39:27 +02:00
|
|
|
* Write configuration to an ini file
|
|
|
|
*
|
|
|
|
* @param Zend_Config $config The configuration to write
|
|
|
|
* @param string $file The config file to write to
|
|
|
|
*
|
|
|
|
* @return bool Whether the configuration was written or not
|
2013-08-20 15:45:04 +02:00
|
|
|
*/
|
2014-08-21 10:22:16 +02:00
|
|
|
protected function writeConfiguration($config, $file = null)
|
2013-08-20 15:45:04 +02:00
|
|
|
{
|
2014-07-25 14:55:36 +02:00
|
|
|
if (is_array($config)) {
|
|
|
|
$config = new Zend_Config($config);
|
|
|
|
}
|
2014-06-24 21:22:38 +02:00
|
|
|
$target = $this->Config($file)->getConfigFile();
|
2014-04-29 11:30:34 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$this->view->tabs = $this->Module()->getConfigTabs()->activate('security');
|
2014-08-19 14:04:00 +02:00
|
|
|
|
|
|
|
$form = new SecurityForm();
|
|
|
|
$form->setConfiguration($this->Config()->get('security'));
|
2014-08-21 11:51:49 +02:00
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isPost() && $form->isValid($request->getPost())) {
|
2014-08-19 14:04:00 +02:00
|
|
|
$config = $this->Config()->toArray();
|
|
|
|
$config['security'] = $form->getConfig();
|
|
|
|
if ($this->writeConfiguration(new Zend_Config($config))) {
|
|
|
|
Notification::success('Configuration modified successfully');
|
|
|
|
$this->redirectNow('monitoring/config/security');
|
|
|
|
} else {
|
|
|
|
$this->render('show-configuration');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->view->form = $form;
|
2014-08-19 11:19:30 +02:00
|
|
|
}
|
2013-08-14 12:42:32 +02:00
|
|
|
}
|