legacy: Support service blacklisting

This commit is contained in:
Markus Frosch 2018-09-20 12:53:21 +02:00
parent e2bd821d26
commit 67222ef437
2 changed files with 42 additions and 1 deletions

View File

@ -197,7 +197,6 @@ class IcingaService extends IcingaObject
*/ */
public function renderHost_id() public function renderHost_id()
{ {
// @codingStandardsIgnoreEnd
if ($this->hasBeenAssignedToHostTemplate()) { if ($this->hasBeenAssignedToHostTemplate()) {
return ''; return '';
} }
@ -205,6 +204,28 @@ class IcingaService extends IcingaObject
return $this->renderRelationProperty('host', $this->get('host_id'), 'host_name'); return $this->renderRelationProperty('host', $this->get('host_id'), 'host_name');
} }
/**
* @codingStandardsIgnoreStart
*/
protected function renderLegacyHost_id($value)
{
// @codingStandardsIgnoreEnd
if (is_array($value)) {
$blacklisted = $this->getBlacklistedHostnames();
$c = c1::renderKeyValue('host_name', c1::renderArray(array_diff($value, $blacklisted)));
// blacklisted in this (zoned) scope?
$bl = array_intersect($blacklisted, $value);
if (! empty($bl)) {
$c .= c1::renderKeyValue('# ignored on', c1::renderArray($bl));
}
return $c;
} else {
return parent::renderLegacyHost_id($value);
}
}
/** /**
* @param IcingaConfig $config * @param IcingaConfig $config
* @throws IcingaException * @throws IcingaException
@ -237,6 +258,12 @@ class IcingaService extends IcingaObject
foreach ($this->mapHostsToZones($hostnames) as $zone => $names) { foreach ($this->mapHostsToZones($hostnames) as $zone => $names) {
$this->set('host_id', $names); $this->set('host_id', $names);
$blacklisted = $this->getBlacklistedHostnames();
$zoneNames = array_diff($names, $blacklisted);
if (empty($zoneNames)) {
continue;
}
$config->configFile('director/' . $zone . '/service_apply', '.cfg') $config->configFile('director/' . $zone . '/service_apply', '.cfg')
->addLegacyObject($this); ->addLegacyObject($this);
} }

View File

@ -230,11 +230,25 @@ class IcingaServiceSet extends IcingaObject
$hostnames = array($this->getRelated('host')->object_name); $hostnames = array($this->getRelated('host')->object_name);
} }
$blacklists = [];
foreach ($this->mapHostsToZones($hostnames) as $zone => $names) { foreach ($this->mapHostsToZones($hostnames) as $zone => $names) {
$file = $config->configFile('director/' . $zone . '/servicesets', '.cfg'); $file = $config->configFile('director/' . $zone . '/servicesets', '.cfg');
$file->addContent($this->getConfigHeaderComment($config)); $file->addContent($this->getConfigHeaderComment($config));
foreach ($this->getServiceObjects() as $service) { foreach ($this->getServiceObjects() as $service) {
$object_name = $service->object_name;
if (! array_key_exists($object_name, $blacklists)) {
$blacklists[$object_name] = $service->getBlacklistedHostnames();
}
// check if all hosts in the zone ignore this service
$zoneNames = array_diff($names, $blacklists[$object_name]);
if (empty($zoneNames)) {
continue;
}
$service->set('object_type', 'object'); $service->set('object_type', 'object');
$service->set('host_id', $names); $service->set('host_id', $names);