#10976 search agents

This commit is contained in:
Jonathan 2023-05-04 16:01:08 +02:00
parent 96305c9528
commit 0336f03a6c
3 changed files with 132 additions and 321 deletions

View File

@ -16,163 +16,51 @@ global $config;
enterprise_include_once('include/functions_policies.php');
require_once $config['homedir'].'/include/functions_users.php';
$searchAgents = check_acl($config['id_user'], 0, 'AR');
$selectNameUp = '';
$selectNameDown = '';
$selectDescriptionUp = '';
$selectDescriptionDown = '';
$selectOsUp = '';
$selectOsDown = '';
$selectIntervalUp = '';
$selectIntervalDown = '';
$selectGroupUp = '';
$selectGroupDown = '';
$selectLastContactUp = '';
$selectLastContactDown = '';
switch ($sortField) {
case 'name':
switch ($sort) {
case 'up':
$selectNameUp = $selected;
$order = [
'field' => 'nombre',
'order' => 'ASC',
];
break;
case 'down':
$selectNameDown = $selected;
$order = [
'field' => 'nombre',
'order' => 'DESC',
];
break;
}
break;
case 'comentarios':
switch ($sort) {
case 'up':
$selectDescriptionUp = $selected;
$order = [
'field' => 'comentarios',
'order' => 'ASC',
];
break;
case 'down':
$selectDescriptionDown = $selected;
$order = [
'field' => 'comentarios',
'order' => 'DESC',
];
break;
}
break;
case 'os':
switch ($sort) {
case 'up':
$selectOsUp = $selected;
$order = [
'field' => 'id_os',
'order' => 'ASC',
];
break;
case 'down':
$selectOsDown = $selected;
$order = [
'field' => 'id_os',
'order' => 'DESC',
];
break;
}
break;
case 'interval':
switch ($sort) {
case 'up':
$selectIntervalUp = $selected;
$order = [
'field' => 'intervalo',
'order' => 'ASC',
];
break;
case 'down':
$selectIntervalDown = $selected;
$order = [
'field' => 'intervalo',
'order' => 'DESC',
];
break;
}
break;
case 'group':
switch ($sort) {
case 'up':
$selectGroupUp = $selected;
$order = [
'field' => 'id_grupo',
'order' => 'ASC',
];
break;
case 'down':
$selectGroupDown = $selected;
$order = [
'field' => 'id_grupo',
'order' => 'DESC',
];
break;
}
break;
case 'last_contact':
switch ($sort) {
case 'up':
$selectLastContactUp = $selected;
$order = [
'field' => 'ultimo_contacto',
'order' => 'ASC',
];
break;
case 'down':
$selectLastContactDown = $selected;
$order = [
'field' => 'ultimo_contacto',
'order' => 'DESC',
];
break;
}
break;
default:
$selectNameUp = $selected;
$selectNameDown = '';
$selectDescriptionUp = '';
$selectDescriptionDown = '';
$selectOsUp = '';
$selectOsDown = '';
$selectIntervalUp = '';
$selectIntervalDown = '';
$selectGroupUp = '';
$selectGroupDown = '';
$selectLastContactUp = '';
$selectLastContactDown = '';
$order = [
'field' => 'nombre',
'order' => 'ASC',
];
break;
$searchAgents = get_parameter('search_agents', 0);
$order = get_datatable_order(true);
if (empty($order)) {
$order = [];
}
$totalAgents = 0;
switch ($order['field']) {
case 'comentarios':
$order['field'] = 't1.comentarios';
break;
case 'os':
$order['field'] = 'tl.id_os';
break;
case 'interval':
$order['field'] = 't1.intervalo';
break;
case 'group_icon':
$order['field'] = 't1.id_grupo';
break;
case 'module':
$order['field'] = 'id_agente';
break;
case 'status':
$order['field'] = 'id_agente';
break;
case 'alert':
$order['field'] = 'id_agente';
break;
case 'last_contact':
$order['field'] = 't1.ultimo_contacto';
break;
case 'agent':
default:
$order['field'] = 't1.alias';
break;
}
$agents = false;
if ($searchAgents) {
@ -247,10 +135,8 @@ if ($searchAgents) {
';
$select = 'SELECT DISTINCT(t1.id_agente), t1.ultimo_contacto, t1.nombre, t1.comentarios, t1.id_os, t1.intervalo, t1.id_grupo, t1.disabled, t1.alias, t1.quiet';
if ($only_count) {
$limit = ' ORDER BY '.$order['field'].' '.$order['order'].' LIMIT '.$config['block_size'].' OFFSET 0';
} else {
$limit = ' ORDER BY '.$order['field'].' '.$order['order'].' LIMIT '.$config['block_size'].' OFFSET '.get_parameter('offset', 0);
if (is_array($order)) {
$limit = ' ORDER BY '.$order['field'].' '.$order['direction'];
}
$query = $select.$sql;
@ -272,4 +158,45 @@ if ($searchAgents) {
'SELECT COUNT(DISTINCT id_agente) AS agent_count '.$sql
);
}
foreach ($agents as $key => $agent) {
if ($agent['disabled']) {
$agents[$key]['agent'] = '<em><a style href=index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].'
title="'.$agent['id_agente'].'"><b><span style>'.ucfirst(strtolower($agent['alias'])).'</span></b></a>'.ui_print_help_tip(__('Disabled'), true).'</em>';
} else {
$agents[$key]['agent'] = '<a style href=index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].'
title='.$agent['nombre'].'><b><span style>'.ucfirst(strtolower($agent['alias'])).'</span></b></a>';
}
$agents[$key]['os'] = ui_print_os_icon($agent['id_os'], false, true);
$agents[$key]['interval'] = human_time_description_raw($agent['intervalo'], false, 'tiny');
$agents[$key]['group_icon'] = ui_print_group_icon($agent['id_grupo'], true);
$agent_info = reporting_get_agent_module_info($agent['id_agente']);
$modulesCell = reporting_tiny_stats($agent_info, true);
$agents[$key]['module'] = $modulesCell;
$agents[$key]['status'] = $agent_info['status_img'];
$agents[$key]['alert'] = $agent_info['alert_img'];
$last_time = time_w_fixed_tz($agent['ultimo_contacto']);
$now = get_system_time();
$diferencia = ($now - $last_time);
$time = ui_print_timestamp($last_time, true);
$time_style = $time;
if ($diferencia > ($agent['intervalo'] * 2)) {
$time_style = '<b><span class="color_ff0">'.$time.'</span></b>';
}
$agents[$key]['last_contact'] = $time_style;
}
// RecordsTotal && recordsfiltered resultados totales.
echo json_encode(
[
'data' => ($agents ?? []),
'recordsTotal' => $totalAgents,
'recordsFiltered' => count($agents),
]
);
}

View File

@ -13,174 +13,57 @@
// GNU General Public License for more details.
global $config;
enterprise_include_once('include/functions_policies.php');
require_once $config['homedir'].'/include/functions_users.php';
/*
enterprise_include_once('include/functions_policies.php');
require_once $config['homedir'].'/include/functions_users.php';*/
if ($only_count) {
ob_start();
}
// Datatables list.
try {
$columns = [
'agent',
'comentarios',
'os',
'interval',
'group_icon',
'module',
'status',
'alert',
'last_contact',
];
// TODO: CLEAN extra_sql
$extra_sql = '';
$column_names = [
__('Agent'),
__('Description'),
__('OS'),
__('Interval'),
__('Group'),
__('Modules'),
__('Status'),
__('Alerts'),
__('Last contact'),
];
$searchAgents = check_acl($config['id_user'], 0, 'AR');
$tableId = 'agents_search';
if (!$agents || !$searchAgents) {
if (!$only_count) {
echo "<br><div class='nf'>".__('Zero results found')."</div>\n";
}
} else {
$table = new stdClass();
$table->cellpadding = 4;
$table->cellspacing = 4;
$table->width = '98%';
$table->class = 'info_table';
$table->head = [];
if ($only_count) {
$table->head[0] = __('Agent');
$table->head[1] = __('Description');
$table->head[2] = __('OS');
$table->head[3] = __('Interval');
$table->head[4] = __('Group');
} else {
$table->head[0] = __('Agent').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=name&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectNameUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=name&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectNameDown]).'</a>';
$table->head[1] = __('Description').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=comentarios&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectDescriptionUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=comentarios&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectDescriptionDown]).'</a>';
$table->head[2] = __('OS').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=os&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectOsUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=os&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectOsDown]).'</a>';
$table->head[3] = __('Interval').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=interval&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectIntervalUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=interval&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectIntervalDown]).'</a>';
$table->head[4] = __('Group').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=group&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectGroupUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=group&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectGroupDown]).'</a>';
}
$table->head[5] = __('Modules');
$table->head[6] = __('Status');
$table->head[7] = __('Alerts');
$table->head[8] = __('Last contact').' '.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=last_contact&sort=up">'.html_print_image('images/sort_up.png', true, ['style' => $selectLastContactUp]).'</a>'.'<a href="index.php?search_category=agents&keywords='.$config['search_keywords'].'&head_search_keywords=abc&offset='.$offset.'&sort_field=last_contact&sort=down">'.html_print_image('images/sort_down.png', true, ['style' => $selectLastContactDown]).'</a>';
$table->head[9] = '';
$table->headstyle = [];
$table->headstyle[0] = 'text-align: left';
$table->headstyle[1] = 'text-align: left';
$table->headstyle[2] = 'text-align: left';
$table->headstyle[3] = 'text-align: left';
$table->headstyle[4] = 'text-align: left';
$table->headstyle[5] = 'text-align: left';
$table->headstyle[6] = 'text-align: left';
$table->headstyle[7] = 'text-align: left';
$table->headstyle[8] = 'text-align: left';
$table->headstyle[9] = 'text-align: center';
$table->align = [];
$table->align[0] = 'left';
$table->align[1] = 'left';
$table->align[2] = 'left';
$table->align[3] = 'left';
$table->align[4] = 'left';
$table->align[5] = 'left';
$table->align[6] = 'left';
$table->align[7] = 'left';
$table->align[8] = 'left';
$table->align[9] = 'center';
$table->data = [];
foreach ($agents as $agent) {
$agent_info = reporting_get_agent_module_info($agent['id_agente']);
$modulesCell = reporting_tiny_stats($agent_info, true);
if ($agent['disabled']) {
$cellName = '<em>'.'<a style href=index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].' title='.$agent['nombre'].'><b>'.'<span style>'.$agent['alias'].'</span></b></a>'.ui_print_help_tip(__('Disabled'), true).'</em>';
} else {
$cellName = '<a style href=index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$agent['id_agente'].' title='.$agent['nombre'].'><b>'.'<span style>'.$agent['alias'].'</span></b></a>';
}
if ($agent['quiet']) {
$cellName .= '&nbsp;';
$cellName .= html_print_image('images/dot_blue.png', true, ['border' => '0', 'title' => __('Quiet'), 'alt' => '']);
}
$in_planned_downtime = db_get_sql(
'SELECT executed FROM tplanned_downtime
INNER JOIN tplanned_downtime_agents
ON tplanned_downtime.id = tplanned_downtime_agents.id_downtime
WHERE tplanned_downtime_agents.id_agent = '.$agent['id_agente'].' AND tplanned_downtime.executed = 1'
);
if ($in_planned_downtime) {
$cellName .= '<em>'.ui_print_help_tip(__('Agent in scheduled downtime'), true, 'images/minireloj-16.png');
$cellName .= '</em>';
}
$last_time = time_w_fixed_tz($agent['ultimo_contacto']);
$now = get_system_time();
$diferencia = ($now - $last_time);
$time = ui_print_timestamp($last_time, true);
$time_style = $time;
if ($diferencia > ($agent['intervalo'] * 2)) {
$time_style = '<b><span class="color_ff0">'.$time.'</span></b>';
}
$manage_agent = '';
if (check_acl($config['id_user'], $agent['id_grupo'], 'AW')) {
$url_manage = 'index.php?sec=estado&sec2=godmode/agentes/configurar_agente&id_agente='.$agent['id_agente'];
$manage_agent = '<a href="'.$url_manage.'">'.html_print_image(
'images/cog.png',
true,
[
'title' => __('Manage'),
'alt' => __('Manage'),
'class' => 'invert_filter',
]
).'</a>';
}
$table->cellclass[][9] = 'table_action_buttons';
array_push(
$table->data,
[
$cellName,
ui_print_truncate_text($agent['comentarios'], 'comentarios', false, true, true, '[&hellip;]'),
ui_print_os_icon($agent['id_os'], false, true),
human_time_description_raw($agent['intervalo'], false, 'tiny'),
ui_print_group_icon($agent['id_grupo'], true),
$modulesCell,
$agent_info['status_img'],
$agent_info['alert_img'],
$time_style,
$manage_agent,
]
);
}
echo '<br />';
html_print_table($table);
unset($table);
if (!$only_count) {
$tablePagination = ui_pagination(
$totalAgents,
false,
0,
0,
true,
'offset',
false
);
}
html_print_action_buttons(
'',
// Load datatables user interface.
ui_print_datatable(
[
'type' => 'data_table',
'class' => 'fixed_action_buttons',
'right_content' => $tablePagination,
'id' => $tableId,
'class' => 'info_table',
'style' => 'width: 99%',
'columns' => $columns,
'column_names' => $column_names,
'ajax_url' => 'operation/search_agents.getdata',
'ajax_postprocces' => 'process_datatables_item(item)',
'ajax_data' => ['search_agents' => 1],
'order' => [
'field' => 'alias',
'direction' => 'asc',
],
'search_button_class' => 'sub filter float-right',
'filter_main_class' => 'box-flat white_table_graph fixed_filter_bar',
]
);
}
if ($only_count) {
$list_agents = ob_get_clean();
} catch (Exception $e) {
echo $e->getMessage();
}

View File

@ -205,6 +205,7 @@ ui_print_standard_header(
);
$only_count = false;
hd($searchTab);
switch ($searchTab) {
case 'main':
$only_count = true;