WIP services

This commit is contained in:
fbsanchez 2020-06-30 16:57:08 +02:00
parent c07daa3199
commit 1d621bd3d5
2 changed files with 140 additions and 109 deletions

View File

@ -56,27 +56,13 @@ class Agent extends Entity
* *
* @param integer $id_agent Agent Id. * @param integer $id_agent Agent Id.
* @param boolean $load_modules Load all modules of this agent. * @param boolean $load_modules Load all modules of this agent.
* @param integer|null $id_node Metaconsole only. ID node.
*/ */
public function __construct( public function __construct(
?int $id_agent=null, ?int $id_agent=null,
?bool $load_modules=false, ?bool $load_modules=false
?int $id_node=null
) { ) {
$table = 'tagente'; $table = 'tagente';
$filter = ['id_agente' => $id_agent]; $filter = ['id_agente' => $id_agent];
if (is_metaconsole() === true
&& $id_node !== null
) {
$table = 'tmetaconsole_agent';
$filter = [
'id_agente' => $id_agent,
'id_tmetaconsole_setup' => (int) $id_node,
];
// Cannot load modules from metaconsole.
$load_modules = false;
}
if (is_numeric($id_agent) === true if (is_numeric($id_agent) === true
&& $id_agent > 0 && $id_agent > 0
@ -106,6 +92,7 @@ class Agent extends Entity
// Customize certain fields. // Customize certain fields.
$this->fields['group'] = new Group($this->fields['id_grupo']); $this->fields['group'] = new Group($this->fields['id_grupo']);
} }
@ -214,9 +201,12 @@ class Agent extends Entity
/** /**
* Calculates cascade protection service value for this service. * Calculates cascade protection service value for this service.
* *
* @param integer|null $id_node Meta searching node will use this field.
*
* @return integer CPS value. * @return integer CPS value.
* @throws \Exception On error.
*/ */
public function calculateCPS() public function calculateCPS(?int $id_node=null)
{ {
if ($this->cps() < 0) { if ($this->cps() < 0) {
return $this->cps(); return $this->cps();
@ -225,17 +215,23 @@ class Agent extends Entity
// 1. check parents. // 1. check parents.
$direct_parents = db_get_all_rows_sql( $direct_parents = db_get_all_rows_sql(
sprintf( sprintf(
'SELECT id_service, cps, cascade_protection 'SELECT id_service, cps, cascade_protection, name
FROM `tservice_element` te FROM `tservice_element` te
INNER JOIN `tservice` t ON te.id_service = t.id INNER JOIN `tservice` t ON te.id_service = t.id
WHERE te.id_agent = %d', WHERE te.id_agent = %d',
$this->id_agente() $this->id_agente()
) ),
false,
false
); );
// Here could happen 2 things.
// 1. Metaconsole service is using this method impersonating node DB.
// 2. Node service is trying to find parents into metaconsole.
if (is_metaconsole() === false if (is_metaconsole() === false
&& has_metaconsole() === true && has_metaconsole() === true
) { ) {
// Node searching metaconsole.
$mc_parents = []; $mc_parents = [];
global $config; global $config;
$mc_db_conn = \enterprise_hook( $mc_db_conn = \enterprise_hook(
@ -257,17 +253,52 @@ class Agent extends Entity
sprintf( sprintf(
'SELECT id_service, 'SELECT id_service,
cps, cps,
cascade_protection cascade_protection,
name
FROM `tservice_element` te FROM `tservice_element` te
INNER JOIN `tservice` t ON te.id_service = t.id INNER JOIN `tservice` t ON te.id_service = t.id
WHERE te.id_agent = %d', WHERE te.id_agent = %d',
$this->id_agente() $this->id_agente()
) ),
false,
false
); );
} }
// Restore the default connection. // Restore the default connection.
\enterprise_hook('metaconsole_restore_db'); \enterprise_hook('metaconsole_restore_db');
} else if ($id_node > 0) {
// Impersonated node.
\enterprise_hook('metaconsole_restore_db');
$mc_parents = db_get_all_rows_sql(
sprintf(
'SELECT id_service,
cps,
cascade_protection,
name
FROM `tservice_element` te
INNER JOIN `tservice` t ON te.id_service = t.id
WHERE te.id_agent = %d',
$this->id_agente()
),
false,
false
);
// Restore impersonation.
\enterprise_include_once('include/functions_metaconsole.php');
$r = \enterprise_hook(
'metaconsole_connect',
[
null,
$id_node,
]
);
if ($r !== NOERR) {
throw new \Exception(__('Cannot connect to node %d', $r));
}
} }
$cps = 0; $cps = 0;
@ -333,6 +364,23 @@ class Agent extends Entity
} }
/**
* Alias for field 'nombre'.
*
* @param string|null $name Name or empty if get operation.
*
* @return string|null Name or empty if set operation.
*/
public function name(?string $name=null)
{
if ($name === null) {
return $this->nombre();
}
$this->nombre($name);
}
/** /**
* Search for modules into this agent. * Search for modules into this agent.
* *

View File

@ -52,13 +52,6 @@ class Module extends Entity
*/ */
private $linkedAgent; private $linkedAgent;
/**
* Metaconsole setup id.
*
* @var integer
*/
private $idNode;
/** /**
* Search a module in db. * Search a module in db.
@ -149,28 +142,13 @@ 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 $id_node Metaconsole only. ID node.
* *
* @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 $id_node=null
) { ) {
if ($id_node !== null) {
$this->idNode = $id_node;
enterprise_include_once('include/functions_metaconsole.php');
\enterprise_hook(
'metaconsole_connect',
[
null,
$this->idNode,
]
);
}
if (is_numeric($id_agent_module) === true if (is_numeric($id_agent_module) === true
&& $id_agent_module > 0 && $id_agent_module > 0
) { ) {
@ -187,10 +165,6 @@ class Module extends Entity
try { try {
$this->linkedAgent = new Agent($this->id_agente()); $this->linkedAgent = new Agent($this->id_agente());
} catch (\Exception $e) { } catch (\Exception $e) {
if ($this->idNode !== null) {
\enterprise_hook('metaconsole_restore_db');
}
// Unexistent agent. // Unexistent agent.
throw new \Exception( throw new \Exception(
__METHOD__.__( __METHOD__.__(
@ -211,9 +185,6 @@ class Module extends Entity
$this->status = new Modulestatus(); $this->status = new Modulestatus();
} }
if ($this->idNode !== null) {
\enterprise_hook('metaconsole_restore_db');
}
} }
@ -270,6 +241,23 @@ class Module extends Entity
} }
/**
* Alias for field 'nombre'.
*
* @param string|null $name Name or empty if get operation.
*
* @return string|null Name or empty if set operation.
*/
public function name(?string $name=null)
{
if ($name === null) {
return $this->nombre();
}
$this->nombre($name);
}
/** /**
* Retrieve all alert templates (ids) assigned to current module. * Retrieve all alert templates (ids) assigned to current module.
* *
@ -391,16 +379,6 @@ class Module extends Entity
if ($this->fields['id_agente_modulo'] > 0) { if ($this->fields['id_agente_modulo'] > 0) {
// Update. // Update.
$updates = $this->fields; $updates = $this->fields;
if ($this->idNode !== null) {
enterprise_include_once('include/functions_metaconsole.php');
\enterprise_hook(
'metaconsole_connect',
[
null,
$this->idNode,
]
);
}
$rs = \db_process_sql_update( $rs = \db_process_sql_update(
'tagente_modulo', 'tagente_modulo',
@ -408,16 +386,10 @@ class Module extends Entity
['id_agente_modulo' => $this->fields['id_agente_modulo']] ['id_agente_modulo' => $this->fields['id_agente_modulo']]
); );
global $config;
$error = $config['dbconnection']->error;
if ($this->idNode !== null) {
\enterprise_hook('metaconsole_restore_db');
}
if ($rs === false) { if ($rs === false) {
global $config;
throw new \Exception( throw new \Exception(
__METHOD__.' error: '.$error __METHOD__.' error: '.$config['dbconnection']->error
); );
} }
} else { } else {
@ -431,33 +403,16 @@ class Module extends Entity
} }
} }
if ($this->idNode !== null) {
enterprise_include_once('include/functions_metaconsole.php');
\enterprise_hook(
'metaconsole_connect',
[
null,
$this->idNode,
]
);
}
$rs = \modules_create_agent_module( $rs = \modules_create_agent_module(
$this->fields['id_agente'], $this->fields['id_agente'],
$updates['nombre'], $updates['nombre'],
$updates $updates
); );
global $config;
$error = $config['dbconnection']->error;
if ($this->idNode !== null) {
\enterprise_hook('metaconsole_restore_db');
}
if ($rs === false) { if ($rs === false) {
global $config;
throw new \Exception( throw new \Exception(
__METHOD__.' error: '.$error __METHOD__.' error: '.$config['dbconnection']->error
); );
} }
@ -475,25 +430,10 @@ class Module extends Entity
*/ */
public function delete() public function delete()
{ {
if ($this->idNode !== null) {
enterprise_include_once('include/functions_metaconsole.php');
\enterprise_hook(
'metaconsole_connect',
[
null,
$this->idNode,
]
);
}
\modules_delete_agent_module( \modules_delete_agent_module(
$this->id_agente_modulo() $this->id_agente_modulo()
); );
if ($this->idNode !== null) {
\enterprise_hook('metaconsole_restore_db');
}
unset($this->fields); unset($this->fields);
unset($this->status); unset($this->status);
} }
@ -547,9 +487,12 @@ class Module extends Entity
/** /**
* Calculates cascade protection service value for this service. * Calculates cascade protection service value for this service.
* *
* @param integer|null $id_node Meta searching node will use this field.
*
* @return integer CPS value. * @return integer CPS value.
* @throws \Exception On error.
*/ */
public function calculateCPS() public function calculateCPS(?int $id_node=null)
{ {
if ($this->cps() < 0) { if ($this->cps() < 0) {
return $this->cps(); return $this->cps();
@ -558,7 +501,7 @@ class Module extends Entity
// 1. check parents. // 1. check parents.
$direct_parents = db_get_all_rows_sql( $direct_parents = db_get_all_rows_sql(
sprintf( sprintf(
'SELECT id_service, cps, cascade_protection 'SELECT id_service, cps, cascade_protection, name
FROM `tservice_element` te FROM `tservice_element` te
INNER JOIN `tservice` t ON te.id_service = t.id INNER JOIN `tservice` t ON te.id_service = t.id
WHERE te.id_agente_modulo = %d', WHERE te.id_agente_modulo = %d',
@ -566,9 +509,14 @@ class Module extends Entity
) )
); );
if (is_metaconsole() === false // Here could happen 2 things.
// 1. Metaconsole service is using this method impersonating node DB.
// 2. Node service is trying to find parents into metaconsole.
if (empty($id_node) === true
&& is_metaconsole() === false
&& has_metaconsole() === true && has_metaconsole() === true
) { ) {
// Node searching metaconsole.
$mc_parents = []; $mc_parents = [];
global $config; global $config;
$mc_db_conn = \enterprise_hook( $mc_db_conn = \enterprise_hook(
@ -590,17 +538,52 @@ class Module extends Entity
sprintf( sprintf(
'SELECT id_service, 'SELECT id_service,
cps, cps,
cascade_protection cascade_protection,
name
FROM `tservice_element` te FROM `tservice_element` te
INNER JOIN `tservice` t ON te.id_service = t.id INNER JOIN `tservice` t ON te.id_service = t.id
WHERE te.id_agente_modulo = %d', WHERE te.id_agente_modulo = %d',
$this->id_agente_modulo() $this->id_agente_modulo()
) ),
false,
false
); );
} }
// Restore the default connection. // Restore the default connection.
\enterprise_hook('metaconsole_restore_db'); \enterprise_hook('metaconsole_restore_db');
} else if ($id_node > 0) {
// Impersonated node.
\enterprise_hook('metaconsole_restore_db');
$mc_parents = db_get_all_rows_sql(
sprintf(
'SELECT id_service,
cps,
cascade_protection,
name
FROM `tservice_element` te
INNER JOIN `tservice` t ON te.id_service = t.id
WHERE te.id_agente_modulo = %d',
$this->id_agente_modulo()
),
false,
false
);
// Restore impersonation.
\enterprise_include_once('include/functions_metaconsole.php');
$r = \enterprise_hook(
'metaconsole_connect',
[
null,
$id_node,
]
);
if ($r !== NOERR) {
throw new \Exception(__('Cannot connect to node %d', $r));
}
} }
$cps = 0; $cps = 0;