From 5d4f90ca10aee38442226cd6b62d256b43578684 Mon Sep 17 00:00:00 2001 From: miguel angel rasteu Date: Wed, 21 Jun 2023 16:10:13 +0200 Subject: [PATCH] #11550 Fix tactical view widget --- pandora_console/include/ajax/events.php | 39 ++++- .../include/functions_reporting.php | 7 +- .../include/functions_reporting_html.php | 11 +- pandora_console/include/functions_servers.php | 158 ++++++++++++++---- .../include/functions_tactical.php | 32 +++- .../lib/Dashboard/Widgets/tactical.php | 7 +- 6 files changed, 206 insertions(+), 48 deletions(-) diff --git a/pandora_console/include/ajax/events.php b/pandora_console/include/ajax/events.php index f03c8af06b..20f2ee01a2 100644 --- a/pandora_console/include/ajax/events.php +++ b/pandora_console/include/ajax/events.php @@ -73,6 +73,7 @@ $meta = get_parameter('meta', 0); $history = get_parameter('history', 0); $table_events = get_parameter('table_events', 0); $total_events = (bool) get_parameter('total_events'); +$filter_groups = (string) get_parameter('filter_groups', ''); $total_event_graph = (bool) get_parameter('total_event_graph'); $graphic_event_group = (bool) get_parameter('graphic_event_group'); $get_table_response_command = (bool) get_parameter('get_table_response_command'); @@ -2109,12 +2110,42 @@ if ($table_events) { if ($total_events) { global $config; - $sql_count_event = 'SELECT SQL_NO_CACHE COUNT(id_evento) FROM tevento '; - if ($config['event_view_hr']) { - $sql_count_event .= 'WHERE utimestamp > (UNIX_TIMESTAMP(NOW()) - '.($config['event_view_hr'] * SECONDS_1HOUR).')'; + if (is_metaconsole() === true) { + $system_events = 0; + $servers = metaconsole_get_servers(); + + // Check if the group can be deleted or not. + if (isset($servers) === true + && is_array($servers) === true + ) { + foreach ($servers as $server) { + if (metaconsole_connect($server) == NOERR) { + $sql_count_event = 'SELECT SQL_NO_CACHE COUNT(id_evento) FROM tevento '; + if ($config['event_view_hr']) { + $sql_count_event .= 'WHERE utimestamp > (UNIX_TIMESTAMP(NOW()) - '.($config['event_view_hr'] * SECONDS_1HOUR).')'; + if ($filter_groups !== '') { + $sql_count_event .= ' AND id_grupo in ('.$filter_groups.')'; + } + } + + $system_events += db_get_value_sql($sql_count_event); + } + + metaconsole_restore_db(); + } + } + } else { + $sql_count_event = 'SELECT SQL_NO_CACHE COUNT(id_evento) FROM tevento '; + if ($config['event_view_hr']) { + $sql_count_event .= 'WHERE utimestamp > (UNIX_TIMESTAMP(NOW()) - '.($config['event_view_hr'] * SECONDS_1HOUR).')'; + if ($filter_groups !== '') { + $sql_count_event .= ' AND id_grupo in ('.$filter_groups.')'; + } + } + + $system_events = db_get_value_sql($sql_count_event); } - $system_events = db_get_value_sql($sql_count_event); echo $system_events; return; } diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 18b5b9eda6..457107a850 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -14696,11 +14696,11 @@ function reporting_get_agentmodule_sla_day($id_agent_module, $period=0, $min_val } -function reporting_get_stats_servers() +function reporting_get_stats_servers($filter=[]) { global $config; - $server_performance = servers_get_performance(); + $server_performance = servers_get_performance($filter); // Alerts table $table_srv = html_get_predefined_table(); @@ -14836,6 +14836,9 @@ function reporting_get_stats_servers() $output .= 'var parameters = {};'; $output .= 'parameters["page"] = "include/ajax/events";'; $output .= 'parameters["total_events"] = 1;'; + if (empty($filter) === false && empty($filter['groups']) === false) { + $output .= 'parameters["filter_groups"] = "'.$filter['groups'].'";'; + } $output .= '$.ajax({type: "GET",url: "'.ui_get_full_url('ajax.php', false, false, false).'",data: parameters,'; $output .= 'success: function(data) {'; diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php index 60b70c751d..5521024cb0 100644 --- a/pandora_console/include/functions_reporting_html.php +++ b/pandora_console/include/functions_reporting_html.php @@ -5255,6 +5255,12 @@ function reporting_get_stats_summary($data, $graph_width, $graph_height) if ($data['monitor_checks'] > 0) { // Fixed width non interactive charts. $status_chart_width = $graph_width; + $monitor_data = []; + $monitor_data['monitor_critical'] = $data['monitor_critical']; + $monitor_data['monitor_warning'] = $data['monitor_warning']; + $monitor_data['monitor_ok'] = $data['monitor_ok']; + $monitor_data['monitor_unknown'] = $data['monitor_unknown']; + $monitor_data['monitor_not_init'] = $data['monitor_not_init']; $tdata[0] = '
'; $tdata[0] .= '
'; @@ -5263,12 +5269,13 @@ function reporting_get_stats_summary($data, $graph_width, $graph_height) $graph_width, $graph_height, true, - true + true, + $monitor_data ); $tdata[0] .= '
'; $tdata[0] .= '
'; } else { - $tdata[2] = html_print_image( + $tdata[0] = html_print_image( 'images/image_problem_area_small.png', true, ['width' => $graph_width] diff --git a/pandora_console/include/functions_servers.php b/pandora_console/include/functions_servers.php index 7ba3c50303..a56379990c 100644 --- a/pandora_console/include/functions_servers.php +++ b/pandora_console/include/functions_servers.php @@ -155,10 +155,16 @@ function servers_get_total_modules() * * @return array with several data. */ -function servers_get_performance() +function servers_get_performance($filter=[]) { global $config; + if (empty($filter) === false && empty($filter['groups']) === false && $filter['groups'] !== 0) { + $filter_group = 'AND tagente.id_grupo IN ('.$filter['groups'].')'; + } else { + $filter_group = ''; + } + $data = []; $data['total_modules'] = 0; $data['total_remote_modules'] = 0; @@ -170,18 +176,58 @@ function servers_get_performance() $data['network_modules_rate'] = 0; if ($config['realtimestats'] == 1) { - $counts = db_get_all_rows_sql( - 'SELECT tagente_modulo.id_modulo, - COUNT(tagente_modulo.id_agente_modulo) modules - FROM tagente_modulo, tagente_estado, tagente - WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo - AND tagente.id_agente = tagente_estado.id_agente - AND tagente_modulo.disabled = 0 - AND tagente_modulo.id_modulo <> 0 - AND delete_pending = 0 - AND tagente.disabled = 0 - GROUP BY tagente_modulo.id_modulo' - ); + if (is_metaconsole() === true) { + $counts = []; + $servers = metaconsole_get_servers(); + + // Check if the group can be deleted or not. + if (isset($servers) === true + && is_array($servers) === true + ) { + foreach ($servers as $server) { + if (metaconsole_connect($server) == NOERR) { + $meta_counts = db_get_all_rows_sql( + 'SELECT tagente_modulo.id_modulo, + COUNT(tagente_modulo.id_agente_modulo) modules + FROM tagente_modulo, tagente_estado, tagente + WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo + AND tagente.id_agente = tagente_estado.id_agente + AND tagente_modulo.disabled = 0 + AND tagente_modulo.id_modulo <> 0 + AND delete_pending = 0 + AND tagente.disabled = 0 + '.$filter_group.' + GROUP BY tagente_modulo.id_modulo' + ); + foreach ($meta_counts as $key => $val) { + if (array_key_exists($key, $counts)) { + if ($meta_counts[$key]['id_modulo'] == $counts[$key]['id_modulo']) { + $counts[$key]['modules'] += $meta_counts[$key]['modules']; + } + } else { + $counts[$key] = $meta_counts[$key]; + } + } + } + + metaconsole_restore_db(); + } + } + } else { + $counts = db_get_all_rows_sql( + 'SELECT tagente_modulo.id_modulo, + COUNT(tagente_modulo.id_agente_modulo) modules + FROM tagente_modulo, tagente_estado, tagente + WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo + AND tagente.id_agente = tagente_estado.id_agente + AND tagente_modulo.disabled = 0 + AND tagente_modulo.id_modulo <> 0 + AND delete_pending = 0 + AND tagente.disabled = 0 + '.$filter_group.' + GROUP BY tagente_modulo.id_modulo' + ); + } if (empty($counts)) { $counts = []; @@ -284,28 +330,76 @@ function servers_get_performance() } $interval_avgs = []; + if (is_metaconsole() === true) { + $interval_avgs_modules = []; + $servers = metaconsole_get_servers(); - // Avg of modules interval when modules have module_interval > 0. - $interval_avgs_modules = db_get_all_rows_sql( - 'SELECT count(tagente_modulo.id_modulo) modules , - tagente_modulo.id_modulo, - AVG(tagente_modulo.module_interval) avg_interval - FROM tagente_modulo, tagente_estado, tagente - WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo - AND tagente_modulo.disabled = 0 - AND module_interval > 0 - AND (utimestamp > 0 OR ( - id_tipo_modulo = 100 - OR (id_tipo_modulo > 21 - AND id_tipo_modulo < 23 + // Check if the group can be deleted or not. + if (isset($servers) === true + && is_array($servers) === true + ) { + foreach ($servers as $server) { + if (metaconsole_connect($server) == NOERR) { + // Avg of modules interval when modules have module_interval > 0. + $meta_interval_avgs_modules = db_get_all_rows_sql( + 'SELECT count(tagente_modulo.id_modulo) modules , + tagente_modulo.id_modulo, + AVG(tagente_modulo.module_interval) avg_interval + FROM tagente_modulo, tagente_estado, tagente + WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo + AND tagente_modulo.disabled = 0 + AND module_interval > 0 + AND (utimestamp > 0 OR ( + id_tipo_modulo = 100 + OR (id_tipo_modulo > 21 + AND id_tipo_modulo < 23 + ) + ) + ) + AND delete_pending = 0 + AND tagente.disabled = 0 + AND tagente.id_agente = tagente_estado.id_agente + GROUP BY tagente_modulo.id_modulo' + ); + + foreach ($meta_interval_avgs_modules as $key => $val) { + if (array_key_exists($key, $interval_avgs_modules)) { + if ($meta_interval_avgs_modules[$key]['id_modulo'] == $interval_avgs_modules[$key]['id_modulo']) { + $interval_avgs_modules[$key]['modules'] += $meta_interval_avgs_modules[$key]['modules']; + $interval_avgs_modules[$key]['avg_interval'] += $meta_interval_avgs_modules[$key]['avg_interval']; + } + } else { + $interval_avgs_modules[$key] = $meta_interval_avgs_modules[$key]; + } + } + } + + metaconsole_restore_db(); + } + } + } else { + // Avg of modules interval when modules have module_interval > 0. + $interval_avgs_modules = db_get_all_rows_sql( + 'SELECT count(tagente_modulo.id_modulo) modules , + tagente_modulo.id_modulo, + AVG(tagente_modulo.module_interval) avg_interval + FROM tagente_modulo, tagente_estado, tagente + WHERE tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo + AND tagente_modulo.disabled = 0 + AND module_interval > 0 + AND (utimestamp > 0 OR ( + id_tipo_modulo = 100 + OR (id_tipo_modulo > 21 + AND id_tipo_modulo < 23 + ) ) ) - ) - AND delete_pending = 0 - AND tagente.disabled = 0 - AND tagente.id_agente = tagente_estado.id_agente - GROUP BY tagente_modulo.id_modulo' - ); + AND delete_pending = 0 + AND tagente.disabled = 0 + AND tagente.id_agente = tagente_estado.id_agente + GROUP BY tagente_modulo.id_modulo' + ); + } if (empty($interval_avgs_modules)) { $interval_avgs_modules = []; diff --git a/pandora_console/include/functions_tactical.php b/pandora_console/include/functions_tactical.php index 8b8575b8d7..223ea01030 100644 --- a/pandora_console/include/functions_tactical.php +++ b/pandora_console/include/functions_tactical.php @@ -293,6 +293,21 @@ function tactical_get_data( $list['_module_sanity_'] = 100; } + $list['_monitors_alerts_'] = 0; + $servers = metaconsole_get_servers(); + + if (isset($servers) === true + && is_array($servers) === true + ) { + foreach ($servers as $server) { + if (metaconsole_connect($server) == NOERR) { + $list['_monitors_alerts_'] += tactical_monitor_alerts($user_strict, $user_groups_ids); + } + + metaconsole_restore_db(); + } + } + if (isset($list['_alerts_'])) { if ($list['_monitors_alerts_fired_'] > 0 && $list['_alerts_'] > 0) { $list['_alert_level_'] = format_numeric((100 - ($list['_monitors_alerts_fired_'] / ($list['_alerts_'] / 100))), 1); @@ -476,13 +491,12 @@ function tactical_get_data( } $list['_monitors_alerts_fired_'] = tactical_monitor_fired_alerts(explode(',', $user_groups_ids), $user_strict, explode(',', $user_groups_ids)); - $list['_monitors_alerts_'] = tactical_monitor_alerts($user_strict); + $list['_monitors_alerts_'] = tactical_monitor_alerts($user_strict, $user_groups_ids); $filter_agents = []; - if (users_is_admin() === false) { + // if (users_is_admin() === false) { $filter_agents = ['id_grupo' => explode(',', $user_groups_ids)]; - } - + // } $total_agentes = agents_get_agents( $filter_agents, ['count(DISTINCT id_agente) as total_agents'], @@ -537,11 +551,15 @@ function tactical_status_modules_agents($id_user=false, $user_strict=false, $acc } -function tactical_monitor_alerts($strict_user=false) +function tactical_monitor_alerts($strict_user=false, $groups_ids='') { global $config; - $groups = users_get_groups($config['id_user'], 'AR', false); - $id_groups = array_keys($groups); + if ($groups_ids === '') { + $groups = users_get_groups($config['id_user'], 'AR', false); + $id_groups = array_keys($groups); + } else { + $id_groups = explode(',', $groups_ids); + } $where_clause = ''; if (empty($id_groups) === true) { diff --git a/pandora_console/include/lib/Dashboard/Widgets/tactical.php b/pandora_console/include/lib/Dashboard/Widgets/tactical.php index ffd5079f3b..b05085c0fc 100755 --- a/pandora_console/include/lib/Dashboard/Widgets/tactical.php +++ b/pandora_console/include/lib/Dashboard/Widgets/tactical.php @@ -382,6 +382,10 @@ class TacticalWidget extends Widget $data = []; + if (isset($all_data['_monitor_total_']) === false) { + $all_data['_monitor_total_'] = (int) $all_data['_monitor_checks_']; + } + $data['monitor_not_init'] = (int) $all_data['_monitors_not_init_']; $data['monitor_unknown'] = (int) $all_data['_monitors_unknown_']; $data['monitor_ok'] = (int) $all_data['_monitors_ok_']; @@ -393,6 +397,7 @@ class TacticalWidget extends Widget $data['monitor_total'] = (int) $all_data['_monitor_total_']; $data['total_agents'] = (int) $all_data['_total_agents_']; + $data['groups'] = $this->values['groupId'][0]; $data['monitor_checks'] = (int) $all_data['_monitor_checks_']; @@ -505,7 +510,7 @@ class TacticalWidget extends Widget $table->data = []; $table->style = []; - $table->data[0][0] = \reporting_get_stats_servers(); + $table->data[0][0] = \reporting_get_stats_servers($data); $output .= \html_print_table($table, true); }