From 94bd55fd39e87629a66590e89cc8786ea4edc7ed Mon Sep 17 00:00:00 2001 From: Daniel Barbero Martin Date: Thu, 2 Dec 2021 16:55:53 +0100 Subject: [PATCH] wip reports alert actions --- .../reporting_builder.item_editor.php | 30 +- .../godmode/reporting/reporting_builder.php | 6 +- pandora_console/include/functions_alerts.php | 319 ++++++++++-------- .../include/functions_reporting.php | 40 +-- .../include/functions_reporting_html.php | 21 +- 5 files changed, 238 insertions(+), 178 deletions(-) diff --git a/pandora_console/godmode/reporting/reporting_builder.item_editor.php b/pandora_console/godmode/reporting/reporting_builder.item_editor.php index 480495c2cf..81b6877cc2 100755 --- a/pandora_console/godmode/reporting/reporting_builder.item_editor.php +++ b/pandora_console/godmode/reporting/reporting_builder.item_editor.php @@ -136,10 +136,10 @@ $percentil = false; $time_compare_overlapped = false; // Added for events items. -$show_summary_group = false; +$show_summary_group = false; $filter_event_severity = false; -$filter_event_type = false; -$filter_event_status = false; +$filter_event_type = false; +$filter_event_status = false; $event_graph_by_agent = false; $event_graph_by_user_validator = false; $event_graph_by_criticity = false; @@ -175,6 +175,8 @@ $agent_max_value = true; $agent_min_value = true; $uncompressed_module = true; +$only_data = false; + // Users. $id_users = []; $users_groups = []; @@ -763,7 +765,6 @@ switch ($action) { break; case 'alert_report_actions': - hd('1'); $description = $item['description']; $es = json_decode($item['external_source'], true); @@ -791,6 +792,8 @@ switch ($action) { $group_by = $es['group_by']; + $only_data = $es['only_data']; + $period = $item['period']; $lapse = $item['lapse']; @@ -2709,6 +2712,23 @@ $class = 'databox filters'; + + + + + + + + + @@ -5782,6 +5802,7 @@ function chooseType() { $("#select_agent_modules").hide(); $("#modules_row").hide(); $("#row_show_summary_group").hide(); + $("#row_show_only_data").hide(); $("#row_event_severity").hide(); $("#row_event_type").hide(); $("#row_event_status").hide(); @@ -6131,6 +6152,7 @@ function chooseType() { $("#row_period").show(); $("#row_lapse").show(); $("#row_show_summary").show(); + $("#row_show_only_data").show(); $("#row_group_by").show(); if('' === 'new'){ $("#group_by").html(''); diff --git a/pandora_console/godmode/reporting/reporting_builder.php b/pandora_console/godmode/reporting/reporting_builder.php index 674dd627a3..9421f120f2 100755 --- a/pandora_console/godmode/reporting/reporting_builder.php +++ b/pandora_console/godmode/reporting/reporting_builder.php @@ -1707,7 +1707,7 @@ switch ($action) { $alert_actions_to_report = get_parameter('alert_actions'); $show_summary = get_parameter('show_summary', 0); $group_by = get_parameter('group_by'); - hd('3'); + $only_data = get_parameter('only_data', 0); $agents_to_report_text = get_parameter('id_agents2-multiple-text'); $modules_to_report_text = get_parameter('module-multiple-text', ''); @@ -1734,6 +1734,7 @@ switch ($action) { $es['actions'] = $alert_actions_to_report; $es['show_summary'] = $show_summary; $es['group_by'] = $group_by; + $es['only_data'] = $only_data; $values['external_source'] = json_encode($es); @@ -2503,11 +2504,11 @@ switch ($action) { break; case 'alert_report_actions': - hd('2'); $alert_templates_to_report = get_parameter('alert_templates'); $alert_actions_to_report = get_parameter('alert_actions'); $show_summary = get_parameter('show_summary', 0); $group_by = get_parameter('group_by'); + $only_data = get_parameter('only_data', 0); $agents_to_report_text = get_parameter('id_agents2-multiple-text'); $modules_to_report_text = get_parameter('module-multiple-text', ''); @@ -2535,6 +2536,7 @@ switch ($action) { $es['actions'] = $alert_actions_to_report; $es['show_summary'] = $show_summary; $es['group_by'] = $group_by; + $es['only_data'] = $only_data; $values['external_source'] = json_encode($es); diff --git a/pandora_console/include/functions_alerts.php b/pandora_console/include/functions_alerts.php index a5d2f26152..1bb8ea002a 100644 --- a/pandora_console/include/functions_alerts.php +++ b/pandora_console/include/functions_alerts.php @@ -2881,12 +2881,18 @@ function alerts_get_actions_names($actions, $reduce=false) } -function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) +/** + * Alert fired. + * + * @param array $filters Filters. + * @param array $groupsBy Groupby and lapse. + * + * @return array Result data. + */ +function alerts_get_alert_fired($filters=[], $groupsBy=[]) { global $config; - hd(5); - $table = 'tevento'; if (is_metaconsole() === true) { $table = 'tmetaconsole_event'; @@ -3012,6 +3018,9 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) } } + $total = (bool) $filters['show_summary']; + $only_data = (bool) $filters['only_data']; + $actions_names = alerts_get_actions_names($filters['actions'], true); $group_array = []; @@ -3071,18 +3080,14 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) case 'module': $fields[] = $table.'.id_agentmodule as module'; $group_array[] = $table.'.id_agentmodule'; - if ($total === false) { - $names_search = modules_get_agentmodule_name_array( - array_values($filters['modules']) - ); - } + $names_search = modules_get_agentmodule_name_array( + array_values($filters['modules']) + ); if (is_metaconsole() === true) { $fields[] = $table.'.server_id as server'; $group_array[] = $table.'.server_id'; - if ($total === false) { - $names_server = metaconsole_get_names(); - } + $names_server = metaconsole_get_names(); } break; @@ -3090,38 +3095,30 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) if (is_metaconsole() === false) { $fields[] = 'talert_template_modules.id_alert_template as template'; $group_array[] = 'talert_template_modules.id_alert_template'; - if ($total === false) { - $names_search = alerts_get_templates_name_array( - array_values($filters['templates']) - ); - } + $names_search = alerts_get_templates_name_array( + array_values($filters['templates']) + ); } break; case 'agent': $fields[] = $table.'.id_agente as agent'; $group_array[] = $table.'.id_agente'; - if ($total === false) { - $names_search = agents_get_alias_array( - array_values($filters['agents']) - ); - } + $names_search = agents_get_alias_array( + array_values($filters['agents']) + ); if (is_metaconsole() === true) { $fields[] = $table.'.server_id as server'; $group_array[] = $table.'.server_id'; - if ($total === false) { - $names_server = metaconsole_get_names(); - } + $names_server = metaconsole_get_names(); } break; case 'group': $fields[] = $table.'.id_grupo as `group`'; $group_array[] = $table.'.id_grupo'; - if ($total === false) { - $names_search = users_get_groups($config['user'], 'AR', false); - } + $names_search = users_get_groups($config['user'], 'AR', false); break; default: @@ -3130,16 +3127,14 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) } } - if ($total === false) { - if (isset($groupsBy['lapse']) === true - && empty($groupsBy['lapse']) === false - ) { - $fields[] = sprintf( - '%s.utimestamp AS Period', - $table - ); - $group_array[] = 'period'; - } + if (isset($groupsBy['lapse']) === true + && empty($groupsBy['lapse']) === false + ) { + $fields[] = sprintf( + '%s.utimestamp AS Period', + $table + ); + $group_array[] = 'period'; } $group_by = ''; @@ -3183,103 +3178,105 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) $group_by ); - hd($query); + $data_query = db_get_all_rows_sql($query); - $data = db_get_all_rows_sql($query); - - if ($data === false) { - $data = []; + if ($data_query === false) { + $data_query = []; } - if ($total === false) { - if (empty($data) === false) { - $data = array_reduce( - $data, - function ($carry, $item) use ($groupsBy) { - $period = (isset($item['Period']) === true) ? (int) $item['Period'] : 0; - if (is_metaconsole() === true - && ($groupsBy['group_by'] === 'agent' - || $groupsBy['group_by'] === 'module') - ) { - $grby = $item[$groupsBy['group_by']]; - $server = $item['server']; - unset($item['Period']); - unset($item[$groupsBy['group_by']]); - unset($item['server']); - $carry[$period][$server][$grby] = $item; - } else { - $grby = $item[$groupsBy['group_by']]; - unset($item['Period']); - unset($item[$groupsBy['group_by']]); - $carry[$period][$grby] = $item; - } - - return $carry; - }, - [] - ); - - $intervals = []; - if (isset($groupsBy['lapse']) === true - && empty($groupsBy['lapse']) === false - ) { - $tend = time(); - $tstart = ($tend - (int) $filters['period']); - for ($current_time = $tstart; $current_time < $tend; ($current_time += $groupsBy['lapse'])) { - $intervals[] = (int) $current_time; + if (empty($data_query) === false) { + $data = array_reduce( + $data_query, + function ($carry, $item) use ($groupsBy) { + $period = (isset($item['Period']) === true) ? (int) $item['Period'] : 0; + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + $grby = $item[$groupsBy['group_by']]; + $server = $item['server']; + unset($item['Period']); + unset($item[$groupsBy['group_by']]); + unset($item['server']); + $carry[$period][$server][$grby] = $item; + } else { + $grby = $item[$groupsBy['group_by']]; + unset($item['Period']); + unset($item[$groupsBy['group_by']]); + $carry[$period][$grby] = $item; } - } - $first_element = reset($data); - $first_element = reset($first_element); - if (is_metaconsole() === true - && ($groupsBy['group_by'] === 'agent' - || $groupsBy['group_by'] === 'module') - ) { - $first_element = reset($first_element); - } + return $carry; + }, + [] + ); - $clone = []; - foreach ($first_element as $key_clone => $value_clone) { - $clone[$key_clone] = 0; + $intervals = []; + if (isset($groupsBy['lapse']) === true + && empty($groupsBy['lapse']) === false + ) { + $tend = time(); + $tstart = ($tend - (int) $filters['period']); + for ($current_time = $tstart; $current_time < $tend; ($current_time += $groupsBy['lapse'])) { + $intervals[] = (int) $current_time; } + } - $result = []; - if (empty($intervals) === true) { - foreach ($data as $period => $array_data) { - if (is_metaconsole() === true - && ($groupsBy['group_by'] === 'agent' - || $groupsBy['group_by'] === 'module') - ) { - foreach ($names_search as $server => $names) { - foreach ($names as $id => $name) { - if (isset($array_data[$server][$id]) === true) { - $result[$period][$server.'|'.$id] = $array_data[$server][$id]; - $result[$period][$server.'|'.$id][$groupsBy['group_by']] = $name; - } else { + $first_element = reset($data); + $first_element = reset($first_element); + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + $first_element = reset($first_element); + } + + $clone = []; + foreach ($first_element as $key_clone => $value_clone) { + $clone[$key_clone] = 0; + } + + $result = []; + if (empty($intervals) === true) { + foreach ($data as $period => $array_data) { + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + foreach ($names_search as $server => $names) { + foreach ($names as $id => $name) { + if (isset($array_data[$server][$id]) === true) { + $result[$period][$server.'|'.$id] = $array_data[$server][$id]; + $result[$period][$server.'|'.$id][$groupsBy['group_by']] = $name; + } else { + if ($only_data === false) { $clone[$groupsBy['group_by']] = $name; $result[$period][$server.'|'.$id] = $clone; } } } - } else { - foreach ($names_search as $id => $name) { - if (isset($array_data[$id]) === true) { - $result[$period][$id] = $array_data[$id]; - $result[$period][$id][$groupsBy['group_by']] = $name; - } else { + } + } else { + foreach ($names_search as $id => $name) { + if (isset($array_data[$id]) === true) { + $result[$period][$id] = $array_data[$id]; + $result[$period][$id][$groupsBy['group_by']] = $name; + } else { + if ($only_data === false) { $clone[$groupsBy['group_by']] = $name; $result[$period][$id] = $clone; } } } } - } else { - $period_lapse = (int) $groupsBy['lapse']; - foreach ($intervals as $interval) { - $start_interval = $interval; - $end_interval = ($interval + $period_lapse); + } + } else { + $period_lapse = (int) $groupsBy['lapse']; + foreach ($intervals as $interval) { + $start_interval = $interval; + $end_interval = ($interval + $period_lapse); + if ($only_data === false) { if (is_metaconsole() === true && ($groupsBy['group_by'] === 'agent' || $groupsBy['group_by'] === 'module') @@ -3297,58 +3294,90 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false) $result[$start_interval][$id][$groupsBy['group_by']] = $name; } } - + } else { foreach ($data as $period => $array_data) { - $period_time = (int) $period; - if ($start_interval < $period_time && $period_time <= $end_interval) { - if (is_metaconsole() === true - && ($groupsBy['group_by'] === 'agent' - || $groupsBy['group_by'] === 'module') - ) { - foreach ($array_data as $server => $datas) { - foreach ($datas as $id_data => $value_data) { - foreach ($value_data as $key_data => $v) { - if ($key_data !== $groupsBy['group_by']) { - if (isset($result[$start_interval][$server.'|'.$id_data][$key_data])) { - $result[$start_interval][$server.'|'.$id_data][$key_data] += $v; - } else { - $result[$start_interval][$server.'|'.$id_data][$key_data] = $v; - } - } - } - } + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + foreach ($array_data as $server => $datas) { + foreach ($datas as $id_data => $value_data) { + $name = $names_server[$server].' » '.$names_search[$server][$id_data]; + $result[$start_interval][$server.'|'.$id_data] = $clone; + $result[$start_interval][$server.'|'.$id_data][$groupsBy['group_by']] = $name; } - } else { - foreach ($array_data as $id_data => $value_data) { + } + } else { + foreach ($array_data as $id_data => $value_data) { + $name = $names_search[$id_data]; + $result[$start_interval][$id_data] = $clone; + $result[$start_interval][$id_data][$groupsBy['group_by']] = $name; + } + } + } + } + + foreach ($data as $period => $array_data) { + $period_time = (int) $period; + if ($start_interval < $period_time && $period_time <= $end_interval) { + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + foreach ($array_data as $server => $datas) { + foreach ($datas as $id_data => $value_data) { foreach ($value_data as $key_data => $v) { if ($key_data !== $groupsBy['group_by']) { - if (isset($result[$start_interval][$id_data][$key_data])) { - $result[$start_interval][$id_data][$key_data] += $v; + if (isset($result[$start_interval][$server.'|'.$id_data][$key_data])) { + $result[$start_interval][$server.'|'.$id_data][$key_data] += $v; } else { - $result[$start_interval][$id_data][$key_data] = $v; + $result[$start_interval][$server.'|'.$id_data][$key_data] = $v; } } } } } - - unset($data[$period]); + } else { + foreach ($array_data as $id_data => $value_data) { + foreach ($value_data as $key_data => $v) { + if ($key_data !== $groupsBy['group_by']) { + if (isset($result[$start_interval][$id_data][$key_data])) { + $result[$start_interval][$id_data][$key_data] += $v; + } else { + $result[$start_interval][$id_data][$key_data] = $v; + } + } + } + } } + + unset($data[$period]); } } } } - } else { + } + + $result['data'] = $result; + + if ($total === true) { $total_values = []; - $result = []; - foreach ($data as $key => $array_data) { + foreach ($data_query as $key => $array_data) { foreach ($array_data as $key_value => $v) { $total_values[$key_value] = ($total_values[$key_value] + $v); } } - $result['total'] = $total_values; - $result['total'][$groupsBy['group_by']] = __('Total'); + if (is_metaconsole() === true + && ($groupsBy['group_by'] === 'agent' + || $groupsBy['group_by'] === 'module') + ) { + unset($total_values['Period']); + unset($total_values['server']); + } + + $result['summary']['total'] = $total_values; + $result['summary']['total'][$groupsBy['group_by']] = __('Total'); } return $result; diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 17d9824943..ff12d6bdd9 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -2665,13 +2665,18 @@ function reporting_inventory($report, $content, $type) } +/** + * Build data for report alert actions. + * + * @param array $report Report info. + * @param array $content Content. + * + * @return array Result data. + */ function reporting_alert_report_actions($report, $content) { - global $config; $return = []; - hd('4'); - $return['type'] = 'alert_report_actions'; if (empty($content['name']) === true) { $content['name'] = __('Alert actions'); @@ -2717,14 +2722,17 @@ function reporting_alert_report_actions($report, $content) $show_summary = $es['show_summary']; $group_by = $es['group_by']; $lapse = $content['lapse']; + $only_data = $es['only_data']; $filters = [ - 'group' => $id_group, - 'agents' => $agents, - 'modules' => $modules, - 'templates' => $templates, - 'actions' => $actions, - 'period' => $period, + 'group' => $id_group, + 'agents' => $agents, + 'modules' => $modules, + 'templates' => $templates, + 'actions' => $actions, + 'period' => $period, + 'show_summary' => (bool) $show_summary, + 'only_data' => (bool) $only_data, ]; $groupsBy = [ @@ -2735,19 +2743,7 @@ function reporting_alert_report_actions($report, $content) $return['filters'] = $filters; $return['groupsBy'] = $groupsBy; - $return['data']['data'] = alerts_get_alert_fired( - $filters, - $groupsBy, - false - ); - - if ((bool) $show_summary === true) { - $return['data']['summary'] = alerts_get_alert_fired( - $filters, - $groupsBy, - true - ); - } + $return['data'] = alerts_get_alert_fired($filters, $groupsBy); return reporting_check_structure_content($return); } diff --git a/pandora_console/include/functions_reporting_html.php b/pandora_console/include/functions_reporting_html.php index b845f025a3..7260843d29 100644 --- a/pandora_console/include/functions_reporting_html.php +++ b/pandora_console/include/functions_reporting_html.php @@ -2733,12 +2733,18 @@ function reporting_html_group_configuration($table, $item, $pdf=0) } +/** + * Html output report alert actions + * + * @param object $table Table. + * @param array $item Data for draw report. + * @param integer $pdf PDF output. + * + * @return string Html output. + */ function reporting_html_alert_report_actions($table, $item, $pdf=0) { - hd(6); - $data = $item['data']; - $filters = $item['filters']; $groupsBy = $item['groupsBy']; $output = ''; @@ -2795,14 +2801,19 @@ function get_alert_table($data) $table->width = '100%'; $table->data = []; $table->head = []; + $table->headstyle = []; + $table->cellstyle = []; + $table->headstyle[0] = 'text-align:left;'; + $head = reset($data); foreach (array_reverse(array_keys($head)) as $name) { - $table->head[$name] = $name; + $table->head[] = $name; } foreach ($data as $key => $params) { + $table->cellstyle[$key][0] = 'text-align:left;'; foreach (array_reverse($params) as $name => $value) { - $table->data[$key][$name] = $value; + $table->data[$key][] = $value; } }