refactor events meta pandora_enterprise#9086

This commit is contained in:
Daniel Barbero Martin 2022-06-17 12:50:40 +02:00
parent f3e645146d
commit 7e25a86d52
5 changed files with 198 additions and 198 deletions

View File

@ -10928,11 +10928,6 @@ function api_set_event_validate_filter_pro($trash1, $trash2, $other, $trash3)
return;
}
$table_events = 'tevento';
if (is_metaconsole()) {
$table_events = 'tmetaconsole_event';
}
if ($other['type'] == 'string') {
if ($other['data'] != '') {
returnError('Parameter error.');
@ -10998,7 +10993,7 @@ function api_set_event_validate_filter_pro($trash1, $trash2, $other, $trash3)
}
$count = db_process_sql_update(
$table_events,
'tevento',
['estado' => 1],
$filterString
);
@ -11024,12 +11019,6 @@ function api_set_event_validate_filter($trash1, $trash2, $other, $trash3)
}
$simulate = false;
$table_events = 'tevento';
if (is_metaconsole()) {
$table_events = 'tmetaconsole_event';
}
if ($other['type'] == 'string') {
if ($other['data'] != '') {
returnError('Parameter error.');
@ -11064,14 +11053,14 @@ function api_set_event_validate_filter($trash1, $trash2, $other, $trash3)
}
if ($simulate) {
$rows = db_get_all_rows_filter($table_events, $filterString);
$rows = db_get_all_rows_filter('tevento', $filterString);
if ($rows !== false) {
returnData('string', count($rows));
return;
}
} else {
$count = db_process_sql_update(
$table_events,
'tevento',
['estado' => 1],
$filterString
);
@ -11341,9 +11330,6 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db
global $config;
$table_events = 'tevento';
if (is_metaconsole() === true) {
$table_events = 'tmetaconsole_event';
}
// By default.
$status = 3;
@ -11561,21 +11547,19 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db
}
if ($group_rep == 0) {
switch ($config['dbtype']) {
case 'mysql':
if ($filter['total']) {
$sql = 'SELECT COUNT(*)
if ($filter['total']) {
$sql = 'SELECT COUNT(*)
FROM '.$table_events.'
WHERE 1=1 '.$sql_post;
} else if ($filter['more_criticity']) {
$sql = 'SELECT criticity
} else if ($filter['more_criticity']) {
$sql = 'SELECT criticity
FROM '.$table_events.'
WHERE 1=1 '.$sql_post.'
ORDER BY criticity DESC
LIMIT 1';
} else {
if (is_metaconsole() === true) {
$sql = 'SELECT *,
} else {
if (is_metaconsole() === true) {
$sql = 'SELECT *,
(SELECT t2.nombre
FROM tgrupo t2
WHERE t2.id_grupo = '.$table_events.'.id_grupo) AS group_name,
@ -11585,8 +11569,8 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db
FROM '.$table_events.$alert_join.'
WHERE 1=1 '.$sql_post.'
ORDER BY utimestamp DESC';
} else {
$sql = 'SELECT *,
} else {
$sql = 'SELECT *,
(SELECT t1.alias
FROM tagente t1
WHERE t1.id_agente = tevento.id_agente) AS agent_name,
@ -11605,60 +11589,7 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db
FROM '.$table_events.$alert_join.'
WHERE 1=1 '.$sql_post.'
ORDER BY utimestamp DESC';
}
}
break;
case 'postgresql':
// TODO TOTAL
$sql = 'SELECT *,
(SELECT t1.alias
FROM tagente t1
WHERE t1.id_agente = tevento.id_agente) AS agent_name,
(SELECT t2.nombre
FROM tgrupo t2
WHERE t2.id_grupo = tevento.id_grupo) AS group_name,
(SELECT t2.icon
FROM tgrupo t2
WHERE t2.id_grupo = tevento.id_grupo) AS group_icon,
(SELECT tmodule.name
FROM tmodule
WHERE id_module IN (
SELECT tagente_modulo.id_modulo
FROM tagente_modulo
WHERE tagente_modulo.id_agente_modulo=tevento.id_agentmodule)) AS module_name
FROM tevento
WHERE 1=1 '.$sql_post.'
ORDER BY utimestamp DESC';
break;
case 'oracle':
// TODO TOTAL
$set = [];
$sql = 'SELECT *,
(SELECT t1.alias
FROM tagente t1
WHERE t1.id_agente = tevento.id_agente) AS alias,
(SELECT t1.nombre
FROM tagente t1
WHERE t1.id_agente = tevento.id_agente) AS agent_name,
(SELECT t2.nombre
FROM tgrupo t2
WHERE t2.id_grupo = tevento.id_grupo) AS group_name,
(SELECT t2.icon
FROM tgrupo t2
WHERE t2.id_grupo = tevento.id_grupo) AS group_icon,
(SELECT tmodule.name
FROM tmodule
WHERE id_module IN (
SELECT tagente_modulo.id_modulo
FROM tagente_modulo
WHERE tagente_modulo.id_agente_modulo=tevento.id_agentmodule)) AS module_name
FROM tevento
WHERE 1=1 '.$sql_post.' ORDER BY utimestamp DESC';
$sql = oracle_recode_query($sql, $set);
break;
}
}
} else {
db_process_sql('SET group_concat_max_len = 9999999');
@ -11801,17 +11732,9 @@ function api_set_event($id_event, $unused1, $params, $unused2, $unused3)
}
}
// In meta or node.
if (is_metaconsole() === true) {
$table = 'tmetaconsole_event';
} else {
$table = 'tevento';
}
// TODO. Stablish security for prevent sql injection?
// Update the row
$result = db_process_sql_update(
$table,
'tevento',
$paramsSerialize,
[ 'id_evento' => $id_event ]
);
@ -11882,15 +11805,7 @@ function api_get_events($node_id, $trash2, $other, $returnType, $user_in_db=null
$filterString = otherParameter2Filter($other, false, $use_agent_name);
}
if (is_metaconsole()) {
if ((int) $node_id !== 0) {
$filterString .= ' AND server_id = '.$node_id;
}
$dataRows = db_get_all_rows_filter('tmetaconsole_event', $filterString);
} else {
$dataRows = db_get_all_rows_filter('tevento', $filterString);
}
$dataRows = db_get_all_rows_filter('tevento', $filterString);
$last_error = error_get_last();
if (empty($dataRows)) {
@ -13107,18 +13022,20 @@ function api_get_event_info($id_event, $trash1, $trash, $returnType)
{
global $config;
$table_events = 'tevento';
if (defined('METACONSOLE')) {
$table_events = 'tmetaconsole_event';
}
$sql = sprintf(
'SELECT *
FROM tevento
WHERE id_evento= %d',
$id_event
);
$sql = 'SELECT *
FROM '.$table_events."
WHERE id_evento=$id_event";
$event_data = db_get_row_sql($sql);
// Check the access to group
if (!empty($event_data['id_grupo']) && $event_data['id_grupo'] > 0 && !$event_data['id_agente']) {
if (!empty($event_data['id_grupo'])
&& $event_data['id_grupo'] > 0
&& !$event_data['id_agente']
) {
if (!check_acl($config['id_user'], $event_data['id_grupo'], 'ER')) {
returnError('forbidden', $returnType);
return;
@ -13126,8 +13043,14 @@ function api_get_event_info($id_event, $trash1, $trash, $returnType)
}
// Check the access to agent
if (!empty($event_data['id_agente']) && $event_data['id_agente'] > 0) {
if (!util_api_check_agent_and_print_error($event_data['id_agente'], $returnType)) {
if (!empty($event_data['id_agente'])
&& $event_data['id_agente'] > 0
) {
if (!util_api_check_agent_and_print_error(
$event_data['id_agente'],
$returnType
)
) {
return;
}
}
@ -13415,13 +13338,7 @@ function api_set_create_event($id, $trash1, $other, $returnType)
if ($other['data'][18] != '') {
$values['id_extra'] = $other['data'][18];
if (is_metaconsole()) {
$table_event = 'tmetaconsole_event';
} else {
$table_event = 'tevento';
}
$sql_validation = 'SELECT id_evento FROM '.$table_event.' where estado IN (0,2) and id_extra ="'.$other['data'][18].'";';
$sql_validation = 'SELECT id_evento FROM tevento where estado IN (0,2) and id_extra ="'.$other['data'][18].'";';
$validation = db_get_all_rows_sql($sql_validation);
if ($validation) {
foreach ($validation as $val) {
@ -13692,17 +13609,12 @@ function api_set_validate_event_by_id($id, $trash1=null, $trash2=null, $returnTy
return;
}
$table_events = 'tevento';
if (is_metaconsole()) {
$table_events = 'tmetaconsole_event';
}
$data['type'] = 'string';
$check_id = db_get_value('id_evento', $table_events, 'id_evento', $id);
$check_id = db_get_value('id_evento', 'tevento', 'id_evento', $id);
if ($check_id) {
// event exists
$status = db_get_value('estado', $table_events, 'id_evento', $id);
$status = db_get_value('estado', 'tevento', 'id_evento', $id);
if ($status == 1) {
// event already validated
$data['data'] = 'Event already validated.';
@ -13716,7 +13628,7 @@ function api_set_validate_event_by_id($id, $trash1=null, $trash2=null, $returnTy
'estado' => 1,
];
$result = db_process_sql_update($table_events, $values, ['id_evento' => $id]);
$result = db_process_sql_update('tevento', $values, ['id_evento' => $id]);
if ($result === false) {
$data['data'] = 'The event could not be validated.';
@ -17554,20 +17466,14 @@ function api_get_is_centralized($server_id, $thrash1, $thrash2, $returnType)
function api_set_event_in_progress($event_id, $trash2, $returnType)
{
global $config;
if (is_metaconsole()) {
$table = 'tmetaconsole_event';
} else {
$table = 'tevento';
}
$event = db_process_sql_update(
$table,
'tevento',
['estado' => 2],
['id_evento' => $event_id]
);
if ($event !== false) {
returnData('string', ['data' => $event]);
returnData('string', ['data' => $event]);
} else {
returnError('id_not_found', 'string');
}

View File

@ -337,13 +337,15 @@ function events_get_column_name($field, $table_alias=false)
*/
function events_get_column_names($fields, $table_alias=false)
{
if (!isset($fields) || !is_array($fields)) {
if (isset($fields) === false
|| is_array($fields) === false
) {
return [];
}
$names = [];
foreach ($fields as $f) {
if (is_array($f)) {
if (is_array($f) === true) {
$name = [];
$name['text'] = events_get_column_name($f['text'], $table_alias);
$name['class'] = ($f['class'] ?? '');
@ -373,25 +375,26 @@ function events_get_column_names($fields, $table_alias=false)
*/
function events_delete($id_evento, $filter=null, $history=false, $force_node=false)
{
if (!isset($id_evento) || $id_evento <= 0) {
if (isset($id_evento) === false
|| $id_evento <= 0
) {
return false;
}
if (!isset($filter) || !is_array($filter)) {
if (isset($filter) === false
|| is_array($filter) === true
) {
$filter = ['group_rep' => 0];
}
$table = 'tevento';
switch ($filter['group_rep']) {
case '0':
case '2':
default:
// No groups option direct update.
$delete_sql = sprintf(
'DELETE FROM %s
'DELETE FROM tevento
WHERE id_evento = %d',
$table,
$id_evento
);
break;
@ -417,13 +420,12 @@ function events_delete($id_evento, $filter=null, $history=false, $force_node=fal
$target_ids = db_get_all_rows_sql(
sprintf(
'SELECT tu.id_evento FROM %s tu INNER JOIN ( %s ) tf
'SELECT tu.id_evento FROM tevento tu INNER JOIN ( %s ) tf
ON tu.estado = tf.estado
AND tu.evento = tf.evento
AND tu.id_agente = tf.id_agente
AND tu.id_agentmodule = tf.id_agentmodule
AND tf.max_id_evento = %d',
$table,
$sql,
$id_evento
)
@ -440,8 +442,7 @@ function events_delete($id_evento, $filter=null, $history=false, $force_node=fal
);
$delete_sql = sprintf(
'DELETE FROM %s WHERE id_evento IN (%s)',
$table,
'DELETE FROM tevento WHERE id_evento IN (%s)',
join(', ', $target_ids)
);
}
@ -1557,8 +1558,21 @@ function events_get_all(
}
$data = [];
$buffers = [
'settings' => [
'total' => $config['max_number_of_events_per_node'],
'translate' => [
'tev' => __('Total Events'),
'nev' => __('Total Events per node'),
'ev' => __('Events'),
'tevn' => __('Total number of events in this node reached'),
],
],
'data' => [],
];
if (empty($result_meta) === false) {
foreach ($result_meta as $node => $value) {
$buffers['data'][$node] = count($value);
if (empty($value) === false) {
foreach ($value as $k => $v) {
$value[$k]['server_id'] = $metaconsole_connections[$node];
@ -1623,8 +1637,9 @@ function events_get_all(
$end = ((int) $offset !== 0) ? ($offset + $limit) : $limit;
$finally = array_slice($data, $offset, $end, true);
$return = [
'data' => $finally,
'total' => $count,
'buffers' => $buffers,
'data' => $finally,
'total' => $count,
];
} else {
$return = array_slice(
@ -5017,21 +5032,12 @@ function events_get_field_value_by_event_id(
) {
global $config;
$meta = false;
$event = db_get_row('tevento', 'id_evento', $event_id);
// Replace each macro.
if (strpos($value, '_agent_address_') !== false) {
if ($meta) {
$agente_table_name = 'tmetaconsole_agent';
$filter = [
'id_tagente' => $event['id_agente'],
'id_tmetaconsole_setup' => $server_id,
];
} else {
$agente_table_name = 'tagente';
$filter = ['id_agente' => $event['id_agente']];
}
$agente_table_name = 'tagente';
$filter = ['id_agente' => $event['id_agente']];
$ip = db_get_value_filter('direccion', $agente_table_name, $filter);
// If agent does not have an IP, display N/A.
@ -5048,11 +5054,6 @@ function events_get_field_value_by_event_id(
if (strpos($value, '_module_address_') !== false) {
if ($event['id_agentmodule'] != 0) {
if ($meta) {
$server = metaconsole_get_connection_by_id($server_id);
metaconsole_connect($server);
}
$module = db_get_row('tagente_modulo', 'id_agente_modulo', $event['id_agentmodule']);
if (empty($module['ip_target'])) {
$module['ip_target'] = __('N/A');
@ -5062,10 +5063,6 @@ function events_get_field_value_by_event_id(
if (empty($module['nombre'])) {
$module['nombre'] = __('N/A');
}
if ($meta) {
metaconsole_restore_db();
}
} else {
$value = str_replace('_module_address_', __('N/A'), $value);
}
@ -5073,11 +5070,6 @@ function events_get_field_value_by_event_id(
if (strpos($value, '_module_name_') !== false) {
if ($event['id_agentmodule'] != 0) {
if ($meta) {
$server = metaconsole_get_connection_by_id($server_id);
metaconsole_connect($server);
}
$module = db_get_row('tagente_modulo', 'id_agente_modulo', $event['id_agentmodule']);
if (empty($module['ip_target'])) {
$module['ip_target'] = __('N/A');
@ -5088,10 +5080,6 @@ function events_get_field_value_by_event_id(
io_safe_output($module['nombre']),
$value
);
if ($meta) {
metaconsole_restore_db();
}
} else {
$value = str_replace('_module_name_', __('N/A'), $value);
}
@ -5274,33 +5262,49 @@ function events_get_field_value_by_event_id(
function events_get_instructions($event)
{
if (!is_array($event)) {
if (is_array($event) === false) {
return '';
}
switch ($event['event_type']) {
case 'going_unknown':
if ($event['unknown_instructions'] != '') {
$value = str_replace("\n", '<br>', io_safe_output($event['unknown_instructions']));
$value = str_replace(
"\n",
'<br>',
io_safe_output($event['unknown_instructions'])
);
}
break;
case 'going_up_warning':
case 'going_down_warning':
if ($event['warning_instructions'] != '') {
$value = str_replace("\n", '<br>', io_safe_output($event['warning_instructions']));
$value = str_replace(
"\n",
'<br>',
io_safe_output($event['warning_instructions'])
);
}
break;
case 'going_up_critical':
case 'going_down_critical':
if ($event['critical_instructions'] != '') {
$value = str_replace("\n", '<br>', io_safe_output($event['critical_instructions']));
$value = str_replace(
"\n",
'<br>',
io_safe_output($event['critical_instructions'])
);
}
break;
default:
// Not posible.
break;
}
if (!isset($value)) {
if (isset($value) === false) {
return '';
}
@ -5318,7 +5322,11 @@ function events_get_instructions($event)
$output .= '<span id="value_event_'.$event['id_evento'].'" class="nowrap">';
$output .= '<span id="value_event_text_'.$event['id_evento'].'"></span>';
$output .= '<a href="javascript:show_instructions('.$event['id_evento'].')">';
$output .= html_print_image('images/default_list.png', true, ['title' => $over_text]).'</a></span>';
$output .= html_print_image(
'images/default_list.png',
true,
['title' => $over_text]
).'</a></span>';
$output .= '</center>';
return $output;

View File

@ -3519,7 +3519,7 @@ function ui_print_datatable(array $parameters)
type: "POST",
dataSrc: function (json) {
if (json.error) {
console.log(json.error);
console.error(json.error);
$("#error-'.$table_id.'").html(json.error);
$("#error-'.$table_id.'").dialog({
title: "Filter failed",
@ -3535,6 +3535,19 @@ function ui_print_datatable(array $parameters)
}
}).parent().addClass("ui-state-error");
} else {';
if (isset($parameters['ajax_return_operation']) === true
&& empty($parameters['ajax_return_operation']) === false
&& isset($parameters['ajax_return_operation_function']) === true
&& empty($parameters['ajax_return_operation_function']) === false
) {
$js .= '
if (json.'.$parameters['ajax_return_operation'].' !== undefined) {
'.$parameters['ajax_return_operation_function'].'(json.'.$parameters['ajax_return_operation'].');
}
';
}
if (isset($parameters['ajax_postprocess'])) {
$js .= '
if (json.data) {

View File

@ -344,3 +344,32 @@ div.multi-response-buttons {
max-width: 100%;
}
}
#events_buffers_display {
margin-bottom: 10px;
width: fit-content;
}
#events_buffers_display ul {
min-width: 250px;
}
#events_buffers_display li {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
}
#events_buffers_display ul li span.info {
flex: 1 1 100px;
text-align: right;
}
#events_buffers_display ul li span.danger {
color: red;
}
#events_buffers_display ul li span.text {
color: #3f3f3f;
}

View File

@ -352,6 +352,7 @@ if (is_ajax() === true) {
$history
);
$buffers = [];
if (is_metaconsole() === false
|| (is_metaconsole() === true
&& empty($filter['server_id']) === false)
@ -370,6 +371,7 @@ if (is_ajax() === true) {
$count = $count['0']['nitems'];
}
} else {
$buffers = $events['buffers'];
$count = $events['total'];
$events = $events['data'];
}
@ -471,6 +473,7 @@ if (is_ajax() === true) {
echo json_encode(
[
'data' => ($data ?? []),
'buffers' => $buffers,
'recordsTotal' => $count,
'recordsFiltered' => $count,
]
@ -1740,6 +1743,7 @@ try {
// Close.
$active_filters_div .= '</div>';
$active_filters_div .= '<div id="events_buffers_display"></div>';
$table_id = 'events';
$form_id = 'events_form';
@ -1747,23 +1751,23 @@ try {
// Print datatable.
ui_print_datatable(
[
'id' => $table_id,
'class' => 'info_table events',
'style' => 'width: 100%;',
'ajax_url' => 'operation/events/events',
'ajax_data' => [
'id' => $table_id,
'class' => 'info_table events',
'style' => 'width: 100%;',
'ajax_url' => 'operation/events/events',
'ajax_data' => [
'get_events' => 1,
'history' => (int) $history,
],
'form' => [
'form' => [
'id' => $form_id,
'class' => 'flex-row',
'html' => $filter,
'inputs' => [],
'extra_buttons' => $buttons,
],
'extra_html' => $active_filters_div,
'pagination_options' => [
'extra_html' => $active_filters_div,
'pagination_options' => [
[
$config['block_size'],
10,
@ -1785,19 +1789,21 @@ try {
'All',
],
],
'order' => [
'order' => [
'field' => 'timestamp',
'direction' => 'desc',
],
'column_names' => $column_names,
'columns' => $fields,
'no_sortable_columns' => [
'column_names' => $column_names,
'columns' => $fields,
'no_sortable_columns' => [
-1,
-2,
'column-instructions',
],
'ajax_postprocess' => 'process_datatables_item(item)',
'drawCallback' => 'process_datatables_callback(this, settings)',
'ajax_postprocess' => 'process_datatables_item(item)',
'ajax_return_operation' => 'buffers',
'ajax_return_operation_function' => 'process_buffers',
'drawCallback' => 'process_datatables_callback(this, settings)',
]
);
} catch (Exception $e) {
@ -2013,8 +2019,46 @@ function process_datatables_callback(table, settings) {
}
}
function process_datatables_item(item) {
function process_buffers(buffers) {
$('#events_buffers_display').empty();
console.log(buffers);
if(buffers.settings != undefined && buffers.data.length > 0) {
var html = '<h3>'+buffers.settings.translate.nev;
html += ': ('+buffers.settings.total+')</h3>';
html += '<ul>';
Object.entries(buffers.data).forEach(function (element) {
html += '<li>';
html += '<span><b>';
html += buffers.settings.translate.ev+' ';
html += element[0];
html += ': ';
html += '</b></span>';
var class_total = 'info';
var str_total = '';
if(buffers.settings.total == element[1]) {
class_total += ' danger';
str_total = buffers.settings.translate.tevn;
}
html += '<span class='+class_total+'>';
html += element[1];
if(str_total != '') {
html += '<span class="text">';
html += ' '+str_total;
html += '</span>';
}
html += '</span>';
html += '</li>';
});
html += '</ul>';
$('#events_buffers_display').html(html);
}
}
function process_datatables_item(item) {
// Url to go to node from meta.
var server_url = '';
var hashdata = '';