wip reports alert actions

This commit is contained in:
Daniel Barbero Martin 2021-12-02 16:55:53 +01:00
parent ec21d9d08a
commit 94bd55fd39
5 changed files with 238 additions and 178 deletions

View File

@ -136,10 +136,10 @@ $percentil = false;
$time_compare_overlapped = false; $time_compare_overlapped = false;
// Added for events items. // Added for events items.
$show_summary_group = false; $show_summary_group = false;
$filter_event_severity = false; $filter_event_severity = false;
$filter_event_type = false; $filter_event_type = false;
$filter_event_status = false; $filter_event_status = false;
$event_graph_by_agent = false; $event_graph_by_agent = false;
$event_graph_by_user_validator = false; $event_graph_by_user_validator = false;
$event_graph_by_criticity = false; $event_graph_by_criticity = false;
@ -175,6 +175,8 @@ $agent_max_value = true;
$agent_min_value = true; $agent_min_value = true;
$uncompressed_module = true; $uncompressed_module = true;
$only_data = false;
// Users. // Users.
$id_users = []; $id_users = [];
$users_groups = []; $users_groups = [];
@ -763,7 +765,6 @@ switch ($action) {
break; break;
case 'alert_report_actions': case 'alert_report_actions':
hd('1');
$description = $item['description']; $description = $item['description'];
$es = json_decode($item['external_source'], true); $es = json_decode($item['external_source'], true);
@ -791,6 +792,8 @@ switch ($action) {
$group_by = $es['group_by']; $group_by = $es['group_by'];
$only_data = $es['only_data'];
$period = $item['period']; $period = $item['period'];
$lapse = $item['lapse']; $lapse = $item['lapse'];
@ -2709,6 +2712,23 @@ $class = 'databox filters';
</td> </td>
</tr> </tr>
<tr id="row_show_only_data" class="datos">
<td class="bolder">
<?php
echo __('Only data');
?>
</td>
<td>
<?php
html_print_checkbox_switch(
'only_data',
true,
$only_data
);
?>
</td>
</tr>
<tr id="row_event_severity" class="datos"> <tr id="row_event_severity" class="datos">
<td class="bolder"><?php echo __('Severity'); ?></td> <td class="bolder"><?php echo __('Severity'); ?></td>
<td> <td>
@ -5782,6 +5802,7 @@ function chooseType() {
$("#select_agent_modules").hide(); $("#select_agent_modules").hide();
$("#modules_row").hide(); $("#modules_row").hide();
$("#row_show_summary_group").hide(); $("#row_show_summary_group").hide();
$("#row_show_only_data").hide();
$("#row_event_severity").hide(); $("#row_event_severity").hide();
$("#row_event_type").hide(); $("#row_event_type").hide();
$("#row_event_status").hide(); $("#row_event_status").hide();
@ -6131,6 +6152,7 @@ function chooseType() {
$("#row_period").show(); $("#row_period").show();
$("#row_lapse").show(); $("#row_lapse").show();
$("#row_show_summary").show(); $("#row_show_summary").show();
$("#row_show_only_data").show();
$("#row_group_by").show(); $("#row_group_by").show();
if('<?php echo $action; ?>' === 'new'){ if('<?php echo $action; ?>' === 'new'){
$("#group_by").html(''); $("#group_by").html('');

View File

@ -1707,7 +1707,7 @@ switch ($action) {
$alert_actions_to_report = get_parameter('alert_actions'); $alert_actions_to_report = get_parameter('alert_actions');
$show_summary = get_parameter('show_summary', 0); $show_summary = get_parameter('show_summary', 0);
$group_by = get_parameter('group_by'); $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'); $agents_to_report_text = get_parameter('id_agents2-multiple-text');
$modules_to_report_text = get_parameter('module-multiple-text', ''); $modules_to_report_text = get_parameter('module-multiple-text', '');
@ -1734,6 +1734,7 @@ switch ($action) {
$es['actions'] = $alert_actions_to_report; $es['actions'] = $alert_actions_to_report;
$es['show_summary'] = $show_summary; $es['show_summary'] = $show_summary;
$es['group_by'] = $group_by; $es['group_by'] = $group_by;
$es['only_data'] = $only_data;
$values['external_source'] = json_encode($es); $values['external_source'] = json_encode($es);
@ -2503,11 +2504,11 @@ switch ($action) {
break; break;
case 'alert_report_actions': case 'alert_report_actions':
hd('2');
$alert_templates_to_report = get_parameter('alert_templates'); $alert_templates_to_report = get_parameter('alert_templates');
$alert_actions_to_report = get_parameter('alert_actions'); $alert_actions_to_report = get_parameter('alert_actions');
$show_summary = get_parameter('show_summary', 0); $show_summary = get_parameter('show_summary', 0);
$group_by = get_parameter('group_by'); $group_by = get_parameter('group_by');
$only_data = get_parameter('only_data', 0);
$agents_to_report_text = get_parameter('id_agents2-multiple-text'); $agents_to_report_text = get_parameter('id_agents2-multiple-text');
$modules_to_report_text = get_parameter('module-multiple-text', ''); $modules_to_report_text = get_parameter('module-multiple-text', '');
@ -2535,6 +2536,7 @@ switch ($action) {
$es['actions'] = $alert_actions_to_report; $es['actions'] = $alert_actions_to_report;
$es['show_summary'] = $show_summary; $es['show_summary'] = $show_summary;
$es['group_by'] = $group_by; $es['group_by'] = $group_by;
$es['only_data'] = $only_data;
$values['external_source'] = json_encode($es); $values['external_source'] = json_encode($es);

View File

@ -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; global $config;
hd(5);
$table = 'tevento'; $table = 'tevento';
if (is_metaconsole() === true) { if (is_metaconsole() === true) {
$table = 'tmetaconsole_event'; $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); $actions_names = alerts_get_actions_names($filters['actions'], true);
$group_array = []; $group_array = [];
@ -3071,18 +3080,14 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
case 'module': case 'module':
$fields[] = $table.'.id_agentmodule as module'; $fields[] = $table.'.id_agentmodule as module';
$group_array[] = $table.'.id_agentmodule'; $group_array[] = $table.'.id_agentmodule';
if ($total === false) { $names_search = modules_get_agentmodule_name_array(
$names_search = modules_get_agentmodule_name_array( array_values($filters['modules'])
array_values($filters['modules']) );
);
}
if (is_metaconsole() === true) { if (is_metaconsole() === true) {
$fields[] = $table.'.server_id as server'; $fields[] = $table.'.server_id as server';
$group_array[] = $table.'.server_id'; $group_array[] = $table.'.server_id';
if ($total === false) { $names_server = metaconsole_get_names();
$names_server = metaconsole_get_names();
}
} }
break; break;
@ -3090,38 +3095,30 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
if (is_metaconsole() === false) { if (is_metaconsole() === false) {
$fields[] = 'talert_template_modules.id_alert_template as template'; $fields[] = 'talert_template_modules.id_alert_template as template';
$group_array[] = 'talert_template_modules.id_alert_template'; $group_array[] = 'talert_template_modules.id_alert_template';
if ($total === false) { $names_search = alerts_get_templates_name_array(
$names_search = alerts_get_templates_name_array( array_values($filters['templates'])
array_values($filters['templates']) );
);
}
} }
break; break;
case 'agent': case 'agent':
$fields[] = $table.'.id_agente as agent'; $fields[] = $table.'.id_agente as agent';
$group_array[] = $table.'.id_agente'; $group_array[] = $table.'.id_agente';
if ($total === false) { $names_search = agents_get_alias_array(
$names_search = agents_get_alias_array( array_values($filters['agents'])
array_values($filters['agents']) );
);
}
if (is_metaconsole() === true) { if (is_metaconsole() === true) {
$fields[] = $table.'.server_id as server'; $fields[] = $table.'.server_id as server';
$group_array[] = $table.'.server_id'; $group_array[] = $table.'.server_id';
if ($total === false) { $names_server = metaconsole_get_names();
$names_server = metaconsole_get_names();
}
} }
break; break;
case 'group': case 'group':
$fields[] = $table.'.id_grupo as `group`'; $fields[] = $table.'.id_grupo as `group`';
$group_array[] = $table.'.id_grupo'; $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; break;
default: default:
@ -3130,16 +3127,14 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
} }
} }
if ($total === false) { if (isset($groupsBy['lapse']) === true
if (isset($groupsBy['lapse']) === true && empty($groupsBy['lapse']) === false
&& empty($groupsBy['lapse']) === false ) {
) { $fields[] = sprintf(
$fields[] = sprintf( '%s.utimestamp AS Period',
'%s.utimestamp AS Period', $table
$table );
); $group_array[] = 'period';
$group_array[] = 'period';
}
} }
$group_by = ''; $group_by = '';
@ -3183,103 +3178,105 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
$group_by $group_by
); );
hd($query); $data_query = db_get_all_rows_sql($query);
$data = db_get_all_rows_sql($query); if ($data_query === false) {
$data_query = [];
if ($data === false) {
$data = [];
} }
if ($total === false) { if (empty($data_query) === false) {
if (empty($data) === false) { $data = array_reduce(
$data = array_reduce( $data_query,
$data, function ($carry, $item) use ($groupsBy) {
function ($carry, $item) use ($groupsBy) { $period = (isset($item['Period']) === true) ? (int) $item['Period'] : 0;
$period = (isset($item['Period']) === true) ? (int) $item['Period'] : 0; if (is_metaconsole() === true
if (is_metaconsole() === true && ($groupsBy['group_by'] === 'agent'
&& ($groupsBy['group_by'] === 'agent' || $groupsBy['group_by'] === 'module')
|| $groupsBy['group_by'] === 'module') ) {
) { $grby = $item[$groupsBy['group_by']];
$grby = $item[$groupsBy['group_by']]; $server = $item['server'];
$server = $item['server']; unset($item['Period']);
unset($item['Period']); unset($item[$groupsBy['group_by']]);
unset($item[$groupsBy['group_by']]); unset($item['server']);
unset($item['server']); $carry[$period][$server][$grby] = $item;
$carry[$period][$server][$grby] = $item; } else {
} else { $grby = $item[$groupsBy['group_by']];
$grby = $item[$groupsBy['group_by']]; unset($item['Period']);
unset($item['Period']); unset($item[$groupsBy['group_by']]);
unset($item[$groupsBy['group_by']]); $carry[$period][$grby] = $item;
$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;
} }
}
$first_element = reset($data); return $carry;
$first_element = reset($first_element); },
if (is_metaconsole() === true []
&& ($groupsBy['group_by'] === 'agent' );
|| $groupsBy['group_by'] === 'module')
) {
$first_element = reset($first_element);
}
$clone = []; $intervals = [];
foreach ($first_element as $key_clone => $value_clone) { if (isset($groupsBy['lapse']) === true
$clone[$key_clone] = 0; && 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 = []; $first_element = reset($data);
if (empty($intervals) === true) { $first_element = reset($first_element);
foreach ($data as $period => $array_data) { if (is_metaconsole() === true
if (is_metaconsole() === true && ($groupsBy['group_by'] === 'agent'
&& ($groupsBy['group_by'] === 'agent' || $groupsBy['group_by'] === 'module')
|| $groupsBy['group_by'] === 'module') ) {
) { $first_element = reset($first_element);
foreach ($names_search as $server => $names) { }
foreach ($names as $id => $name) {
if (isset($array_data[$server][$id]) === true) { $clone = [];
$result[$period][$server.'|'.$id] = $array_data[$server][$id]; foreach ($first_element as $key_clone => $value_clone) {
$result[$period][$server.'|'.$id][$groupsBy['group_by']] = $name; $clone[$key_clone] = 0;
} else { }
$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; $clone[$groupsBy['group_by']] = $name;
$result[$period][$server.'|'.$id] = $clone; $result[$period][$server.'|'.$id] = $clone;
} }
} }
} }
} else { }
foreach ($names_search as $id => $name) { } else {
if (isset($array_data[$id]) === true) { foreach ($names_search as $id => $name) {
$result[$period][$id] = $array_data[$id]; if (isset($array_data[$id]) === true) {
$result[$period][$id][$groupsBy['group_by']] = $name; $result[$period][$id] = $array_data[$id];
} else { $result[$period][$id][$groupsBy['group_by']] = $name;
} else {
if ($only_data === false) {
$clone[$groupsBy['group_by']] = $name; $clone[$groupsBy['group_by']] = $name;
$result[$period][$id] = $clone; $result[$period][$id] = $clone;
} }
} }
} }
} }
} else { }
$period_lapse = (int) $groupsBy['lapse']; } else {
foreach ($intervals as $interval) { $period_lapse = (int) $groupsBy['lapse'];
$start_interval = $interval; foreach ($intervals as $interval) {
$end_interval = ($interval + $period_lapse); $start_interval = $interval;
$end_interval = ($interval + $period_lapse);
if ($only_data === false) {
if (is_metaconsole() === true if (is_metaconsole() === true
&& ($groupsBy['group_by'] === 'agent' && ($groupsBy['group_by'] === 'agent'
|| $groupsBy['group_by'] === 'module') || $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; $result[$start_interval][$id][$groupsBy['group_by']] = $name;
} }
} }
} else {
foreach ($data as $period => $array_data) { foreach ($data as $period => $array_data) {
$period_time = (int) $period; if (is_metaconsole() === true
if ($start_interval < $period_time && $period_time <= $end_interval) { && ($groupsBy['group_by'] === 'agent'
if (is_metaconsole() === true || $groupsBy['group_by'] === 'module')
&& ($groupsBy['group_by'] === 'agent' ) {
|| $groupsBy['group_by'] === 'module') foreach ($array_data as $server => $datas) {
) { foreach ($datas as $id_data => $value_data) {
foreach ($array_data as $server => $datas) { $name = $names_server[$server].' &raquo; '.$names_search[$server][$id_data];
foreach ($datas as $id_data => $value_data) { $result[$start_interval][$server.'|'.$id_data] = $clone;
foreach ($value_data as $key_data => $v) { $result[$start_interval][$server.'|'.$id_data][$groupsBy['group_by']] = $name;
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;
}
}
}
}
} }
} 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) { foreach ($value_data as $key_data => $v) {
if ($key_data !== $groupsBy['group_by']) { if ($key_data !== $groupsBy['group_by']) {
if (isset($result[$start_interval][$id_data][$key_data])) { if (isset($result[$start_interval][$server.'|'.$id_data][$key_data])) {
$result[$start_interval][$id_data][$key_data] += $v; $result[$start_interval][$server.'|'.$id_data][$key_data] += $v;
} else { } else {
$result[$start_interval][$id_data][$key_data] = $v; $result[$start_interval][$server.'|'.$id_data][$key_data] = $v;
} }
} }
} }
} }
} }
} else {
unset($data[$period]); 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 = []; $total_values = [];
$result = []; foreach ($data_query as $key => $array_data) {
foreach ($data as $key => $array_data) {
foreach ($array_data as $key_value => $v) { foreach ($array_data as $key_value => $v) {
$total_values[$key_value] = ($total_values[$key_value] + $v); $total_values[$key_value] = ($total_values[$key_value] + $v);
} }
} }
$result['total'] = $total_values; if (is_metaconsole() === true
$result['total'][$groupsBy['group_by']] = __('Total'); && ($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; return $result;

View File

@ -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) function reporting_alert_report_actions($report, $content)
{ {
global $config;
$return = []; $return = [];
hd('4');
$return['type'] = 'alert_report_actions'; $return['type'] = 'alert_report_actions';
if (empty($content['name']) === true) { if (empty($content['name']) === true) {
$content['name'] = __('Alert actions'); $content['name'] = __('Alert actions');
@ -2717,14 +2722,17 @@ function reporting_alert_report_actions($report, $content)
$show_summary = $es['show_summary']; $show_summary = $es['show_summary'];
$group_by = $es['group_by']; $group_by = $es['group_by'];
$lapse = $content['lapse']; $lapse = $content['lapse'];
$only_data = $es['only_data'];
$filters = [ $filters = [
'group' => $id_group, 'group' => $id_group,
'agents' => $agents, 'agents' => $agents,
'modules' => $modules, 'modules' => $modules,
'templates' => $templates, 'templates' => $templates,
'actions' => $actions, 'actions' => $actions,
'period' => $period, 'period' => $period,
'show_summary' => (bool) $show_summary,
'only_data' => (bool) $only_data,
]; ];
$groupsBy = [ $groupsBy = [
@ -2735,19 +2743,7 @@ function reporting_alert_report_actions($report, $content)
$return['filters'] = $filters; $return['filters'] = $filters;
$return['groupsBy'] = $groupsBy; $return['groupsBy'] = $groupsBy;
$return['data']['data'] = alerts_get_alert_fired( $return['data'] = alerts_get_alert_fired($filters, $groupsBy);
$filters,
$groupsBy,
false
);
if ((bool) $show_summary === true) {
$return['data']['summary'] = alerts_get_alert_fired(
$filters,
$groupsBy,
true
);
}
return reporting_check_structure_content($return); return reporting_check_structure_content($return);
} }

View File

@ -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) function reporting_html_alert_report_actions($table, $item, $pdf=0)
{ {
hd(6);
$data = $item['data']; $data = $item['data'];
$filters = $item['filters'];
$groupsBy = $item['groupsBy']; $groupsBy = $item['groupsBy'];
$output = ''; $output = '';
@ -2795,14 +2801,19 @@ function get_alert_table($data)
$table->width = '100%'; $table->width = '100%';
$table->data = []; $table->data = [];
$table->head = []; $table->head = [];
$table->headstyle = [];
$table->cellstyle = [];
$table->headstyle[0] = 'text-align:left;';
$head = reset($data); $head = reset($data);
foreach (array_reverse(array_keys($head)) as $name) { foreach (array_reverse(array_keys($head)) as $name) {
$table->head[$name] = $name; $table->head[] = $name;
} }
foreach ($data as $key => $params) { foreach ($data as $key => $params) {
$table->cellstyle[$key][0] = 'text-align:left;';
foreach (array_reverse($params) as $name => $value) { foreach (array_reverse($params) as $name => $value) {
$table->data[$key][$name] = $value; $table->data[$key][] = $value;
} }
} }