Skip to content

Commit

Permalink
feat: Add remoteinventory scan support in netscan
Browse files Browse the repository at this point in the history
  • Loading branch information
g-bougard committed Oct 9, 2023
1 parent 959aea3 commit 57bdbf6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 34 deletions.
11 changes: 11 additions & 0 deletions lib/GLPI/Agent/Target/Local.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ sub getPath {
return $self->{path};
}

sub setPath {
my ($self, $path) = @_;

$self->{path} = $path
if $path && -d $path;
}

sub getName {
my ($self) = @_;

Expand Down Expand Up @@ -95,6 +102,10 @@ Reset the local target counter.
Return the local output directory for this target.
=head2 setPath($path)
Set the local output directory for this target.
=head2 getName()
Return the target name
Expand Down
81 changes: 47 additions & 34 deletions lib/GLPI/Agent/Task/NetDiscovery.pm
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ sub run {
if ($result && $result->{IP}) {
$result->{ENTITY} = $range->{entity} if defined($range->{entity});

# Keep private attributs from the result
my $esxscan = delete $result->{_esxscan};
# Keep _found private attribut from the result
my $found = delete $result->{_found};

my $authsnmp = $result->{AUTHSNMP};
my $deviceid;
Expand Down Expand Up @@ -444,17 +444,41 @@ sub run {
);
};
my $credentials = first { $_->{ID} eq $authremote } @{$jobaddress->{remote_credentials}};
if ($credentials) {
if ($credentials && $found) {
my $path;
$path = $self->{target}->getPath() if $self->{target}->isType('local');
# When target path is agent folder, inventory should be saved in inventory subfolder
$path .= '/inventory' if $path eq '.';
if ($credentials->{TYPE} eq 'esx' && $esxscan) {
# As we still have run the connection part in _scanAddressByRemote(), we reuse the connected object
$esxscan->serverInventory($path, $collectdeviceid, $deviceid);
# As we still have run the connection part in _scanAddressByRemote(), we reuse the connected object
if ($credentials->{TYPE} eq 'esx') {
$found->serverInventory($path, $collectdeviceid, $deviceid);
} else {
GLPI::Agent::Task::RemoteInventory->require();
# TODO Support RemoteInventory task run
# Setup a remote inventory as it is done in GLPI::Agent::Task::RemoteInventory
GLPI::Agent::Task::Inventory->require();

# Update local target path if the case it has been set to agent folder
$self->{target}->setPath($path)
if $self->{target}->isType('local');

my $task = GLPI::Agent::Task::Inventory->new(
logger => $self->{logger},
config => $self->{config},
datadir => $self->{datadir},
target => $self->{target},
agentid => $self->{agentid},
deviceid => $found->deviceid // $found->safe_url(),
);

# Set now task is a remote one
$task->setRemote($found->protocol());

setRemoteForTools($found);

$task->run();

$found->disconnect();

resetRemoteForTools();
}
}
}
Expand Down Expand Up @@ -911,7 +935,7 @@ sub _scanAddressBySNMPReal {
sub _scanAddressByRemote {
my ($self, $params) = @_;

my (%device, $found, $error);
my (%device, $error);
my %params = map { $_ => $self->{$_} } qw(config datadir target deviceid logger agentid);

foreach my $credential (@{$params->{remote_credentials}}) {
Expand All @@ -929,8 +953,7 @@ sub _scanAddressByRemote {
user => $credential->{USERNAME},
password => $credential->{PASSWORD}
)) {
$device{_esxscan} = $esxscan;
$found++;
$device{_found} = $esxscan;
} else {
$error = $esxscan->lastError();
my %errors = (
Expand All @@ -942,14 +965,6 @@ sub _scanAddressByRemote {
# Anyway set COMPUTER type if we got an answer
$device{TYPE} = 'COMPUTER' if $error;
}

# no result means either no host, no response, or invalid credentials
$self->{logger}->debug(
sprintf "- scanning %s with ESX, credentials %s: %s",
$params->{ip},
$credential->{ID},
$found ? 'success' : $error ? "no result, $error" : 'no result'
);
} else {

GLPI::Agent::Task::RemoteInventory::Remote->require();
Expand All @@ -972,23 +987,21 @@ sub _scanAddressByRemote {
$remote->prepare();

$error = $remote->checking_error();
unless ($error) {
$device{_remote} = $remote;
$found++;
}

# no result means either no host, no response, or invalid credentials
$self->{logger}->debug(
sprintf "- scanning %s%s with %sRemoteInventory, credentials %s: %s",
$params->{ip},
$credential->{PORT} ? ':'.$credential->{PORT} : '',
$credential->{TYPE} ? $credential->{TYPE}.' ' : '',
$credential->{ID},
$found ? 'success' : $error ? "no result, $error" : 'no result'
);
$device{_found} = $remote
unless $error;
}

if ($found) {
# no result means either no host, no response, or invalid credentials
$self->{logger}->debug(
sprintf "- scanning %s%s with %s, credentials %s: %s",
$params->{ip},
$credential->{TYPE} ne 'esx' && $credential->{PORT} ? ':'.$credential->{PORT} : '',
$credential->{TYPE} eq 'esx' ? 'ESX' : $credential->{TYPE}.' RemoteInventory',
$credential->{ID},
$device{_found} ? 'success' : $error ? "no result, $error" : 'no result'
);

if ($device{_found}) {
$device{AUTHREMOTE} = $credential->{ID};
$device{TYPE} = 'COMPUTER';
last;
Expand Down

0 comments on commit 57bdbf6

Please sign in to comment.