2014-09-02 15:39:21 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Form\Config\Resource;
|
|
|
|
|
|
|
|
use Exception;
|
2014-11-07 13:53:03 +01:00
|
|
|
use Icinga\Application\Config;
|
2014-09-02 15:39:21 +02:00
|
|
|
use Icinga\Web\Form;
|
|
|
|
use Icinga\Web\Request;
|
|
|
|
use Icinga\Application\Icinga;
|
|
|
|
use Icinga\Data\ResourceFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form class for adding/modifying livestatus resources
|
|
|
|
*/
|
|
|
|
class LivestatusResourceForm extends Form
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Initialize this form
|
|
|
|
*/
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->setName('form_config_resource_livestatus');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Form::createElements()
|
|
|
|
*/
|
|
|
|
public function createElements(array $formData)
|
|
|
|
{
|
2014-09-29 11:20:39 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'name',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'label' => t('Resource Name'),
|
|
|
|
'description' => t('The unique name of this resource')
|
|
|
|
)
|
|
|
|
);
|
2014-09-03 12:21:31 +02:00
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'socket',
|
|
|
|
array(
|
|
|
|
'required' => true,
|
|
|
|
'label' => t('Socket'),
|
|
|
|
'description' => t('The path to your livestatus socket used for querying monitoring data'),
|
|
|
|
'value' => realpath(Icinga::app()->getApplicationDir() . '/../var/rw/livestatus')
|
2014-09-02 15:39:21 +02:00
|
|
|
)
|
|
|
|
);
|
2014-09-03 12:21:31 +02:00
|
|
|
|
|
|
|
return $this;
|
2014-09-02 15:39:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate that the current configuration points to a valid resource
|
|
|
|
*
|
|
|
|
* @see Form::onSuccess()
|
|
|
|
*/
|
|
|
|
public function onSuccess(Request $request)
|
|
|
|
{
|
2014-09-29 11:02:45 +02:00
|
|
|
if (false === static::isValidResource($this)) {
|
2014-09-02 15:39:21 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the resource configuration by trying to connect with it
|
|
|
|
*
|
|
|
|
* @param Form $form The form to fetch the configuration values from
|
|
|
|
*
|
|
|
|
* @return bool Whether validation succeeded or not
|
|
|
|
*/
|
2014-09-29 11:02:45 +02:00
|
|
|
public static function isValidResource(Form $form)
|
2014-09-02 15:39:21 +02:00
|
|
|
{
|
|
|
|
try {
|
2014-11-07 13:53:03 +01:00
|
|
|
$resource = ResourceFactory::createResource(new Config($form->getValues()));
|
2014-09-02 15:39:21 +02:00
|
|
|
$resource->connect()->disconnect();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$form->addError(t('Connectivity validation failed, connection to the given resource not possible.'));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|