From 0deff70f8bab1b634fd5632df6c923edce3dc8d1 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 7 Oct 2020 14:07:17 +0200 Subject: [PATCH] Alert report (group) Group + secondary groups + recursion --- pandora_console/include/functions_alerts.php | 80 +++++++++++++++++++ pandora_console/include/functions_events.php | 14 ++-- .../include/functions_reporting.php | 28 +------ 3 files changed, 91 insertions(+), 31 deletions(-) diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index f4bd91645e..2566e265be 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -2830,3 +2830,83 @@ function alerts_ui_update_or_create_actions($update=true) $update ? __('Could not be updated') : __('Could not be created') ); } + + +/** + * Retrieve all agent_modules with configured alerts filtered by group. + * + * @param integer|null $id_grupo Filter by group. + * @param boolean $recursion Filter by group recursive. + * + * @return array With agent module ids. + */ +function alerts_get_agent_modules( + ?int $id_grupo, + bool $recursion=false +) : array { + if ($id_grupo === null) { + $agent_modules = db_get_all_rows_sql( + 'SELECT distinct(atm.id_agent_module) + FROM talert_template_modules atm + INNER JOIN tagente_modulo am + ON am.id_agente_modulo = atm.id_agent_module + WHERE atm.disabled = 0' + ); + } else if ($recursion !== true) { + $sql = sprintf( + 'SELECT distinct(atm.id_agent_module) + FROM talert_template_modules atm + INNER JOIN tagente_modulo am + ON am.id_agente_modulo = atm.id_agent_module + INNER JOIN tagente ta + ON am.id_agente = ta.id_agente + LEFT JOIN tagent_secondary_group tasg + ON tasg.id_agent = ta.id_agente + WHERE atm.disabled = 0 + AND (tasg.id_group = %d + OR ta.id_grupo = %d) + ', + $id_grupo, + $id_grupo + ); + $agent_modules = db_get_all_rows_sql($sql); + } else { + $groups = groups_get_children($id_grupo, true); + if (empty($groups) === false) { + $groups = array_reduce( + $groups, + function ($carry, $item) { + $carry[] = $item['id_grupo']; + return $carry; + }, + [$id_grupo] + ); + + $sql = sprintf( + 'SELECT distinct(atm.id_agent_module) + FROM talert_template_modules atm + INNER JOIN tagente_modulo am + ON am.id_agente_modulo = atm.id_agent_module + INNER JOIN tagente ta + ON am.id_agente = ta.id_agente + LEFT JOIN tagent_secondary_group tasg + ON tasg.id_agent = ta.id_agente + WHERE atm.disabled = 0 + AND (tasg.id_group IN (%s) + OR ta.id_grupo IN (%s)) + ', + implode(',', $groups), + implode(',', $groups) + ); + } + + $agent_modules = db_get_all_rows_sql($sql); + } + + if ($agent_modules === false) { + return []; + } + + return $agent_modules; + +} diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index b7f8d861d7..7a337fbdab 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -1317,14 +1317,14 @@ function events_get_all( if (is_array($groups)) { $tgrupo_join_filters[] = sprintf( ' (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) - OR (te.id_agente = tasg.id_agent AND tasg.id_group IN (%s))', + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))', join(', ', $groups), join(', ', $groups) ); } else { $tgrupo_join_filters[] = sprintf( ' (te.id_grupo = tg.id_grupo AND tg.id_grupo = %s) - OR (te.id_agente = tasg.id_agent AND tasg.id_group = %s)', + OR (tg.id_grupo = tasg.id_group AND tasg.id_group = %s)', $groups, $groups ); @@ -2992,7 +2992,7 @@ function events_get_agent( $sql_where .= sprintf( ' INNER JOIN tgrupo tg ON (te.id_grupo = tg.id_grupo AND tg.id_grupo = %s) - OR (te.id_agente = tasg.id_agent AND tasg.id_group = %s) + OR (tg.id_grupo = tasg.id_group AND tasg.id_group = %s) WHERE utimestamp > %d AND utimestamp <= %d ', join(',', $id_group), @@ -5042,7 +5042,7 @@ function events_get_count_events_by_agent( ON te.id_agente = ta.id_agente INNER JOIN tgrupo tg ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) - OR (te.id_agente = tasg.id_agent AND tasg.id_group IN (%s)) + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s)) WHERE utimestamp > %d AND utimestamp <= %d GROUP BY ta.id_agente', $tevento, @@ -5118,7 +5118,7 @@ function events_get_count_events_validated_by_user( '%s INNER JOIN tgrupo tg ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) - OR (te.id_agente = tasg.id_agent AND tasg.id_group IN (%s))', + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))', events_get_secondary_groups_left_join($tevento), implode(',', $id_group), implode(',', $id_group) @@ -5308,7 +5308,7 @@ function events_get_count_events_by_criticity( '%s INNER JOIN tgrupo tg ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) - OR (te.id_agente = tasg.id_agent AND tasg.id_group IN (%s))', + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))', events_get_secondary_groups_left_join($tevento), implode(',', $id_group), implode(',', $id_group) @@ -5490,7 +5490,7 @@ function events_get_count_events_validated( '%s INNER JOIN tgrupo tg ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s)) - OR (te.id_agente = tasg.id_agent AND tasg.id_group IN (%s))', + OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))', events_get_secondary_groups_left_join($tevento), implode(',', $id_group), implode(',', $id_group) diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 818af98197..f5372e1404 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -3909,30 +3909,10 @@ function reporting_alert_report_group($report, $content) $return['description'] = $content['description']; $return['date'] = reporting_get_date_text($report, $content); - if ($content['id_group'] == 0) { - $agent_modules = db_get_all_rows_sql( - ' - SELECT distinct(id_agent_module) - FROM talert_template_modules - WHERE disabled = 0 - AND id_agent_module IN ( - SELECT id_agente_modulo - FROM tagente_modulo)' - ); - } else { - $agent_modules = db_get_all_rows_sql( - ' - SELECT distinct(id_agent_module) - FROM talert_template_modules - WHERE disabled = 0 - AND id_agent_module IN ( - SELECT id_agente_modulo - FROM tagente_modulo - WHERE id_agente IN ( - SELECT id_agente - FROM tagente WHERE id_grupo = '.$content['id_group'].'))' - ); - } + $agent_modules = alerts_get_agent_modules( + $content['id_group'], + $content['recursion'] + ); if (empty($alerts)) { $alerts = [];