WIP Smart services

This commit is contained in:
fbsanchez 2020-06-26 15:48:58 +02:00
parent a8671078f1
commit 032faf8c0c
7 changed files with 232 additions and 22 deletions

View File

@ -27,6 +27,8 @@
*/
// Begin.
require 'vendor/autoload.php';
define('AJAX', true);
if (!defined('__PAN_XHPROF__')) {

View File

@ -259,15 +259,24 @@ define('SERVICE_STATUS_NORMAL', 0);
define('SERVICE_STATUS_CRITICAL', 1);
define('SERVICE_STATUS_WARNING', 2);
define('SERVICE_STATUS_ALERT', 4);
// Default weights.
// Default service weights.
define('SERVICE_WEIGHT_CRITICAL', 1);
define('SERVICE_WEIGHT_WARNING', 0.5);
define('SERVICE_SMART_WEIGHT_CRITICAL', 50);
define('SERVICE_SMART_WEIGHT_WARNING', 30);
// Default service element weights.
define('SERVICE_ELEMENT_WEIGHT_CRITICAL', 1);
define('SERVICE_ELEMENT_WEIGHT_WARNING', 0.5);
define('SERVICE_ELEMENT_WEIGHT_OK', 0);
define('SERVICE_ELEMENT_WEIGHT_UNKNOWN', 0);
define('SERVICE_ELEMENT_SMART_CRITICAL', 100);
define('SERVICE_ELEMENT_SMART_WARNING', 50);
// Service element types.
define('SERVICE_ELEMENT_AGENT', 'agent');
define('SERVICE_ELEMENT_MODULE', 'module');
define('SERVICE_ELEMENT_SERVICE', 'service');
define('SERVICE_ELEMENT_DYNAMIC', 'dynamic');
// Modes.
define('SERVICE_MODE_MANUAL', 0);
define('SERVICE_MODE_SMART', 1);

View File

@ -43,7 +43,7 @@ var TreeController = {
var $group = $("<ul></ul>");
// First group
if (typeof rootGroup != "undefinded" && rootGroup == true) {
if (typeof rootGroup != "undefined" && rootGroup == true) {
$group
.addClass("tree-root")
.hide()

View File

@ -54,19 +54,38 @@ class Agent extends Entity
/**
* Builds a PandoraFMS\Agent object from a agent id.
*
* @param integer $id_agent Agent Id.
* @param boolean $load_modules Load all modules of this agent. Be careful.
* @param integer $id_agent Agent Id.
* @param boolean $load_modules Load all modules of this agent.
* @param integer|null $id_node Metaconsole only. ID node.
*/
public function __construct(?int $id_agent=null, ?bool $load_modules=false)
{
public function __construct(
?int $id_agent=null,
?bool $load_modules=false,
?int $id_node=null
) {
$table = 'tagente';
$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
&& $id_agent > 0
) {
parent::__construct('tagente', ['id_agente' => $id_agent]);
parent::__construct($table, $filter);
if ($load_modules === true) {
$rows = \db_get_all_rows_filter(
'tagente_modulo',
['id_agente' => $id_agent]
$filter
);
if (is_array($rows) === true) {
@ -79,7 +98,7 @@ class Agent extends Entity
}
} else {
// Create empty skel.
parent::__construct('tagente');
parent::__construct($table);
// New agent has no modules.
$this->modulesLoaded = true;

View File

@ -30,6 +30,8 @@
// Begin.
namespace PandoraFMS;
use PandoraFMS\Agent;
/**
* PandoraFMS agent entity.
*/
@ -43,6 +45,20 @@ class Module extends Entity
*/
private $status;
/**
* Agent where module is stored.
*
* @var PandoraFMS\Agent
*/
private $linkedAgent;
/**
* Metaconsole setup id.
*
* @var integer
*/
private $idNode;
/**
* Search a module in db.
@ -131,10 +147,24 @@ class Module extends Entity
/**
* Builds a PandoraFMS\Module object from given id.
*
* @param integer $id_agent_module Module id.
* @param integer|null $id_agent_module Module id.
* @param boolean $link_agent Link agent object.
* @param integer|null $id_node Metaconsole only. ID node.
*
* @throws \Exception On error.
*/
public function __construct(?int $id_agent_module=null)
{
public function __construct(
?int $id_agent_module=null,
bool $link_agent=false,
?int $id_node=null
) {
if ($id_node !== null) {
$this->idNode = $id_node;
enterprise_include_once('include/functions_metaconsole.php');
\metaconsole_connect(null, $this->idNode);
}
if (is_numeric($id_agent_module) === true
&& $id_agent_module > 0
) {
@ -142,17 +172,83 @@ class Module extends Entity
'tagente_modulo',
['id_agente_modulo' => $id_agent_module]
);
if ($this->nombre() === 'delete_pending') {
return null;
}
if ($link_agent === true) {
try {
$this->linkedAgent = new Agent($this->id_agente());
} catch (\Exception $e) {
if ($this->idNode !== null) {
\metaconsole_restore_db();
}
// Unexistent agent.
throw new \Exception(
__METHOD__.__(
' error: Module has no agent assigned.'
)
);
}
}
} else {
// Create empty skel.
parent::__construct('tagente_modulo');
}
if ($this->nombre() === 'delete_pending') {
return null;
try {
// Customize certain fields.
$this->status = new ModuleStatus($this->fields['id_agente_modulo']);
} catch (\Exception $e) {
$this->status = new Modulestatus();
if ($this->idNode !== null) {
\metaconsole_restore_db();
}
}
}
/**
* Return agent object where module is defined.
*
* @return PandoraFMS\Agent Where module is defined.
*/
public function agent()
{
if ($this->linkedAgent === null) {
try {
$this->linkedAgent = new Agent($this->id_agente());
} catch (\Exception $e) {
// Unexistent agent.
return null;
}
}
// Customize certain fields.
$this->status = new ModuleStatus($this->fields['id_agente_modulo']);
return $this->linkedAgent;
}
/**
* Return last value reported by the module.
*
* @return mixed Data depending on module type.
*/
public function lastValue()
{
return $this->status->datos();
}
/**
* Return last status reported by the module.
*
* @return mixed Data depending on module type.
*/
public function lastStatus()
{
return $this->status->estado();
}
@ -288,6 +384,10 @@ class Module extends Entity
if ($this->fields['id_agente_modulo'] > 0) {
// Update.
$updates = $this->fields;
if ($this->idNode !== null) {
enterprise_include_once('include/functions_metaconsole.php');
\metaconsole_connect(null, $this->idNode);
}
$rs = \db_process_sql_update(
'tagente_modulo',
@ -295,10 +395,16 @@ class Module extends Entity
['id_agente_modulo' => $this->fields['id_agente_modulo']]
);
global $config;
$error = $config['dbconnection']->error;
if ($this->idNode !== null) {
\metaconsole_restore_db();
}
if ($rs === false) {
global $config;
throw new \Exception(
__METHOD__.' error: '.$config['dbconnection']->error
__METHOD__.' error: '.$error
);
}
} else {
@ -312,16 +418,27 @@ class Module extends Entity
}
}
if ($this->idNode !== null) {
enterprise_include_once('include/functions_metaconsole.php');
\metaconsole_connect(null, $this->idNode);
}
$rs = \modules_create_agent_module(
$this->fields['id_agente'],
$updates['nombre'],
$updates
);
global $config;
$error = $config['dbconnection']->error;
if ($this->idNode !== null) {
\metaconsole_restore_db();
}
if ($rs === false) {
global $config;
throw new \Exception(
__METHOD__.' error: '.$config['dbconnection']->error
__METHOD__.' error: '.$error
);
}
@ -339,10 +456,19 @@ class Module extends Entity
*/
public function delete()
{
if ($this->idNode !== null) {
enterprise_include_once('include/functions_metaconsole.php');
\metaconsole_connect(null, $this->idNode);
}
\modules_delete_agent_module(
$this->id_agente_modulo()
);
if ($this->idNode !== null) {
\metaconsole_restore_db();
}
unset($this->fields);
unset($this->status);
}
@ -393,4 +519,58 @@ class Module extends Entity
}
/**
* Calculates CPS value and updates its content.
*
* @return integer CPS value set.
*/
public function calculateCPS()
{
$cps = 0;
if (class_exists('\PandoraFMS\Enterprise\Service') === false) {
return 0;
}
// Check if is child of services in local console.
$service_parents = \db_get_all_rows_filter(
'tservice_element',
[ 'id_agente_modulo' => $this->id_agente_modulo() ],
'id_service'
);
if (is_array($service_parents) === true) {
foreach ($service_parents as $ref) {
$service = new \PandoraFMS\Enterprise\Service(
$ref['id_service']
);
if (((bool) $service->cascade_protection()) === true) {
$cps += $service->cps();
}
}
}
// Check if is child of services in metaconsole.
if (is_metaconsole() === false) {
global $config;
$nodo_connect = \metaconsole_load_external_db(
[
'dbhost' => $config['replication_dbhost'],
'dbuser' => $config['replication_dbuser'],
'dbpass' => \io_output_password(
$config['replication_dbpass']
),
'dbname' => $config['replication_dbname'],
]
);
\metaconsole_restore_db();
}
return $cps;
}
}

View File

@ -31,6 +31,8 @@ if (!defined('__PAN_XHPROF__')) {
define('__PAN_XHPROF__', 0);
}
require 'vendor/autoload.php';
if (__PAN_XHPROF__ === 1) {
if (function_exists('tideways_xhprof_enable')) {
tideways_xhprof_enable();

View File

@ -15,8 +15,6 @@ if (! isset($config['id_user'])) {
return;
}
require 'vendor/autoload.php';
use PandoraFMS\Dashboard\Manager;
require_once 'include/functions_menu.php';