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

@ -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,57 +3080,45 @@ 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;
case 'template': case 'template':
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,7 +3127,6 @@ 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
) { ) {
@ -3140,7 +3136,6 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
); );
$group_array[] = 'period'; $group_array[] = 'period';
} }
}
$group_by = ''; $group_by = '';
if (is_array($group_array) === true && empty($group_array) === false) { if (is_array($group_array) === true && empty($group_array) === false) {
@ -3183,18 +3178,15 @@ 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, $data_query,
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
@ -3257,29 +3249,34 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
$result[$period][$server.'|'.$id] = $array_data[$server][$id]; $result[$period][$server.'|'.$id] = $array_data[$server][$id];
$result[$period][$server.'|'.$id][$groupsBy['group_by']] = $name; $result[$period][$server.'|'.$id][$groupsBy['group_by']] = $name;
} else { } 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 { } else {
foreach ($names_search as $id => $name) { foreach ($names_search as $id => $name) {
if (isset($array_data[$id]) === true) { if (isset($array_data[$id]) === true) {
$result[$period][$id] = $array_data[$id]; $result[$period][$id] = $array_data[$id];
$result[$period][$id][$groupsBy['group_by']] = $name; $result[$period][$id][$groupsBy['group_by']] = $name;
} else { } else {
if ($only_data === false) {
$clone[$groupsBy['group_by']] = $name; $clone[$groupsBy['group_by']] = $name;
$result[$period][$id] = $clone; $result[$period][$id] = $clone;
} }
} }
} }
} }
}
} else { } else {
$period_lapse = (int) $groupsBy['lapse']; $period_lapse = (int) $groupsBy['lapse'];
foreach ($intervals as $interval) { foreach ($intervals as $interval) {
$start_interval = $interval; $start_interval = $interval;
$end_interval = ($interval + $period_lapse); $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,6 +3294,28 @@ 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) {
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].' &raquo; '.$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) {
$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) { foreach ($data as $period => $array_data) {
$period_time = (int) $period; $period_time = (int) $period;
@ -3338,17 +3357,27 @@ function alerts_get_alert_fired($filters=[], $groupsBy=[], $total=false)
} }
} }
} }
} 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,6 +2722,7 @@ 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,
@ -2725,6 +2731,8 @@ function reporting_alert_report_actions($report, $content)
'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;
} }
} }