13639-Fix snmp console group acl & filter by alias

This commit is contained in:
Pablo Aragon 2024-05-06 13:43:36 +02:00
parent ea963d6ab4
commit 0a397dbbec
2 changed files with 154 additions and 121 deletions

View File

@ -85,6 +85,13 @@ class SnmpConsole extends HTML
*/ */
private $filter_free_search; private $filter_free_search;
/**
* Filter alias.
*
* @var string
*/
private $filter_alias_search;
/** /**
* Filter status. * Filter status.
* *
@ -135,7 +142,8 @@ class SnmpConsole extends HTML
int $filter_group_by, int $filter_group_by,
int $filter_hours_ago, int $filter_hours_ago,
int $filter_trap_type, int $filter_trap_type,
int $refr int $refr,
string $filter_alias_search
) { ) {
global $config; global $config;
@ -157,6 +165,7 @@ class SnmpConsole extends HTML
$this->filter_alert = $filter_alert; $this->filter_alert = $filter_alert;
$this->filter_severity = $filter_severity; $this->filter_severity = $filter_severity;
$this->filter_free_search = $filter_free_search; $this->filter_free_search = $filter_free_search;
$this->filter_alias_search = $filter_alias_search;
$this->filter_status = $filter_status; $this->filter_status = $filter_status;
$this->filter_group_by = $filter_group_by; $this->filter_group_by = $filter_group_by;
$this->filter_hours_ago = $filter_hours_ago; $this->filter_hours_ago = $filter_hours_ago;
@ -375,13 +384,21 @@ class SnmpConsole extends HTML
'style' => 'widht:100%', 'style' => 'widht:100%',
], ],
[ [
'label' => __('Free search'), 'label' => __('Search').ui_print_help_tip(__('It will search through the contents of the raw trap, which does not include the agent name. You can search by the next values: IP, OID, Value, Text, Description, User ID and Source.'), true),
'type' => 'text', 'type' => 'text',
'id' => 'filter_free_search', 'id' => 'filter_free_search',
'input_class' => 'filter_input_datatable', 'input_class' => 'filter_input_datatable',
'name' => 'filter_free_search', 'name' => 'filter_free_search',
'value' => $this->filter_free_search, 'value' => $this->filter_free_search,
], ],
[
'label' => __('Search Alias').ui_print_help_tip(__('It will search by the agent alias. This option could make the system slower.'), true),
'type' => 'text',
'id' => 'filter_alias_search',
'input_class' => 'filter_input_datatable',
'name' => 'filter_alias_search',
'value' => $this->filter_alias_search,
],
[ [
'label' => __('Status'), 'label' => __('Status'),
'type' => 'select', 'type' => 'select',
@ -449,7 +466,7 @@ class SnmpConsole extends HTML
1000, 1000,
], ],
], ],
'filter_main_class' => 'box-flat white_table_graph fixed_filter_bar', 'filter_main_class' => 'box-flat white_table_graph fixed_filter_bar snmp-console-filter',
] ]
); );
} catch (Exception $e) { } catch (Exception $e) {
@ -554,10 +571,6 @@ class SnmpConsole extends HTML
$filters = get_parameter('filter', []); $filters = get_parameter('filter', []);
// Build ranges. // Build ranges.
if (empty($filters['filter_hours_ago']) === true) {
$filters['filter_hours_ago'] = 8;
}
$now_timestamp = time(); $now_timestamp = time();
$interval_seconds = ($filters['filter_hours_ago'] * 3600); $interval_seconds = ($filters['filter_hours_ago'] * 3600);
$ago_timestamp = ($now_timestamp - $interval_seconds); $ago_timestamp = ($now_timestamp - $interval_seconds);
@ -577,141 +590,148 @@ class SnmpConsole extends HTML
ob_start(); ob_start();
$data = []; $data = [];
$user_groups = users_get_groups($config['id_user'], 'AR', false); $whereSubquery = '';
$prea = array_keys($user_groups); $sql = 'SELECT
$ids = join(',', $prea); *
$sql_count = sprintf(
'SELECT
count(DISTINCT(id_usuario))
FROM FROM
tusuario_perfil ttrap
WHERE %s
id_usuario = "%s" WHERE 1=1
AND id_grupo IN ( %s
SELECT ORDER BY
id_grupo timestamp DESC
FROM LIMIT %d, %d';
tgrupo
WHERE
id_grupo IN (%s)
AND id_grupo NOT IN (
SELECT
id_grupo
FROM
tagente))',
$config['id_user'],
$ids
);
$user_in_group_wo_agents = db_get_value_sql($sql_count); $sql_count = 'SELECT
if ($user_in_group_wo_agents == 0) { COUNT(`ttrap`.`id_trap`)
FROM
ttrap
%s
WHERE 1=1
%s';
if (users_is_admin() === false || users_can_manage_group_all('AR') === false) {
$user_groups = users_get_groups($config['id_user'], 'AR', false);
$rows = db_get_all_rows_filter( $rows = db_get_all_rows_filter(
'tagente', 'tagente',
['id_grupo' => array_keys($user_groups)], ['id_grupo' => array_keys($user_groups)],
['id_agente'] ['id_agente']
); );
$id_agents = []; $id_agents = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$id_agents[] = $row['id_agente']; $id_agents[] = $row['id_agente'];
} }
if (!empty($id_agents)) { if (empty($id_agents) === false) {
$address_by_user_groups = agents_get_addresses($id_agents); $address_by_user_groups = agents_get_addresses($id_agents);
foreach ($address_by_user_groups as $i => $a) { foreach ($address_by_user_groups as $i => $a) {
$address_by_user_groups[$i] = '"'.$a.'"'; $address_by_user_groups[$i] = '"'.$a.'"';
} }
} }
} else {
$rows = db_get_all_rows_filter( if (empty($address_by_user_groups) === true) {
'tagente', $address_by_user_groups = [];
[], array_unshift($address_by_user_groups, '""');
['id_agente'] }
$whereSubquery .= sprintf(
'AND (
`ttrap`.`source` IN (%s) OR
`ttrap`.`source`=""
)',
implode(',', $address_by_user_groups)
); );
$id_agents = [];
foreach ($rows as $row) {
$id_agents[] = $row['id_agente'];
}
$all_address_agents = agents_get_addresses($id_agents);
foreach ($all_address_agents as $i => $a) {
$all_address_agents[$i] = '"'.$a.'"';
}
} }
if (empty($address_by_user_groups)) { if ($filters['filter_alias_search'] !== '') {
$address_by_user_groups = []; $sql_join = '
array_unshift($address_by_user_groups, '""'); LEFT JOIN `tagente`
ON `tagente`.`direccion` = `ttrap`.`source`
LEFT JOIN `taddress_agent`
ON `tagente`.`id_agente` = `taddress_agent`.`id_agent`
LEFT JOIN `taddress`
ON `taddress_agent`.`id_a` = `taddress`.`id_a`
';
} else {
$sql_join = '';
} }
if (empty($all_address_agents)) {
$all_address_agents = [];
array_unshift($all_address_agents, '""');
}
$sql = 'SELECT * FROM ttrap
WHERE (
`source` IN ('.implode(',', $address_by_user_groups).") OR
`source`='' OR
`source` NOT IN (".implode(',', $all_address_agents).')
)
%s
ORDER BY timestamp DESC
LIMIT %d,%d';
$whereSubquery = '';
if ($filters['filter_alert'] != -1) { if ($filters['filter_alert'] != -1) {
$whereSubquery .= ' AND alerted = '.$filters['filter_alert']; $whereSubquery .= ' AND `ttrap`.`alerted` = '.$filters['filter_alert'];
} }
$filters['filter_severity'] = (int) $filters['filter_severity'];
if ($filters['filter_severity'] != -1) { if ($filters['filter_severity'] != -1) {
// There are two special severity values aimed to match two different trap standard severities // There are two special severity values aimed to match two different trap standard severities
// in database: warning/critical and critical/normal. // in database: warning/critical and critical/normal.
if ($filters['filter_severity'] !== EVENT_CRIT_OR_NORMAL if ($filters['filter_severity'] != EVENT_CRIT_OR_NORMAL
&& $filters['filter_severity'] !== EVENT_CRIT_WARNING_OR_CRITICAL && $filters['filter_severity'] != EVENT_CRIT_WARNING_OR_CRITICAL
&& $filters['filter_severity'] !== EVENT_CRIT_NOT_NORMAL
) { ) {
// Test if enterprise is installed to search oid in text or oid field in ttrap. // Test if enterprise is installed to search oid in text or oid field in ttrap.
$whereSubquery .= ' AND severity = '.$filters['filter_severity']; if ($config['enterprise_installed']) {
$whereSubquery .= ' AND (
(`ttrap`.`alerted` = 0 AND `ttrap`.`severity` = '.$filters['filter_severity'].') OR
(`ttrap`.`alerted` = 1 AND `ttrap`.`priority` = '.$filters['filter_severity'].'))';
} else {
$whereSubquery .= ' AND (
(`ttrap`.`alerted` = 0 AND 1 = '.$filters['filter_severity'].') OR
(`ttrap`.`alerted` = 1 AND `ttrap`.`priority` = '.$filters['filter_severity'].'))';
}
} else if ($filters['filter_severity'] === EVENT_CRIT_WARNING_OR_CRITICAL) { } else if ($filters['filter_severity'] === EVENT_CRIT_WARNING_OR_CRITICAL) {
// Test if enterprise is installed to search oid in text or oid field in ttrap. // Test if enterprise is installed to search oid in text or oid field in ttrap.
$whereSubquery .= ' AND (severity = '.EVENT_CRIT_WARNING.' OR severity = '.EVENT_CRIT_CRITICAL.' OR severity = '.EVENT_CRIT_WARNING_OR_CRITICAL.')'; if ($config['enterprise_installed']) {
$whereSubquery .= ' AND (
(`ttrap`.`alerted` = 0 AND (`ttrap`.`severity` = '.EVENT_CRIT_WARNING.' OR `ttrap`.`severity` = '.EVENT_CRIT_CRITICAL.')) OR
(`ttrap`.`alerted` = 1 AND (`ttrap`.`priority` = '.EVENT_CRIT_WARNING.' OR `ttrap`.`priority` = '.EVENT_CRIT_CRITICAL.')))';
} else {
$whereSubquery .= ' AND (
(`ttrap`.`alerted` = 1 AND (`ttrap`.`priority` = '.EVENT_CRIT_WARNING.' OR `ttrap`.`priority` = '.EVENT_CRIT_CRITICAL.')))';
}
} else if ($filters['filter_severity'] === EVENT_CRIT_OR_NORMAL) { } else if ($filters['filter_severity'] === EVENT_CRIT_OR_NORMAL) {
// Test if enterprise is installed to search oid in text or oid field in ttrap. // Test if enterprise is installed to search oid in text or oid field in ttrap.
$whereSubquery .= ' AND (severity = '.EVENT_CRIT_NORMAL.' OR severity = '.EVENT_CRIT_CRITICAL.' OR severity = '.EVENT_CRIT_OR_NORMAL.')'; if ($config['enterprise_installed']) {
} else if ($filters['filter_severity'] === EVENT_CRIT_NOT_NORMAL) { $whereSubquery .= ' AND (
// Test if enterprise is installed to search oid in text or oid field in ttrap. (`ttrap`.`alerted` = 0 AND (`ttrap`.`severity` = '.EVENT_CRIT_NORMAL.' OR `ttrap`.`severity` = '.EVENT_CRIT_CRITICAL.')) OR
$whereSubquery .= ' AND (severity = '.EVENT_CRIT_WARNING.' OR severity = '.EVENT_CRIT_CRITICAL.' OR severity = '.EVENT_CRIT_NOT_NORMAL.')'; (`ttrap`.`alerted` = 1 AND (`ttrap`.`priority` = '.EVENT_CRIT_NORMAL.' OR `ttrap`.`priority` = '.EVENT_CRIT_CRITICAL.')))';
} else {
$whereSubquery .= ' AND (
(`ttrap`.`alerted` = 1 AND (`ttrap`.`priority` = '.EVENT_CRIT_NORMAL.' OR `ttrap`.`priority` = '.EVENT_CRIT_CRITICAL.')))';
}
} }
} }
if ($filters['filter_free_search'] !== '') { if ($filters['filter_free_search'] !== '') {
$free_search_str = io_safe_output($filters['filter_free_search']); $free_search_str = io_safe_output($filters['filter_free_search']);
$whereSubquery .= ' $whereSubquery .= '
AND (source LIKE "%'.$free_search_str.'%" OR AND (`ttrap`.`source` LIKE "%'.$free_search_str.'%" OR
oid LIKE "%'.$free_search_str.'%" OR `ttrap`.`oid` LIKE "%'.$free_search_str.'%" OR
oid_custom LIKE "%'.$free_search_str.'%" OR `ttrap`.`oid_custom` LIKE "%'.$free_search_str.'%" OR
type_custom LIKE "%'.$free_search_str.'%" OR `ttrap`.`type_custom` LIKE "%'.$free_search_str.'%" OR
value LIKE "%'.$free_search_str.'%" OR `ttrap`.`value` LIKE "%'.$free_search_str.'%" OR
value_custom LIKE "%'.$free_search_str.'%" OR `ttrap`.`value_custom` LIKE "%'.$free_search_str.'%" OR
id_usuario LIKE "%'.$free_search_str.'%" OR `ttrap`.`id_usuario` LIKE "%'.$free_search_str.'%" OR
text LIKE "%'.$free_search_str.'%" OR `ttrap`.`text` LIKE "%'.$free_search_str.'%" OR
description LIKE "%'.$free_search_str.'%")'; `ttrap`.`description` LIKE "%'.$free_search_str.'%")';
}
if ($filters['filter_alias_search'] !== '') {
$alias_search = io_safe_output($filters['filter_alias_search']);
$whereSubquery .= '
AND `tagente`.`alias` LIKE "%'.$alias_search.'%"';
} }
if ($filters['filter_status'] != -1) { if ($filters['filter_status'] != -1) {
$whereSubquery .= ' AND status = '.$filters['filter_status']; $whereSubquery .= ' AND `ttrap`.`status` = '.$filters['filter_status'];
} }
if ($date_from_trap != '') { if ($date_from_trap != '') {
if ($time_from_trap != '') { if ($time_from_trap != '') {
$whereSubquery .= ' $whereSubquery .= '
AND (utimestamp > '.$ago_timestamp.') AND (`ttrap`.`utimestamp` > '.$ago_timestamp.')
'; ';
} else { } else {
$whereSubquery .= ' $whereSubquery .= '
AND (UNIX_TIMESTAMP(timestamp) > UNIX_TIMESTAMP("'.$date_from_trap.' 23:59:59")) AND (UNIX_TIMESTAMP(`ttrap`.`timestamp`) > UNIX_TIMESTAMP("'.$date_from_trap.' 23:59:59"))
'; ';
} }
} }
@ -719,37 +739,31 @@ class SnmpConsole extends HTML
if ($date_to_trap != '') { if ($date_to_trap != '') {
if ($time_to_trap) { if ($time_to_trap) {
$whereSubquery .= ' $whereSubquery .= '
AND (utimestamp < '.$now_timestamp.') AND (`ttrap`.`utimestamp` < '.$now_timestamp.')
'; ';
} else { } else {
$whereSubquery .= ' $whereSubquery .= '
AND (UNIX_TIMESTAMP(timestamp) < UNIX_TIMESTAMP("'.$date_to_trap.' 23:59:59")) AND (UNIX_TIMESTAMP(`ttrap`.`timestamp`) < UNIX_TIMESTAMP("'.$date_to_trap.' 23:59:59"))
'; ';
} }
} }
if ($filters['filter_trap_type'] == 5) { if ($filters['filter_trap_type'] == 5) {
$whereSubquery .= ' AND type NOT IN (0, 1, 2, 3, 4)'; $whereSubquery .= ' AND `ttrap`.`type` NOT IN (0, 1, 2, 3, 4)';
} else if ($filters['filter_trap_type'] != -1) { } else if ($filters['filter_trap_type'] != -1) {
$whereSubquery .= ' AND type = '.$filters['filter_trap_type']; $whereSubquery .= ' AND `ttrap`.`type` = '.$filters['filter_trap_type'];
} }
$where_without_group = '';
if ($filters['filter_group_by']) { if ($filters['filter_group_by']) {
$where_without_group = $whereSubquery; $where_without_group = $whereSubquery;
$whereSubquery .= ' GROUP BY source,oid'; $whereSubquery .= ' GROUP BY `ttrap`.`source`,`ttrap`.`oid`,`ttrap`.`id_trap`';
} else if ($filters['filter_alias_search'] !== '') {
$whereSubquery .= ' GROUP BY `ttrap`.`id_trap`';
} }
$sql = sprintf($sql, $whereSubquery, $start, $length); $sql = sprintf($sql, $sql_join, $whereSubquery, $start, $length);
$sql_count = sprintf($sql_count, $sql_join, $whereSubquery);
$sql_count = 'SELECT COUNT(id_trap) FROM ttrap
WHERE (
source IN ('.implode(',', $address_by_user_groups).") OR
source='' OR
source NOT IN (".implode(',', $all_address_agents).')
)
%s';
$sql_count = sprintf($sql_count, $whereSubquery);
$traps = db_get_all_rows_sql($sql, true); $traps = db_get_all_rows_sql($sql, true);
$total = (int) db_get_value_sql($sql_count, false, false); $total = (int) db_get_value_sql($sql_count, false, false);
@ -804,8 +818,8 @@ class SnmpConsole extends HTML
$tmp->snmp_agent .= '<a class="'.$severity_class.'" href="index.php?sec=estado&sec2=godmode/agentes/configurar_agente&new_agent=1&direccion='.$tmp->source.'" title="'.__('Create agent').'">'.$tmp->source.'</a>'; $tmp->snmp_agent .= '<a class="'.$severity_class.'" href="index.php?sec=estado&sec2=godmode/agentes/configurar_agente&new_agent=1&direccion='.$tmp->source.'" title="'.__('Create agent').'">'.$tmp->source.'</a>';
} else { } else {
$tmp->snmp_agent .= '<div class="'.$severity_class.' snmp-div"><a href="index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].'" title="'.__('View agent details').'">'; $tmp->snmp_agent .= '<div class="'.$severity_class.' snmp-div"><a href="index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].'" title="'.__('View agent details').'">';
$tmp->snmp_agent .= '<strong>'.$agent['alias'].'</strong></a>'.ui_print_help_tip($tmp->source, true); $tmp->snmp_agent .= '<strong>'.$agent['alias'].ui_print_help_tip($tmp->source, true);
'</div>'; '</strong></a></div>';
} }
// Enterprise string. // Enterprise string.
@ -1152,25 +1166,42 @@ class SnmpConsole extends HTML
$whereSubquery .= ' AND alerted = '.$$alert; $whereSubquery .= ' AND alerted = '.$$alert;
} }
$severity = (int) $severity; if ($severity != -1) {
if ($severity !== -1) {
// There are two special severity values aimed to match two different trap standard severities // There are two special severity values aimed to match two different trap standard severities
// in database: warning/critical and critical/normal. // in database: warning/critical and critical/normal.
if ($severity !== EVENT_CRIT_OR_NORMAL if ($severity != EVENT_CRIT_OR_NORMAL
&& $severity !== EVENT_CRIT_WARNING_OR_CRITICAL && $severity != EVENT_CRIT_WARNING_OR_CRITICAL
&& $severity !== EVENT_CRIT_NOT_NORMAL
) { ) {
// Test if enterprise is installed to search oid in text or oid field in ttrap. // Test if enterprise is installed to search oid in text or oid field in ttrap.
$whereSubquery .= ' AND severity = '.$severity; if ($config['enterprise_installed']) {
$whereSubquery .= ' AND (
(alerted = 0 AND severity = '.$severity.') OR
(alerted = 1 AND priority = '.$severity.'))';
} else {
$whereSubquery .= ' AND (
(alerted = 0 AND 1 = '.$severity.') OR
(alerted = 1 AND priority = '.$severity.'))';
}
} else if ($severity === EVENT_CRIT_WARNING_OR_CRITICAL) { } else if ($severity === EVENT_CRIT_WARNING_OR_CRITICAL) {
// Test if enterprise is installed to search oid in text or oid field in ttrap. // Test if enterprise is installed to search oid in text or oid field in ttrap.
$whereSubquery .= ' AND (severity = '.EVENT_CRIT_WARNING.' OR severity = '.EVENT_CRIT_CRITICAL.' OR severity = '.EVENT_CRIT_WARNING_OR_CRITICAL.')'; if ($config['enterprise_installed']) {
$whereSubquery .= ' AND (
(alerted = 0 AND (severity = '.EVENT_CRIT_WARNING.' OR severity = '.EVENT_CRIT_CRITICAL.')) OR
(alerted = 1 AND (priority = '.EVENT_CRIT_WARNING.' OR priority = '.EVENT_CRIT_CRITICAL.')))';
} else {
$whereSubquery .= ' AND (
(alerted = 1 AND (priority = '.EVENT_CRIT_WARNING.' OR priority = '.EVENT_CRIT_CRITICAL.')))';
}
} else if ($severity === EVENT_CRIT_OR_NORMAL) { } else if ($severity === EVENT_CRIT_OR_NORMAL) {
// Test if enterprise is installed to search oid in text or oid field in ttrap. // Test if enterprise is installed to search oid in text or oid field in ttrap.
$whereSubquery .= ' AND (severity = '.EVENT_CRIT_NORMAL.' OR severity = '.EVENT_CRIT_CRITICAL.' OR severity = '.EVENT_CRIT_OR_NORMAL.')'; if ($config['enterprise_installed']) {
} else if ($severity === EVENT_CRIT_NOT_NORMAL) { $whereSubquery .= ' AND (
// Test if enterprise is installed to search oid in text or oid field in ttrap. (alerted = 0 AND (severity = '.EVENT_CRIT_NORMAL.' OR severity = '.EVENT_CRIT_CRITICAL.')) OR
$whereSubquery .= ' AND (severity = '.EVENT_CRIT_WARNING.' OR severity = '.EVENT_CRIT_CRITICAL.' OR severity = '.EVENT_CRIT_NOT_NORMAL.')'; (alerted = 1 AND (priority = '.EVENT_CRIT_NORMAL.' OR priority = '.EVENT_CRIT_CRITICAL.')))';
} else {
$whereSubquery .= ' AND (
(alerted = 1 AND (priority = '.EVENT_CRIT_NORMAL.' OR priority = '.EVENT_CRIT_CRITICAL.')))';
}
} }
} }

View File

@ -36,6 +36,7 @@ $ajaxPage = $config['homedir'].'/operation/snmpconsole/snmp_view';
$filter_alert = get_parameter('filter_alert', -1); $filter_alert = get_parameter('filter_alert', -1);
$filter_severity = get_parameter('filter_severity', -1); $filter_severity = get_parameter('filter_severity', -1);
$filter_free_search = get_parameter('filter_free_search', ''); $filter_free_search = get_parameter('filter_free_search', '');
$filter_alias_search = get_parameter('filter_alias_search', '');
$filter_status = get_parameter('filter_status', 0); $filter_status = get_parameter('filter_status', 0);
$filter_group_by = get_parameter('filter_group_by', 0); $filter_group_by = get_parameter('filter_group_by', 0);
$filter_hours_ago = get_parameter('filter_hours_ago', 8); $filter_hours_ago = get_parameter('filter_hours_ago', 8);
@ -54,7 +55,8 @@ try {
$filter_group_by, $filter_group_by,
$filter_hours_ago, $filter_hours_ago,
$filter_trap_type, $filter_trap_type,
$refr $refr,
$filter_alias_search
); );
} catch (Exception $e) { } catch (Exception $e) {
if ((bool) is_ajax() === true) { if ((bool) is_ajax() === true) {