monitoring: It's now a command transport, not an instance anymore

refs #9651
This commit is contained in:
Johannes Meyer 2015-08-26 15:43:30 +02:00
parent 6651d31481
commit ec8567cbbf
9 changed files with 124 additions and 122 deletions

View File

@ -16,12 +16,12 @@ use Icinga\Module\Monitoring\Forms\Config\SecurityConfigForm;
class Monitoring_ConfigController extends Controller
{
/**
* Display a list of available backends and instances
* Display a list of available backends and command transports
*/
public function indexAction()
{
$this->view->backendsConfig = $this->Config('backends');
$this->view->instancesConfig = $this->Config('instances');
$this->view->transportConfig = $this->Config('commandtransports');
$this->view->tabs = $this->Module()->getConfigTabs()->activate('backends');
}
@ -145,31 +145,34 @@ class Monitoring_ConfigController extends Controller
}
/**
* Remove a monitoring instance
* Remove a command transport
*/
public function removeinstanceAction()
public function removetransportAction()
{
$instanceName = $this->params->getRequired('instance');
$transportName = $this->params->getRequired('transport');
$instanceForm = new InstanceConfigForm();
$instanceForm->setIniConfig($this->Config('instances'));
$transportForm = new InstanceConfigForm();
$transportForm->setIniConfig($this->Config('commandtransports'));
$form = new ConfirmRemovalForm();
$form->setRedirectUrl('monitoring/config');
$form->setTitle(sprintf($this->translate('Remove Monitoring Instance %s'), $instanceName));
$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.'
));
$form->setOnSuccess(function (ConfirmRemovalForm $form) use ($instanceName, $instanceForm) {
$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) {
try {
$instanceForm->delete($instanceName);
$transportForm->delete($transportName);
} catch (Exception $e) {
$form->error($e->getMessage());
return false;
}
if ($instanceForm->save()) {
Notification::success(sprintf(t('Monitoring instance "%s" successfully removed'), $instanceName));
if ($transportForm->save()) {
Notification::success(sprintf(t('Command transport "%s" successfully removed'), $transportName));
return true;
}
@ -182,19 +185,19 @@ class Monitoring_ConfigController extends Controller
}
/**
* Edit a monitoring instance
* Edit a command transport
*/
public function editinstanceAction()
public function edittransportAction()
{
$instanceName = $this->params->getRequired('instance');
$transportName = $this->params->getRequired('transport');
$form = new InstanceConfigForm();
$form->setRedirectUrl('monitoring/config');
$form->setTitle(sprintf($this->translate('Edit Monitoring Instance %s'), $instanceName));
$form->setIniConfig($this->Config('instances'));
$form->setOnSuccess(function (InstanceConfigForm $form) use ($instanceName) {
$form->setTitle(sprintf($this->translate('Edit Command Transport %s'), $transportName));
$form->setIniConfig($this->Config('commandtransports'));
$form->setOnSuccess(function (InstanceConfigForm $form) use ($transportName) {
try {
$form->edit($instanceName, array_map(
$form->edit($transportName, array_map(
function ($v) {
return $v !== '' ? $v : null;
},
@ -206,7 +209,7 @@ class Monitoring_ConfigController extends Controller
}
if ($form->save()) {
Notification::success(sprintf(t('Monitoring instance "%s" successfully updated'), $instanceName));
Notification::success(sprintf(t('Command transport "%s" successfully updated'), $transportName));
return true;
}
@ -214,10 +217,10 @@ class Monitoring_ConfigController extends Controller
});
try {
$form->load($instanceName);
$form->load($transportName);
$form->handleRequest();
} catch (NotFoundError $_) {
$this->httpNotFound(sprintf($this->translate('Monitoring instance "%s" not found'), $instanceName));
$this->httpNotFound(sprintf($this->translate('Command transport "%s" not found'), $transportName));
}
$this->view->form = $form;
@ -225,14 +228,14 @@ class Monitoring_ConfigController extends Controller
}
/**
* Create a new monitoring instance
* Create a new command transport
*/
public function createinstanceAction()
public function createtransportAction()
{
$form = new InstanceConfigForm();
$form->setRedirectUrl('monitoring/config');
$form->setTitle($this->translate('Create New Monitoring Instance'));
$form->setIniConfig($this->Config('instances'));
$form->setTitle($this->translate('Create New Command Transport'));
$form->setIniConfig($this->Config('commandtransports'));
$form->setOnSuccess(function (InstanceConfigForm $form) {
try {
$form->add(array_filter($form->getValues()));
@ -242,7 +245,7 @@ class Monitoring_ConfigController extends Controller
}
if ($form->save()) {
Notification::success(t('Monitoring instance successfully created'));
Notification::success(t('Command transport successfully created'));
return true;
}

View File

@ -52,9 +52,9 @@ abstract class CommandForm extends Form
*/
public function getTransport(Request $request)
{
$instance = $request->getParam('instance');
if ($instance !== null) {
$transport = CommandTransport::create($instance);
$transportName = $request->getParam('transport');
if ($transportName !== null) {
$transport = CommandTransport::create($transportName);
} else {
$transport = CommandTransport::first();
}

View File

@ -13,7 +13,7 @@ class LocalInstanceForm extends Form
*/
public function init()
{
$this->setName('form_config_monitoring_instance_local');
$this->setName('form_config_command_transport_local');
}
/**

View File

@ -10,7 +10,7 @@ use Icinga\Web\Form;
class RemoteInstanceForm extends Form
{
/**
* The available monitoring instance resources split by type
* The available resources split by type
*
* @var array
*/
@ -22,7 +22,7 @@ class RemoteInstanceForm extends Form
*/
public function init()
{
$this->setName('form_config_monitoring_instance_remote');
$this->setName('form_config_command_transport_remote');
}
/**
@ -44,7 +44,7 @@ class RemoteInstanceForm extends Form
}
if (empty($resources)) {
throw new ConfigurationError($this->translate('Could not find any valid monitoring instance resources'));
throw new ConfigurationError($this->translate('Could not find any valid SSH resources'));
}
$this->resources = $resources;

View File

@ -13,36 +13,36 @@ use Icinga\Module\Monitoring\Forms\Config\Instance\LocalInstanceForm;
use Icinga\Module\Monitoring\Forms\Config\Instance\RemoteInstanceForm;
/**
* Form for managing monitoring instances
* Form for managing command transports
*/
class InstanceConfigForm extends ConfigForm
{
/**
* The instance to load when displaying the form for the first time
* The transport to load when displaying the form for the first time
*
* @var string
*/
protected $instanceToLoad;
protected $transportToLoad;
/**
* Initialize this form
*/
public function init()
{
$this->setName('form_config_monitoring_instance');
$this->setName('form_config_command_transports');
$this->setSubmitLabel($this->translate('Save Changes'));
}
/**
* Return a form object for the given instance type
* Return a form object for the given transport type
*
* @param string $type The instance type for which to return a form
* @param string $type The transport type for which to return a form
*
* @return Form
*
* @throws InvalidArgumentException In case the given instance type is invalid
* @throws InvalidArgumentException In case the given transport type is invalid
*/
public function getInstanceForm($type)
public function getTransportForm($type)
{
switch (strtolower($type)) {
case LocalCommandFile::TRANSPORT:
@ -51,41 +51,41 @@ class InstanceConfigForm extends ConfigForm
return new RemoteInstanceForm();
default:
throw new InvalidArgumentException(
sprintf($this->translate('Invalid monitoring instance type "%s" given'), $type)
sprintf($this->translate('Invalid command transport type "%s" given'), $type)
);
}
}
/**
* Populate the form with the given instance's config
* Populate the form with the given transport's config
*
* @param string $name
*
* @return $this
*
* @throws NotFoundError In case no instance with the given name is found
* @throws NotFoundError In case no transport with the given name is found
*/
public function load($name)
{
if (! $this->config->hasSection($name)) {
throw new NotFoundError('No monitoring instance called "%s" found', $name);
throw new NotFoundError('No command transport called "%s" found', $name);
}
$this->instanceToLoad = $name;
$this->transportToLoad = $name;
return $this;
}
/**
* Add a new instance
* Add a new command transport
*
* The instance to add is identified by the array-key `name'.
* The transport to add is identified by the array-key `name'.
*
* @param array $data
*
* @return $this
*
* @throws InvalidArgumentException In case $data does not contain a instance name
* @throws IcingaException In case a instance with the same name already exists
* @throws InvalidArgumentException In case $data does not contain a transport name
* @throws IcingaException In case a transport with the same name already exists
*/
public function add(array $data)
{
@ -93,36 +93,36 @@ class InstanceConfigForm extends ConfigForm
throw new InvalidArgumentException('Key \'name\' missing');
}
$instanceName = $data['name'];
if ($this->config->hasSection($instanceName)) {
$transportName = $data['name'];
if ($this->config->hasSection($transportName)) {
throw new IcingaException(
$this->translate('A monitoring instance with the name "%s" does already exist'),
$instanceName
$this->translate('A command transport with the name "%s" does already exist'),
$transportName
);
}
unset($data['name']);
$this->config->setSection($instanceName, $data);
$this->config->setSection($transportName, $data);
return $this;
}
/**
* Edit an existing instance
* Edit an existing command transport
*
* @param string $name
* @param array $data
*
* @return $this
*
* @throws NotFoundError In case no instance with the given name is found
* @throws NotFoundError In case no transport with the given name is found
*/
public function edit($name, array $data)
{
if (! $this->config->hasSection($name)) {
throw new NotFoundError('No monitoring instance called "%s" found', $name);
throw new NotFoundError('No command transport called "%s" found', $name);
}
$instanceConfig = $this->config->getSection($name);
$transportConfig = $this->config->getSection($name);
if (isset($data['name'])) {
if ($data['name'] !== $name) {
$this->config->removeSection($name);
@ -132,19 +132,19 @@ class InstanceConfigForm extends ConfigForm
unset($data['name']);
}
$instanceConfig->merge($data);
foreach ($instanceConfig->toArray() as $k => $v) {
$transportConfig->merge($data);
foreach ($transportConfig->toArray() as $k => $v) {
if ($v === null) {
unset($instanceConfig->$k);
unset($transportConfig->$k);
}
}
$this->config->setSection($name, $instanceConfig);
$this->config->setSection($name, $transportConfig);
return $this;
}
/**
* Remove a instance
* Remove a command transport
*
* @param string $name
*
@ -168,9 +168,9 @@ class InstanceConfigForm extends ConfigForm
'name',
array(
'required' => true,
'label' => $this->translate('Instance Name'),
'label' => $this->translate('Transport Name'),
'description' => $this->translate(
'The name of this monitoring instance that is used to differentiate it from others'
'The name of this command transport that is used to differentiate it from others'
),
'validators' => array(
array(
@ -189,14 +189,14 @@ class InstanceConfigForm extends ConfigForm
)
);
$instanceTypes = array(
$transportTypes = array(
LocalCommandFile::TRANSPORT => $this->translate('Local Command File'),
RemoteCommandFile::TRANSPORT => $this->translate('Remote Command File')
);
$instanceType = isset($formData['transport']) ? $formData['transport'] : null;
if ($instanceType === null) {
$instanceType = key($instanceTypes);
$transportType = isset($formData['transport']) ? $formData['transport'] : null;
if ($transportType === null) {
$transportType = key($transportTypes);
}
$this->addElements(array(
@ -206,24 +206,23 @@ class InstanceConfigForm extends ConfigForm
array(
'required' => true,
'autosubmit' => true,
'label' => $this->translate('Instance Type'),
'description' => $this->translate('The type of transport to use for this monitoring instance'),
'multiOptions' => $instanceTypes
'label' => $this->translate('Transport Type'),
'multiOptions' => $transportTypes
)
)
));
$this->addSubForm($this->getInstanceForm($instanceType)->create($formData), 'instance_form');
$this->addSubForm($this->getTransportForm($transportType)->create($formData), 'transport_form');
}
/**
* Populate the configuration of the instance to load
* Populate the configuration of the transport to load
*/
public function onRequest()
{
if ($this->instanceToLoad) {
$data = $this->config->getSection($this->instanceToLoad)->toArray();
$data['name'] = $this->instanceToLoad;
if ($this->transportToLoad) {
$data = $this->config->getSection($this->transportToLoad)->toArray();
$data['name'] = $this->transportToLoad;
$this->populate($data);
}
}
@ -238,8 +237,8 @@ class InstanceConfigForm extends ConfigForm
public function getValues($suppressArrayNotation = false)
{
$values = parent::getValues();
$values = array_merge($values, $values['instance_form']);
unset($values['instance_form']);
$values = array_merge($values, $values['transport_form']);
unset($values['transport_form']);
return $values;
}
}

View File

@ -10,23 +10,23 @@ class InstancePage extends Form
{
public function init()
{
$this->setName('setup_monitoring_instance');
$this->setTitle($this->translate('Monitoring Instance', 'setup.page.title'));
$this->setName('setup_command_transport');
$this->setTitle($this->translate('Command Transport', 'setup.page.title'));
$this->addDescription($this->translate(
'Please define the settings specific to your monitoring instance below.'
'Please define below how you want to send commands to your monitoring instance.'
));
}
public function createElements(array $formData)
{
$instanceConfigForm = new InstanceConfigForm();
$this->addSubForm($instanceConfigForm, 'instance_form');
$instanceConfigForm->create($formData);
$instanceConfigForm->getElement('name')->setValue('icinga');
$transportConfigForm = new InstanceConfigForm();
$this->addSubForm($transportConfigForm, 'transport_form');
$transportConfigForm->create($formData);
$transportConfigForm->getElement('name')->setValue('icinga2');
}
public function getValues($suppressArrayNotation = false)
{
return $this->getSubForm('instance_form')->getValues($suppressArrayNotation);
return $this->getSubForm('transport_form')->getValues($suppressArrayNotation);
}
}

View File

@ -47,28 +47,28 @@
<?php endforeach; ?>
</tbody>
</table>
<h1><?= $this->translate('Monitoring Instances') ?></h1>
<h1><?= $this->translate('Command Transports') ?></h1>
<p>
<a href="<?= $this->href('/monitoring/config/createinstance'); ?>">
<?= $this->icon('plus'); ?> <?= $this->translate('Create New Instance'); ?>
<a href="<?= $this->href('/monitoring/config/createtransport'); ?>">
<?= $this->icon('plus'); ?> <?= $this->translate('Create New Transport'); ?>
</a>
</p>
<table class="action alternating">
<thead>
<th><?= $this->translate('Instance'); ?></th>
<th><?= $this->translate('Transport'); ?></th>
<th style="width: 5em"><?= $this->translate('Remove'); ?></th>
</thead>
<tbody>
<?php foreach ($this->instancesConfig as $instanceName => $config): ?>
<?php foreach ($this->transportConfig as $transportName => $config): ?>
<tr>
<td>
<?= $this->qlink(
$instanceName,
'/monitoring/config/editinstance',
array('instance' => $instanceName),
$transportName,
'/monitoring/config/edittransport',
array('transport' => $transportName),
array(
'icon' => 'edit',
'title' => sprintf($this->translate('Edit monitoring instance %s'), $instanceName)
'title' => sprintf($this->translate('Edit command transport %s'), $transportName)
)
); ?>
<small>(<?= sprintf(
@ -79,11 +79,11 @@
<td>
<?= $this->qlink(
'',
'/monitoring/config/removeinstance',
array('instance' => $instanceName),
'/monitoring/config/removetransport',
array('transport' => $transportName),
array(
'icon' => 'trash',
'title' => sprintf($this->translate('Remove monitoring instance %s'), $instanceName)
'title' => sprintf($this->translate('Remove command transport %s'), $transportName)
)
); ?>
</td>

View File

@ -21,13 +21,13 @@ class InstanceStep extends Step
public function apply()
{
$instanceConfig = $this->data['instanceConfig'];
$instanceName = $instanceConfig['name'];
unset($instanceConfig['name']);
$transportConfig = $this->data['transportConfig'];
$transportName = $transportConfig['name'];
unset($transportConfig['name']);
try {
Config::fromArray(array($instanceName => $instanceConfig))
->setConfigFile(Config::resolvePath('modules/monitoring/instances.ini'))
Config::fromArray(array($transportName => $transportConfig))
->setConfigFile(Config::resolvePath('modules/monitoring/commandtransports.ini'))
->saveIni();
} catch (Exception $e) {
$this->error = $e;
@ -40,16 +40,16 @@ class InstanceStep extends Step
public function getSummary()
{
$pageTitle = '<h2>' . mt('monitoring', 'Monitoring Instance', 'setup.page.title') . '</h2>';
$pageTitle = '<h2>' . mt('monitoring', 'Command Transport', 'setup.page.title') . '</h2>';
if (isset($this->data['instanceConfig']['host'])) {
if (isset($this->data['transportConfig']['host'])) {
$pipeHtml = '<p>' . sprintf(
mt(
'monitoring',
'Icinga Web 2 will use the named pipe located on a remote machine at "%s" to send commands'
. ' to your monitoring instance by using the connection details listed below:'
),
$this->data['instanceConfig']['path']
$this->data['transportConfig']['path']
) . '</p>';
$pipeHtml .= ''
@ -57,15 +57,15 @@ class InstanceStep extends Step
. '<tbody>'
. '<tr>'
. '<td><strong>' . mt('monitoring', 'Remote Host') . '</strong></td>'
. '<td>' . $this->data['instanceConfig']['host'] . '</td>'
. '<td>' . $this->data['transportConfig']['host'] . '</td>'
. '</tr>'
. '<tr>'
. '<td><strong>' . mt('monitoring', 'Remote SSH Port') . '</strong></td>'
. '<td>' . $this->data['instanceConfig']['port'] . '</td>'
. '<td>' . $this->data['transportConfig']['port'] . '</td>'
. '</tr>'
. '<tr>'
. '<td><strong>' . mt('monitoring', 'Remote SSH User') . '</strong></td>'
. '<td>' . $this->data['instanceConfig']['user'] . '</td>'
. '<td>' . $this->data['transportConfig']['user'] . '</td>'
. '</tr>'
. '</tbody>'
. '</table>';
@ -76,7 +76,7 @@ class InstanceStep extends Step
'Icinga Web 2 will use the named pipe located at "%s"'
. ' to send commands to your monitoring instance.'
),
$this->data['instanceConfig']['path']
$this->data['transportConfig']['path']
) . '</p>';
}
@ -87,17 +87,17 @@ class InstanceStep extends Step
{
if ($this->error === false) {
return array(sprintf(
mt('monitoring', 'Monitoring instance configuration has been successfully created: %s'),
Config::resolvePath('modules/monitoring/instances.ini')
mt('monitoring', 'Command transport configuration has been successfully created: %s'),
Config::resolvePath('modules/monitoring/commandtransports.ini')
));
} elseif ($this->error !== null) {
return array(
sprintf(
mt(
'monitoring',
'Monitoring instance configuration could not be written to: %s. An error occured:'
'Command transport configuration could not be written to: %s. An error occured:'
),
Config::resolvePath('modules/monitoring/instances.ini')
Config::resolvePath('modules/monitoring/commandtransports.ini')
),
sprintf(mt('setup', 'ERROR: %s'), IcingaException::describe($this->error))
);

View File

@ -151,7 +151,7 @@ class MonitoringWizard extends Wizard implements SetupWizard
$setup->addStep(
new InstanceStep(array(
'instanceConfig' => $pageData['setup_monitoring_instance']
'transportConfig' => $pageData['setup_command_transport']
))
);