Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Hosts parentship master #5868

Merged
merged 2 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/Centreon/Object/Relation/Host/Parent/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ class Centreon_Object_Relation_Host_Parent_Host extends Centreon_Object_Relation
protected $relationTable = "host_hostparent_relation";
protected $firstKey = "host_parent_hp_id";
protected $secondKey = "host_host_id";
/**
* Constructor
*
* @return void
*/
public function __construct(\Pimple\Container $dependencyInjector)
{
parent::__construct($dependencyInjector);
$this->firstObject = new Centreon_Object_Host($dependencyInjector);
$this->secondObject = new Centreon_Object_Host($dependencyInjector);
}
}
13 changes: 13 additions & 0 deletions lib/Centreon/Object/Relation/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ protected function getResult($sql, $params = array())
return $result;
}

/**
* Get relation Ids
*
* @return array
* @throws Exception
*/
public function getRelations()
{
$sql = 'SELECT ' . $this->firstKey . ',' . $this->secondKey . ' ' .
'FROM ' . $this->relationTable;
return $this->getResult($sql);
}

/**
* Get Merged Parameters from seperate tables
*
Expand Down
67 changes: 67 additions & 0 deletions www/class/centreon-clapi/centreonHost.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class CentreonHost extends CentreonObject
*/
protected $timezoneObject;

protected $register;

public static $aDepends = array(
'CMD',
'TP',
Expand Down Expand Up @@ -890,6 +892,55 @@ public function __call($name, $arg)
}
}

/**
* @param $elements
* @return array
*/
protected function getHostListByParent(&$elements)
{
$hostParent = new \Centreon_Object_Relation_Host_Parent_Host($this->dependencyInjector);
$parentShip = array();
$relations = $hostParent->getRelations();
foreach ($relations as $relation) {
$firstKey = $relation[$hostParent->getFirstKey()];
$secondKey = $relation[$hostParent->getSecondKey()];
$parentShip[$secondKey][] = $firstKey;
}
$hosts = array();
foreach ($elements as $element) {
$hosts[] = $element['host_id'];
}
$sortedHosts = array();
while ($hostId = array_pop($hosts)) {
if (!in_array($hostId, array_keys($parentShip))) {
$sortedHosts[] = $hostId;
} else {
$parents = $parentShip[$hostId];
$parentExported = true;
foreach ($parents as $parentId) {
if(!in_array($parentId, $sortedHosts)){
$parentExported = false;
break;
}
}
if ($parentExported) {
$sortedHosts[] = $hostId;
} else {
array_unshift($hosts, $hostId);
}
}
}
$elementsIndexedById = array();
foreach ($elements as $element) {
$elementsIndexedById[$element['host_id']] = $element;
}
$elements = array();
foreach ($sortedHosts as $hostId) {
$elements[$hostId] = $elementsIndexedById[$hostId];
}
return $parentShip;
}

/**
* Export
*
Expand All @@ -904,6 +955,8 @@ public function export($filters = null)
$tpObj = new \Centreon_Object_Timeperiod($this->dependencyInjector);
$macroObj = new \Centreon_Object_Host_Macro_Custom($this->dependencyInjector);
$instanceRel = new \Centreon_Object_Relation_Instance_Host($this->dependencyInjector);
$parentShip = array();

if ($this->register) {
$instElements = $instanceRel->getMergedParameters(
array("name"),
Expand Down Expand Up @@ -966,6 +1019,20 @@ public function export($filters = null)
. $value . "\n";
}
}

// Set parentship
if ($this->register == 1) {
$parentShip = $this->getHostListByParent($elements);
}
if (isset($parentShip[$element[$this->object->getPrimaryKey()]])) {
foreach ($parentShip[$element[$this->object->getPrimaryKey()]] as $parentId) {
echo $this->action . $this->delim
. "addparent" . $this->delim
. $element[$this->object->getUniqueLabelField()] . $this->delim
. $elements[$parentId][$this->object->getUniqueLabelField()] . "\n";
}
}

$params = $extendedObj->getParameters(
$element[$this->object->getPrimaryKey()],
array(
Expand Down