Merge branch 'pulido/discovery-duplicados-alhambra' into 'develop'

Locate instead search before create new Discovered agent

See merge request artica/pandorafms!2790
This commit is contained in:
Alejandro Fraguas 2019-10-10 18:51:23 +02:00
commit 41b84ccd81
1 changed files with 12 additions and 40 deletions

View File

@ -360,8 +360,9 @@ sub PandoraFMS::Recon::Base::guess_os($$) {
if (-x $self->{'pa_config'}->{'xprobe2'}) { if (-x $self->{'pa_config'}->{'xprobe2'}) {
my $return = `"$self->{pa_config}->{xprobe2}" $device 2>$DEVNULL`; my $return = `"$self->{pa_config}->{xprobe2}" $device 2>$DEVNULL`;
if ($? == 0) { if ($? == 0) {
my ($output) = $a =~ /Running OS:(.*)/; if($return =~ /Running OS:(.*)/) {
return pandora_get_os($self->{'dbh'}, $output); return pandora_get_os($self->{'dbh'}, $1);
}
} }
} }
@ -370,8 +371,9 @@ sub PandoraFMS::Recon::Base::guess_os($$) {
my $return = `"$self->{pa_config}->{nmap}" -F -O $device 2>$DEVNULL`; my $return = `"$self->{pa_config}->{nmap}" -F -O $device 2>$DEVNULL`;
return OS_OTHER if ($? != 0); return OS_OTHER if ($? != 0);
my ($output) = $return =~ /Aggressive OS guesses:\s*(.*)/; if ($return =~ /Aggressive OS guesses:\s*(.*)/) {
return pandora_get_os($self->{'dbh'}, $output); return pandora_get_os($self->{'dbh'}, $1);
}
} }
return OS_OTHER; return OS_OTHER;
@ -536,7 +538,7 @@ sub PandoraFMS::Recon::Base::create_agents($$) {
return undef; return undef;
} }
if ($agent->{'address'} ne '') { if (defined($agent->{'address'}) && $agent->{'address'} ne '') {
pandora_add_agent_address( pandora_add_agent_address(
$pa_config, $agent_id, $agent->{'agent_name'}, $pa_config, $agent_id, $agent->{'agent_name'},
$agent->{'address'}, $dbh $agent->{'address'}, $dbh
@ -582,44 +584,14 @@ sub PandoraFMS::Recon::Base::create_agent($$) {
# Clean name. # Clean name.
$device = clean_blank($device); $device = clean_blank($device);
my @agents = get_db_rows($self->{'dbh'}, # Resolve hostnames.
'SELECT * FROM taddress, taddress_agent, tagente my $host_name = (($self->{'resolve_names'} == 1) ? gethostbyaddr(inet_aton($device), AF_INET) : $device);
WHERE tagente.id_agente = taddress_agent.id_agent # Fallback to device IP if host name could not be resolved.
AND taddress_agent.id_a = taddress.id_a $host_name = $device if (!defined($host_name) || $host_name eq '');
AND ip = ?', $device my $agent = locate_agent($self->{'pa_config'}, $self->{'dbh'}, $host_name);
);
# Does the host already exist?
my $agent;
foreach my $candidate (@agents) {
$agent = {map {$_} %$candidate}; # copy contents, do not use shallow copy
# exclude $device itself, because it handle corner case when target includes NAT
my @registered = map {$_->{ip}} get_db_rows($self->{'dbh'},
'SELECT ip FROM taddress, taddress_agent, tagente
WHERE tagente.id_agente = taddress_agent.id_agent
AND taddress_agent.id_a = taddress.id_a
AND tagente.id_agente = ?
AND taddress.ip != ?', $agent->{id_agente}, $device
);
foreach my $ip_addr (@registered) {
my @matched = grep { $_ =~ /^$ip_addr$/ } $self->get_addresses($device);
if (scalar(@matched) == 0) {
$agent = undef;
last;
}
}
last if(defined($agent)); # exit loop if match all ip_addr
}
if (!defined($agent)) {
$agent = get_agent_from_name($self->{'dbh'}, $device);
}
my ($agent_id, $agent_learning); my ($agent_id, $agent_learning);
if (!defined($agent)) { if (!defined($agent)) {
# Resolve hostnames.
my $host_name = $self->{'resolve_names'} == 1 ? gethostbyaddr (inet_aton($device), AF_INET) : $device;
$host_name = $device unless defined ($host_name); $host_name = $device unless defined ($host_name);
# Guess the OS. # Guess the OS.