Merge branch 'ent-11705-15916-campo-editable-en-vista-de-eventos' into 'develop'

Ent 11705 15916 campo editable en vista de eventos

See merge request artica/pandorafms!6486
This commit is contained in:
Rafael Ameijeiras 2023-11-30 09:18:28 +00:00
commit 0957a87dcb
12 changed files with 241 additions and 6 deletions

View File

@ -1,4 +1,8 @@
START TRANSACTION;
ALTER TABLE `tevento`
ADD COLUMN `event_custom_id` TEXT NULL AFTER `module_status`;
-- Telegram and vonage default alerts
UPDATE talert_actions
SET field2='[PANDORA] Alert FIRED on _agent_ / _module_ / _timestamp_ / _data_'

View File

@ -115,6 +115,7 @@ $fields_available['module_status'] = __('Module Status');
$fields_available['mini_severity'] = __('Severity mini');
$fields_available['module_custom_id'] = __('Module custom ID');
$fields_available['custom_data'] = __('Custom data');
$fields_available['event_custom_id'] = __('Event Custom ID');
// Remove fields already selected.

View File

@ -733,7 +733,7 @@ $table->data[$i++][] = html_print_label_input_block(
);
$help_tip = ui_print_help_tip(
__('If there are any "In process" events with a specific Extra ID and a New event with that Extra ID is received, it will be created as "In process" instead.'),
__('If there are any "In process" events with a specific Extra ID and a New event with that Extra ID is received, it will be created as "In process" instead. The new events also inherit Event Custom ID'),
true
);

View File

@ -92,6 +92,7 @@ $get_id_source_event = get_parameter('get_id_source_event');
$node_id = (int) get_parameter('node_id', 0);
$settings_modal = get_parameter('settings', 0);
$parameters_modal = get_parameter('parameters', 0);
$update_event_custom_id = get_parameter('update_event_custom_id', 0);
$draw_events_graph = get_parameter('drawEventsGraph', false);
// User private filter.
@ -2754,6 +2755,52 @@ if ($draw_row_response_info === true) {
return;
}
if ($update_event_custom_id) {
$event_custom_id = get_parameter('event_custom_id');
$event_id = get_parameter('event_id');
$server_id = 0;
if (is_metaconsole() === true) {
$server_id = (int) get_parameter('server_id');
}
// Safe custom fields for hacks.
if (preg_match('/script/i', io_safe_output($event_custom_id))) {
$return = false;
} else {
try {
if (is_metaconsole() === true
&& $server_id > 0
) {
$node = new Node($server_id);
$node->connect();
}
$return = events_event_custom_id(
$event_id,
$event_custom_id
);
} catch (\Exception $e) {
// Unexistent agent.
if (is_metaconsole() === true
&& $server_id > 0
) {
$node->disconnect();
}
$return = false;
} finally {
if (is_metaconsole() === true
&& $server_id > 0
) {
$node->disconnect();
}
}
}
echo ($return === true) ? 'update_ok' : 'update_error';
return;
}
if ((bool) $draw_events_graph === true) {
$filter = get_parameter('filter');
$output = event_print_graph($filter);

View File

@ -13132,7 +13132,7 @@ function api_set_create_event($id, $trash1, $other, $returnType)
if ($other['data'][18] != '') {
$values['id_extra'] = $other['data'][18];
$sql_validation = 'SELECT id_evento,estado,ack_utimestamp,id_usuario
$sql_validation = 'SELECT id_evento,estado,ack_utimestamp,id_usuario,event_custom_id
FROM tevento
WHERE estado IN (0,2) AND id_extra ="'.$other['data'][18].'";';
@ -13147,6 +13147,7 @@ function api_set_create_event($id, $trash1, $other, $returnType)
$values['status'] = 2;
$ack_utimestamp = $val['ack_utimestamp'];
$values['id_usuario'] = $val['id_usuario'];
$values['event_custom_id'] = $val['event_custom_id'];
}
api_set_validate_event_by_id($val['id_evento']);
@ -13177,7 +13178,8 @@ function api_set_create_event($id, $trash1, $other, $returnType)
$custom_data,
$values['server_id'],
$values['id_extra'],
$ack_utimestamp
$ack_utimestamp,
$values['event_custom_id'] ?? null
);
if ($other['data'][12] != '') {
@ -17787,6 +17789,48 @@ function api_token_check(string $token)
}
/**
* Set custom field value in tevento
*
* @param mixed $id_event Event id.
* @param mixed $custom_field Custom field to set.
* @return void
*/
function api_set_event_custom_id($id, $value)
{
// Get the event
$event = events_get_event($id, false, is_metaconsole());
// If event not exists, end the execution.
if ($event === false) {
returnError(
'event_not_exists',
'Event not exists'
);
$result = false;
}
// Safe custom fields for hacks.
if (preg_match('/script/i', io_safe_output($value))) {
$result = false;
}
$result = events_event_custom_id(
$id,
$value
);
// If update results failed
if (empty($result) === true || $result === false) {
returnError(
'The event could not be updated'
);
return false;
} else {
returnData('string', ['data' => 'Event updated.']);
}
}
/**
* Extract info Agents for inventories ITSM.
*

View File

@ -219,6 +219,7 @@ function events_get_all_fields()
$columns['module_status'] = __('Module status');
$columns['module_custom_id'] = __('Module custom id');
$columns['custom_data'] = __('Custom data');
$columns['event_custom_id'] = __('Event Custom ID');
return $columns;
}
@ -322,6 +323,9 @@ function events_get_column_name($field, $table_alias=false)
case 'custom_data':
return __('Custom data');
case 'event_custom_id':
return __('Event Custom ID');
default:
return __($field);
}
@ -2356,7 +2360,8 @@ function events_create_event(
$custom_data='',
$server_id=0,
$id_extra='',
$ack_utimestamp=0
$ack_utimestamp=0,
$event_custom_id=null
) {
if ($source === false) {
$source = get_product_name();
@ -2388,6 +2393,7 @@ function events_create_event(
'custom_data' => $custom_data,
'data' => '',
'module_status' => 0,
'event_custom_id' => $event_custom_id,
];
return (int) db_process_sql_insert('tevento', $values);
@ -4658,6 +4664,30 @@ function events_page_details($event, $server_id=0)
$data[1] = '<i>'.__('N/A').'</i>';
}
$table_details->data[] = $data;
$readonly = true;
if (check_acl($config['id_user'], 0, 'EW')) {
$readonly = false;
}
$data = [];
$data[0] = __('Event Custom ID');
$data[1] = '<div class="flex-row-center">'.html_print_input_text('event_custom_id', $event['event_custom_id'], '', false, 255, true, $readonly, false, '', 'w60p');
if ($readonly === false) {
$data[1] .= html_print_button(
__('Update'),
'update_event_custom_id',
false,
'update_event_custom_id('.$event['id_evento'].', '.$event['server_id'].');',
[
'icon' => 'next',
'mode' => 'link',
],
true
);
}
$data[1] .= '</div>';
$table_details->data[] = $data;
$details = '<div id="extended_event_details_page" class="extended_event_pages">'.html_print_table($table_details, true).'</div>';
@ -6260,6 +6290,63 @@ function event_get_counter_extraId(array $event, ?array $filters)
}
/**
* Update event detail custom field
*
* @param mixed $id_event Event ID or array of events.
* @param string $event_custom_id Event custom ID to be update.
*
* @return boolean Whether or not it was successful
*/
function events_event_custom_id(
$id_event,
$event_custom_id,
) {
global $config;
// Cleans up the selection for all unwanted values also casts any single
// values as an array.
if (![$id_event]) {
$id_event = (array) safe_int($id_event, 1);
}
// Check ACL.
foreach ($id_event as $k => $id) {
$event_group = events_get_group($id);
if (check_acl($config['id_user'], $event_group, 'EW') == 0) {
db_pandora_audit(
AUDIT_LOG_ACL_VIOLATION,
'Attempted updating event #'.$id
);
unset($id_event[$k]);
}
}
if (empty($id_event) === true) {
return false;
}
// Get the current event comments.
$first_event = $id_event;
if (is_array($id_event) === true) {
$first_event = reset($id_event);
}
// Update comment.
$ret = db_process_sql_update(
'tevento',
['event_custom_id' => $event_custom_id],
['id_evento' => $first_event]
);
if (($ret === false) || ($ret === 0)) {
return false;
}
return true;
}
function event_print_graph(
$filter,
$graph_height=100,

View File

@ -492,6 +492,37 @@ function event_comment(current_event) {
return false;
}
// Save custom_field into an event.
function update_event_custom_id(event_id, server_id) {
var event_custom_id = $("#text-event_custom_id").val();
var params = {
page: "include/ajax/events",
update_event_custom_id: 1,
event_custom_id: event_custom_id,
event_id: event_id,
server_id: server_id
};
$("#button-update_custom_field").attr("disabled", "disabled");
$("#response_loading").show();
jQuery.ajax({
data: params,
type: "POST",
url: getUrlAjax(),
dataType: "html",
success: function(data) {
if (data === "update_error") {
alert("Event Custom ID not valid");
}
$("#button-update_custom_field").removeAttr("disabled");
$("#response_loading").hide();
$("#button-events_form_search_bt").trigger("click");
}
});
}
var processed = 0;
function update_event(table, id_evento, type, event_rep, row, server_id) {
var inputs = $("#events_form :input");

View File

@ -838,6 +838,7 @@ class EventsListWidget extends Widget
'mini_severity' => __('Severity mini'),
'module_custom_id' => __('Module custom ID'),
'custom_data' => __('Custom data'),
'event_custom_id' => __('Event Custom ID'),
];
}

View File

@ -378,6 +378,7 @@ if (is_ajax() === true) {
'te.owner_user',
'if(te.ack_utimestamp > 0, te.ack_utimestamp,"") as ack_utimestamp',
'te.custom_data',
'te.event_custom_id',
'te.data',
'te.module_status',
'ta.alias as agent_name',

View File

@ -726,6 +726,7 @@ CREATE TABLE IF NOT EXISTS `tevento` (
`custom_data` TEXT,
`data` TINYTEXT,
`module_status` INT NOT NULL DEFAULT 0,
`event_custom_id` TEXT,
PRIMARY KEY (`id_evento`),
KEY `idx_agente` (`id_agente`),
KEY `idx_agentmodule` (`id_agentmodule`),

View File

@ -4304,6 +4304,7 @@ sub pandora_event {
my $utimestamp = time ();
my $timestamp = strftime ("%Y-%m-%d %H:%M:%S", localtime ($utimestamp));
my $event_custom_id = undef;
$id_agentmodule = 0 unless defined ($id_agentmodule);
# Validate events with the same event id
@ -4321,6 +4322,7 @@ sub pandora_event {
logger($pa_config, "Keeping In process status from last event with extended id '$id_extra'.", 10);
$ack_utimestamp = get_db_value ($dbh, 'SELECT ack_utimestamp FROM tevento WHERE id_extra=? AND estado=2', $id_extra);
$event_status = 2;
$event_custom_id = get_db_value ($dbh, 'SELECT event_custom_id FROM tevento WHERE id_extra=? AND estado=2', $id_extra);
}
}
@ -4332,8 +4334,8 @@ sub pandora_event {
# Create the event
logger($pa_config, "Generating event '$evento' for agent ID $id_agente module ID $id_agentmodule.", 10);
$event_id = db_insert ($dbh, 'id_evento','INSERT INTO tevento (id_agente, id_grupo, evento, timestamp, estado, utimestamp, event_type, id_agentmodule, id_alert_am, criticity, tags, source, id_extra, id_usuario, critical_instructions, warning_instructions, unknown_instructions, ack_utimestamp, custom_data, data, module_status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $id_agente, $id_grupo, safe_input ($evento), $timestamp, $event_status, $utimestamp, $event_type, $id_agentmodule, $id_alert_am, $severity, $module_tags, $source, $id_extra, $user_name, $critical_instructions, $warning_instructions, $unknown_instructions, $ack_utimestamp, $custom_data, safe_input($module_data), $module_status);
$event_id = db_insert ($dbh, 'id_evento','INSERT INTO tevento (id_agente, id_grupo, evento, timestamp, estado, utimestamp, event_type, id_agentmodule, id_alert_am, criticity, tags, source, id_extra, id_usuario, critical_instructions, warning_instructions, unknown_instructions, ack_utimestamp, custom_data, data, module_status, event_custom_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $id_agente, $id_grupo, safe_input ($evento), $timestamp, $event_status, $utimestamp, $event_type, $id_agentmodule, $id_alert_am, $severity, $module_tags, $source, $id_extra, $user_name, $critical_instructions, $warning_instructions, $unknown_instructions, $ack_utimestamp, $custom_data, safe_input($module_data), $module_status, $event_custom_id);
if(defined($event_id) && $comment ne '') {
my $comment_id = db_insert ($dbh, 'id','INSERT INTO tevent_comment (id_event, utimestamp, comment, id_user, action)

View File

@ -201,6 +201,7 @@ sub help_screen{
help_screen_line('--disable_double_auth', '<user_name>', 'Disable the double authentication for the specified user');
print "\nEVENTS:\n\n" unless $param ne '';
help_screen_line('--create_event', "<event> <event_type> <group_name> [<agent_name> <module_name>\n\t <event_status> <severity> <template_name> <user_name> <comment> \n\t <source> <id_extra> <tags> <custom_data_json> <force_create_agent> \n\t <critical_instructions> <warning_instructions> <unknown_instructions> <use_alias>]", 'Add event');
help_screen_line('--update_event_custom_id', "<event> <event_custom_id>", 'Update Event Custom ID');
help_screen_line('--validate_event', "<agent_name> <module_name> <datetime_min> <datetime_max>\n\t <user_name> <criticity> <template_name> [<use_alias>]", 'Validate events');
help_screen_line('--validate_event_id', '<event_id>', 'Validate event given a event id');
help_screen_line('--get_event_info', '<event_id>[<csv_separator>]', 'Show info about a event given a event id');
@ -4549,6 +4550,17 @@ sub cli_create_event() {
}
}
##############################################################################
# Update event custom id
# Related option: --update_event_custom_id
##############################################################################
sub cli_update_event_custom_id() {
my ($id_event, $event_custom_id) = @ARGV[2..3];
my $result = api_call(\%conf, 'set', 'event_custom_id', $id_event, $event_custom_id);
print "\n$result\n";
}
##############################################################################
# Validate event.
# Related option: --validate_event
@ -8335,6 +8347,10 @@ sub pandora_manage_main ($$$) {
param_check($ltotal, 4, 0);
cli_insert_gis_data();
}
elsif ($param eq '--update_event_custom_id'){
param_check($ltotal, 2);
cli_update_event_custom_id();
}
else {
print_log "[ERROR] Invalid option '$param'.\n\n";
$param = '';