Some improvements in multi-server communications (Entity)

This commit is contained in:
fbsanchez 2021-02-03 14:21:59 +01:00
parent 93908626a0
commit 600b914715
3 changed files with 126 additions and 14 deletions

View File

@ -57,6 +57,20 @@ abstract class Entity
*/ */
private $enterprise; private $enterprise;
/**
* MC Node id.
*
* @var integer|null
*/
protected $nodeId = null;
/**
* Connected to external node.
*
* @var boolean
*/
private $connected = false;
/** /**
* Instances a new object using array definition. * Instances a new object using array definition.
@ -193,6 +207,53 @@ abstract class Entity
} }
/**
* Connects to current nodeId target.
* If no nodeId is defined, then returns without doing anything.
*
* @return void
* @throws \Exception On error.
*/
public function connectNode()
{
if ($this->nodeId === null) {
return;
}
\enterprise_include_once('include/functions_metaconsole.php');
$r = \enterprise_hook(
'metaconsole_connect',
[
null,
$this->nodeId,
]
);
if ($r !== NOERR) {
throw new \Exception(
__('Cannot connect to node %d', $this->nodeId)
);
}
$this->connected = true;
}
/**
* Restore connection after connectNode.
*
* @return void
*/
public function restoreConnection()
{
if ($this->connected === true) {
\enterprise_include_once('include/functions_metaconsole.php');
\enterprise_hook('metaconsole_restore_db');
}
}
/** /**
* Saves current object definition to database. * Saves current object definition to database.
* *

View File

@ -74,14 +74,29 @@ class Event extends Entity
parent::__construct($table); parent::__construct($table);
} }
try {
if ((bool) \is_metaconsole() === true
&& $this->server_id() !== null
) {
$this->nodeId = $this->server_id();
}
$this->connectNode();
if ($this->id_agente() !== null) { if ($this->id_agente() !== null) {
$this->linkedAgent = new Agent($this->id_agente()); $this->linkedAgent = new Agent((int) $this->id_agente());
} }
if ($this->id_agentmodule() !== null) { if ($this->id_agentmodule() !== null) {
$this->linkedModule = new Module($this->id_agentmodule()); $this->linkedModule = new Module((int) $this->id_agentmodule());
}
} catch (\Exception $e) {
// Do not link items if failed to find them.
$this->restoreConnection();
} }
// Restore if needed.
$this->restoreConnection();
} }

View File

@ -91,8 +91,10 @@ class Module extends Entity
* @return PandoraFMS\Module found or null if not found. * @return PandoraFMS\Module found or null if not found.
* @throws \Exception On error. * @throws \Exception On error.
*/ */
public static function search(array $params, ?int $limit=0) public static function search(
{ array $params,
?int $limit=0
) {
if (empty($params) === true) { if (empty($params) === true) {
return null; return null;
} }
@ -192,21 +194,40 @@ class Module extends Entity
* *
* @param integer|null $id_agent_module Module id. * @param integer|null $id_agent_module Module id.
* @param boolean $link_agent Link agent object. * @param boolean $link_agent Link agent object.
* @param integer|null $nodeId Target node (if metaconsole
* environment).
* *
* @throws \Exception On error. * @throws \Exception On error.
*/ */
public function __construct( public function __construct(
?int $id_agent_module=null, ?int $id_agent_module=null,
bool $link_agent=false bool $link_agent=false,
?int $nodeId=null
) { ) {
if (is_numeric($id_agent_module) === true if (is_numeric($id_agent_module) === true
&& $id_agent_module > 0 && $id_agent_module > 0
) { ) {
if ($nodeId !== null) {
$this->nodeId = $nodeId;
}
try {
// Connect to node if needed.
$this->connectNode();
parent::__construct( parent::__construct(
'tagente_modulo', 'tagente_modulo',
['id_agente_modulo' => $id_agent_module] ['id_agente_modulo' => $id_agent_module]
); );
// Restore.
$this->restoreConnection();
} catch (\Exception $e) {
$this->restoreConnection();
// Forward exception.
throw $e;
}
if ($this->nombre() === 'delete_pending') { if ($this->nombre() === 'delete_pending') {
return null; return null;
} }
@ -229,14 +250,22 @@ class Module extends Entity
} }
try { try {
// Connect to node if needed.
$this->connectNode();
// Customize certain fields. // Customize certain fields.
$this->status = new ModuleStatus($this->fields['id_agente_modulo']); $this->status = new ModuleStatus($this->fields['id_agente_modulo']);
// Restore.
$this->restoreConnection();
} catch (\Exception $e) { } catch (\Exception $e) {
// Restore.
$this->restoreConnection();
$this->status = new Modulestatus(); $this->status = new Modulestatus();
} }
// Customize certain fields. // Customize certain fields.
$this->status = new ModuleStatus($this->fields['id_agente_modulo']);
$this->moduleType = new ModuleType($this->id_tipo_modulo()); $this->moduleType = new ModuleType($this->id_tipo_modulo());
// Include some enterprise dependencies. // Include some enterprise dependencies.
@ -267,8 +296,15 @@ class Module extends Entity
{ {
if ($this->linkedAgent === null) { if ($this->linkedAgent === null) {
try { try {
// Connect to node if needed.
$this->connectNode();
$this->linkedAgent = new Agent($this->id_agente()); $this->linkedAgent = new Agent($this->id_agente());
// Connect to node if needed.
$this->restoreConnection();
} catch (\Exception $e) { } catch (\Exception $e) {
// Connect to node if needed.
$this->restoreConnection();
// Unexistent agent. // Unexistent agent.
return null; return null;
} }