Templates

This commit is contained in:
fbsanchez 2020-03-18 19:31:58 +01:00
parent 6504fe49b5
commit f58276ddbb
8 changed files with 379 additions and 156 deletions

View File

@ -10,7 +10,7 @@ CREATE TABLE `tdiscovery_tmp_agents` (
`label` varchar(600) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`data` text,
`review_date` datetime DEFAULT NULL,
`created` int(1) unsigned NOT NULL DEFAULT '0',
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id_rt` (`id_rt`),
INDEX `label` (`label`),

View File

@ -1672,7 +1672,7 @@ CREATE TABLE `tdiscovery_tmp_agents` (
`label` varchar(600) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`data` text,
`review_date` datetime DEFAULT NULL,
`created` int(1) unsigned NOT NULL DEFAULT '0',
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id_rt` (`id_rt`),
INDEX `label` (`label`),

View File

@ -289,7 +289,7 @@ class DiscoveryTaskList extends HTML
[
'utimestamp' => 0,
'status' => 1,
'direct_report' => DISCOVERY_RESULTS,
'direct_report' => $direct_report,
],
['id_rt' => $id]
);
@ -526,14 +526,22 @@ class DiscoveryTaskList extends HTML
if ($task['direct_report'] == 1) {
if ($task['status'] <= 0) {
$data[5] = __('Done');
} else if ($task['utimestamp'] == 0
&& empty($task['summary'])
) {
$data[5] = __('Not started');
} else {
$data[5] = __('Pending');
}
} else {
if ($task['status'] <= 0) {
if ($task['status'] <= 0 && $task['utimestamp'] > 0) {
$data[5] = '<span class="link review" onclick="show_review('.$task['id_rt'].',\''.$task['name'].'\')">';
$data[5] .= __('Review');
$data[5] .= '</span>';
} else if ($task['utimestamp'] == 0
&& empty($task['summary'])
) {
$data[5] = __('Not started');
} else {
$data[5] = __('Searching');
}
@ -1121,68 +1129,83 @@ class DiscoveryTaskList extends HTML
return;
}
$task_data = db_get_row('tdiscovery_tmp_agents', 'id_rt', $id_task);
$task_data = db_get_all_rows_filter(
'tdiscovery_tmp_agents',
['id_rt' => $id_task]
);
$task = db_get_row('trecon_task', 'id_rt', $id_task);
$simple_data = [];
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'];
foreach ($task_data as $agent) {
$data = json_decode(base64_decode($agent['data']), true);
$tmp = [
'id' => $id,
'name' => $item['agent']['nombre'],
'checked' => $item['checked'],
];
if (is_array($data) === false) {
continue;
}
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;
}
}
$id = $data['agent']['nombre'];
$carry[] = $tmp;
$childs = array_reduce(
$item['modules'],
function ($c, $i) use ($id, $agent_id) {
$tmp = [
'name' => $i['name'],
'id' => $id.'-'.$i['name'],
'pid' => $id,
'checked' => $i['checked'],
];
// Partial.
$tmp = [
'id' => $id,
'name' => $id,
'checked' => $data['agent']['checked'],
];
$agent_id = $data['agent']['agent_id'];
if (empty($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;
}
// Store.
$simple_data[] = $tmp;
if (is_array($data['modules'])) {
$simple_data = array_merge(
$simple_data,
array_reduce(
$data['modules'],
function ($carry, $item) use ($id, $agent_id) {
if ($item['name'] == 'Host Alive') {
return $carry;
}
$tmp = [
'name' => $item['name'],
'id' => $id.'-'.$item['name'],
'pid' => $id,
'checked' => $item['checked'],
];
$agentmodule_id = $item['agentmodule_id'];
if (empty($agentmodule_id)) {
$agentmodule_id = modules_get_agentmodule_id(
io_safe_input($item['name']),
$agent_id
);
}
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;
},
[]
$carry[] = $tmp;
return $carry;
},
[]
)
);
$carry = array_merge(
$carry,
$childs
);
return $carry;
},
[]
);
}
}
echo '<div>';
echo $this->progressTaskSummary($task);
@ -1225,10 +1248,9 @@ class DiscoveryTaskList extends HTML
return;
}
$ids = [];
$selection = io_safe_output(get_parameter('tree-data-tree', ''));
if (empty($selection)) {
$ids = [];
} else {
if (empty($selection) === false) {
$selection = json_decode($selection, true);
$ids = array_reduce(
$selection,
@ -1239,57 +1261,73 @@ class DiscoveryTaskList extends HTML
);
}
$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);
}
$task_data = db_get_all_rows_filter(
'tdiscovery_tmp_agents',
['id_rt' => $id_task]
);
$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>';
if (is_array($ids)) {
foreach ($task_data as $row) {
$data = json_decode(base64_decode($row['data']), true);
if (is_array($data)) {
// Analize each agent.
$agent_name = $data['agent']['nombre'];
if (in_array($agent_name, $ids)) {
if ($data['agent']['checked'] != 1) {
$summary[] = '<li class="added">'.$agent_name.'</li>';
}
$data['agent']['checked'] = 1;
} else {
if ($data['agent']['checked'] == 1) {
$summary[] = '<li class="removed">'.__('Removed').' '.$agent_name.'</li>';
}
$data['agent']['checked'] = 0;
}
$data[$agent_name]['checked'] = 0;
}
// Modules.
if (is_array($data['modules'])) {
$n_modules = count($data['modules']);
for ($i = 0; $i < $n_modules; $i++) {
$module_name = $data['modules'][$i]['name'];
if (in_array($agent_name.'-'.$module_name, $ids)) {
if ($data['modules'][$i]['checked'] != 1) {
$summary[] = '<li class="added">'.$agent_name.' - '.$module_name.'</li>';
}
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['modules'][$i]['checked'] = 1;
} else {
if ($data['modules'][$i]['checked'] == 1) {
$summary[] = '<li class="removed">'.__('Removed').' '.$agent_name.' - '.$module_name.'</li>';
}
$data[$agent_name]['modules'][$i]['checked'] = 0;
$data['modules'][$i]['checked'] = 0;
}
}
}
// Update data.
db_process_sql_update(
'tdiscovery_tmp_agents',
[
'data' => base64_encode(json_encode($data)),
'review_date' => date('Y-m-d H:i:s'),
],
[
'id_rt' => $id_task,
'label' => $agent_name,
]
);
}
}
}
// Update targets.
db_process_sql_update(
'tdiscovery_tmp_agents',
[
'data' => base64_encode(json_encode($data)),
],
['id_rt' => $id_task]
);
if (empty($summary)) {
$out .= __('No changes');
} else {
// Schedule execution.
db_process_sql_update(
'trecon_task',
@ -1300,11 +1338,7 @@ class DiscoveryTaskList extends HTML
],
['id_rt' => $id_task]
);
}
if (empty($summary)) {
$out .= __('No changes');
} else {
$out .= __('Summary');
$out .= '<ul>';
$out .= join('', $summary);

View File

@ -1081,6 +1081,7 @@ var simTree_'.$target.';
\'<input type="hidden" id="tree-data-'.$target.'" name="tree-data-'.$target.'">\'
);
$("#tree-data-'.$target.'").val(JSON.stringify(simTree_'.$target.'.sels));
});
</script>';

View File

@ -819,7 +819,7 @@ CREATE TABLE `tdiscovery_tmp_agents` (
`label` varchar(600) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`data` text,
`review_date` datetime DEFAULT NULL,
`created` int(1) unsigned NOT NULL DEFAULT '0',
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id_rt` (`id_rt`),
INDEX `label` (`label`),

View File

@ -3266,7 +3266,7 @@ sub pandora_create_agent ($$$$$$$$$$;$$$$$$$$$$) {
$agent_mode = 1 unless defined($agent_mode);
$alias = $agent_name unless defined($alias);
$description = "Created by $server_name" unless ($description ne '');
$description = "Created by $server_name" unless (defined($description) && $description ne '');
my ($columns, $values) = db_insert_get_values ({ 'nombre' => safe_input($agent_name),
'direccion' => $address,
'comentarios' => $description,

View File

@ -61,6 +61,8 @@ use constant {
STEP_AFT => 2,
STEP_TRACEROUTE => 3,
STEP_GATEWAY => 4,
STEP_MONITORING => 5,
STEP_APPLY => 6,
STEP_STATISTICS => 1,
STEP_APP_SCAN => 2,
STEP_CUSTOM_QUERIES => 3,
@ -452,6 +454,14 @@ sub PandoraFMS::Recon::Base::create_network_profile_modules($$$) {
# ICMP
#
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
print Dumper($device);
print Dumper($self->{'task_data'});
die();
return if empty($self->{'id_network_profile'});
my @templates = split /,/, $self->{'id_network_profile'};
@ -481,7 +491,12 @@ sub PandoraFMS::Recon::Base::create_network_profile_modules($$$) {
$component->{'plugin_user'} = $self->{'snmp_auth_user'};
$component->{'plugin_pass'} = $self->{'snmp_auth_pass'};
pandora_create_module_from_network_component($self->{'pa_config'}, $component, $agent_id, $self->{'dbh'});
pandora_create_module_from_network_component(
$self->{'pa_config'},
$component,
$agent_id,
$self->{'dbh'}
);
}
}
@ -491,8 +506,19 @@ sub PandoraFMS::Recon::Base::create_network_profile_modules($$$) {
sub PandoraFMS::Recon::Base::report_scanned_agents($) {
my ($self) = @_;
my $force_creation = 0;
if (defined($self->{'task_data'}{'direct_report'})
&& $self->{'task_data'}{'direct_report'} == DISCOVERY_STANDARD
) {
$force_creation = 1;
}
#
# Creation
#
if(defined($self->{'task_data'}{'direct_report'})
&& $self->{'task_data'}{'direct_report'} == DISCOVERY_RESULTS
&& $self->{'task_data'}{'direct_report'} == DISCOVERY_RESULTS
) {
# Load cache.
my @rows = get_db_rows(
@ -501,63 +527,214 @@ sub PandoraFMS::Recon::Base::report_scanned_agents($) {
$self->{'task_data'}{'id_rt'}
);
my @agents;
my $progress = 0;
my $step = 100.00 / scalar @rows;
foreach my $row (@rows) {
$progress += $step;
$self->call('update_progress', $progress);
my $name = safe_output($row->{'label'});
my $data;
eval {
local $SIG{__DIE__};
$data = decode_json(decode_base64($row->{'data'}));
};
if ($@) {
$self->call('message', "ERROR JSON: $@", 3);
}
# Store.
$self->{'agents_found'}{$name} = $data;
}
}
# Register target agent if enabled.
if (is_enabled($data->{'agent'}{'checked'})
|| $force_creation
) {
my $parent_id;
my $os_id = $self->guess_os($data->{'agent'}{'direccion'});
foreach my $label (keys %{$self->{'agents_found'}}) {
if (!is_enabled($self->{'direct_report'})) {
# Store temporally. Wait user approval.
my $encoded;
eval {
local $SIG{__DIE__};
$encoded = encode_base64(encode_json($self->{'agents_found'}));
};
$self->call('message', "Agent accepted: ".$data->{'agent'}{'nombre'}, 5);
my $id = get_db_value(
$self->{'dbh'},
'SELECT id FROM tdiscovery_tmp_agents WHERE id_rt = ? AND label = ?',
$self->{'task_data'}{'id_rt'},
safe_input($label)
);
if (defined($id)) {
# Already defined.
$self->{'agents_found'}{$label}{'id'} = $id;
# Agent creation.
my $agent_id = $data->{'agent'}{'agent_id'};
my $agent_learning = 1;
if (!defined($agent_id) || $agent_id == 0) {
$agent_id = pandora_create_agent(
$self->{'pa_config'}, $self->{'servername'}, $data->{'agent'}{'nombre'},
$data->{'agent'}{'direccion'}, $self->{'task_data'}{'group_id'}, $parent_id,
$os_id, $data->{'agent'}->{'description'},
$data->{'agent'}{'interval'}, $self->{'dbh'},
$data->{'agent'}{'timezone_offset'}
);
$data->{'agent'}{'agent_id'} = $agent_id;
$agent_learning = 1;
} else {
$agent_learning = get_db_value(
$self->{'dbh'},
'SELECT modo FROM tagente WHERE id_agente = ?',
$agent_id
);
}
$self->call('message', "Agent id: ".$data->{'agent'}{'agent_id'}, 5);
# Create selected modules.
if(ref($data->{'modules'}) eq "ARRAY") {
for (my $i=0; $i < scalar @{$data->{'modules'}}; $i++) {
my $module = $data->{'modules'}[$i];
# Do not create any modules if the agent is not in learning mode.
next unless ($agent_learning == 1);
# Host alive is always being created.
if ($module->{'name'} ne 'Host Alive') {
next unless (is_enabled($module->{'checked'}) || $force_creation);
}
$self->call('message', "[$agent_id] Module: ".$module->{'name'}, 5);
my $agentmodule_id = $module->{'agentmodule_id'};
if (!defined($agentmodule_id) || $agentmodule_id == 0) {
# Create module.
$agentmodule_id = pandora_create_module_from_hash(
$self->{'pa_config'},
{
'id_tipo_modulo' => get_module_id($self->{'dbh'}, $module->{'type'}),
'id_modulo' => $module->{'id_modulo'},
'nombre' => safe_input($module->{'name'}),
'descripcion' => '',
'id_agente' => $agent_id,
'ip_target' => $data->{'agent'}{'direccion'}
},
$self->{'dbh'}
);
# Store.
$data->{'modules'}[$i]{'agentmodule_id'} = $agentmodule_id;
$self->call(
'message',
"[$agent_id] Module: ".$module->{'name'}." ID: $agentmodule_id",
5
);
}
}
}
my $encoded;
eval {
local $SIG{__DIE__};
$encoded = encode_base64(
encode_json($data)
);
};
# Update.
db_do(
$self->{'dbh'},
'UPDATE tdiscovery_tmp_agents SET `data` = ? '
.'WHERE `id_rt` = ? AND `label` = ?',
$encoded,
$self->{'task_data'}{'id_rt'},
safe_input($label)
$name
);
next;
}
# Insert.
$self->{'agents_found'}{$label}{'id'} = db_insert(
$self->{'dbh'},
'id',
'INSERT INTO tdiscovery_tmp_agents (`id_rt`,`label`,`data`,`created`) '
.'VALUES (?, ?, ?, now())',
$self->{'task_data'}{'id_rt'},
safe_input($label),
$encoded
);
} else {
# Create agents.
}
# Data creation finished.
return;
}
#
# Cleanup previous results.
#
db_do(
$self->{'dbh'},
'DELETE FROM tdiscovery_tmp_agents '
.'WHERE `id_rt` = ?',
$self->{'task_data'}{'id_rt'}
);
#
# Store and review.
#
my @hosts = keys %{$self->{'agents_found'}};
$self->{'step'} = STEP_APPLY;
my ($progress, $step) = (90, 10.0 / scalar(@hosts)); # From 90% to 100%.
foreach my $label (keys %{$self->{'agents_found'}}) {
$self->call('update_progress', $progress);
$progress += $step;
# Store temporally. Wait user approval.
my $encoded;
eval {
local $SIG{__DIE__};
$encoded = encode_base64(
encode_json($self->{'agents_found'}->{$label})
);
};
my $id = get_db_value(
$self->{'dbh'},
'SELECT id FROM tdiscovery_tmp_agents WHERE id_rt = ? AND label = ?',
$self->{'task_data'}{'id_rt'},
safe_input($label)
);
if (defined($id)) {
# Already defined.
$self->{'agents_found'}{$label}{'id'} = $id;
db_do(
$self->{'dbh'},
'UPDATE tdiscovery_tmp_agents SET `data` = ? '
.'WHERE `id_rt` = ? AND `label` = ?',
$encoded,
$self->{'task_data'}{'id_rt'},
safe_input($label)
);
next;
}
# Insert.
$self->{'agents_found'}{$label}{'id'} = db_insert(
$self->{'dbh'},
'id',
'INSERT INTO tdiscovery_tmp_agents (`id_rt`,`label`,`data`,`created`) '
.'VALUES (?, ?, ?, now())',
$self->{'task_data'}{'id_rt'},
safe_input($label),
$encoded
);
}
}
################################################################################
# Apply monitoring templates selected to detected agents.
################################################################################
sub PandoraFMS::Recon::Base::apply_monitoring($) {
my ($self) = @_;
my @hosts = keys %{$self->{'agents_found'}};
$self->{'step'} = STEP_MONITORING;
# From 80% to 90%.
my ($progress, $step) = (80, 10.0 / scalar(@hosts));
use Data::Dumper;
print Dumper($self->{'task_data'});
foreach my $label (keys %{$self->{'agents_found'}}) {
$self->call('update_progress', $progress);
$progress += $step;
print ">> $label\n";
}
}
################################################################################

View File

@ -22,6 +22,8 @@ use constant {
STEP_AFT => 2,
STEP_TRACEROUTE => 3,
STEP_GATEWAY => 4,
STEP_MONITORING => 5,
STEP_APPLY => 6,
STEP_STATISTICS => 1,
STEP_APP_SCAN => 2,
STEP_CUSTOM_QUERIES => 3,
@ -408,7 +410,7 @@ sub icmp_discovery($$) {
'ip_target' => $addr,
'name' => "Host Alive",
'description' => '',
'type' => 'generic_data',
'type' => 'remote_icmp_proc',
'id_modulo' => 2,
}
);
@ -1389,7 +1391,7 @@ sub scan_subnet($) {
my $host_block_size = $self->{'block_size'};
# The first 50% of the recon task approx.
my $step = 40.0 / scalar(@subnets) / (($total_hosts / $host_block_size)+1);
my $step = 25.0 / scalar(@subnets) / (($total_hosts / $host_block_size)+1);
my $subnet_step = 50.0 / (($total_hosts / $host_block_size)+1);
for (my $block_index=0;
@ -1438,7 +1440,7 @@ sub scan_subnet($) {
$self->call('update_progress', ceil($progress));
$total_hosts = scalar keys %hosts_alive;
$step = 40.0 / scalar(@subnets) / $total_hosts;
$step = 25.0 / scalar(@subnets) / $total_hosts;
$subnet_step = 50.0 / $total_hosts;
foreach my $addr (keys %hosts_alive) {
# Increase self summary.alive hosts.
@ -1807,18 +1809,21 @@ sub scan($) {
return $self->deploy_scan();
}
}
# XXX
print "Metodo: ".$self->{'task_data'}{'direct_report'}."\n";
if(defined($self->{'task_data'}{'direct_report'})
&& $self->{'task_data'}{'direct_report'} == DISCOVERY_RESULTS
) {
# Use Cached results.
return $self->call('report_scanned_agents');
$self->call('report_scanned_agents');
# Done!
$self->{'step'} = '';
$self->call('update_progress', -1);
return;
}
# Find devices.
$self->call('message', "[1/4] Scanning the network...", 3);
$self->call('message', "[1/6] Scanning the network...", 3);
$self->{'step'} = STEP_SCANNING;
$self->call('update_progress', $progress);
@ -1834,9 +1839,9 @@ sub scan($) {
$self->call('delete_connections');
# Connectivity from address forwarding tables.
$self->call('message', "[2/4] Finding address forwarding table connectivity...", 3);
$self->call('message', "[2/6] Finding address forwarding table connectivity...", 3);
$self->{'step'} = STEP_AFT;
($progress, $step) = (80, 8.0 / scalar(@hosts)); # From 50% to 70%.
($progress, $step) = (50, 10.0 / scalar(@hosts)); # From 50% to 60%.
for (my $i = 0; defined($hosts[$i]); $i++) {
$self->call('update_progress', $progress);
$progress += $step;
@ -1844,9 +1849,9 @@ sub scan($) {
}
# Connect hosts that are still unconnected using traceroute.
$self->call('message', "[3/4] Finding traceroute connectivity.", 3);
$self->call('message', "[3/6] Finding traceroute connectivity.", 3);
$self->{'step'} = STEP_TRACEROUTE;
($progress, $step) = (88, 8.0 / scalar(@hosts)); # From 70% to 90%.
($progress, $step) = (60, 10.0 / scalar(@hosts)); # From 60% to 70%.
foreach my $host (@hosts) {
$self->call('update_progress', $progress);
$progress += $step;
@ -1855,9 +1860,9 @@ sub scan($) {
}
# Connect hosts that are still unconnected using known gateways.
$self->call('message', "[4/4] Finding host to gateway connectivity.", 3);
$self->call('message', "[4/6] Finding host to gateway connectivity.", 3);
$self->{'step'} = STEP_GATEWAY;
($progress, $step) = (94, 6.0 / scalar(@hosts)); # From 70% to 90%.
($progress, $step) = (70, 10.0 / scalar(@hosts)); # From 70% to 80%.
$self->get_routes(); # Update the route cache.
foreach my $host (@hosts) {
$self->call('update_progress', $progress);
@ -1867,9 +1872,9 @@ sub scan($) {
}
}
# Done!
$self->{'step'} = '';
$self->call('update_progress', -1);
# Apply monitoring templates
$self->call('message', "[5/6] Applying monitoring.", 3);
$self->call('apply_monitoring', $self);
# Print debug information on found devices.
$self->call('message', "[Summary]", 3);
@ -1887,9 +1892,15 @@ sub scan($) {
$self->call('message', $dev_info, 3);
}
# Apply monitoring templates
$self->call('message', "[6/6] Process results.", 3);
# Send agent information to Database (Discovery) or XML (satellite.).
$self->call('report_scanned_agents', $self->{'agents_found'});
# Done!
$self->{'step'} = '';
$self->call('update_progress', -1);
}
################################################################################