From ee2810e680357b80f2b9e97689b2234ed29078d9 Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Tue, 7 Jun 2022 17:25:51 +0200 Subject: [PATCH] refactor events meta pandora_enterprise#9086 --- pandora_console/include/functions_events.php | 543 ++++++++++-------- .../include/functions_reporting.php | 150 ++--- .../include/functions_reporting_html.php | 36 +- pandora_console/operation/events/events.php | 101 ++-- 4 files changed, 411 insertions(+), 419 deletions(-) diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index ef7c7a3223..d746352d94 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -33,6 +33,7 @@ require_once $config['homedir'].'/vendor/autoload.php'; use Amp\Promise; use PandoraFMS\Enterprise\Metaconsole\Node; +use PandoraFMS\Event; use function Amp\ParallelFunctions\parallelMap; @@ -833,17 +834,39 @@ function events_get_all( } if (empty($filter['event_type']) === false && $filter['event_type'] !== 'all') { - if ($filter['event_type'] === 'warning' - || $filter['event_type'] === 'critical' - || $filter['event_type'] === 'normal' - ) { - $sql_filters[] = ' AND event_type LIKE "%'.$filter['event_type'].'%"'; - } else if ($filter['event_type'] === 'not_normal') { - $sql_filters[] = ' AND (event_type LIKE "%warning%" + if (is_array($filter['event_type']) === true) { + $type = []; + if (in_array('all', $filter['event_type']) === false) { + foreach ($filter['event_type'] as $event_type) { + if ($event_type != '') { + // If normal, warning, could be several + // (going_up_warning, going_down_warning... too complex. + // Shown to user only "warning, critical and normal". + if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') { + $type[] = " event_type LIKE '%".$event_type."%' "; + } else if ($event_type == 'not_normal') { + $type[] = " (event_type LIKE '%warning%' OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%') "; + } else if ($event_type != 'all') { + $type[] = " event_type = '".$event_type."'"; + } + } + } + + $sql_filters[] = ' AND ('.implode(' OR ', $type).')'; + } + } else { + if ($filter['event_type'] === 'warning' + || $filter['event_type'] === 'critical' + || $filter['event_type'] === 'normal' + ) { + $sql_filters[] = ' AND event_type LIKE "%'.$filter['event_type'].'%"'; + } else if ($filter['event_type'] === 'not_normal') { + $sql_filters[] = ' AND (event_type LIKE "%warning%" OR event_type LIKE "%critical%" OR event_type LIKE "%unknown%")'; - } else { - $sql_filters[] = ' AND event_type = "'.$filter['event_type'].'"'; + } else { + $sql_filters[] = ' AND event_type = "'.$filter['event_type'].'"'; + } } } @@ -988,38 +1011,62 @@ function events_get_all( } if (isset($filter['status']) === true) { - switch ($filter['status']) { - case EVENT_ALL: - default: - // Do not filter. - break; + if (is_array($filter['status']) === true) { + $status_all = 0; + foreach ($filter['status'] as $key => $value) { + switch ($value) { + case EVENT_ALL: + $status_all = 1; + break; - case EVENT_NEW: - case EVENT_VALIDATE: - case EVENT_PROCESS: - $sql_filters[] = sprintf( - ' AND estado = %d', - $filter['status'] - ); - break; - - case EVENT_NO_VALIDATED: - // Show comments in validated events. - $validatedState = ''; - if ($validatedEvents === true) { - $validatedState = sprintf( - 'OR estado = %d', - EVENT_VALIDATE - ); + case EVENT_NO_VALIDATED: + $filter['status'][$key] = (EVENT_NEW.', '.EVENT_PROCESS); + default: + // Ignore. + break; } + } + if ($status_all === 0) { $sql_filters[] = sprintf( - ' AND (estado = %d OR estado = %d %s)', - EVENT_NEW, - EVENT_PROCESS, - $validatedState + ' AND estado IN (%s)', + implode(', ', $filter['status']) ); - break; + } + } else { + switch ($filter['status']) { + case EVENT_ALL: + default: + // Do not filter. + break; + + case EVENT_NEW: + case EVENT_VALIDATE: + case EVENT_PROCESS: + $sql_filters[] = sprintf( + ' AND estado = %d', + $filter['status'] + ); + break; + + case EVENT_NO_VALIDATED: + // Show comments in validated events. + $validatedState = ''; + if ($validatedEvents === true) { + $validatedState = sprintf( + 'OR estado = %d', + EVENT_VALIDATE + ); + } + + $sql_filters[] = sprintf( + ' AND (estado = %d OR estado = %d %s)', + EVENT_NEW, + EVENT_PROCESS, + $validatedState + ); + break; + } } } @@ -1039,6 +1086,7 @@ function events_get_all( // Prepare agent join sql filters. $table = 'tevento'; $tevento = 'tevento te'; + // TODO: XXX. $agent_join_filters = []; $tagente_table = 'tagente'; $tagente_field = 'id_agente'; @@ -1054,7 +1102,7 @@ function events_get_all( // Free search. if (empty($filter['search']) === false) { - if (isset($config['dbconnection']->server_version) + if (isset($config['dbconnection']->server_version) === true && $config['dbconnection']->server_version > 50600 ) { // Use "from_base64" requires mysql 5.6 or greater. @@ -1077,6 +1125,19 @@ function events_get_all( ); } + // Free search exclude. + if (empty($filter['search_exclude']) === false) { + $sql_filters[] = vsprintf( + ' AND (lower(ta.alias) not like lower("%%%s%%") + AND te.id_evento not like "%%%s%%" + AND lower(te.evento) not like lower("%%%s%%") + AND lower(te.user_comment) not like lower("%%%s%%") + AND lower(te.id_extra) not like lower("%%%s%%") + AND lower(te.source) not like lower("%%%s%%") )', + array_fill(0, 6, $filter['search_exclude']) + ); + } + // Id extra. if (empty($filter['id_extra']) === false) { $sql_filters[] = sprintf( @@ -1150,7 +1211,7 @@ function events_get_all( if ($user_tags != null) { foreach ($tags as $id_tag) { // User cannot filter with those tags. - if (!array_search($id_tag, $user_tags)) { + if (array_search($id_tag, $user_tags) === false) { return false; } } @@ -1246,7 +1307,7 @@ function events_get_all( } // Filter/ Only alerts. - if (isset($filter['filter_only_alert'])) { + if (isset($filter['filter_only_alert']) === true) { if ($filter['filter_only_alert'] == 0) { $sql_filters[] = ' AND event_type NOT LIKE "%alert%"'; } else if ($filter['filter_only_alert'] == 1) { @@ -1363,9 +1424,18 @@ function events_get_all( } } + // Id server. + $id_server = 0; + if (empty($filter['id_server']) === false) { + $id_server = $filter['id_server']; + } + // Pagination. $pagination = ''; - if (isset($limit, $offset) === true && $limit > 0) { + if (is_metaconsole() === true) { + // TODO: XXX TOTAL 10000 - 10000000. settins meta 300000; TIP. capturra el error. + $pagination = ' LIMIT 10 '; + } else if (isset($limit, $offset) === true && $limit > 0) { $pagination = sprintf(' LIMIT %d OFFSET %d', $limit, $offset); } @@ -1391,7 +1461,7 @@ function events_get_all( $tagente_join = 'INNER'; $group_by = ''; $order_by = events_get_sql_order('te.id_agente', 'asc'); - if (isset($order, $sort_field)) { + if (isset($order, $sort_field) === true) { $order_by .= ','.events_get_sql_order( $sort_field, $order, @@ -1405,12 +1475,12 @@ function events_get_all( $tgrupo_join = 'LEFT'; $tgrupo_join_filters = []; - if (isset($groups) - && (is_array($groups) + if (isset($groups) === true + && (is_array($groups) === true || $groups > 0) ) { $tgrupo_join = 'INNER'; - if (is_array($groups)) { + if (is_array($groups) === true) { $tgrupo_join_filters[] = sprintf( ' (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))', @@ -1429,11 +1499,9 @@ function events_get_all( $tgrupo_join_filters[] = ' te.id_grupo = tg.id_grupo'; } - $server_join = ''; - // Secondary groups. $event_lj = ''; - if (!$user_is_admin || ($user_is_admin && isset($groups) && $groups > 0)) { + if (!$user_is_admin || ($user_is_admin && isset($groups) === true && $groups > 0)) { db_process_sql('SET group_concat_max_len = 9999999'); $event_lj = events_get_secondary_groups_left_join($table); } @@ -1471,7 +1539,6 @@ function events_get_all( %s %s JOIN tgrupo tg ON %s - %s WHERE 1=1 %s %s @@ -1491,7 +1558,6 @@ function events_get_all( join(' ', $agent_join_filters), $tgrupo_join, join(' ', $tgrupo_join_filters), - $server_join, join(' ', $sql_filters), $group_by, $order_by, @@ -1499,65 +1565,10 @@ function events_get_all( $having ); - if ($count !== true) { - if (is_metaconsole() === true) { - $result_meta = []; - $metaconsole_connections = metaconsole_get_names(); - if (isset($metaconsole_connections) === true - && is_array($metaconsole_connections) === true - ) { - try { - $metaconsole_connections = array_flip($metaconsole_connections); - $metaconsole_connections['meta'] = 0; - - hd($metaconsole_connections, true); - hd($sql, true); - - $result_meta = Promise\wait( - parallelMap( - $metaconsole_connections, - function ($node) use ($sql) { - if ($node !== 0) { - $node = new Node((int) $node); - $node->connect(); - } - - $res = db_get_all_rows_sql($sql); - hd($res, true); - if ($res === false) { - $res = []; - } - - if ($node !== 0) { - $node->disconnect(); - } - - return $res; - } - ) - ); - } catch (\Exception $e) { - $e->getReasons(); - } - } - - hd($result_meta, true); - - $data = []; - if (empty($result_meta) === false) { - foreach ($result_meta as $key => $value) { - $data = array_merge($data, $value); - } - } - - return $data; - } - } - if (!$user_is_admin) { // XXX: Confirm there's no extra grants unhandled!. $can_manage = '0 as user_can_manage'; - if (!empty($EM_groups)) { + if (empty($EM_groups) === false) { $can_manage = sprintf( '(tbase.id_grupo IN (%s)) as user_can_manage', join(', ', array_keys($EM_groups)) @@ -1565,7 +1576,7 @@ function events_get_all( } $can_write = '0 as user_can_write'; - if (!empty($EW_groups)) { + if (empty($EW_groups) === false) { $can_write = sprintf( '(tbase.id_grupo IN (%s)) as user_can_write', join(', ', array_keys($EW_groups)) @@ -1591,7 +1602,129 @@ function events_get_all( ('.$sql.') tbase'; } - if ($count === true) { + hd($sql, true); + + if ($count !== true) { + if (is_metaconsole() === true) { + $result_meta = []; + $metaconsole_connections = metaconsole_get_names(); + if (isset($metaconsole_connections) === true + && is_array($metaconsole_connections) === true + ) { + try { + if (empty($id_server) === true) { + $metaconsole_connections = array_flip($metaconsole_connections); + $metaconsole_connections['meta'] = 0; + } else { + $only_id_server[$metaconsole_connections[$id_server]] = $id_server; + $metaconsole_connections = $only_id_server; + } + + $result_meta = Promise\wait( + parallelMap( + $metaconsole_connections, + function ($node) use ($sql) { + if ($node !== 0) { + $node = new Node((int) $node); + $node->connect(); + } + + $res = db_get_all_rows_sql($sql); + if ($res === false) { + $res = []; + } + + if ($node !== 0) { + $node->disconnect(); + } + + return $res; + } + ) + ); + } catch (\Exception $e) { + $e->getReasons(); + } + } + + $data = []; + if (empty($result_meta) === false) { + foreach ($result_meta as $node => $value) { + if (empty($value) === false) { + foreach ($value as $k => $v) { + $value[$k]['server_id'] = $metaconsole_connections[$node]; + $value[$k]['server_name'] = $node; + } + + $data = array_merge($data, $value); + } + } + } + + // TODO: XXX; + hd($sort_field, true); + hd($order, true); + + if ($sort_field !== 'agent_name' + && $sort_field !== 'server_name' + ) { + $sort_field = explode('.', $sort_field)[1]; + if ($sort_field === 'user_comment') { + $sort_field = 'comments'; + } + } + + usort( + $data, + function ($a, $b) use ($sort_field, $order) { + switch ($sort_field) { + default: + case 'utimestamp': + case 'criticity': + case 'estado': + if ($a[$sort_field] === $b[$sort_field]) { + $res = 0; + } else if ($a[$sort_field] > $b[$sort_field]) { + $res = ($order === 'asc') ? 1 : (-1); + } else { + $res = ($order === 'asc') ? (-1) : 1; + } + break; + case 'evento': + case 'agent_name': + case 'timestamp': + case 'tags': + case 'comments': + case 'server_name': + if ($order === 'asc') { + $res = strcasecmp($a[$sort_field], $b[$sort_field]); + } else { + $res = strcasecmp($b[$sort_field], $a[$sort_field]); + } + break; + } + + return $res; + } + ); + + if (isset($limit, $offset) === true && $limit > 0) { + $count = count($data); + $end = ((int) $offset !== 0) ? ($offset + $limit) : $limit; + $finally = array_slice($data, $offset, $end, true); + + $return = [ + 'data' => $finally, + 'total' => $count, + ]; + } else { + // TODO: XXX limit * nodes. + $return = array_slice($data, 0, 1000000, true); + } + + return $return; + } + } else { $sql = 'SELECT count(*) as nitems FROM ('.$sql.') tt'; } @@ -1719,6 +1852,8 @@ function events_get_events_no_grouped( return $sql; } + hd($sql); + $events = db_get_all_rows_sql($sql, $history); return $events; @@ -2991,10 +3126,10 @@ function events_get_group_events_steps( * @param integer $date Beginning date to get events. * @param boolean $history History. * @param boolean $show_summary_group Show_summary_group. - * @param boolean $filter_event_severity Filter_event_severity. - * @param boolean $filter_event_type Filter_event_type. - * @param boolean $filter_event_status Filter_event_status. - * @param boolean $filter_event_filter_search Filter_event_filter_search. + * @param array $filter_event_severity Filter_event_severity. + * @param array $filter_event_type Filter_event_type. + * @param array $filter_event_status Filter_event_status. + * @param string $filter_event_filter_search Filter_event_filter_search. * @param boolean $id_group Id_group. * @param boolean $events_group Events_group. * @param boolean $id_agent_module Id_agent_module. @@ -3011,10 +3146,10 @@ function events_get_agent( $date=0, $history=false, $show_summary_group=false, - $filter_event_severity=false, - $filter_event_type=false, - $filter_event_status=false, - $filter_event_filter_search=false, + $filter_event_severity=[], + $filter_event_type=[], + $filter_event_status=[], + $filter_event_filter_search='', $id_group=false, $events_group=false, $id_agent_module=false, @@ -3024,6 +3159,13 @@ function events_get_agent( ) { global $config; + $filters = []; + // Id Agent. + if ($id_agent !== false && empty($id_agent) === false) { + $filters['id_agent'] = $id_agent; + } + + // Date. if (is_numeric($date) === false) { $date = time_w_fixed_tz($date); } @@ -3032,153 +3174,80 @@ function events_get_agent( $date = get_system_time(); } - if ($events_group) { - $id_group = groups_safe_acl($config['id_user'], $id_group, 'ER'); - - if (empty($id_group)) { - // An empty array means the user doesn't have access. - return false; - } - } - $datelimit = ($date - $period); + $filters['date_from'] = date('Y-m-d', $datelimit); + $filters['date_to'] = date('Y-m-d', $date); + $filters['time_from'] = date('H:i:s', $datelimit); + $filters['time_to'] = date('H:i:s', $date); - $sql_where = ''; - $severity_all = 0; - if (!empty($filter_event_severity)) { - foreach ($filter_event_severity as $key => $value) { - switch ($value) { - case -1: - $severity_all = 1; - break; - - case 34: - $filter_event_severity[$key] = '3, 4'; - break; - - case 20: - $filter_event_severity[$key] = '0, 1, 3, 4, 5, 6'; - break; - - case 21: - $filter_event_severity[$key] = '4, 2'; - break; - - default: - // Ignore. - break; - } - } - - if (!$severity_all) { - $sql_where .= ' AND criticity IN ('.implode(', ', $filter_event_severity).')'; - } + // Severity. + if (empty($filter_event_severity) === false) { + $filters['severity'] = $filter_event_severity; } - $status_all = 0; - if (!empty($filter_event_status)) { - foreach ($filter_event_status as $key => $value) { - switch ($value) { - case -1: - $status_all = 1; - break; - - case 3: - $filter_event_status[$key] = ('0, 2'); - default: - // Ignore. - break; - } - } - - if (!$status_all) { - $sql_where .= ' AND estado IN ('.implode( - ', ', - $filter_event_status - ).')'; - } + // Type. + if (empty($filter_event_type) === false) { + $filters['event_type'] = $filter_event_type; } - if (!empty($filter_event_type) && $filter_event_type[0] != 'all') { - $sql_where .= ' AND ('; - $type = []; - foreach ($filter_event_type as $event_type) { - if ($event_type != '') { - // If normal, warning, could be several - // (going_up_warning, going_down_warning... too complex. - // Shown to user only "warning, critical and normal". - if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') { - $type[] = " event_type LIKE '%".$event_type."%' "; - } else if ($event_type == 'not_normal') { - $type[] = " (event_type LIKE '%warning%' OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%') "; - } else if ($event_type != 'all') { - $type[] = " event_type = '".$event_type."'"; - } - } - } - - $sql_where .= implode(' OR ', $type).')'; + // Status. + if (empty($filter_event_status) === false) { + $filters['status'] = $filter_event_status; } - if (!empty($filter_event_filter_search)) { - $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; + // ID group. + if (empty($id_group) === false) { + $filters['id_group_filter'] = $id_group; } - if (!empty($filter_event_filter_exclude)) { - $sql_where .= ' AND (evento NOT LIKE "%'.io_safe_input($filter_event_filter_exclude).'%" AND id_evento NOT LIKE "%'.io_safe_input($filter_event_filter_exclude).'%")'; + // Filter search. + if (empty($filter_event_filter_search) === false) { + $filters['search'] = $filter_event_filter_search; } - if ($events_group) { - $secondary_groups = sprintf( - ' INNER JOIN tgrupo tg - ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) - OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s)) - WHERE utimestamp > %d - AND utimestamp <= %d ', - join(',', $id_group), - join(',', $id_group), - $datelimit, - $date - ); - $sql_where = $secondary_groups.' '.$sql_where; - } else { - $sql_where = ' WHERE 1=1 '.$sql_where; - - if ($events_module) { - $sql_where .= sprintf( - ' AND id_agentmodule = %d AND utimestamp > %d - AND utimestamp <= %d ', - $id_agent_module, - $datelimit, - $date - ); - } else { - $sql_where .= sprintf( - ' AND id_agente = %d AND utimestamp > %d - AND utimestamp <= %d ', - $id_agent, - $datelimit, - $date - ); - } + // Filter search exclude. + if (empty($filter_event_filter_exclude) === false) { + $filters['search_exclude'] = $filter_event_filter_exclude; } + if (empty($id_agent_module) === false) { + // TODO: XXX por id_agente_modulo. + $filters['module_search'] = modules_get_agentmodule_name($id_agent_module); + } + + if (empty($id_server) === false) { + $filters['id_server'] = $id_server; + } + + // Group by agent. + // TODO: XXX constants. + $filters['group_rep'] = 2; + + $max_register = 1000; + + // TODO: XXX if ($show_summary_group) { + hd('tt'); return events_get_events_grouped( $sql_where, 0, - 1000, + $max_register, is_metaconsole(), false, false, $history ); } else { - return events_get_events_no_grouped( - $sql_where, - (is_metaconsole() === true && (int) $id_server === 0) ? true : false, - $history + $events = Event::search( + ['te.*'], + $filters, + 0, + $max_register, + 'desc', + 'te.utimestamp' ); + + return $events['data']; } } diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 6bf29c1235..1a7faa6516 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -1978,6 +1978,10 @@ function reporting_event_report_group( } } + if (is_metaconsole() === true) { + metaconsole_restore_db(); + } + $data = events_get_agent( false, $content['period'], @@ -3785,12 +3789,14 @@ function reporting_event_report_agent( $event_graph_by_criticity = $style['event_graph_by_criticity']; $event_graph_validated_vs_unvalidated = $style['event_graph_validated_vs_unvalidated']; + if (is_metaconsole() === true) { + metaconsole_restore_db(); + } + $return['data'] = reporting_get_agents_detailed_event( $content['id_agent'], $content['period'], $report['datetime'], - true, - true, $history, $show_summary_group, $filter_event_severity, @@ -3801,10 +3807,6 @@ function reporting_event_report_agent( $id_server ); - if (is_metaconsole() === true) { - metaconsole_restore_db(); - } - reporting_set_conf_charts( $width, $height, @@ -10585,8 +10587,6 @@ function reporting_get_agents_detailed_event( $id_agents, $period=0, $date=0, - $return=false, - $only_data=false, $history=false, $show_summary_group=false, $filter_event_severity=false, @@ -10598,10 +10598,7 @@ function reporting_get_agents_detailed_event( ) { global $config; - if ($only_data) { - $return_data = []; - } - + $return_data = []; $id_agents = (array) safe_int($id_agents, 1); if (!is_numeric($date)) { @@ -10612,8 +10609,6 @@ function reporting_get_agents_detailed_event( $date = get_system_time(); } - $events = []; - foreach ($id_agents as $id_agent) { $event = events_get_agent( $id_agent, @@ -10637,112 +10632,35 @@ function reporting_get_agents_detailed_event( $event = []; } - if ($only_data) { - $nevents = count($event); - for ($i = ($nevents - 1); $i >= 0; $i--) { - $e = $event[$i]; - if ($show_summary_group) { - $return_data[] = [ - 'status' => $e['estado'], - 'count' => $e['event_rep'], - 'name' => $e['evento'], - 'type' => $e['event_type'], - 'criticity' => $e['criticity'], - 'validated_by' => $e['id_usuario'], - 'timestamp' => $e['timestamp_rep'], - 'id_evento' => $e['id_evento'], - ]; - } else { - $return_data[] = [ - 'status' => $e['estado'], - 'name' => $e['evento'], - 'type' => $e['event_type'], - 'criticity' => $e['criticity'], - 'validated_by' => $e['id_usuario'], - 'timestamp' => $e['timestamp'], - 'id_evento' => $e['id_evento'], - ]; - } - } - } else { - if (!empty($event)) { - array_push($events, $event); + $nevents = count($event); + for ($i = ($nevents - 1); $i >= 0; $i--) { + $e = $event[$i]; + if ($show_summary_group) { + $return_data[] = [ + 'status' => $e['estado'], + 'count' => $e['event_rep'], + 'name' => $e['evento'], + 'type' => $e['event_type'], + 'criticity' => $e['criticity'], + 'validated_by' => $e['id_usuario'], + 'timestamp' => $e['timestamp_rep'], + 'id_evento' => $e['id_evento'], + ]; + } else { + $return_data[] = [ + 'status' => $e['estado'], + 'name' => $e['evento'], + 'type' => $e['event_type'], + 'criticity' => $e['criticity'], + 'validated_by' => $e['id_usuario'], + 'timestamp' => $e['timestamp'], + 'id_evento' => $e['id_evento'], + ]; } } } - if ($only_data) { - return $return_data; - } - - if ($events) { - $note = ''; - if (count($events) >= 1000) { - $note .= '* '.__('Maximum of events shown').' (1000)
'; - } - - foreach ($events as $eventRow) { - foreach ($eventRow as $k => $event) { - // First pass along the class of this row. - $table->cellclass[$k][1] = $table->cellclass[$k][2] = $table->cellclass[$k][4] = $table->cellclass[$k][5] = $table->cellclass[$k][6] = get_priority_class($event['criticity']); - - $data = []; - // Colored box. - switch ($event['estado']) { - case 0: - $img_st = 'images/star.png'; - $title_st = __('New event'); - break; - - case 1: - $img_st = 'images/tick.png'; - $title_st = __('Event validated'); - break; - - case 2: - $img_st = 'images/hourglass.png'; - $title_st = __('Event in process'); - break; - } - - $data[] = html_print_image( - $img_st, - true, - [ - 'class' => 'image_status invert filter', - 'width' => 16, - 'title' => $title_st, - ] - ); - - $data[] = $event['event_rep']; - - $data[] = ui_print_truncate_text( - io_safe_output($event['evento']), - 140, - false, - true - ); - // $data[] = $event['event_type']; - $data[] = events_print_type_img($event['event_type'], true); - - $data[] = get_priority_name($event['criticity']); - if (empty($event['id_usuario']) && $event['estado'] == EVENT_VALIDATE) { - $data[] = ''.__('System').''; - } else { - $user_name = db_get_value('fullname', 'tusuario', 'id_user', $event['id_usuario']); - $data[] = io_safe_output($user_name); - } - - $data[] = ''.date($config['date_format'], $event['timestamp_rep']).''; - array_push($table->data, $data); - } - } - } - - if ($events) { - return html_print_table($table, $return).$note; - } + return $return_data; } diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php index cd6af07794..25620b2de6 100644 --- a/pandora_console/include/functions_reporting_html.php +++ b/pandora_console/include/functions_reporting_html.php @@ -5898,7 +5898,7 @@ function reporting_get_event_histogram($events, $text_header_event=false) } -function reporting_get_event_histogram_meta($width) +function reporting_get_event_histogram_meta($width, $events) { global $config; if (!defined('METACONSOLE')) { @@ -5941,21 +5941,6 @@ function reporting_get_event_histogram_meta($width) EVENT_CRIT_CRITICAL => COL_CRITICAL, ]; - $user_groups = users_get_groups($config['id_user'], 'ER'); - $user_groups_ids = array_keys($user_groups); - - if (empty($user_groups)) { - $groups_condition = ' AND 1 = 0 '; - } else { - $groups_condition = ' AND id_grupo IN ('.implode(',', $user_groups_ids).') '; - } - - if (!check_acl($config['id_user'], 0, 'PM')) { - $groups_condition .= ' AND id_grupo != 0'; - } - - $status_condition = ' AND estado = 0 '; - $cont = 0; for ($i = 0; $i < $interval; $i++) { $bottom = ($datelimit + ($periodtime * $i)); @@ -5979,23 +5964,12 @@ function reporting_get_event_histogram_meta($width) $top = ($datelimit + ($periodtime * ($i + 1))); - $time_condition = 'utimestamp > '.$bottom.' AND utimestamp < '.$top; - $sql = sprintf( - 'SELECT criticity,utimestamp - FROM tmetaconsole_event - WHERE %s %s %s - ORDER BY criticity DESC', - $time_condition, - $groups_condition, - $status_condition - ); - - $events = db_get_all_rows_sql($sql); - $events_criticity = []; if (is_array($events)) { - foreach ($events as $key => $value) { - array_push($events_criticity, $value['criticity']); + foreach ($events as $value) { + if ($value['utimestamp'] >= $bottom && $value['utimestamp'] < $top) { + array_push($events_criticity, $value['criticity']); + } } } diff --git a/pandora_console/operation/events/events.php b/pandora_console/operation/events/events.php index 2bb5c32488..c3312a0db3 100644 --- a/pandora_console/operation/events/events.php +++ b/pandora_console/operation/events/events.php @@ -266,12 +266,15 @@ if (is_metaconsole() === true) { // Ajax responses. if (is_ajax() === true) { - $get_events = get_parameter('get_events', 0); + $get_events = (int) get_parameter('get_events', 0); // Datatables offset, limit. $start = get_parameter('start', 0); - $length = get_parameter('length', $config['block_size']); + $length = get_parameter( + 'length', + $config['block_size'] + ); - if ($get_events) { + if ($get_events !== 0) { try { ob_start(); @@ -313,8 +316,17 @@ if (is_ajax() === true) { // Find the order field and set the table and field name. foreach ($fields as $field) { if (str_contains($field, $order['field']) === true) { - $order['field'] = $field; - break; + switch ($field) { + case 'ta.alias as agent_name': + $order['field'] = 'agent_name'; + break; + + default: + $order['field'] = $field; + break; + } + + continue; } } @@ -339,56 +351,75 @@ if (is_ajax() === true) { // History. $history ); - // $count = events_get_all( - // 'count', - // $filter, - // null, - // null, - // null, - // null, - // $history - // ); - // - // if ($count !== false) { - // $count = $count['0']['nitems']; - // } - $count = count($events); - if ($events) { + if (is_metaconsole() === false) { + $count = events_get_all( + 'count', + $filter, + null, + null, + null, + null, + $history + ); + + if ($count !== false) { + $count = $count['0']['nitems']; + } + } else { + $count = $events['total']; + $events = $events['data']; + } + + if (empty($events) === false) { $data = array_reduce( $events, function ($carry, $item) { global $config; $tmp = (object) $item; - // $tmp->meta = is_metaconsole(); + $tmp->meta = is_metaconsole(); //// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps - // if ($tmp->meta === true) { - // if ($tmp->server_name !== null) { - // $tmp->data_server = metaconsole_get_servers($tmp->server_id); - // $tmp->server_url_hash = metaconsole_get_servers_url_hash($tmp->data_server); - // } - // } - $tmp->evento = str_replace('"', '', io_safe_output($tmp->evento)); - if (strlen($tmp->evento) >= 255) { - $tmp->evento = ui_print_truncate_text($tmp->evento, 255, $tmp->evento, true, false); + if ($tmp->meta === true) { + if ($tmp->server_name !== null) { + $tmp->data_server = metaconsole_get_servers( + $tmp->server_id + ); + $tmp->server_url_hash = metaconsole_get_servers_url_hash( + $tmp->data_server + ); + } } - if ($tmp->module_name) { + $tmp->evento = str_replace('"', '', io_safe_output($tmp->evento)); + if (strlen($tmp->evento) >= 255) { + $tmp->evento = ui_print_truncate_text( + $tmp->evento, + 255, + $tmp->evento, + true, + false + ); + } + + if (empty($tmp->module_name) === false) { $tmp->module_name = io_safe_output($tmp->module_name); } - if ($tmp->comments) { + if (empty($tmp->comments) === false) { $tmp->comments = ui_print_comments($tmp->comments); } // Show last event. - if (isset($tmp->max_id_evento) && $tmp->max_id_evento !== $tmp->id_evento) { + if (isset($tmp->max_id_evento) === true + && $tmp->max_id_evento !== $tmp->id_evento + ) { $max_event = db_get_row_sql( sprintf( - 'SELECT criticity, timestamp FROM %s + 'SELECT criticity, + `timestamp` + FROM tevento WHERE id_evento = %s', - ($tmp->meta === true) ? 'tmetaconsole_event' : 'tevento', $tmp->max_id_evento ) );