ajaxController = $ajaxController; $this->filter_alert = $filter_alert; $this->filter_severity = $filter_severity; $this->filter_free_search = $filter_free_search; $this->filter_status = $filter_status; $this->filter_group_by = $filter_group_by; $this->filter_hours_ago = $filter_hours_ago; $this->filter_trap_type = $filter_trap_type; $this->refr = $refr; } /** * Run view * * @return void */ public function run() { global $config; // Javascript. ui_require_jquery_file('pandora'); // CSS. ui_require_css_file('wizard'); ui_require_css_file('discovery'); $default_refr = 300; if (!isset($config['pure']) || $config['pure'] === false) { $statistics['text'] = ''.html_print_image( 'images/logs@svg.svg', true, [ 'title' => __('Statistics'), 'class' => 'main_menu_icon invert_filter', ] ).''; $list['text'] = ''.html_print_image( 'images/SNMP-network-numeric-data@svg.svg', true, [ 'title' => __('List'), 'class' => 'main_menu_icon invert_filter', ] ).''; $list['active'] = true; $screen['text'] = ''.html_print_image( 'images/fullscreen@svg.svg', true, [ 'title' => __('View in full screen'), 'class' => 'main_menu_icon invert_filter', ] ).''; // Header. ui_print_standard_header( __('SNMP Console'), 'images/op_snmp.png', false, 'snmp_console', false, [ $screen, $list, $statistics, ], [ [ 'link' => '', 'label' => __('Monitoring'), ], [ 'link' => '', 'label' => __('SNMP'), ], ] ); } else { echo '
';
$legend .= ' '.__('Severity').''; $legend .= '';
$priorities = get_priorities();
$half = (count($priorities) / 2);
$count = 0;
foreach ($priorities as $num => $name) {
if ($count == $half) {
$legend .= ' ';
}
$legend .= ''.$name.'';
$legend .= ' '; $count++; } $legend .= ' | ';
$legend .= '';
$legend .= ' '.__('Status').''; $legend .= ''.__('Validated').''; $legend .= ''; $legend .= ''.__('Not validated').''; $legend .= ' | ';
$legend .= '';
$legend .= ' '.__('Alert').''; $legend .= ''.__('Alert').''; $legend .= ''; $legend .= ''.__('Not fired').''; $legend .= ' | ';
$legend .= '';
$legend .= ' '.__('Action').''; $legend .= '';
$legend .= html_print_image('images/validate.svg', true, ['class' => 'main_menu_icon invert_filter']).' - '.__('Validate');
$legend .= ' ';
$legend .= ''; $legend .= ' ';
$legend .= html_print_image('images/delete.svg', true, ['class' => 'main_menu_icon invert_filter']).' - '.__('Delete');
$legend .= ' ';
$legend .= ' | ';
ui_toggle($legend, __('Legend'));
// Load own javascript file.
echo $this->loadJS();
}
/**
* Get the data for draw the table.
*
* @return void.
*/
public function draw()
{
global $config;
// Init data.
$data = [];
// Count of total records.
$count = 0;
// Catch post parameters.
$start = get_parameter('start', 0);
$length = get_parameter('length', $config['block_size']);
// There is a limit of (2^32)^2 (18446744073709551615) rows in a MyISAM table, show for show all use max nrows.
$length = ($length != '-1') ? $length : '18446744073709551615';
$order = get_datatable_order(true);
$filters = get_parameter('filter', []);
// Build ranges.
$now = new DateTime();
$ago = new DateTime();
$interval = new DateInterval(sprintf('PT%dH', $filters['filter_hours_ago']));
$ago->sub($interval);
$date_from_trap = $ago->format('Y/m/d');
$date_to_trap = $now->format('Y/m/d');
$time_from_trap = $ago->format('H:i:s');
$time_to_trap = $now->format('H:i:s');
try {
ob_start();
$data = [];
$user_groups = users_get_groups($config['id_user'], 'AR', false);
$prea = array_keys($user_groups);
$ids = join(',', $prea);
$user_in_group_wo_agents = db_get_value_sql('select count(DISTINCT(id_usuario)) from tusuario_perfil where id_usuario ="'.$config['id_user'].'" and id_perfil = 1 and id_grupo in (select id_grupo from tgrupo where id_grupo in ('.$ids.') and id_grupo not in (select id_grupo from tagente))');
if ($user_in_group_wo_agents == 0) {
$rows = db_get_all_rows_filter(
'tagente',
['id_grupo' => array_keys($user_groups)],
['id_agente']
);
$id_agents = [];
foreach ($rows as $row) {
$id_agents[] = $row['id_agente'];
}
if (!empty($id_agents)) {
$address_by_user_groups = agents_get_addresses($id_agents);
foreach ($address_by_user_groups as $i => $a) {
$address_by_user_groups[$i] = '"'.$a.'"';
}
}
} else {
$rows = db_get_all_rows_filter(
'tagente',
[],
['id_agente']
);
$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)) {
$address_by_user_groups = [];
array_unshift($address_by_user_groups, '""');
}
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) {
$whereSubquery .= ' AND alerted = '.$filters['filter_alert'];
}
if ($filters['filter_severity'] != -1) {
// There are two special severity values aimed to match two different trap standard severities
// in database: warning/critical and critical/normal.
if ($filters['filter_severity'] != EVENT_CRIT_OR_NORMAL
&& $filters['filter_severity'] != EVENT_CRIT_WARNING_OR_CRITICAL
) {
// Test if enterprise is installed to search oid in text or oid field in ttrap.
if ($config['enterprise_installed']) {
$whereSubquery .= ' AND (
(alerted = 0 AND severity = '.$filters['filter_severity'].') OR
(alerted = 1 AND priority = '.$filters['filter_severity'].'))';
} else {
$whereSubquery .= ' AND (
(alerted = 0 AND 1 = '.$filters['filter_severity'].') OR
(alerted = 1 AND priority = '.$filters['filter_severity'].'))';
}
} 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.
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 ($filters['filter_severity'] === EVENT_CRIT_OR_NORMAL) {
// Test if enterprise is installed to search oid in text or oid field in ttrap.
if ($config['enterprise_installed']) {
$whereSubquery .= ' AND (
(alerted = 0 AND (severity = '.EVENT_CRIT_NORMAL.' OR severity = '.EVENT_CRIT_CRITICAL.')) OR
(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.')))';
}
}
}
if ($filters['filter_free_search'] !== '') {
$whereSubquery .= '
AND (source LIKE "%'.$filters['filter_free_search'].'%" OR
oid LIKE "%'.$filters['filter_free_search'].'%" OR
oid_custom LIKE "%'.$filters['filter_free_search'].'%" OR
type_custom LIKE "%'.$filters['filter_free_search'].'%" OR
value LIKE "%'.$filters['filter_free_search'].'%" OR
value_custom LIKE "%'.$filters['filter_free_search'].'%" OR
id_usuario LIKE "%'.$filters['filter_free_search'].'%" OR
text LIKE "%'.$filters['filter_free_search'].'%" OR
description LIKE "%'.$filters['filter_free_search'].'%")';
}
if ($filters['filter_status'] != -1) {
$whereSubquery .= ' AND status = '.$filters['filter_status'];
}
if ($date_from_trap != '') {
if ($time_from_trap != '') {
$whereSubquery .= '
AND (UNIX_TIMESTAMP(timestamp) > UNIX_TIMESTAMP("'.$date_from_trap.' '.$time_from_trap.'"))
';
} else {
$whereSubquery .= '
AND (UNIX_TIMESTAMP(timestamp) > UNIX_TIMESTAMP("'.$date_from_trap.' 23:59:59"))
';
}
}
if ($date_to_trap != '') {
if ($time_to_trap) {
$whereSubquery .= '
AND (UNIX_TIMESTAMP(timestamp) < UNIX_TIMESTAMP("'.$date_to_trap.' '.$time_to_trap.'"))
';
} else {
$whereSubquery .= '
AND (UNIX_TIMESTAMP(timestamp) < UNIX_TIMESTAMP("'.$date_to_trap.' 23:59:59"))
';
}
}
if ($filters['filter_trap_type'] == 5) {
$whereSubquery .= ' AND type NOT IN (0, 1, 2, 3, 4)';
} else if ($filters['filter_trap_type'] != -1) {
$whereSubquery .= ' AND type = '.$filters['filter_trap_type'];
}
if ($filters['filter_group_by']) {
$where_without_group = $whereSubquery;
$whereSubquery .= ' GROUP BY source,oid';
}
$sql = sprintf($sql, $whereSubquery, $start, $length);
$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);
$total = (int) db_get_value_sql($sql_count, false, true);
if (empty($traps) === false) {
$data = $traps;
$data = array_reduce(
$data,
function ($carry, $item) use ($filters, $where_without_group) {
global $config;
if (empty($carry) === true) {
$count = 0;
} else {
$count = count($carry);
}
// Transforms array of arrays $data into an array
// of objects, making a post-process of certain fields.
$tmp = (object) $item;
$severity_class = get_priority_class($tmp->severity);
$status = $tmp->status;
// Status.
if ($status == 0) {
$tmp->status = html_print_image(
'images/pixel_red.png',
true,
[
'title' => __('Not validated'),
'width' => '20',
'height' => '20',
]
);
} else {
$tmp->status = html_print_image(
'images/pixel_green.png',
true,
[
'title' => __('Validated'),
'width' => '20',
'height' => '20',
]
);
}
// SNMP Agent.
$agent = agents_get_agent_with_ip($tmp->source);
if ($agent === false) {
$tmp->snmp_agent .= ''.$tmp->source.'';
} else {
$tmp->snmp_agent .= '';
}
// Enterprise string.
if (empty($tmp->text) === false) {
$enterprise_string = $tmp->text;
} else if (empty($tmp->oid) === false) {
$enterprise_string = $tmp->oid;
} else {
$enterprise_string = __('N/A');
}
$tmp->enterprise_string = '';
// Count.
if ($filters['filter_group_by']) {
$sql = 'SELECT count(*) FROM ttrap WHERE 1=1
'.$where_without_group.'
AND oid="'.$tmp->oid.'"
AND source="'.$tmp->source.'"';
$group_traps = db_get_value_sql($sql);
$tmp->count = '