Fixed template events

This commit is contained in:
daniel 2020-10-30 13:29:50 +01:00 committed by Daniel Rodriguez
parent 2063849dd0
commit b3043c2e01
4 changed files with 88 additions and 60 deletions

View File

@ -1552,26 +1552,18 @@ function events_get_event($id, $fields=false, $meta=false, $history=false)
/** /**
* Retrieve all events ungrouped. * Retrieve all events ungrouped.
* *
* @param string $sql_post Sql_post. * @param string $sql_post Sql_post.
* @param integer $offset Offset. * @param boolean $meta Meta.
* @param integer $pagination Pagination. * @param boolean $history History.
* @param boolean $meta Meta. * @param boolean $returnSql Only Query.
* @param boolean $history History.
* @param boolean $total Total.
* @param boolean $history_db History_db.
* @param string $order Order.
* *
* @return mixed Array of events or false. * @return mixed Array of events or false.
*/ */
function events_get_events_no_grouped( function events_get_events_no_grouped(
$sql_post, $sql_post,
$offset=0,
$pagination=1,
$meta=false, $meta=false,
$history=false, $history=false,
$total=false, $returnSql=false
$history_db=false,
$order='ASC'
) { ) {
global $config; global $config;
@ -1581,7 +1573,11 @@ function events_get_events_no_grouped(
$sql .= events_get_secondary_groups_left_join($table); $sql .= events_get_secondary_groups_left_join($table);
$sql .= $sql_post; $sql .= $sql_post;
$events = db_get_all_rows_sql($sql, $history_db); if ($returnSql === true) {
return $sql;
}
$events = db_get_all_rows_sql($sql, $history);
return $events; return $events;
} }
@ -1634,29 +1630,59 @@ function events_get_events_grouped(
$event_lj = events_get_secondary_groups_left_join($table); $event_lj = events_get_secondary_groups_left_join($table);
if ($total) { if ($total) {
$sql = "SELECT COUNT(*) FROM (SELECT id_evento $sql = "SELECT COUNT(*) FROM (SELECT id_evento
FROM $table te $event_lj FROM $table te $event_lj ".$sql_post.'
WHERE 1=1 ".$sql_post.'
GROUP BY estado, evento, id_agente, id_agentmodule'.$groupby_extra.') AS t'; GROUP BY estado, evento, id_agente, id_agentmodule'.$groupby_extra.') AS t';
} else { } else {
$sql = "SELECT *, MAX(id_evento) AS id_evento, $sql = sprintf(
GROUP_CONCAT(DISTINCT user_comment SEPARATOR '<br>') AS user_comment, 'SELECT *,
GROUP_CONCAT(DISTINCT id_evento SEPARATOR ',') AS similar_ids, MAX(id_evento) AS id_evento,
COUNT(id_evento) AS event_rep, MAX(utimestamp) AS timestamp_rep, GROUP_CONCAT(
MIN(utimestamp) AS timestamp_rep_min, DISTINCT user_comment SEPARATOR "<br>"
(SELECT owner_user FROM $table WHERE id_evento = MAX(te.id_evento)) owner_user, ) AS user_comment,
(SELECT id_usuario FROM $table WHERE id_evento = MAX(te.id_evento)) id_usuario, GROUP_CONCAT(
(SELECT id_agente FROM $table WHERE id_evento = MAX(te.id_evento)) id_agente, DISTINCT id_evento SEPARATOR ","
(SELECT criticity FROM $table WHERE id_evento = MAX(te.id_evento)) AS criticity, ) AS similar_ids,
(SELECT ack_utimestamp FROM $table WHERE id_evento = MAX(te.id_evento)) AS ack_utimestamp, COUNT(id_evento) AS event_rep, MAX(utimestamp) AS timestamp_rep,
(SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = te.id_agentmodule) AS module_name MIN(utimestamp) AS timestamp_rep_min,
FROM $table te $event_lj (SELECT owner_user
WHERE 1=1 ".$sql_post.' FROM %s
GROUP BY estado, evento, id_agente, id_agentmodule'.$groupby_extra; WHERE id_evento = MAX(te.id_evento)) AS owner_user,
$sql .= ' '.events_get_sql_order($sort_field, $order, 2); (SELECT id_usuario
$sql .= ' LIMIT '.$offset.','.$pagination; FROM %s
WHERE id_evento = MAX(te.id_evento)) AS id_usuario,
(SELECT id_agente
FROM %s
WHERE id_evento = MAX(te.id_evento)) AS id_agente,
(SELECT criticity
FROM %s
WHERE id_evento = MAX(te.id_evento)) AS criticity,
(SELECT ack_utimestamp
FROM %s
WHERE id_evento = MAX(te.id_evento)) AS ack_utimestamp,
(SELECT nombre
FROM tagente_modulo
WHERE id_agente_modulo = te.id_agentmodule) AS module_name
FROM %s te %s
%s
GROUP BY estado, evento, id_agente, id_agentmodule %s
%s
LIMIT %d, %d',
$table,
$table,
$table,
$table,
$table,
$table,
$event_lj,
$sql_post,
$groupby_extra,
events_get_sql_order($sort_field, $order, 2),
$offset,
$pagination
);
} }
// Extract the events by filter (or not) from db // Extract the events by filter (or not) from db.
$events = db_get_all_rows_sql($sql, $history_db); $events = db_get_all_rows_sql($sql, $history_db);
if ($total) { if ($total) {
@ -2971,7 +2997,8 @@ function events_get_agent(
$type = []; $type = [];
foreach ($filter_event_type as $event_type) { foreach ($filter_event_type as $event_type) {
if ($event_type != '') { if ($event_type != '') {
// If normal, warning, could be several (going_up_warning, going_down_warning... too complex. // If normal, warning, could be several
// (going_up_warning, going_down_warning... too complex.
// Shown to user only "warning, critical and normal". // Shown to user only "warning, critical and normal".
if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') { if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') {
$type[] = " event_type LIKE '%".$event_type."%' "; $type[] = " event_type LIKE '%".$event_type."%' ";
@ -2991,10 +3018,10 @@ function events_get_agent(
} }
if ($events_group) { if ($events_group) {
$sql_where .= sprintf( $secondary_groups = sprintf(
' INNER JOIN tgrupo tg ' INNER JOIN tgrupo tg
ON (te.id_grupo = tg.id_grupo AND tg.id_grupo = %s) ON (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s))
OR (tg.id_grupo = tasg.id_group AND tasg.id_group = %s) OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))
WHERE utimestamp > %d WHERE utimestamp > %d
AND utimestamp <= %d ', AND utimestamp <= %d ',
join(',', $id_group), join(',', $id_group),
@ -3002,22 +3029,27 @@ function events_get_agent(
$datelimit, $datelimit,
$date $date
); );
} else if ($events_module) { $sql_where = $secondary_groups.' '.$sql_where;
$sql_where .= sprintf(
' AND id_agentmodule = %d AND utimestamp > %d
AND utimestamp <= %d ',
$id_agent_module,
$datelimit,
$date
);
} else { } else {
$sql_where .= sprintf( $sql_where = ' WHERE 1=1 '.$sql_where;
' AND id_agente = %d AND utimestamp > %d
AND utimestamp <= %d ', if ($events_module) {
$id_agent, $sql_where .= sprintf(
$datelimit, ' AND id_agentmodule = %d AND utimestamp > %d
$date AND utimestamp <= %d ',
); $id_agent_module,
$datelimit,
$date
);
} else {
$sql_where .= sprintf(
' AND id_agente = %d AND utimestamp > %d
AND utimestamp <= %d ',
$id_agent,
$datelimit,
$date
);
}
} }
if (is_metaconsole() && $id_server) { if (is_metaconsole() && $id_server) {
@ -3037,11 +3069,7 @@ function events_get_agent(
} else { } else {
return events_get_events_no_grouped( return events_get_events_no_grouped(
$sql_where, $sql_where,
0,
1000,
(is_metaconsole() && $id_server) ? true : false, (is_metaconsole() && $id_server) ? true : false,
false,
false,
$history $history
); );
} }

View File

@ -2957,7 +2957,7 @@ function reporting_group_report($report, $content)
if (empty($id_group)) { if (empty($id_group)) {
$events = []; $events = [];
} else { } else {
$sql_where = sprintf(' AND id_grupo IN (%s) AND estado<>1 ', implode(',', $id_group)); $sql_where = sprintf(' WHERE id_grupo IN (%s) AND estado<>1 ', implode(',', $id_group));
$events = events_get_events_grouped( $events = events_get_events_grouped(
$sql_where, $sql_where,
0, 0,

View File

@ -731,7 +731,7 @@ class Events
$system = System::getInstance(); $system = System::getInstance();
// --------------Fill the SQL POST------------------------------- // --------------Fill the SQL POST-------------------------------
$sql_post = ''; $sql_post = ' WHERE 1=1 ';
switch ($this->status) { switch ($this->status) {
case 0: case 0:

View File

@ -311,7 +311,7 @@ if ($user_filter != 0 && empty($id_name) && !$update_from_filter_table) {
} }
// Build the condition of the events query. // Build the condition of the events query.
$sql_post = ''; $sql_post = ' WHERE 1=1 ';
$id_user = $config['id_user']; $id_user = $config['id_user'];
@ -1214,7 +1214,7 @@ if ($group_rep == 0) {
$sql = 'SELECT COUNT(DISTINCT id_evento) $sql = 'SELECT COUNT(DISTINCT id_evento)
FROM $event_table te FROM $event_table te
$event_lj $event_lj
WHERE 1=1 $sql_post'; $sql_post';
$total_events = (int) db_get_sql($sql); $total_events = (int) db_get_sql($sql);
} else if ($group_rep == 1) { } else if ($group_rep == 1) {
$total_events = events_get_events_grouped( $total_events = events_get_events_grouped(