2020-05-20 19:04:42 +02:00
|
|
|
<?php
|
|
|
|
// phpcs:disable Squiz.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
|
|
|
|
/**
|
|
|
|
* Module entity class.
|
|
|
|
*
|
|
|
|
* @category Class
|
|
|
|
* @package Pandora FMS
|
|
|
|
* @subpackage OpenSource
|
|
|
|
* @version 1.0.0
|
|
|
|
* @license See below
|
|
|
|
*
|
|
|
|
* ______ ___ _______ _______ ________
|
|
|
|
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
|
|
|
|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
|
|
|
|
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
|
|
|
|
*
|
|
|
|
* ============================================================================
|
|
|
|
* Copyright (c) 2005-2019 Artica Soluciones Tecnologicas
|
|
|
|
* Please see http://pandorafms.org for full contribution list
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation for version 2.
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* ============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Begin.
|
|
|
|
namespace PandoraFMS;
|
|
|
|
|
2020-06-26 15:48:58 +02:00
|
|
|
use PandoraFMS\Agent;
|
2020-06-30 12:16:51 +02:00
|
|
|
use PandoraFMS\ModuleType;
|
2020-06-26 15:48:58 +02:00
|
|
|
|
2020-05-20 19:04:42 +02:00
|
|
|
/**
|
|
|
|
* PandoraFMS agent entity.
|
|
|
|
*/
|
|
|
|
class Module extends Entity
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module status (From tagente_estado).
|
|
|
|
*
|
|
|
|
* @var PandoraFMS\ModuleStatus
|
|
|
|
*/
|
|
|
|
private $status;
|
|
|
|
|
2020-06-26 15:48:58 +02:00
|
|
|
/**
|
|
|
|
* Agent where module is stored.
|
|
|
|
*
|
|
|
|
* @var PandoraFMS\Agent
|
|
|
|
*/
|
|
|
|
private $linkedAgent;
|
|
|
|
|
2020-06-30 12:16:51 +02:00
|
|
|
/**
|
|
|
|
* Module type matching id_tipo_modulo.
|
|
|
|
*
|
|
|
|
* @var PandoraFMS\ModuleType
|
|
|
|
*/
|
|
|
|
private $moduleType;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration data (only local modules).
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $configurationData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration data (before updates) (only local modules).
|
|
|
|
* Compatibility with classic functions.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $configurationDataOld;
|
|
|
|
|
2020-05-20 19:04:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search a module in db.
|
|
|
|
*
|
|
|
|
* @param array $params Search parameters (fields from tagente_modulo).
|
|
|
|
* @param integer $limit Limit results to N rows.
|
|
|
|
*
|
|
|
|
* @return PandoraFMS\Module found or null if not found.
|
|
|
|
* @throws \Exception On error.
|
|
|
|
*/
|
|
|
|
public static function search(array $params, ?int $limit=0)
|
|
|
|
{
|
|
|
|
if (empty($params) === true) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rs = \db_get_all_rows_filter(
|
|
|
|
'tagente_modulo',
|
|
|
|
$params
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($rs === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($rs) === true) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($limit !== 1) {
|
|
|
|
$modules = [];
|
|
|
|
$i = 0;
|
|
|
|
foreach ($rs as $row) {
|
|
|
|
if ($limit > 1 && (++$i) > $limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$modules[] = self::build($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $modules;
|
|
|
|
} else {
|
|
|
|
return self::build($rs[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns current object as array.
|
|
|
|
*
|
|
|
|
* @return array Of fields.
|
|
|
|
*/
|
|
|
|
public function toArray()
|
|
|
|
{
|
|
|
|
return $this->fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a module object from given data. Avoid query duplication.
|
|
|
|
*
|
|
|
|
* @param array $data Module information.
|
|
|
|
*
|
|
|
|
* @return PandoraFMS\Module Object.
|
|
|
|
*/
|
|
|
|
public static function build(array $data=[])
|
|
|
|
{
|
|
|
|
$obj = new Module();
|
|
|
|
|
|
|
|
// Set values.
|
|
|
|
foreach ($data as $k => $v) {
|
|
|
|
$obj->{$k}($v);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($obj->nombre() === 'delete_pending') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Customize certain fields.
|
|
|
|
$obj->status = new ModuleStatus($obj->id_agente_modulo());
|
2020-06-30 12:16:51 +02:00
|
|
|
$obj->moduleType = new ModuleType($obj->id_tipo_modulo());
|
|
|
|
|
|
|
|
// Include some enterprise dependencies.
|
|
|
|
enterprise_include_once('include/functions_config_agents.php');
|
|
|
|
|
|
|
|
// Load configuration data from agent configuration if available.
|
|
|
|
$obj->configuration_data(
|
|
|
|
\enterprise_hook(
|
|
|
|
'config_agents_get_module_from_conf',
|
|
|
|
[
|
|
|
|
$obj->id_agente(),
|
|
|
|
\io_safe_output($obj->nombre()),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Classic compat.
|
|
|
|
$obj->configurationDataOld = $obj->configurationData;
|
2020-05-20 19:04:42 +02:00
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a PandoraFMS\Module object from given id.
|
|
|
|
*
|
2020-06-26 15:48:58 +02:00
|
|
|
* @param integer|null $id_agent_module Module id.
|
|
|
|
* @param boolean $link_agent Link agent object.
|
|
|
|
*
|
|
|
|
* @throws \Exception On error.
|
2020-05-20 19:04:42 +02:00
|
|
|
*/
|
2020-06-26 15:48:58 +02:00
|
|
|
public function __construct(
|
|
|
|
?int $id_agent_module=null,
|
2020-06-30 16:57:08 +02:00
|
|
|
bool $link_agent=false
|
2020-06-26 15:48:58 +02:00
|
|
|
) {
|
2020-05-20 19:04:42 +02:00
|
|
|
if (is_numeric($id_agent_module) === true
|
|
|
|
&& $id_agent_module > 0
|
|
|
|
) {
|
|
|
|
parent::__construct(
|
|
|
|
'tagente_modulo',
|
|
|
|
['id_agente_modulo' => $id_agent_module]
|
|
|
|
);
|
2020-06-26 15:48:58 +02:00
|
|
|
|
|
|
|
if ($this->nombre() === 'delete_pending') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($link_agent === true) {
|
|
|
|
try {
|
|
|
|
$this->linkedAgent = new Agent($this->id_agente());
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Unexistent agent.
|
|
|
|
throw new \Exception(
|
|
|
|
__METHOD__.__(
|
|
|
|
' error: Module has no agent assigned.'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 19:04:42 +02:00
|
|
|
} else {
|
|
|
|
// Create empty skel.
|
|
|
|
parent::__construct('tagente_modulo');
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:48:58 +02:00
|
|
|
try {
|
|
|
|
// Customize certain fields.
|
|
|
|
$this->status = new ModuleStatus($this->fields['id_agente_modulo']);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->status = new Modulestatus();
|
2020-06-29 19:17:40 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 19:04:42 +02:00
|
|
|
// Customize certain fields.
|
|
|
|
$this->status = new ModuleStatus($this->fields['id_agente_modulo']);
|
2020-06-30 12:16:51 +02:00
|
|
|
$this->moduleType = new ModuleType($this->id_tipo_modulo());
|
|
|
|
|
|
|
|
// Include some enterprise dependencies.
|
|
|
|
enterprise_include_once('include/functions_config_agents.php');
|
|
|
|
|
|
|
|
// Load configuration data from agent configuration if available.
|
|
|
|
$this->configuration_data(
|
|
|
|
\enterprise_hook(
|
|
|
|
'config_agents_get_module_from_conf',
|
|
|
|
[
|
|
|
|
$this->id_agente(),
|
|
|
|
\io_safe_output($this->nombre()),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Backup. Classic compat.
|
|
|
|
$this->configurationDataOld = $this->configurationData;
|
2020-06-26 15:48:58 +02:00
|
|
|
}
|
2020-05-20 19:04:42 +02:00
|
|
|
|
2020-06-26 15:48:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->linkedAgent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-30 12:16:51 +02:00
|
|
|
/**
|
|
|
|
* Dynamically call methods in this object.
|
|
|
|
*
|
|
|
|
* @param string $methodName Name of target method or attribute.
|
|
|
|
* @param array $params Arguments for target method.
|
|
|
|
*
|
|
|
|
* @return mixed Return of method.
|
|
|
|
* @throws \Exception On error.
|
|
|
|
*/
|
|
|
|
public function __call(string $methodName, ?array $params=null)
|
|
|
|
{
|
|
|
|
// Prioritize written methods over dynamic ones.
|
|
|
|
if (method_exists($this, $methodName) === true) {
|
|
|
|
return $this->{$methodName}($params);
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:10:24 +02:00
|
|
|
if (is_array($this->fields) === false) {
|
|
|
|
// Element deleted.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:16:51 +02:00
|
|
|
if (array_key_exists($methodName, $this->fields) === true) {
|
|
|
|
if (empty($params) === false) {
|
|
|
|
if ($this->is_local() === true) {
|
|
|
|
$keyName = $methodName;
|
|
|
|
if ($methodName === 'nombre') {
|
|
|
|
$keyName = 'name';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($methodName === 'descripcion') {
|
|
|
|
$keyName = 'description';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($methodName === 'post_process') {
|
|
|
|
$keyName = 'postprocess';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($methodName === 'max_timeout') {
|
|
|
|
$keyName = 'timeout';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($methodName === 'max_retries') {
|
|
|
|
$keyName = 'retries';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array(
|
|
|
|
'module_'.$keyName,
|
|
|
|
[
|
|
|
|
'module_name',
|
|
|
|
'module_description',
|
|
|
|
'module_type',
|
|
|
|
'module_max',
|
|
|
|
'module_min',
|
|
|
|
'module_postprocess',
|
|
|
|
'module_interval',
|
|
|
|
'module_timeout',
|
|
|
|
'module_retries',
|
|
|
|
'module_min_critical',
|
|
|
|
'module_max_critical',
|
|
|
|
'module_min_warning',
|
|
|
|
'module_max_warning',
|
|
|
|
]
|
|
|
|
) === true
|
|
|
|
) {
|
|
|
|
$this->updateConfigurationData(
|
|
|
|
'module_'.$methodName,
|
|
|
|
$params[0]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fields[$methodName] = $params[0];
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return $this->fields[$methodName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \Exception(
|
|
|
|
get_class($this).' error, method '.$methodName.' does not exist'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-26 15:48:58 +02:00
|
|
|
/**
|
|
|
|
* Return last value reported by the module.
|
|
|
|
*
|
|
|
|
* @return mixed Data depending on module type.
|
|
|
|
*/
|
|
|
|
public function lastValue()
|
|
|
|
{
|
|
|
|
return $this->status->datos();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-30 12:16:51 +02:00
|
|
|
/**
|
|
|
|
* Sets or retrieves value of id_tipo_modulo (complex).
|
|
|
|
*
|
|
|
|
* @param integer|null $id_tipo_modulo Id module type.
|
|
|
|
*
|
|
|
|
* @return PandoraFMS\ModuleType corresponding to this module type.
|
|
|
|
* @throws \Exception On error.
|
|
|
|
*/
|
|
|
|
public function moduleType(?int $id_tipo_modulo=null)
|
|
|
|
{
|
|
|
|
if ($id_tipo_modulo === null) {
|
|
|
|
return $this->moduleType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($id_tipo_modulo) === true && $id_tipo_modulo > 0) {
|
|
|
|
$this->moduleType = new ModuleType($id_tipo_modulo);
|
|
|
|
$this->fields['id_tipo_modulo'] = $this->moduleType->id_tipo();
|
|
|
|
} else {
|
|
|
|
throw new \Exception('Invalid id_tipo_modulo '.$id_tipo_modulo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-26 15:48:58 +02:00
|
|
|
/**
|
|
|
|
* Return last status reported by the module.
|
|
|
|
*
|
|
|
|
* @return mixed Data depending on module type.
|
|
|
|
*/
|
|
|
|
public function lastStatus()
|
|
|
|
{
|
|
|
|
return $this->status->estado();
|
2020-05-20 19:04:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-02 14:53:33 +02:00
|
|
|
/**
|
|
|
|
* Retrieves last status in text format.
|
|
|
|
*
|
|
|
|
* @return string Status in text format.
|
|
|
|
*/
|
|
|
|
public function lastStatusText()
|
|
|
|
{
|
|
|
|
switch ($this->lastStatus()) {
|
|
|
|
case AGENT_MODULE_STATUS_CRITICAL_ALERT:
|
|
|
|
case AGENT_MODULE_STATUS_CRITICAL_BAD:
|
|
|
|
return 'critical';
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_WARNING_ALERT:
|
|
|
|
case AGENT_MODULE_STATUS_WARNING:
|
|
|
|
return 'warning';
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_UNKNOWN:
|
|
|
|
return 'unknown';
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_NO_DATA:
|
|
|
|
case AGENT_MODULE_STATUS_NOT_INIT:
|
|
|
|
return 'not_init';
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_NORMAL_ALERT:
|
|
|
|
case AGENT_MODULE_STATUS_NORMAL:
|
|
|
|
default:
|
|
|
|
return 'ok';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return path to image representing last status.
|
|
|
|
*
|
|
|
|
* @return string Relative URL to image.
|
|
|
|
*/
|
|
|
|
public function lastStatusImage()
|
|
|
|
{
|
|
|
|
switch ($this->lastStatus()) {
|
|
|
|
case AGENT_MODULE_STATUS_CRITICAL_ALERT:
|
|
|
|
case AGENT_MODULE_STATUS_CRITICAL_BAD:
|
|
|
|
return STATUS_MODULE_CRITICAL_BALL;
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_WARNING_ALERT:
|
|
|
|
case AGENT_MODULE_STATUS_WARNING:
|
|
|
|
return STATUS_MODULE_WARNING_BALL;
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_UNKNOWN:
|
|
|
|
return STATUS_MODULE_UNKNOWN_BALL;
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_NO_DATA:
|
|
|
|
case AGENT_MODULE_STATUS_NOT_INIT:
|
|
|
|
return STATUS_MODULE_NO_DATA_BALL;
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_NORMAL_ALERT:
|
|
|
|
case AGENT_MODULE_STATUS_NORMAL:
|
|
|
|
default:
|
|
|
|
return STATUS_MODULE_OK_BALL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return translated string representing last status of the module.
|
|
|
|
*
|
|
|
|
* @return string Title.
|
|
|
|
*/
|
|
|
|
public function lastStatusTitle()
|
|
|
|
{
|
|
|
|
switch ($this->lastStatus()) {
|
|
|
|
case AGENT_MODULE_STATUS_CRITICAL_ALERT:
|
|
|
|
case AGENT_MODULE_STATUS_CRITICAL_BAD:
|
|
|
|
return __('CRITICAL');
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_WARNING_ALERT:
|
|
|
|
case AGENT_MODULE_STATUS_WARNING:
|
|
|
|
return __('WARNING');
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_UNKNOWN:
|
|
|
|
return __('UNKNOWN');
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_NO_DATA:
|
|
|
|
case AGENT_MODULE_STATUS_NOT_INIT:
|
|
|
|
return __('NO DATA');
|
|
|
|
|
|
|
|
case AGENT_MODULE_STATUS_NORMAL_ALERT:
|
|
|
|
case AGENT_MODULE_STATUS_NORMAL:
|
|
|
|
default:
|
|
|
|
return __('NORMAL');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-30 12:16:51 +02:00
|
|
|
/**
|
|
|
|
* Sets or retrieves value of id_tipo_modulo (complex).
|
|
|
|
*
|
|
|
|
* @param integer|null $id_tipo_modulo Id module type.
|
|
|
|
*
|
|
|
|
* @return integer corresponding to this module type.
|
|
|
|
* @throws \Exception On error.
|
|
|
|
*/
|
|
|
|
public function id_tipo_modulo(?int $id_tipo_modulo=null)
|
|
|
|
{
|
|
|
|
if ($id_tipo_modulo === null) {
|
|
|
|
return $this->fields['id_tipo_modulo'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->moduleType($id_tipo_modulo);
|
2020-05-20 19:04:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns current status.
|
|
|
|
*
|
|
|
|
* @return PandoraFMS\ModuleStatus Status of the module.
|
|
|
|
*/
|
|
|
|
public function getStatus()
|
|
|
|
{
|
|
|
|
return $this->status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-30 16:57:08 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-24 18:01:34 +02:00
|
|
|
/**
|
|
|
|
* Retrieve all alert templates (ids) assigned to current module.
|
|
|
|
*
|
|
|
|
* @return array Of ids.
|
|
|
|
*/
|
|
|
|
public function alertTemplatesAssigned()
|
|
|
|
{
|
|
|
|
if ($this->id_agente_modulo() === null) {
|
|
|
|
// Need to be stored first.
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = db_get_all_rows_filter(
|
|
|
|
'talert_template_modules',
|
|
|
|
['id_agent_module' => $this->id_agente_modulo()],
|
|
|
|
'id_alert_template'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_reduce(
|
|
|
|
$result,
|
|
|
|
function ($carry, $item) {
|
|
|
|
$carry[] = $item['id_alert_template'];
|
|
|
|
return $carry;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a alert template assignment.
|
|
|
|
*
|
|
|
|
* @param integer $id_alert_template Target id.
|
|
|
|
*
|
|
|
|
* @return boolean Success or not.
|
|
|
|
*/
|
|
|
|
public function unassignAlertTemplate(int $id_alert_template)
|
|
|
|
{
|
|
|
|
if ($this->id_agente_modulo() === null) {
|
|
|
|
// Need to be stored first.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($id_alert_template) === false
|
|
|
|
|| $id_alert_template <= 0
|
|
|
|
) {
|
|
|
|
// Invalid alert template.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (bool) \db_process_sql_delete(
|
|
|
|
'talert_template_modules',
|
|
|
|
[
|
|
|
|
'id_agent_module' => $this->id_agente_modulo(),
|
|
|
|
'id_alert_template' => $id_alert_template,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an alert template to this module.
|
|
|
|
*
|
|
|
|
* @param integer|null $id_alert_template Target alert template.
|
|
|
|
*
|
|
|
|
* @return boolean Status of adding process.
|
|
|
|
*/
|
|
|
|
public function addAlertTemplate(?int $id_alert_template=null)
|
|
|
|
{
|
|
|
|
if ($this->id_agente_modulo() === null) {
|
|
|
|
// Need to be stored first.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($id_alert_template) === false
|
|
|
|
|| $id_alert_template <= 0
|
|
|
|
) {
|
|
|
|
// Invalid alert template.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (bool) \db_process_sql_insert(
|
|
|
|
'talert_template_modules',
|
|
|
|
[
|
|
|
|
'id_agent_module' => $this->id_agente_modulo(),
|
|
|
|
'id_alert_template' => $id_alert_template,
|
|
|
|
'last_reference' => time(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-20 19:04:42 +02:00
|
|
|
/**
|
|
|
|
* Saves current definition to database.
|
|
|
|
*
|
|
|
|
* @return mixed Affected rows of false in case of error.
|
|
|
|
* @throws \Exception On error.
|
|
|
|
*/
|
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
if (empty($this->fields['nombre']) === true) {
|
|
|
|
throw new \Exception(
|
|
|
|
get_class($this).' error, "nombre" is not set'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->fields['id_agente']) === true) {
|
|
|
|
throw new \Exception(
|
|
|
|
get_class($this).' error, "id_agente" is not set'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:16:51 +02:00
|
|
|
// Include some enterprise dependencies.
|
|
|
|
enterprise_include_once('include/functions_config_agents.php');
|
|
|
|
|
|
|
|
$updates = $this->fields;
|
|
|
|
$updates['id_tipo_modulo'] = $this->moduleType()->id_tipo();
|
|
|
|
|
2020-05-20 19:04:42 +02:00
|
|
|
if ($this->fields['id_agente_modulo'] > 0) {
|
|
|
|
// Update.
|
|
|
|
$rs = \db_process_sql_update(
|
|
|
|
'tagente_modulo',
|
|
|
|
$updates,
|
|
|
|
['id_agente_modulo' => $this->fields['id_agente_modulo']]
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($rs === false) {
|
2020-06-30 16:57:08 +02:00
|
|
|
global $config;
|
2020-05-20 19:04:42 +02:00
|
|
|
throw new \Exception(
|
2020-06-30 16:57:08 +02:00
|
|
|
__METHOD__.' error: '.$config['dbconnection']->error
|
2020-05-20 19:04:42 +02:00
|
|
|
);
|
|
|
|
}
|
2020-06-30 12:16:51 +02:00
|
|
|
|
|
|
|
// Save configuration data if needed.
|
|
|
|
if ($this->configurationData !== null) {
|
|
|
|
\enterprise_hook(
|
|
|
|
'config_agents_update_module_in_conf',
|
|
|
|
[
|
|
|
|
$this->id_agente(),
|
|
|
|
$this->configurationDataOld,
|
|
|
|
$this->configurationData,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2020-05-20 19:04:42 +02:00
|
|
|
} else {
|
|
|
|
// Creation.
|
|
|
|
// Clean null fields.
|
|
|
|
foreach ($updates as $k => $v) {
|
|
|
|
if ($v === null) {
|
|
|
|
unset($updates[$k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rs = \modules_create_agent_module(
|
|
|
|
$this->fields['id_agente'],
|
|
|
|
$updates['nombre'],
|
|
|
|
$updates
|
|
|
|
);
|
|
|
|
|
2020-06-30 12:16:51 +02:00
|
|
|
if ($rs === false || $rs < 0) {
|
2020-06-30 16:57:08 +02:00
|
|
|
global $config;
|
2020-06-30 12:16:51 +02:00
|
|
|
if ($rs === ERR_EXIST) {
|
|
|
|
throw new \Exception(
|
|
|
|
__METHOD__.': '.__(
|
|
|
|
'Module already exists: "%s"',
|
|
|
|
$updates['nombre']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:04:42 +02:00
|
|
|
throw new \Exception(
|
2020-06-30 16:57:08 +02:00
|
|
|
__METHOD__.' error: '.$config['dbconnection']->error
|
2020-05-20 19:04:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fields['id_agente_modulo'] = $rs;
|
2020-06-30 12:16:51 +02:00
|
|
|
|
|
|
|
\enterprise_hook(
|
|
|
|
'config_agents_add_module_in_conf',
|
|
|
|
[
|
|
|
|
$this->id_agente(),
|
|
|
|
$this->configurationData,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies if module is local or not.
|
|
|
|
*
|
|
|
|
* @return boolean Is local, or not (false).
|
|
|
|
*/
|
|
|
|
public function is_local()
|
|
|
|
{
|
|
|
|
if ($this->moduleType()->is_local_datatype() === true) {
|
|
|
|
if ($this->fields['id_modulo'] === MODULE_DATA) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms configuration data into an array.
|
|
|
|
*
|
|
|
|
* @return array Configuration data in array format.
|
|
|
|
*/
|
|
|
|
protected function configurationDataToArray()
|
|
|
|
{
|
|
|
|
$rr = explode("\n", $this->configurationData);
|
|
|
|
|
|
|
|
$configuration = [];
|
|
|
|
|
|
|
|
foreach ($rr as $line) {
|
|
|
|
if (empty($line) === true) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/module_begin/', $line) === 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/module_end/', $line) === 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$_tmp = explode(' ', $line, 2);
|
|
|
|
|
|
|
|
$key = $_tmp[0];
|
|
|
|
$value = $_tmp[1];
|
|
|
|
|
|
|
|
$configuration[$key] = $value;
|
2020-05-20 19:04:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:16:51 +02:00
|
|
|
return $configuration;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates remote configuration.
|
|
|
|
*
|
|
|
|
* @param string $key Left side (module_XXX).
|
|
|
|
* @param string $value Value, could be empty.
|
|
|
|
*
|
|
|
|
* @return boolean True - configurationData updated, false if not.
|
|
|
|
*/
|
|
|
|
public function updateConfigurationData(string $key, ?string $value=null)
|
|
|
|
{
|
|
|
|
if ($this->is_local() !== true) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cnf = $this->configurationDataToArray();
|
|
|
|
|
|
|
|
$cnf[$key] = $value;
|
|
|
|
|
|
|
|
$str = "module_begin\n";
|
|
|
|
foreach ($cnf as $k => $v) {
|
|
|
|
$str .= $k.' '.$v."\n";
|
2020-05-20 19:04:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:16:51 +02:00
|
|
|
$str .= "module_end\n";
|
|
|
|
|
|
|
|
$this->configuration_data($str);
|
|
|
|
|
2020-05-20 19:04:42 +02:00
|
|
|
return true;
|
2020-06-30 12:16:51 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get/set configuration data for current module.
|
|
|
|
*
|
|
|
|
* @param string|null $conf Configuration data (block).
|
|
|
|
*
|
|
|
|
* @return mixed Content or void if set.
|
|
|
|
*/
|
|
|
|
public function configuration_data(?string $conf=null)
|
|
|
|
{
|
|
|
|
if ($conf === null) {
|
|
|
|
return $this->configurationData;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->configurationData = $conf;
|
2020-05-20 19:04:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Erases this module.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
\modules_delete_agent_module(
|
|
|
|
$this->id_agente_modulo()
|
|
|
|
);
|
|
|
|
|
|
|
|
unset($this->fields);
|
|
|
|
unset($this->status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-24 18:01:34 +02:00
|
|
|
/**
|
|
|
|
* Transforms results from classic mode into modern exceptions.
|
|
|
|
*
|
|
|
|
* @param integer|boolean $result Result received from module management.
|
|
|
|
*
|
|
|
|
* @return integer Module id created or result.
|
|
|
|
* @throws \Exception On error.
|
|
|
|
*/
|
|
|
|
public static function errorToException($result)
|
|
|
|
{
|
|
|
|
if ($result === ERR_INCOMPLETE) {
|
|
|
|
throw new \Exception(
|
|
|
|
__('Module name empty.')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result === ERR_GENERIC) {
|
|
|
|
throw new \Exception(
|
|
|
|
__('Invalid characters in module name')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result === ERR_EXIST) {
|
|
|
|
throw new \Exception(
|
|
|
|
__('Module already exists please select another name or agent.')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
throw new \Exception(
|
|
|
|
__('Insufficent permissions to perform this action')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result === ERR_DB) {
|
|
|
|
global $config;
|
|
|
|
throw new \Exception(
|
|
|
|
__('Error while processing: %s', $config['dbconnection']->error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-26 15:48:58 +02:00
|
|
|
/**
|
2020-06-29 19:17:40 +02:00
|
|
|
* Calculates cascade protection service value for this service.
|
2020-06-26 15:48:58 +02:00
|
|
|
*
|
2020-06-30 16:57:08 +02:00
|
|
|
* @param integer|null $id_node Meta searching node will use this field.
|
|
|
|
*
|
2020-06-29 19:17:40 +02:00
|
|
|
* @return integer CPS value.
|
2020-06-30 16:57:08 +02:00
|
|
|
* @throws \Exception On error.
|
2020-06-26 15:48:58 +02:00
|
|
|
*/
|
2020-06-30 16:57:08 +02:00
|
|
|
public function calculateCPS(?int $id_node=null)
|
2020-06-26 15:48:58 +02:00
|
|
|
{
|
2020-06-29 19:17:40 +02:00
|
|
|
if ($this->cps() < 0) {
|
|
|
|
return $this->cps();
|
2020-06-26 15:48:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:17:40 +02:00
|
|
|
// 1. check parents.
|
|
|
|
$direct_parents = db_get_all_rows_sql(
|
|
|
|
sprintf(
|
2020-06-30 16:57:08 +02:00
|
|
|
'SELECT id_service, cps, cascade_protection, name
|
2020-06-29 19:17:40 +02:00
|
|
|
FROM `tservice_element` te
|
|
|
|
INNER JOIN `tservice` t ON te.id_service = t.id
|
|
|
|
WHERE te.id_agente_modulo = %d',
|
|
|
|
$this->id_agente_modulo()
|
|
|
|
)
|
2020-06-26 15:48:58 +02:00
|
|
|
);
|
|
|
|
|
2020-06-30 16:57:08 +02:00
|
|
|
// 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
|
2020-06-29 19:17:40 +02:00
|
|
|
&& has_metaconsole() === true
|
|
|
|
) {
|
2020-06-30 16:57:08 +02:00
|
|
|
// Node searching metaconsole.
|
2020-06-29 19:17:40 +02:00
|
|
|
$mc_parents = [];
|
|
|
|
global $config;
|
|
|
|
$mc_db_conn = \enterprise_hook(
|
|
|
|
'metaconsole_load_external_db',
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'dbhost' => $config['replication_dbhost'],
|
|
|
|
'dbuser' => $config['replication_dbuser'],
|
|
|
|
'dbpass' => io_output_password(
|
|
|
|
$config['replication_dbpass']
|
|
|
|
),
|
|
|
|
'dbname' => $config['replication_dbname'],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($mc_db_conn === NOERR) {
|
|
|
|
$mc_parents = db_get_all_rows_sql(
|
|
|
|
sprintf(
|
|
|
|
'SELECT id_service,
|
|
|
|
cps,
|
2020-06-30 16:57:08 +02:00
|
|
|
cascade_protection,
|
|
|
|
name
|
2020-06-29 19:17:40 +02:00
|
|
|
FROM `tservice_element` te
|
|
|
|
INNER JOIN `tservice` t ON te.id_service = t.id
|
|
|
|
WHERE te.id_agente_modulo = %d',
|
|
|
|
$this->id_agente_modulo()
|
2020-06-30 16:57:08 +02:00
|
|
|
),
|
|
|
|
false,
|
|
|
|
false
|
2020-06-26 15:48:58 +02:00
|
|
|
);
|
|
|
|
}
|
2020-06-29 19:17:40 +02:00
|
|
|
|
|
|
|
// Restore the default connection.
|
|
|
|
\enterprise_hook('metaconsole_restore_db');
|
2020-06-30 16:57:08 +02:00
|
|
|
} 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));
|
|
|
|
}
|
2020-06-26 15:48:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:17:40 +02:00
|
|
|
$cps = 0;
|
2020-06-26 15:48:58 +02:00
|
|
|
|
2020-06-29 19:17:40 +02:00
|
|
|
if (is_array($direct_parents) === false) {
|
|
|
|
$direct_parents = [];
|
|
|
|
}
|
2020-06-26 15:48:58 +02:00
|
|
|
|
2020-06-29 19:17:40 +02:00
|
|
|
if (is_array($mc_parents) === false) {
|
|
|
|
$mc_parents = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge all parents (node and meta).
|
|
|
|
$parents = array_merge($direct_parents, $mc_parents);
|
|
|
|
|
|
|
|
foreach ($parents as $parent) {
|
|
|
|
$cps += $parent['cps'];
|
|
|
|
if (((bool) $parent['cascade_protection']) === true) {
|
|
|
|
$cps++;
|
|
|
|
}
|
2020-06-26 15:48:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $cps;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-20 19:04:42 +02:00
|
|
|
}
|