target selections

This commit is contained in:
fbsanchez 2020-03-17 18:34:30 +01:00
parent c1337a4318
commit b860c49383
7 changed files with 341 additions and 76 deletions

View File

@ -123,8 +123,9 @@ class DiscoveryTaskList extends HTML
}
$force_run = (bool) get_parameter('force_run');
if ($force_run === true) {
return $this->forceConsoleTask();
$force = (bool) get_parameter('force');
if ($force_run === true || $force === true) {
return $this->forceTask();
}
$delete_console_task = (bool) get_parameter('delete_console_task');
@ -241,7 +242,7 @@ class DiscoveryTaskList extends HTML
*
* @return void
*/
public function forceConsoleTask()
public function forceTask()
{
global $config;
@ -256,12 +257,46 @@ class DiscoveryTaskList extends HTML
$id_console_task = (int) get_parameter('id_console_task');
if ($id_console_task !== null) {
if ($id_console_task != null) {
// --------------------------------
// FORCE A CONSOLE TASK
// --------------------------------
enterprise_hook('cron_task_run', [$id_console_task, true]);
// Trick to avoid double execution.
header('Location: '.$this->url);
}
} else {
// --------------------------------
// FORCE A RECON TASK
// --------------------------------
if (check_acl($config['id_user'], 0, 'AW')) {
if (isset($_GET['force'])) {
$id = (int) get_parameter_get('force', 0);
// Schedule execution.
$direct_report = db_get_value(
'direct_report',
'trecon_task',
'id_rt',
$id
);
if ($direct_report != DISCOVERY_STANDARD) {
// Force re-scan for supervised tasks.
$direct_report = DISCOVERY_SEARCH;
}
db_process_sql_update(
'trecon_task',
[
'utimestamp' => 0,
'status' => 1,
'direct_report' => DISCOVERY_RESULTS,
],
['id_rt' => $id]
);
header('Location: '.$this->url);
}
}
}
}
@ -334,28 +369,7 @@ class DiscoveryTaskList extends HTML
include_once $config['homedir'].'/include/functions_servers.php';
include_once $config['homedir'].'/include/functions_network_profiles.php';
$modules_server = 0;
$total_modules = 0;
$total_modules_data = 0;
// --------------------------------
// FORCE A RECON TASK
// --------------------------------
if (check_acl($config['id_user'], 0, 'AW')) {
if (isset($_GET['force'])) {
$id = (int) get_parameter_get('force', 0);
servers_force_recon_task($id);
header(
'Location: '.ui_get_full_url(
'index.php?sec=gservers&sec2=godmode/servers/discovery&wiz=tasklist'
)
);
}
}
$recon_tasks = db_get_all_rows_sql('SELECT * FROM trecon_task');
$user_groups = implode(',', array_keys(users_get_groups()));
// Show network tasks for Recon Server.
if ($recon_tasks === false) {
$recon_tasks = [];
@ -636,7 +650,7 @@ class DiscoveryTaskList extends HTML
'data' => [
'wiz' => 'tasklist',
'id' => $task['id_rt'],
'method' => 'task_progress',
'method' => 'taskProgress',
],
]
);
@ -842,10 +856,11 @@ class DiscoveryTaskList extends HTML
*
* @return void
*/
public function task_progress()
public function taskProgress()
{
if (!is_ajax()) {
return '';
echo json_encode(['error' => true]);
return;
}
$id_task = get_parameter('id', 0);
@ -871,11 +886,11 @@ class DiscoveryTaskList extends HTML
*
* @return string Charts in HTML.
*/
private function progress_task_graph($task)
private function progressTaskGraph($task)
{
$result .= '<div style="display: flex;">';
$result .= '<div style="width: 100%; text-align: center; margin-top: 40px;">';
$result .= '<span style="font-size: 1.9em; font-family: "lato-bolder", "Open Sans", sans-serif !important;">'._('Overall Progress').'</span>';
$result .= '<div class="subtitle">';
$result .= '<span>'._('Overall Progress').'</span>';
$result .= '<div style="margin-top: 25px;">';
$result .= progress_circular_bar(
@ -928,8 +943,8 @@ class DiscoveryTaskList extends HTML
}
$result .= '</div>';
$result .= '<div style="width: 100%; text-align: center; margin-top: 40px;">';
$result .= '<span style="font-size: 1.9em; font-family: "lato-bolder", "Open Sans", sans-serif !important;">'.$str.' ';
$result .= '<div class="subtitle">';
$result .= '<span>'.$str.' ';
if (!empty($str)) {
$result .= $task['stats']['c_network_name'];
}
@ -964,7 +979,7 @@ class DiscoveryTaskList extends HTML
*
* @return html code with summary.
*/
private function progress_task_summary($task)
private function progressTaskSummary($task)
{
global $config;
include_once $config['homedir'].'/include/graphs/functions_d3.php';
@ -975,6 +990,10 @@ class DiscoveryTaskList extends HTML
$output = '';
if (is_array($task['stats']) === false) {
$task['stats'] = json_decode($task['summary'], true);
}
if (is_array($task['stats'])) {
$i = 0;
$table = new StdClasS();
@ -1010,7 +1029,7 @@ class DiscoveryTaskList extends HTML
$table->data[$i][1] .= $task['stats']['summary']['WMI'];
$table->data[$i++][1] .= '</span>';
$output = '<div style="margin-top: 40px; text-align: center;"><span style="font-size: 1.9em; font-family: "lato-bolder", "Open Sans", sans-serif !important;">'.__('Summary').'</span></div>';
$output = '<div class="subtitle"><span>'.__('Summary').'</span></div>';
$output .= html_print_table($table, true).'</div>';
}
@ -1023,7 +1042,7 @@ class DiscoveryTaskList extends HTML
*
* @return void
*/
public function progress_task_discovery()
public function progressTaskDiscovery()
{
if (!is_ajax()) {
return;
@ -1038,8 +1057,7 @@ class DiscoveryTaskList extends HTML
$task = db_get_row('trecon_task', 'id_rt', $id_task);
$task['stats'] = json_decode($task['summary'], true);
$global_progress = $task['status'];
$summary = $this->progress_task_summary($task);
$summary = $this->progressTaskSummary($task);
$output = '';
@ -1059,7 +1077,7 @@ class DiscoveryTaskList extends HTML
true
).'</div>';
} else {
$output .= $this->progress_task_graph($task);
$output .= $this->progressTaskGraph($task);
}
$output .= $summary;
@ -1073,7 +1091,7 @@ class DiscoveryTaskList extends HTML
*
* @return void
*/
public function task_showmap()
public function taskShowmap()
{
global $config;
include_once $config['homedir'].'/include/class/NetworkMap.class.php';
@ -1095,7 +1113,7 @@ class DiscoveryTaskList extends HTML
*
* @return void
*/
public function show_task_review()
public function showTaskReview()
{
$id_task = get_parameter('id', 0);
if ($id_task <= 0) {
@ -1103,28 +1121,56 @@ class DiscoveryTaskList extends HTML
return;
}
$task = db_get_row('tdiscovery_tmp_agents', 'id_rt', $id_task);
$task_data = db_get_row('tdiscovery_tmp_agents', 'id_rt', $id_task);
$task = db_get_row('trecon_task', 'id_rt', $id_task);
if (is_array($task)) {
$data = json_decode(base64_decode($task['data']), true);
if (is_array($task_data)) {
$data = json_decode(base64_decode($task_data['data']), true);
$simple_data = array_reduce(
$data,
function ($carry, $item) {
$id = $item['agent']['nombre'];
$carry[] = [
'id' => $id,
'name' => $item['agent']['nombre'],
$tmp = [
'id' => $id,
'name' => $item['agent']['nombre'],
'checked' => $item['checked'],
];
if (empty($item['agent']['agent_id'])) {
$agent_id = agents_get_agent_id($id, true);
if ($agent_id > 0) {
$tmp['disabled'] = 1;
$tmp['checked'] = 1;
$tmp['agent_id'] = $agent_id;
}
}
$carry[] = $tmp;
$childs = array_reduce(
$item['modules'],
function ($c, $i) use ($id) {
$c[] = [
'name' => $i['name'],
'id' => $id.'-'.$i['name'],
'pid' => $id,
];
return $c;
function ($c, $i) use ($id, $agent_id) {
$tmp = [
'name' => $i['name'],
'id' => $id.'-'.$i['name'],
'pid' => $id,
'checked' => $i['checked'],
];
if (empty($i['agentmodule_id'])) {
$agentmodule_id = modules_get_agentmodule_id(
io_safe_input($i['name']),
$agent_id
);
if ($agentmodule_id > 0) {
$tmp['disabled'] = 1;
$tmp['checked'] = 1;
$tmp['module_id'] = $agentmodule_id;
}
}
$c[] = $tmp;
return $c;
},
[]
);
@ -1138,8 +1184,29 @@ class DiscoveryTaskList extends HTML
[]
);
echo '<div>';
echo $this->progressTaskSummary($task);
echo '</div>';
echo '<div class="subtitle">';
echo '<span>';
echo __('Please select devices to be monitored');
echo '</span><div class="manage">';
echo '<button onclick="$(\'.sim-tree li a\').each(function(){simTree_tree.doCheck($(this), false); simTree_tree.clickNode($(this));});">';
echo __('select all');
echo '</button>';
echo '<button onclick="$(\'.sim-tree li a\').each(function(){simTree_tree.doCheck($(this), true); simTree_tree.clickNode($(this));});">';
echo __('deselect all');
echo '</button>';
echo '</div>';
echo '</div>';
echo '<form id="review">';
echo '<div id="tree"></div>';
echo parent::printTree('tree', $simple_data);
echo parent::printTree(
'tree',
$simple_data
);
echo '</form>';
}
}
@ -1150,7 +1217,7 @@ class DiscoveryTaskList extends HTML
*
* @return void
*/
public function parse_task_review()
public function parseTaskReview()
{
$id_task = get_parameter('id', 0);
if ($id_task <= 0) {
@ -1158,7 +1225,91 @@ class DiscoveryTaskList extends HTML
return;
}
$out = obhd($_REQUEST);
$selection = io_safe_output(get_parameter('tree-data-tree', ''));
if (empty($selection)) {
$ids = [];
} else {
$selection = json_decode($selection, true);
$ids = array_reduce(
$selection,
function ($carry, $item) {
$carry[] = $item['id'];
return $carry;
}
);
}
$task_data = db_get_row('tdiscovery_tmp_agents', 'id', $id_task);
if (is_array($task_data)) {
$data = json_decode(base64_decode($task_data['data']), true);
}
$summary = [];
if (is_array($data)) {
foreach ($data as $agent_name => $row) {
if (in_array($agent_name, $ids)) {
$data[$agent_name]['checked'] = 1;
if ($data[$agent_name]['checked'] != 1) {
$summary[] = '<li class="added">'.$agent_name.'</li>';
}
} else {
if ($data[$agent_name]['checked'] == 1) {
$summary[] = '<li class="removed">'.__('Removed').' '.$agent_name.'</li>';
}
$data[$agent_name]['checked'] = 0;
}
if (is_array($row['modules'])) {
$n_modules = count($row['modules']);
for ($i = 0; $i < $n_modules; $i++) {
$module_name = $row['modules'][$i]['name'];
if (in_array($agent_name.'-'.$module_name, $ids)) {
$data[$agent_name]['modules'][$i]['checked'] = 1;
if ($row['modules'][$i]['checked'] != 1) {
$summary[] = '<li class="added">'.$agent_name.' - '.$module_name.'</li>';
}
} else {
if ($row['modules'][$i]['checked'] == 1) {
$summary[] = '<li class="removed">'.__('Removed').' '.$agent_name.' - '.$module_name.'</li>';
}
$data[$agent_name]['modules'][$i]['checked'] = 0;
}
}
}
}
// Update targets.
db_process_sql_update(
'tdiscovery_tmp_agents',
[
'data' => base64_encode(json_encode($data)),
],
['id_rt' => $id_task]
);
// Schedule execution.
db_process_sql_update(
'trecon_task',
[
'utimestamp' => 0,
'status' => 1,
'direct_report' => DISCOVERY_RESULTS,
],
['id_rt' => $id_task]
);
}
if (empty($summary)) {
$out .= __('No changes');
} else {
$out .= __('Summary');
$out .= '<ul>';
$out .= join('', $summary);
$out .= '</ul>';
}
echo json_encode(
['result' => $out]

View File

@ -38,7 +38,7 @@ class HTML
/**
* Breadcrum
*
* @var array.
* @var array
*/
public $breadcrum;
@ -119,7 +119,7 @@ class HTML
/**
* Add an element to breadcrum array.
*
* @param string $breads Elements to add to breadcrum.
* @param array $breads Elements to add to breadcrum.
*
* @return void
*/
@ -335,6 +335,27 @@ class HTML
}
/**
* Return formatted html for error handler.
*
* @param string $message Error mesage.
*
* @return string
*/
public function error($message)
{
if (is_ajax()) {
echo json_encode(
[
'error' => ui_print_error_message($message, '', true),
]
);
} else {
return ui_print_error_message($message, '', true);
}
}
/**
* Print input using functions html lib.
*
@ -1003,27 +1024,62 @@ class HTML
/**
* Returns a n-dimensional array (data) into a html tree structure.
*
* Advanced documentation:
* https://www.jqueryscript.net/other/Checkable-Hierarchical-Tree.html
*
* @param string $target Target DOM id.
* @param array $data N-dimensional array.
* @param string $callback Callback function.
* @param string $onclick Onclick function.
* @param string $onchange Onchange function.
*
* @return string
*/
public static function printTree($target, $data, $callback='')
{
public static function printTree(
$target,
$data,
$onclick='',
$onchange=''
) {
ui_require_css_file('simTree');
ui_require_javascript_file('simTree');
/*
* SAMPLE SELECT ALL.
*
* echo '<div class="subtitle">';
* echo '<span>';
* echo __('Please select devices to be monitored');
* echo '</span><div class="manage">';
* echo '<button onclick="$(\'.sim-tree li a\').each(function(){simTree_tree.doCheck($(this), false); simTree_tree.clickNode($(this));});">';
* echo __('select all');
* echo '</button>';
* echo '<button onclick="$(\'.sim-tree li a\').each(function(){simTree_tree.doCheck($(this), true); simTree_tree.clickNode($(this));});">';
* echo __('deselect all');
* echo '</button>';
* echo '</div>';
* echo '</div>';
*
* END SAMPLE SELECT ALL.
*/
$output = '
<script type="text/javascript">
var simTree_'.$target.';
$(document).ready(function() {
simTree({
simTree_'.$target.' = simTree({
el: $('.$target.'),
data: '.json_encode($data).',
onClick: function (item) {'.$callback.';},
onClick: function (item) {'.$onclick.';},
onChange: function (item) {
'.$onchange.';
$("#tree-data-'.$target.'").val(JSON.stringify(this.sels));
},
check: true,
linkParent: true
});
$("#'.$target.'").append(
\'<input type="hidden" id="tree-data-'.$target.'" name="tree-data-'.$target.'">\'
);
});
</script>';

View File

@ -597,6 +597,11 @@ define('DISCOVERY_DEPLOY_AGENTS', 9);
define('DISCOVERY_APP_SAP', 10);
// Force task build tmp results.
define('DISCOVERY_SEARCH', 0);
define('DISCOVERY_STANDARD', 1);
define('DISCOVERY_RESULTS', 2);
// Discovery types matching definition.
define('DISCOVERY_SCRIPT_HOSTDEVICES_CUSTOM', 0);
// Standard applications.

View File

@ -50,7 +50,7 @@ function progress_task_list(id, title) {
if (!$elem.dialog("isOpen"))
timeoutRef = setInterval(function() {
xhr = fetchTaskList(id, handleFetchTaskList);
}, 3000);
}, 300000);
xhr = fetchTaskList(id, handleFetchTaskList);
}
@ -66,7 +66,7 @@ function fetchTaskList(id, callback) {
data: {
page: "godmode/servers/discovery",
wiz: "tasklist",
method: "progress_task_discovery",
method: "progressTaskDiscovery",
id: id
},
type: "POST",
@ -100,7 +100,7 @@ function show_map(id, name) {
data: {
page: "godmode/servers/discovery",
wiz: "tasklist",
method: "task_showmap",
method: "taskShowmap",
id: id
},
type: "POST",
@ -157,6 +157,7 @@ function show_review(id, name) {
of: window,
collision: "fit"
},
maxHeight: 400,
title: title,
buttons: [
{
@ -166,9 +167,8 @@ function show_review(id, name) {
click: function(e) {
if (!failed) {
$(".ui-dialog-content").dialog("close");
$(".info").hide();
cleanupDOM();
dt_keystore.draw(false);
$("#task_review").empty();
location.reload();
} else {
$(this).dialog("close");
}
@ -189,11 +189,12 @@ function show_review(id, name) {
],
onshow: {
page: "godmode/servers/discovery",
method: "show_task_review"
method: "showTaskReview",
maxHeight: 800
},
onsubmit: {
page: "godmode/servers/discovery",
method: "parse_task_review"
method: "parseTaskReview"
}
});
}

View File

@ -335,6 +335,12 @@ function load_modal(settings) {
? settings.onshow.maxHeight
: "auto",
overlay: settings.modal.overlay,
position: {
my: "top+20%",
at: "top",
of: window,
collision: "fit"
},
buttons: required_buttons,
closeOnEscape: true,
open: function() {

View File

@ -1,3 +1,7 @@
/**
* IMPORTANT. Official plugin does not allow string ids
* This modificated one yes.
*/
!(function(e) {
var t = {};
function i(s) {
@ -373,7 +377,7 @@
}
},
expandNode: function(e) {
var t = e.addClass ? e : this.$el.find("[data-id=" + e + "]"),
var t = e.addClass ? e : this.$el.find("[data-id='" + e + "']"),
i = t.data("data"),
s = i[this.options.response.pid],
n = t.children(".sim-tree-spread"),
@ -392,7 +396,7 @@
$.isArray(i) &&
(this.options.check || (i = [i[0]]),
$.each(i, function(e, i) {
var a = t.$el.find("[data-id=" + i + "]"),
var a = t.$el.find("[data-id='" + i + "']"),
r = a.children("a"),
d = r.children(".sim-tree-checkbox"),
o = a.data("data");
@ -415,7 +419,7 @@
("string" != typeof i && "number" != typeof i) || (i = [i]),
$.isArray(i) &&
$.each(i, function(e, i) {
var s = t.$el.find("[data-id=" + i + "]");
var s = t.$el.find("[data-id='" + i + "']");
s.addClass("disabled");
});
},

View File

@ -36,6 +36,48 @@ ul.progress_task_discovery {
flex-direction: column;
}
div.subtitle {
width: 100%;
margin-top: 40px;
text-align: center;
}
div.subtitle span {
font-size: 1.9em;
font-family: "lato-bolder", "Open Sans", sans-serif;
}
div.subtitle div.manage {
text-align: center;
padding-top: 2em;
padding-bottom: 1em;
}
div.subtitle div.manage button {
font-size: 1.2em;
font-family: "lato", "Open Sans", sans-serif;
display: inline-block;
padding: 5px;
margin-right: 1em;
cursor: pointer;
text-align: left;
}
#msg ul li {
margin-left: 3em;
}
li.added {
list-style: url("../../images/header_ready.png");
}
li.removed {
list-style: url("../../images/header_down.png");
}
#msg ul {
list-style: url("../../images/header_ready.png");
}
ul.progress_task_discovery li span {
font-size: 9pt;
margin-left: 20px;