2015-06-03 14:34:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Forms;
|
|
|
|
|
2016-09-06 17:18:29 +02:00
|
|
|
use Icinga\Module\Director\Data\PropertiesFilter\ArrayCustomVariablesFilter;
|
2016-10-13 12:25:11 +02:00
|
|
|
use Icinga\Module\Director\Exception\NestingError;
|
2015-06-03 14:34:54 +02:00
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
2016-03-18 12:15:25 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaHost;
|
2016-05-25 08:14:00 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaService;
|
2015-06-03 14:34:54 +02:00
|
|
|
|
|
|
|
class IcingaServiceForm extends DirectorObjectForm
|
|
|
|
{
|
2016-03-18 12:15:25 +01:00
|
|
|
private $host;
|
|
|
|
|
2016-05-25 08:14:00 +02:00
|
|
|
private $apply;
|
|
|
|
|
2016-06-26 16:38:34 +02:00
|
|
|
private $hostGenerated = false;
|
|
|
|
|
2016-09-08 13:25:48 +02:00
|
|
|
private $inheritedFrom;
|
|
|
|
|
2016-06-26 16:38:34 +02:00
|
|
|
public function setHostGenerated($hostGenerated = true)
|
|
|
|
{
|
|
|
|
$this->hostGenerated = $hostGenerated;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:25:48 +02:00
|
|
|
public function setInheritedFrom($hostname)
|
|
|
|
{
|
|
|
|
$this->inheritedFrom = $hostname;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-06-03 14:34:54 +02:00
|
|
|
public function setup()
|
2016-03-18 12:15:25 +01:00
|
|
|
{
|
2016-09-09 15:25:57 +02:00
|
|
|
if ($this->object && $this->object->usesVarOverrides()) {
|
2016-09-08 20:43:25 +02:00
|
|
|
return $this->setupForVarOverrides();
|
|
|
|
}
|
|
|
|
|
2016-06-26 16:38:34 +02:00
|
|
|
if ($this->hostGenerated) {
|
|
|
|
return $this->setupHostGenerated();
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:25:48 +02:00
|
|
|
if ($this->inheritedFrom) {
|
|
|
|
return $this->setupInherited();
|
|
|
|
}
|
|
|
|
|
2016-10-13 12:25:11 +02:00
|
|
|
try {
|
|
|
|
if (!$this->isNew() && $this->host === null) {
|
|
|
|
$this->host = $this->object->getResolvedRelated('host');
|
|
|
|
}
|
2016-10-21 23:13:33 +02:00
|
|
|
} catch (NestingError $nestingError) {
|
2016-10-13 12:25:11 +02:00
|
|
|
// ignore for the form to load
|
2016-03-18 12:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->host === null) {
|
|
|
|
$this->setupServiceElements();
|
|
|
|
} else {
|
|
|
|
$this->setupHostRelatedElements();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 08:14:00 +02:00
|
|
|
public function createApplyRuleFor(IcingaService $service)
|
|
|
|
{
|
|
|
|
$this->apply = $service;
|
|
|
|
$object = $this->object();
|
|
|
|
$object->imports = $service->object_name;
|
|
|
|
$object->object_type = 'apply';
|
|
|
|
$object->object_name = $service->object_name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-18 12:15:25 +01:00
|
|
|
protected function setupServiceElements()
|
2015-06-03 14:34:54 +02:00
|
|
|
{
|
2016-05-25 08:14:00 +02:00
|
|
|
if ($this->object) {
|
|
|
|
$this->addHidden('object_type', $this->object->object_type);
|
|
|
|
} else {
|
|
|
|
$this->addHidden('object_type', 'template');
|
|
|
|
}
|
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()
|
2016-09-06 17:18:29 +02:00
|
|
|
->addApplyForElement()
|
2016-03-10 20:44:12 +01:00
|
|
|
->groupMainProperties()
|
2016-03-26 16:18:50 +01:00
|
|
|
->addAssignmentElements()
|
2016-03-10 20:44:12 +01:00
|
|
|
->addCheckCommandElements()
|
|
|
|
->addCheckExecutionElements()
|
2016-06-17 09:38:48 +02:00
|
|
|
->addExtraInfoElements()
|
2016-03-10 20:44:12 +01:00
|
|
|
->addAgentAndZoneElements()
|
|
|
|
->setButtons();
|
|
|
|
}
|
|
|
|
|
2016-09-08 20:43:25 +02:00
|
|
|
protected function setupForVarOverrides()
|
|
|
|
{
|
|
|
|
$msg = $this->translate(
|
|
|
|
'This service has been generated in an automated way, but still'
|
|
|
|
. ' allows you to override the following properties in a safe way.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addHtmlHint($msg);
|
2016-10-11 16:36:07 +02:00
|
|
|
$this->setButtons();
|
|
|
|
$this->setSubmitLabel(
|
2016-09-08 20:43:25 +02:00
|
|
|
$this->translate('Override vars')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-06-26 16:38:34 +02:00
|
|
|
protected function setupHostGenerated()
|
|
|
|
{
|
2016-09-08 15:10:42 +02:00
|
|
|
$msg = $this->translate(
|
|
|
|
'This service has been generated from host properties.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addHtmlHint($msg);
|
2016-06-26 16:38:34 +02:00
|
|
|
|
|
|
|
$this->setSubmitLabel(
|
|
|
|
$this->translate('Override vars')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:25:48 +02:00
|
|
|
protected function setupInherited()
|
|
|
|
{
|
2016-09-08 15:10:42 +02:00
|
|
|
$view = $this->getView();
|
|
|
|
$msg = $view->escape($this->translate(
|
2016-09-08 13:25:48 +02:00
|
|
|
'This service has been inherited from %s. Still, you might want'
|
|
|
|
. ' to change the following properties for this host only.'
|
2016-09-08 15:10:42 +02:00
|
|
|
));
|
2016-09-08 13:25:48 +02:00
|
|
|
|
|
|
|
$name = $this->inheritedFrom;
|
2016-09-08 15:10:42 +02:00
|
|
|
$link = $view->qlink(
|
2016-09-08 13:25:48 +02:00
|
|
|
$name,
|
|
|
|
'director/service',
|
|
|
|
array(
|
|
|
|
'host' => $name,
|
|
|
|
'name' => $this->object->object_name,
|
|
|
|
),
|
|
|
|
array('data-base-target' => '_next')
|
|
|
|
);
|
|
|
|
|
2016-10-20 03:35:36 +02:00
|
|
|
$this->addHtmlHint(
|
|
|
|
sprintf($msg, $link),
|
|
|
|
array('name' => 'inheritance_hint')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addElementsToGroup(
|
|
|
|
array('inheritance_hint'),
|
|
|
|
'custom_fields',
|
|
|
|
50,
|
|
|
|
$this->translate('Custom properties')
|
|
|
|
);
|
|
|
|
|
2016-09-08 13:25:48 +02:00
|
|
|
$this->setSubmitLabel(
|
|
|
|
$this->translate('Override vars')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-26 16:18:50 +01:00
|
|
|
protected function addAssignmentElements()
|
|
|
|
{
|
|
|
|
if (!$this->object || !$this->object->isApplyRule()) {
|
|
|
|
return $this;
|
|
|
|
}
|
2016-10-21 08:56:44 +02:00
|
|
|
$this->addElement('dataFilter', 'assign_filter', array(
|
|
|
|
'columns' => IcingaHost::enumProperties($this->db),
|
|
|
|
'required' => true,
|
|
|
|
));
|
|
|
|
$el = $this->getElement('assign_filter');
|
|
|
|
$el->setDecorators(array(
|
|
|
|
'ViewHelper',
|
|
|
|
array('HtmlTag', array(
|
|
|
|
'tag' => 'ul',
|
|
|
|
'class' => 'assign-rule required'
|
|
|
|
)),
|
|
|
|
));
|
2016-03-26 16:18:50 +01:00
|
|
|
|
2016-10-21 08:56:44 +02:00
|
|
|
$this->addDisplayGroup(array($el), 'assign', array(
|
|
|
|
'decorators' => array(
|
|
|
|
'FormElements',
|
|
|
|
array('HtmlTag', array('tag' => 'dl')),
|
|
|
|
'Fieldset',
|
|
|
|
),
|
|
|
|
'order' => 30,
|
|
|
|
'legend' => $this->translate('Assign where')
|
|
|
|
));
|
2016-03-26 16:18:50 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
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');
|
2016-09-08 18:19:57 +02:00
|
|
|
|
|
|
|
if ($this->hasBeenSent()) {
|
|
|
|
$imports = $this->getElement('imports')->setValue($imports)->getValue();
|
|
|
|
}
|
|
|
|
|
2016-03-22 03:24:43 +01:00
|
|
|
if ($this->isNew() && empty($imports)) {
|
2016-03-18 12:15:25 +01:00
|
|
|
return $this->groupMainProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addNameElement()
|
|
|
|
->addDisabledElement()
|
|
|
|
->groupMainProperties()
|
|
|
|
->addCheckCommandElements()
|
2016-06-17 09:38:48 +02:00
|
|
|
->addExtraInfoElements()
|
2016-03-18 12:15:25 +01:00
|
|
|
->setButtons();
|
|
|
|
|
|
|
|
if ($this->hasBeenSent()) {
|
2016-09-08 18:19:57 +02:00
|
|
|
$name = $this->getSentOrObjectValue('object_name');
|
2016-03-18 12:15:25 +01:00
|
|
|
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'),
|
2016-10-22 02:05:08 +02:00
|
|
|
'required' => !$this->object()->isApplyRule(),
|
2016-03-18 12:15:25 +01:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
2016-09-06 17:18:29 +02:00
|
|
|
protected function addApplyForElement()
|
|
|
|
{
|
|
|
|
if ($this->object->isApplyRule()) {
|
2016-10-21 23:13:33 +02:00
|
|
|
$hostProperties = IcingaHost::enumProperties(
|
|
|
|
$this->object->getConnection(),
|
|
|
|
'host.',
|
|
|
|
new ArrayCustomVariablesFilter()
|
|
|
|
);
|
|
|
|
|
2016-09-06 17:18:29 +02:00
|
|
|
$this->addElement('select', 'apply_for', array(
|
|
|
|
'label' => $this->translate('Apply For'),
|
|
|
|
'class' => 'assign-property autosubmit',
|
2016-10-21 22:58:28 +02:00
|
|
|
'multiOptions' => $this->optionalEnum($hostProperties, $this->translate('None')),
|
2016-09-06 17:18:29 +02:00
|
|
|
'description' => $this->translate(
|
|
|
|
'Evaluates the apply for rule for ' .
|
|
|
|
'all objects with the custom attribute specified. ' .
|
2016-09-09 00:37:23 +02:00
|
|
|
'E.g selecting "host.vars.custom_attr" will generate "for (config in ' .
|
|
|
|
'host.vars.array_var)" where "config" will be accessible through "$config$". ' .
|
2016-09-06 17:18:29 +02:00
|
|
|
'NOTE: only custom variables of type "Array" are eligible.'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-03-10 20:44:12 +01:00
|
|
|
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);
|
|
|
|
}
|
2016-06-26 16:38:34 +02:00
|
|
|
|
2016-09-08 13:25:48 +02:00
|
|
|
protected function succeedForOverrides()
|
2016-06-26 16:38:34 +02:00
|
|
|
{
|
|
|
|
|
2016-09-08 13:32:41 +02:00
|
|
|
$vars = array();
|
2016-06-26 16:38:34 +02:00
|
|
|
foreach ($this->object->vars() as $key => $var) {
|
2016-09-08 13:32:41 +02:00
|
|
|
$vars[$key] = $var->getValue();
|
2016-06-26 16:38:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$host = $this->host;
|
2016-09-08 13:25:48 +02:00
|
|
|
$serviceName = $this->object->object_name;
|
|
|
|
|
2016-09-08 13:32:41 +02:00
|
|
|
$this->host->overrideServiceVars($serviceName, (object) $vars);
|
2016-06-26 16:38:34 +02:00
|
|
|
|
|
|
|
if ($host->hasBeenModified()) {
|
2016-09-08 13:25:48 +02:00
|
|
|
$msg = sprintf(
|
2016-09-08 13:32:41 +02:00
|
|
|
empty($vars)
|
2016-09-08 13:25:48 +02:00
|
|
|
? $this->translate('All overrides have been removed from "%s"')
|
|
|
|
: $this->translate('The given properties have been stored for "%s"'),
|
|
|
|
$this->translate($host->object_name)
|
|
|
|
);
|
|
|
|
|
2016-06-26 16:38:34 +02:00
|
|
|
$host->store();
|
2016-09-08 13:25:48 +02:00
|
|
|
} else {
|
|
|
|
if ($this->isApiRequest()) {
|
|
|
|
$this->setHttpResponseCode(304);
|
|
|
|
}
|
|
|
|
|
|
|
|
$msg = $this->translate('No action taken, object has not been modified');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->redirectOnSuccess($msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onSuccess()
|
|
|
|
{
|
2016-09-08 20:43:25 +02:00
|
|
|
if ($this->hostGenerated || $this->inheritedFrom || $this->object->usesVarOverrides()) {
|
2016-09-08 13:25:48 +02:00
|
|
|
return $this->succeedForOverrides();
|
2016-06-26 16:38:34 +02:00
|
|
|
}
|
2016-09-08 13:25:48 +02:00
|
|
|
|
|
|
|
return parent::onSuccess();
|
2016-06-26 16:38:34 +02:00
|
|
|
}
|
2015-06-03 14:34:54 +02:00
|
|
|
}
|