ObjectCommand: --all-services

fixes #2571
This commit is contained in:
Thomas Gelf 2022-07-21 07:49:12 +02:00
parent 431d0cfe75
commit fca804e74e
2 changed files with 53 additions and 14 deletions

View File

@ -47,7 +47,7 @@ class ObjectCommand extends Command
* --no-defaults Per default JSON output ships null or default
* values. This flag skips those properties
* --with-services For hosts only, also shows attached services
* --resolve-services For hosts only, show applied and inherited services
* --all-services For hosts only, show applied and inherited services
* too
*/
public function showAction()
@ -57,16 +57,16 @@ class ObjectCommand extends Command
$exporter = new Exporter($db);
$resolve = (bool) $this->params->shift('resolved');
$withServices = (bool) $this->params->get('with-services');
$resolveServices = (bool) $this->params->get('resolve-services');
$allServices = (bool) $this->params->get('all-services');
if ($withServices) {
if (!$object instanceof IcingaHost) {
$this->fail('--with-services is available for Hosts only');
}
$exporter->enableHostServices();
}
if ($resolveServices) {
if ($allServices) {
if (!$object instanceof IcingaHost) {
$this->fail('--resolve-services is available for Hosts only');
$this->fail('--all-services is available for Hosts only');
}
$exporter->resolveHostServices();
}

View File

@ -65,7 +65,7 @@ class Exporter
protected $fieldReferenceLoader;
protected $exportHostServices = false;
protected $resolveHostServices = false;
protected $fetchAllHostServices = false;
protected $showDefaults = false;
protected $showIds = false;
protected $resolveObjects = false;
@ -115,7 +115,7 @@ class Exporter
public function resolveHostServices($enable = true)
{
$this->resolveHostServices = $enable;
$this->fetchAllHostServices = $enable;
return $this;
}
@ -208,8 +208,8 @@ class Exporter
{
$table = (new ObjectsTableService($this->connection))->setHost($host);
$services = $this->fetchServicesForTable($table);
if ($this->resolveHostServices) {
foreach ($this->fetchRelatedServicesForHost($host) as $service) {
if ($this->fetchAllHostServices) {
foreach ($this->fetchAllServicesForHost($host) as $service) {
$services[] = $service;
}
}
@ -240,7 +240,7 @@ class Exporter
return $services;
}
protected function fetchRelatedServicesForHost(IcingaHost $host)
protected function fetchAllServicesForHost(IcingaHost $host)
{
$services = [];
/** @var IcingaHost[] $parents */
@ -253,12 +253,16 @@ class Exporter
$services[] = $service;
}
}
/*
$this->addHostServiceSetTables($host);
foreach ($parents as $parent) {
$this->addHostServiceSetTables($parent, $host);
foreach ($this->getHostServiceSetTables($host) as $service) {
$services[] = $service;
}
*/
foreach ($parents as $parent) {
foreach ($this->getHostServiceSetTables($parent, $host) as $service) {
$services[] = $service;
}
}
$appliedSets = AppliedServiceSetLoader::fetchForHost($host);
foreach ($appliedSets as $set) {
$table = IcingaServiceSetServiceTable::load($set)
@ -277,6 +281,41 @@ class Exporter
return $services;
}
/**
* Duplicates Logic in HostController
*
* @param IcingaHost $host
* @param IcingaHost|null $affectedHost
* @return IcingaServiceSetServiceTable[]
*/
protected function getHostServiceSetTables(IcingaHost $host, IcingaHost $affectedHost = null)
{
$tables = [];
$db = $this->connection;
if ($affectedHost === null) {
$affectedHost = $host;
}
if ($host->get('id') === null) {
return $tables;
}
$query = $db->getDbAdapter()->select()
->from(['ss' => 'icinga_service_set'], 'ss.*')
->join(['hsi' => 'icinga_service_set_inheritance'], 'hsi.parent_service_set_id = ss.id', [])
->join(['hs' => 'icinga_service_set'], 'hs.id = hsi.service_set_id', [])
->where('hs.host_id = ?', $host->get('id'));
$sets = IcingaServiceSet::loadAll($db, $query, 'object_name');
/** @var IcingaServiceSet $set*/
foreach ($sets as $name => $set) {
$tables[] = IcingaServiceSetServiceTable::load($set)
->setHost($host)
->setAffectedHost($affectedHost);
}
return $tables;
}
protected function loadTemplateName($table, $id)
{
$db = $this->db;