2015-06-03 14:34:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
2016-03-18 12:15:25 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaHost;
|
2015-06-03 14:34:54 +02:00
|
|
|
|
|
|
|
class IcingaServiceForm extends DirectorObjectForm
|
|
|
|
{
|
2016-03-18 12:15:25 +01:00
|
|
|
private $host;
|
|
|
|
|
2015-06-03 14:34:54 +02:00
|
|
|
public function setup()
|
2016-03-18 12:15:25 +01:00
|
|
|
{
|
|
|
|
if (!$this->isNew() && $this->host === null) {
|
|
|
|
$this->host = $this->object->getResolvedRelated('host');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->host === null) {
|
|
|
|
$this->setupServiceElements();
|
|
|
|
} else {
|
|
|
|
$this->setupHostRelatedElements();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setupServiceElements()
|
2015-06-03 14:34:54 +02:00
|
|
|
{
|
2016-02-17 21:58:28 +01:00
|
|
|
$this->addObjectTypeElement();
|
2016-03-07 14:20:58 +01:00
|
|
|
if (! $this->hasObjectType()) {
|
2016-03-10 20:44:12 +01:00
|
|
|
return $this->groupMainProperties();
|
2016-03-07 14:20:58 +01:00
|
|
|
}
|
2016-02-17 21:58:28 +01:00
|
|
|
|
2015-06-03 14:34:54 +02:00
|
|
|
|
2016-03-18 12:15:25 +01:00
|
|
|
$this->addNameElement()
|
|
|
|
->addHostObjectElement()
|
2016-03-10 20:44:12 +01:00
|
|
|
->addImportsElement()
|
|
|
|
->addGroupsElement()
|
|
|
|
->addDisabledElement()
|
|
|
|
->groupMainProperties()
|
|
|
|
->addCheckCommandElements()
|
|
|
|
->addCheckExecutionElements()
|
|
|
|
->addAgentAndZoneElements()
|
|
|
|
->setButtons();
|
|
|
|
}
|
|
|
|
|
2016-03-18 12:15:25 +01:00
|
|
|
protected function setupHostRelatedElements()
|
|
|
|
{
|
|
|
|
$this->addHidden('host_id', $this->host->id);
|
|
|
|
$this->addHidden('object_type', 'object');
|
|
|
|
$this->addImportsElement();
|
|
|
|
$imports = $this->getSentOrObjectValue('imports');
|
|
|
|
if (empty($imports)) {
|
|
|
|
return $this->groupMainProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addNameElement()
|
|
|
|
->addDisabledElement()
|
|
|
|
->groupMainProperties()
|
|
|
|
->addCheckCommandElements()
|
|
|
|
->setButtons();
|
|
|
|
|
|
|
|
if ($this->hasBeenSent()) {
|
|
|
|
$name = $this->getValue('object_name');
|
|
|
|
if (!strlen($name)) {
|
|
|
|
$this->setElementValue('object_name', end($imports));
|
|
|
|
$this->object->object_name = end($imports);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHost(IcingaHost $host)
|
|
|
|
{
|
|
|
|
$this->host = $host;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addNameElement()
|
|
|
|
{
|
|
|
|
$this->addElement('text', 'object_name', array(
|
|
|
|
'label' => $this->translate('Name'),
|
|
|
|
'required' => true,
|
|
|
|
'description' => $this->translate(
|
|
|
|
'Name for the Icinga service you are going to create'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-10 20:44:12 +01:00
|
|
|
protected function addHostObjectElement()
|
|
|
|
{
|
2016-03-06 09:53:02 +01:00
|
|
|
if ($this->isObject()) {
|
|
|
|
$this->addElement('select', 'host_id', array(
|
|
|
|
'label' => $this->translate('Host'),
|
|
|
|
'required' => true,
|
|
|
|
'multiOptions' => $this->optionalEnum($this->enumHostsAndTemplates()),
|
2016-03-10 20:44:12 +01:00
|
|
|
'description' => $this->translate(
|
|
|
|
'Choose the host this single service should be assigned to'
|
|
|
|
)
|
2016-03-06 09:53:02 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2016-03-10 20:44:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addGroupsElement()
|
|
|
|
{
|
2016-03-07 18:39:19 +01:00
|
|
|
$groups = $this->enumServicegroups();
|
2016-03-10 20:44:12 +01:00
|
|
|
|
2016-03-07 18:39:19 +01:00
|
|
|
if (! empty($groups)) {
|
|
|
|
$this->addElement('extensibleSet', 'groups', array(
|
|
|
|
'label' => $this->translate('Groups'),
|
|
|
|
'multiOptions' => $this->optionallyAddFromEnum($groups),
|
|
|
|
'positional' => false,
|
|
|
|
'description' => $this->translate(
|
2016-03-10 20:44:12 +01:00
|
|
|
'Service groups that should be directly assigned to this service.'
|
|
|
|
. ' Servicegroups can be useful for various reasons. They are'
|
|
|
|
. ' helpful to provided service-type specific view in Icinga Web 2,'
|
|
|
|
. ' either for custom dashboards or as an instrument to enforce'
|
|
|
|
. ' restrictions. Service groups can be directly assigned to'
|
|
|
|
. ' single services or to service templates.'
|
2016-03-07 18:39:19 +01:00
|
|
|
)
|
|
|
|
));
|
|
|
|
}
|
2016-03-06 09:53:02 +01:00
|
|
|
|
2016-03-10 20:44:12 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2015-10-15 23:53:59 +02:00
|
|
|
|
2016-03-10 20:44:12 +01:00
|
|
|
protected function addAgentAndZoneElements()
|
|
|
|
{
|
2016-03-18 12:15:25 +01:00
|
|
|
if (!$this->isTemplate()) {
|
2016-03-10 20:44:12 +01:00
|
|
|
return $this;
|
2016-03-06 09:53:02 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 20:44:12 +01:00
|
|
|
$this->optionalBoolean(
|
|
|
|
'use_agent',
|
|
|
|
$this->translate('Run on agent'),
|
|
|
|
$this->translate(
|
|
|
|
'Whether the check commmand for this service should be executed'
|
|
|
|
. ' on the Icinga agent'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->addZoneElement();
|
2016-03-07 18:39:19 +01:00
|
|
|
|
|
|
|
$elements = array(
|
2016-03-10 20:44:12 +01:00
|
|
|
'use_agent',
|
|
|
|
'zone_id',
|
2016-03-07 18:39:19 +01:00
|
|
|
);
|
2016-03-10 20:44:12 +01:00
|
|
|
$this->addDisplayGroup($elements, 'clustering', array(
|
2016-03-07 18:39:19 +01:00
|
|
|
'decorators' => array(
|
|
|
|
'FormElements',
|
|
|
|
array('HtmlTag', array('tag' => 'dl')),
|
|
|
|
'Fieldset',
|
|
|
|
),
|
2016-03-10 20:44:12 +01:00
|
|
|
'order' => 40,
|
|
|
|
'legend' => $this->translate('Icinga Agent and zone settings')
|
2016-03-07 18:39:19 +01:00
|
|
|
));
|
2016-03-10 20:44:12 +01:00
|
|
|
|
|
|
|
return $this;
|
2016-03-07 18:39:19 +01:00
|
|
|
}
|
|
|
|
|
2016-03-06 09:53:02 +01:00
|
|
|
protected function enumHostsAndTemplates()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
$this->translate('Templates') => $this->db->enumHostTemplates(),
|
|
|
|
$this->translate('Hosts') => $this->db->enumHosts(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function enumServicegroups()
|
|
|
|
{
|
|
|
|
$db = $this->db->getDbAdapter();
|
|
|
|
$select = $db->select()->from(
|
|
|
|
'icinga_servicegroup',
|
|
|
|
array(
|
|
|
|
'name' => 'object_name',
|
|
|
|
'display' => 'COALESCE(display_name, object_name)'
|
|
|
|
)
|
|
|
|
)->where('object_type = ?', 'object')->order('display');
|
|
|
|
|
|
|
|
return $db->fetchPairs($select);
|
|
|
|
}
|
2015-06-03 14:34:54 +02:00
|
|
|
}
|