Merge branch 'ent-6665-axa-datos-heredados-en-evento-alert-fired' into 'develop'

Added macros to event response

See merge request artica/pandorafms!3712
This commit is contained in:
Daniel Rodriguez 2021-02-03 15:22:30 +01:00
commit 181362feb2
4 changed files with 309 additions and 52 deletions

View File

@ -3718,6 +3718,14 @@ function events_get_response_target(
) {
global $config;
include_once $config['homedir'].'/vendor/autoload.php';
try {
$eventObjt = new PandoraFMS\Event($event_id);
} catch (Exception $e) {
$eventObjt = new PandoraFMS\Event();
}
// If server_id > 0, it's a metaconsole query.
$meta = $server_id > 0 || is_metaconsole();
$event_table = events_get_events_table($meta, $history);
@ -3915,9 +3923,69 @@ function events_get_response_target(
}
if (strpos($target, '_event_instruction_') !== false) {
// Fallback to module instructions if not defined in event.
$instructions = [];
foreach ([
'warning_instructions',
'critical_instructions',
'unknown_instructions',
] as $i) {
$instructions[$i] = $event[$i];
if (empty($instructions[$i]) === true
&& $eventObjt->module() !== null
) {
try {
$instructions[$i] = $eventObjt->module()->{$i}();
} catch (Exception $e) {
// Method not found.
$instructions[$i] = null;
}
}
}
$target = str_replace(
'_event_instruction_',
events_display_instructions($event['event_type'], $event, false),
events_display_instructions(
$event['event_type'],
$instructions,
false,
$eventObjt->toArray()
),
$target
);
}
if (strpos($target, '_data_') !== false
&& $eventObjt !== null
&& $eventObjt->module() !== null
) {
$target = str_replace(
'_data_',
$eventObjt->module()->lastValue(),
$target
);
} else {
$target = str_replace(
'_data_',
__('N/A'),
$target
);
}
if (strpos($target, '_moduledescription_') !== false
&& $eventObjt !== null
&& $eventObjt->module() !== null
) {
$target = str_replace(
'_moduledescription_',
io_safe_output($eventObjt->module()->descripcion()),
$target
);
} else {
$target = str_replace(
'_moduledescription_',
__('N/A'),
$target
);
}
@ -4404,7 +4472,14 @@ function events_page_details($event, $server='')
$data = [];
$data[0] = __('Instructions');
$data[1] = html_entity_decode(events_display_instructions($event['event_type'], $event, true));
$data[1] = html_entity_decode(
events_display_instructions(
$event['event_type'],
$event,
true,
$event
)
);
$table_details->data[] = $data;
$data = [];
@ -4535,13 +4610,50 @@ function events_display_status($status)
* instructions.
* @param boolean $italic Display N/A between italic html marks if
* instruction is not found.
* @param array $eventObj Event object.
*
* @return string Safe output.
*/
function events_display_instructions($event_type='', $inst=[], $italic=true)
function events_display_instructions($event_type='', $inst=[], $italic=true, $event=null)
{
if ($event_type === 'alert_fired') {
if ($event !== null) {
// Retrieve alert template type.
if ((bool) is_metaconsole() === true
&& $event['server_id'] > 0
) {
enterprise_include_once('include/functions_metaconsole.php');
$r = enterprise_hook(
'metaconsole_connect',
[
null,
$event['server_id'],
]
);
}
$event_type = db_get_value_sql(
sprintf(
'SELECT ta.type
FROM talert_templates ta
INNER JOIN talert_template_modules tam
ON ta.id=tam.id_alert_template
WHERE tam.id = %d',
$event['id_alert_am']
)
);
if ((bool) is_metaconsole() === true
&& $event['server_id'] > 0
) {
enterprise_hook('metaconsole_restore_db');
}
}
}
switch ($event_type) {
case 'going_unknown':
case 'unknown':
if ($inst['unknown_instructions'] != '') {
return str_replace("\n", '<br>', io_safe_output($inst['unknown_instructions']));
}
@ -4549,6 +4661,7 @@ function events_display_instructions($event_type='', $inst=[], $italic=true)
case 'going_up_warning':
case 'going_down_warning':
case 'warning':
if ($inst['warning_instructions'] != '') {
return str_replace("\n", '<br>', io_safe_output($inst['warning_instructions']));
}
@ -4556,6 +4669,7 @@ function events_display_instructions($event_type='', $inst=[], $italic=true)
case 'going_up_critical':
case 'going_down_critical':
case 'critical':
if ($inst['critical_instructions'] != '') {
return str_replace("\n", '<br>', io_safe_output($inst['critical_instructions']));
}
@ -7146,7 +7260,12 @@ function events_get_field_value_by_event_id(
if (strpos($value, '_event_instruction_') !== false) {
$value = str_replace(
'_event_instruction_',
events_display_instructions($event['event_type'], $event, false),
events_display_instructions(
$event['event_type'],
$event,
false,
$event
),
$value
);
}

View File

@ -57,6 +57,20 @@ abstract class Entity
*/
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.
@ -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.
*

View File

@ -33,40 +33,106 @@ global $config;
require_once $config['homedir'].'/include/functions_events.php';
/**
* PandoraFMS Group entity.
* PandoraFMS event entity.
*/
class Event extends Entity
{
/**
* List of available ajax methods.
* Agent related to this event.
*
* @var array
* @var \PandoraFMS\Agent
*/
private static $ajaxMethods = [];
private $linkedAgent;
/**
* Module related to this event.
*
* @var \PandoraFMS\Module
*/
private $linkedModule;
/**
* Builds a PandoraFMS\Group object from a group id.
* Builds a PandoraFMS\Event object from given event id.
*
* @param integer $id_group Group Id.
* @param integer $event_id Event Id.
*/
public function __construct(?int $id_group=null)
public function __construct(?int $event_id=null)
{
$table = 'tevento';
if ((bool) \is_metaconsole() === true) {
$table = 'tmetaconsole_event';
}
if ($id_group === 0) {
if ($event_id === 0) {
parent::__construct($table);
} else if (is_numeric($id_group) === true) {
parent::__construct($table, ['id_grupo' => $id_group]);
} else if (is_numeric($event_id) === true) {
parent::__construct($table, ['id_evento' => $event_id]);
} else {
// Empty skel.
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) {
$this->linkedAgent = new Agent((int) $this->id_agente());
}
if ($this->id_agentmodule() !== null) {
$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();
}
/**
* Get/set linked agent.
*
* @param Agent|null $agent New agent to link.
*
* @return Agent|null Agent or null if set operation.
*/
public function agent(?Agent $agent=null) : ?Agent
{
if ($agent === null) {
return $this->linkedAgent;
}
$this->linkedAgent = $agent;
$this->id_agentmodule($agent->id_agentmodule());
}
/**
* Get/set linked agent.
*
* @param Module|null $module New module to link.
*
* @return Module|null module or null if set operation.
*/
public function module(?Module $module=null) : ?Module
{
if ($module === null) {
return $this->linkedModule;
}
$this->linkedModule = $module;
$this->id_agentmodule($module->id_agentmodule());
}
@ -83,7 +149,8 @@ class Event extends Entity
* @param boolean $return_sql Return sql or execute it.
* @param string $having Having.
*
* @return array|string|falsse Found events or SQL query or error.
* @return array|string|false Found events or SQL query or error.
* @throws \Exception On error.
*/
public static function search(
array $fields,
@ -121,10 +188,10 @@ class Event extends Entity
global $config;
if (isset($config['centralized_management']) === true
&& $config['centralized_management'] > 0
&& (bool) $config['centralized_management'] === true
) {
throw new \Exception(
get_class($this).' error, cannot be modified while centralized management environment.'
'error, cannot save in centralized management environment.'
);
}
@ -147,30 +214,4 @@ class Event extends Entity
}
/**
* Return error message to target.
*
* @param string $msg Error message.
*
* @return void
*/
public static function error(string $msg)
{
echo json_encode(['error' => $msg]);
}
/**
* Verifies target method is allowed to be called using AJAX call.
*
* @param string $method Method to be invoked via AJAX.
*
* @return boolean Available (true), or not (false).
*/
public static function ajaxMethod(string $method):bool
{
return in_array($method, self::$ajaxMethods) === true;
}
}

View File

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