Merge branch 'ent-6346-9940-event-group-no-muestra-eventos-para-grupos-secundarios' into 'develop'

Fix secondary groups match while group filtering

See merge request artica/pandorafms!3502
This commit is contained in:
Daniel Rodriguez 2020-10-08 16:34:20 +02:00
commit 76c3fe8e83
4 changed files with 404 additions and 96 deletions

View File

@ -2830,3 +2830,83 @@ function alerts_ui_update_or_create_actions($update=true)
$update ? __('Could not be updated') : __('Could not be created') $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;
}

View File

@ -707,7 +707,9 @@ function events_get_all(
} }
$count = false; $count = false;
if (!is_array($fields) && $fields == 'count') { if (is_array($fields) === false && $fields === 'count'
|| (is_array($fields) === true && $fields[0] === 'count')
) {
$fields = ['te.*']; $fields = ['te.*'];
$count = true; $count = true;
} else if (!is_array($fields)) { } else if (!is_array($fields)) {
@ -877,9 +879,10 @@ function events_get_all(
$groups $groups
); );
if (!$propagate) { if (!$propagate && isset($groups)) {
$sql_filters[] = sprintf( $sql_filters[] = sprintf(
' AND (te.id_grupo = %d OR tasg.id_group = %d)', ' AND (te.id_grupo = %d OR tasg.id_group = %d)',
$groups,
$groups $groups
); );
} else { } else {
@ -1063,6 +1066,7 @@ function events_get_all(
} }
} }
$_tmp = '';
foreach ($tags as $id_tag) { foreach ($tags as $id_tag) {
if (!isset($tags_names[$id_tag])) { if (!isset($tags_names[$id_tag])) {
$tags_names[$id_tag] = tags_get_name($id_tag); $tags_names[$id_tag] = tags_get_name($id_tag);
@ -1304,6 +1308,7 @@ function events_get_all(
$tgrupo_join = 'LEFT'; $tgrupo_join = 'LEFT';
$tgrupo_join_filters = []; $tgrupo_join_filters = [];
if (isset($groups) if (isset($groups)
&& (is_array($groups) && (is_array($groups)
|| $groups > 0) || $groups > 0)
@ -1311,17 +1316,21 @@ function events_get_all(
$tgrupo_join = 'INNER'; $tgrupo_join = 'INNER';
if (is_array($groups)) { if (is_array($groups)) {
$tgrupo_join_filters[] = sprintf( $tgrupo_join_filters[] = sprintf(
' AND (tg.id_grupo IN (%s) OR tasg.id_group IN (%s))', ' (te.id_grupo = tg.id_grupo AND tg.id_grupo IN (%s))
OR (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))',
join(', ', $groups), join(', ', $groups),
join(', ', $groups) join(', ', $groups)
); );
} else { } else {
$tgrupo_join_filters[] = sprintf( $tgrupo_join_filters[] = sprintf(
' AND (tg.id_grupo = %s OR tasg.id_group = %s)', ' (te.id_grupo = tg.id_grupo AND tg.id_grupo = %s)
OR (tg.id_grupo = tasg.id_group AND tasg.id_group = %s)',
$groups, $groups,
$groups $groups
); );
} }
} else {
$tgrupo_join_filters[] = ' te.id_grupo = tg.id_grupo';
} }
$server_join = ''; $server_join = '';
@ -1376,8 +1385,7 @@ function events_get_all(
%s %s
%s %s
%s JOIN tgrupo tg %s JOIN tgrupo tg
ON te.id_grupo = tg.id_grupo ON %s
%s
%s %s
WHERE 1=1 WHERE 1=1
%s %s
@ -1567,7 +1575,9 @@ function events_get_events_no_grouped(
$table = events_get_events_table($meta, $history); $table = events_get_events_table($meta, $history);
$sql = 'SELECT * FROM '.$table.' te WHERE 1=1 '.$sql_post; $sql = 'SELECT * FROM '.$table.' te ';
$sql .= events_get_secondary_groups_left_join($table);
$sql .= $sql_post;
$events = db_get_all_rows_sql($sql, $history_db); $events = db_get_all_rows_sql($sql, $history_db);
@ -1942,7 +1952,7 @@ function events_change_status(
* *
* @param mixed $id_event Event ID or array of events. * @param mixed $id_event Event ID or array of events.
* @param string $new_owner Id_user of the new owner. If is false, the current * @param string $new_owner Id_user of the new owner. If is false, the current
* owner will be setted. * owner will be set, if empty, will be cleaned.
* @param boolean $force Flag to force the change or not (not force is * @param boolean $force Flag to force the change or not (not force is
* change only when it hasn't owner). * change only when it hasn't owner).
* @param boolean $meta Metaconsole mode flag. * @param boolean $meta Metaconsole mode flag.
@ -1982,11 +1992,10 @@ function events_change_owner(
return false; return false;
} }
// If no new_owner is provided, the current user will be the owner if ($new_owner === false) {
// * #2250: Comment this lines because if possible selected None owner. $new_owner = $config['id_user'];
// if (empty($new_owner)) { }
// $new_owner = $config['id_user'];
// }
// Only generate comment when is forced (sometimes is owner changes when // Only generate comment when is forced (sometimes is owner changes when
// comment). // comment).
if ($force) { if ($force) {
@ -1995,7 +2004,9 @@ function events_change_owner(
'', '',
'Change owner to '.$new_owner, 'Change owner to '.$new_owner,
$meta, $meta,
$history $history,
true,
false
); );
} }
@ -2051,13 +2062,14 @@ function events_get_events_table($meta, $history)
/** /**
* Comment events in a transresponse * Comment events in a transresponse
* *
* @param mixed $id_event Event ID or array of events. * @param mixed $id_event Event ID or array of events.
* @param string $comment Comment to be registered. * @param string $comment Comment to be registered.
* @param string $action Action performed with comment. By default just add * @param string $action Action performed with comment. By default just add
* a comment. * a comment.
* @param boolean $meta Flag of metaconsole mode. * @param boolean $meta Flag of metaconsole mode.
* @param boolean $history Flag of history mode. * @param boolean $history Flag of history mode.
* @param boolean $similars Similars. * @param boolean $similars Similars.
* @param boolean $update_owner Update owner.
* *
* @return boolean Whether or not it was successful * @return boolean Whether or not it was successful
*/ */
@ -2067,7 +2079,8 @@ function events_comment(
$action='Added comment', $action='Added comment',
$meta=false, $meta=false,
$history=false, $history=false,
$similars=true $similars=true,
$update_owner=true
) { ) {
global $config; global $config;
@ -2095,8 +2108,10 @@ function events_comment(
return false; return false;
} }
// If the event hasn't owner, assign the user as owner. if ($update_owner) {
events_change_owner($id_event); // If the event hasn't owner, assign the user as owner.
events_change_owner($id_event);
}
// Get the current event comments. // Get the current event comments.
$first_event = $id_event; $first_event = $id_event;
@ -2975,9 +2990,13 @@ function events_get_agent(
if ($events_group) { if ($events_group) {
$sql_where .= sprintf( $sql_where .= sprintf(
' AND id_grupo IN (%s) AND utimestamp > %d ' INNER JOIN tgrupo tg
AND utimestamp <= %d ', ON (te.id_grupo = tg.id_grupo AND tg.id_grupo = %s)
implode(',', $id_group), OR (tg.id_grupo = tasg.id_group AND tasg.id_group = %s)
WHERE utimestamp > %d
AND utimestamp <= %d ',
join(',', $id_group),
join(',', $id_group),
$datelimit, $datelimit,
$date $date
); );
@ -5013,20 +5032,26 @@ function events_get_count_events_by_agent(
$tevento = 'tevento'; $tevento = 'tevento';
$sql = sprintf( $sql = sprintf(
'SELECT id_agente, 'SELECT
(SELECT t2.alias ta.id_agente,
FROM %s t2 ta.alias as agent_name,
WHERE t2.id_agente = t3.id_agente) AS agent_name, count(*) as count
COUNT(*) AS count FROM %s te
FROM %s t3 %s
INNER JOIN %s ta
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 (tg.id_grupo = tasg.id_group AND tasg.id_group IN (%s))
WHERE utimestamp > %d AND utimestamp <= %d WHERE utimestamp > %d AND utimestamp <= %d
AND id_grupo IN (%s) GROUP BY ta.id_agente',
GROUP BY id_agente',
$tagente,
$tevento, $tevento,
events_get_secondary_groups_left_join($tevento),
$tagente,
implode(',', $id_group),
implode(',', $id_group),
$datelimit, $datelimit,
$date, $date,
implode(',', $id_group),
$sql_where $sql_where
); );
@ -5077,8 +5102,10 @@ function events_get_count_events_validated_by_user(
$dbmeta=false $dbmeta=false
) { ) {
global $config; global $config;
$tevento = 'tevento';
// Group. // Group.
$sql_filter = ' AND 1=1 '; $tgroup_join = '';
if (isset($filter['id_group'])) { if (isset($filter['id_group'])) {
$id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR'); $id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR');
@ -5087,7 +5114,15 @@ function events_get_count_events_validated_by_user(
return false; return false;
} }
$sql_filter .= sprintf(' AND id_grupo IN (%s) ', implode(',', $id_group)); $tgroup_join = sprintf(
'%s
INNER JOIN tgrupo tg
ON (te.id_grupo = tg.id_grupo AND tg.id_grupo 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)
);
} }
if (!empty($filter['id_agent'])) { if (!empty($filter['id_agent'])) {
@ -5186,24 +5221,29 @@ function events_get_count_events_validated_by_user(
$sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")';
} }
$tevento = 'tevento';
$sql = sprintf( $sql = sprintf(
'SELECT id_usuario, 'SELECT
(SELECT t2.fullname te.id_usuario,
FROM tusuario t2 tu.fullname as user_name,
WHERE t2.id_user = t3.id_usuario) AS user_name, count(*) as count
COUNT(*) AS count FROM %s te
FROM %s t3 %s
WHERE utimestamp > %d AND utimestamp <= %d LEFT JOIN tusuario tu
%s %s ON te.owner_user = tu.id_user
GROUP BY id_usuario', WHERE
te.utimestamp > %d AND te.utimestamp <= %d
AND te.estado = %d
%s
GROUP BY te.id_usuario',
$tevento, $tevento,
$tgroup_join,
$datelimit, $datelimit,
$date, $date,
EVENT_VALIDATE,
$sql_filter, $sql_filter,
$sql_where $sql_where
); );
$rows = db_get_all_rows_sql($sql); $rows = db_get_all_rows_sql($sql);
if ($rows == false) { if ($rows == false) {
@ -5214,7 +5254,7 @@ function events_get_count_events_validated_by_user(
foreach ($rows as $row) { foreach ($rows as $row) {
$user_name = $row['user_name']; $user_name = $row['user_name'];
if (empty($row['user_name'])) { if (empty($row['user_name'])) {
$user_name = __('Unknown'); $user_name = __('Validated but not assigned');
} }
$return[$user_name] = $row['count']; $return[$user_name] = $row['count'];
@ -5252,7 +5292,10 @@ function events_get_count_events_by_criticity(
) { ) {
global $config; global $config;
$sql_filter = ' AND 1=1 '; $tevento = 'tevento';
$sql_filter = '';
$tgroup_join = '';
if (isset($filter['id_group'])) { if (isset($filter['id_group'])) {
$id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR'); $id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR');
@ -5261,7 +5304,15 @@ function events_get_count_events_by_criticity(
return false; return false;
} }
$sql_filter .= sprintf(' AND id_grupo IN (%s) ', implode(',', $id_group)); $tgroup_join = sprintf(
'%s
INNER JOIN tgrupo tg
ON (te.id_grupo = tg.id_grupo AND tg.id_grupo 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)
);
} }
if (!empty($filter['id_agent'])) { if (!empty($filter['id_agent'])) {
@ -5361,16 +5412,19 @@ function events_get_count_events_by_criticity(
$sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")';
} }
$tevento = 'tevento';
$sql = sprintf( $sql = sprintf(
'SELECT criticity, 'SELECT
COUNT(*) AS count te.criticity,
FROM %s count(*) as count
WHERE utimestamp > %d AND utimestamp <= %d FROM %s te
%s %s %s
GROUP BY criticity', WHERE
te.utimestamp > %d AND te.utimestamp <= %d
%s
%s
GROUP BY te.id_usuario',
$tevento, $tevento,
$tgroup_join,
$datelimit, $datelimit,
$date, $date,
$sql_filter, $sql_filter,
@ -5419,23 +5473,26 @@ function events_get_count_events_validated(
$dbmeta=false $dbmeta=false
) { ) {
global $config; global $config;
$tevento = 'tevento';
// Group. // Group.
$sql_filter = ' 1=1 '; $sql_filter = '';
$tgroup_join = '';
if (isset($filter['id_group'])) { if (isset($filter['id_group'])) {
$id_group = groups_safe_acl( $id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR');
$config['id_user'],
$filter['id_group'],
'AR'
);
if (empty($id_group)) { if (empty($id_group)) {
// An empty array means the user doesn't have access. // An empty array means the user doesn't have access.
return false; return false;
} }
$sql_filter .= sprintf( $tgroup_join = sprintf(
' AND id_grupo IN (%s) ', '%s
INNER JOIN tgrupo tg
ON (te.id_grupo = tg.id_grupo AND tg.id_grupo 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) implode(',', $id_group)
); );
} }
@ -5566,9 +5623,24 @@ function events_get_count_events_validated(
$sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")';
} }
$tevento = 'tevento'; $sql = sprintf(
'SELECT
$sql = sprintf('SELECT estado, COUNT(*) AS count FROM %s WHERE %s %s GROUP BY estado', $tevento, $sql_filter, $sql_where); te.estado,
count(*) as count
FROM %s te
%s
WHERE
te.utimestamp > %d AND te.utimestamp <= %d
%s
%s
GROUP BY te.id_usuario',
$tevento,
$tgroup_join,
$datelimit,
$date,
$sql_filter,
$sql_where
);
$rows = db_get_all_rows_sql($sql); $rows = db_get_all_rows_sql($sql);

View File

@ -3910,30 +3910,10 @@ function reporting_alert_report_group($report, $content)
$return['description'] = $content['description']; $return['description'] = $content['description'];
$return['date'] = reporting_get_date_text($report, $content); $return['date'] = reporting_get_date_text($report, $content);
if ($content['id_group'] == 0) { $agent_modules = alerts_get_agent_modules(
$agent_modules = db_get_all_rows_sql( $content['id_group'],
' $content['recursion']
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'].'))'
);
}
if (empty($alerts)) { if (empty($alerts)) {
$alerts = []; $alerts = [];

View File

@ -0,0 +1,176 @@
<?php
/**
* Event entity class.
*
* @category Class
* @package Pandora FMS
* @subpackage OpenSource
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2019 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
// Begin.
namespace PandoraFMS;
global $config;
require_once $config['homedir'].'/include/functions_events.php';
/**
* PandoraFMS Group entity.
*/
class Event extends Entity
{
/**
* List of available ajax methods.
*
* @var array
*/
private static $ajaxMethods = [];
/**
* Builds a PandoraFMS\Group object from a group id.
*
* @param integer $id_group Group Id.
*/
public function __construct(?int $id_group=null)
{
$table = 'tevento';
if ((bool) \is_metaconsole() === true) {
$table = 'tmetaconsole_event';
}
if ($id_group === 0) {
parent::__construct($table);
} else if (is_numeric($id_group) === true) {
parent::__construct($table, ['id_grupo' => $id_group]);
} else {
// Empty skel.
parent::__construct($table);
}
}
/**
* Retrieves all events matching given filters.
*
* @param array $fields Fields to retrieve.
* @param array $filter Filter.
* @param integer $offset Offset.
* @param integer $limit Limit.
* @param string $order Order (asc or desc).
* @param string $sort_field Sort field.
* @param boolean $history Search history.
* @param boolean $return_sql Return sql or execute it.
* @param string $having Having.
*
* @return array|string|falsse Found events or SQL query or error.
*/
public static function search(
array $fields,
array $filter,
?int $offset=null,
?int $limit=null,
?string $order=null,
?string $sort_field=null,
bool $history=false,
bool $return_sql=false,
string $having=''
):array {
return \events_get_all(
$fields,
$filter,
$offset,
$limit,
$order,
$sort_field,
$history,
$return_sql,
$having
);
}
/**
* Saves current group definition to database.
*
* @return mixed Affected rows of false in case of error.
* @throws \Exception On error.
*/
public function save()
{
global $config;
if (isset($config['centralized_management']) === true
&& $config['centralized_management'] > 0
) {
throw new \Exception(
get_class($this).' error, cannot be modified while centralized management environment.'
);
}
if ($this->id_evento === null) {
// New.
return db_process_sql_insert(
'tgrupo',
$this->fields
);
} else if ($this->fields['id_evento'] > 0) {
// Update.
return db_process_sql_update(
'tgrupo',
$this->fields,
['id_evento' => $this->fields['id_evento']]
);
}
return false;
}
/**
* Return error message to target.
*
* @param string $msg Error message.
*
* @return void
*/
public static function error(string $msg)
{
echo json_encode(['error' => $msg]);
}
/**
* Verifies target method is allowed to be called using AJAX call.
*
* @param string $method Method to be invoked via AJAX.
*
* @return boolean Available (true), or not (false).
*/
public static function ajaxMethod(string $method):bool
{
return in_array($method, self::$ajaxMethods) === true;
}
}