Skip to content

Commit

Permalink
fix: Fix current folder local target support
Browse files Browse the repository at this point in the history
  • Loading branch information
g-bougard committed Dec 12, 2023
1 parent 56e2020 commit 6961b4c
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ core:
* Move runPowerShell() API remote case support in RemoteInventory API
* Update httpd default page to show targets id and server url or local path only
for trusted clients
* Update local target API to support getFullPath() & setFullPath() calls
* Handle '.' local target when run as a service to save inventories in vardir

inventory:
* PR #531: Add SentinelOne Antivirus support on Linux, thanks to @MarcSamD
Expand Down Expand Up @@ -36,6 +38,7 @@ netdiscovery/netinventory:
* Update timeout to backend-collect-timeout configuration when scanning ESX or
RemoteInventory after a successful scan requested by ToolBox
* Fix possible concurrency error leading to an unrecoverable blocked task
* Use new local target API to set expected saving folder

esx:
* Fix first connection timeout support
Expand All @@ -44,6 +47,7 @@ esx:
toolbox:
* fix #533: Fix Toolbox export buttons in inventory results
* Fix wrong remote inventory results when using a short timeout for quickier detection
* Handle agent folder as vardir folder when agent is running as a service

injector:
* fix #537: Make -x, --xml-ua & --json-ua options equivalent and update help text
Expand Down
4 changes: 2 additions & 2 deletions lib/GLPI/Agent/Daemon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ sub run {
foreach my $target (@targets) {
my $date = $target->getFormatedNextRunDate();
my $id = $target->id();
my $name = $target->getName();
$logger->info("target $id: next run: $date - $name");
my $info = $target->isType('local') ? $target->getFullPath() : $target->getName();
$logger->info("target $id: next run: $date - $info");
}
}

Expand Down
13 changes: 13 additions & 0 deletions lib/GLPI/Agent/Daemon/Win32.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ sub new {
return $self;
}

sub init {
my ($self, %params) = @_;

$self->SUPER::init(%params);

# When running as a service the script makes a chdir to perl/bin and this isn't
# convenient for local target set to "." so we need to fix it to vardir folder
foreach my $target (@{$self->{targets}}) {
next unless $target->isType('local') && $target->getPath() eq '.';
$target->setFullPath($self->{config}->{vardir});
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion lib/GLPI/Agent/HTTP/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ sub _handle_root {
$self->{agent}->getTargets();

my @local_targets =
map { { id => $_->id(), target => $trust ? $_->getPath() : '', date => $_->getFormatedNextRunDate() } }
map { { id => $_->id(), target => $trust ? $_->getFullPath() : '', date => $_->getFormatedNextRunDate() } }
grep { $_->isType('local') }
$self->{agent}->getTargets();

Expand Down
30 changes: 26 additions & 4 deletions lib/GLPI/Agent/HTTP/Server/ToolBox/Inventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ sub init {

return unless $self->read_yaml();

# Update networktask_save folder is running as a service and folder set to '.'
my $yaml_config = $self->yaml('configuration') || {};
if (empty($yaml_config->{'networktask_save'}) || $yaml_config->{'networktask_save'} eq '.') {
my $agent = $self->{toolbox}->{server}->{agent};
if (($OSNAME eq 'MSWin32' && ref($agent) eq 'GLPI::Agent::Daemon::Win32') || getppid() == 1) {
# We are running as a service and we must fix networktask_save to vardir
$yaml_config->{'networktask_save'} = $agent->{vardir};
$self->need_save("configuration");
$self->write_yaml();
}
}

$self->_load_jobs();
}

Expand Down Expand Up @@ -224,11 +236,14 @@ sub update_template_hash {
next if $target->isType('listener');
my $id = $target->id()
or next;
$hash->{targets}->{$id} = [ $target->getType(), $target->getName() ];
$hash->{targets}->{$id} = [ $target->getType(), $target->isType('local') ? $target->getFullPath() : $target->getName() ];
}
# Default target when creating a new task
$hash->{default_target} = $hash->{targets}->{server0} ? 'server0' : '';

# Default folder: '.' means "Agent Folder"
$hash->{default_local} = $yaml_config->{networktask_save} // '.';

# Set running task
$hash->{outputid} = $self->{taskid} || '';
$hash->{tasks} = $self->{tasks} || {};
Expand Down Expand Up @@ -842,7 +857,7 @@ sub netscan {

# If not using an agent target, create a local target and update it to run now
unless ($target) {
my $path = $yaml_config->{networktask_save} // '.';
my $path = $yaml_config->{networktask_save} || '.';

# Make sure path exists as folder
mkdir $path unless -d $path;
Expand All @@ -854,6 +869,10 @@ sub netscan {
basevardir => $agent->{vardir},
path => $path
);

# When running as a service we need to use vardir as default local folder
$target->setFullPath($agent->{vardir})
if $path eq '.' && (($OSNAME eq 'MSWin32' && ref($agent) eq 'GLPI::Agent::Daemon::Win32') || getppid() == 1);
}

# Create an NetDiscovery task
Expand Down Expand Up @@ -965,8 +984,7 @@ sub _run_local {

# If not using an agent target, create a local target and update it to run now
unless ($target) {
my $path = !$yaml_config->{networktask_save} || $yaml_config->{networktask_save} eq '.' ?
"inventory" : $yaml_config->{networktask_save}."/inventory";
my $path = $yaml_config->{networktask_save} || '.';

# Make sure path exists as folder
mkdir $path unless -d $path;
Expand All @@ -978,6 +996,10 @@ sub _run_local {
basevardir => $agent->{vardir},
path => $path
);

# When running as a service we need to use vardir as default local folder
$target->setFullPath($agent->{vardir})
if $path eq '.' && (($OSNAME eq 'MSWin32' && ref($agent) eq 'GLPI::Agent::Daemon::Win32') || getppid() == 1);
}

# Create an Inventory task
Expand Down
20 changes: 20 additions & 0 deletions lib/GLPI/Agent/Target/Local.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use warnings;

use parent 'GLPI::Agent::Target';

use File::Spec;
use Cwd qw(abs_path);

my $count = 0;

sub new {
Expand All @@ -15,6 +18,8 @@ sub new {
my $self = $class->SUPER::new(%params);

$self->{path} = $params{path};
$self->{fullpath} = abs_path(File::Spec->rel2abs($params{path}))
unless $self->{path} eq '-';

$self->{format} = $params{json} ? 'json' : $params{html} ? 'html' : 'xml';

Expand All @@ -36,13 +41,28 @@ sub getPath {
return $self->{path};
}

sub getFullPath {
my ($self, $subfolder) = @_;

my $fullpath = $self->{fullpath} || $self->{path};

return $subfolder ? "$fullpath/$subfolder" : $fullpath;
}

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

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion lib/GLPI/Agent/Task/Inventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ sub submit {

if ($self->{target}->isType('local')) {

my $file = $inventory->save($self->{target}->getPath());
my $file = $inventory->save($self->{target}->getFullPath());
$self->{logger}->info("Inventory ".($file eq '-' ? "dumped on standard output" : "saved in $file"))
if $file;

Expand Down
19 changes: 10 additions & 9 deletions lib/GLPI/Agent/Task/NetDiscovery.pm
Original file line number Diff line number Diff line change
Expand Up @@ -459,20 +459,21 @@ sub run {
$timeout = $self->{config}->{"backend-collect-timeout"};
$found->timeout($timeout);

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 '.';
my ($path, $agentfolder);
if ($self->{target}->isType('local')) {
$agentfolder = $self->{target}->getPath() eq '.' ? 'inventory' : '';
# When target path is agent folder, inventory should be saved in inventory subfolder
$path = $self->{target}->getFullPath($agentfolder);
}
# 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 {
# 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');
# Update local target path in the case it has been updated
$self->{target}->setFullPath($path) if $agentfolder;

my $task = GLPI::Agent::Task::Inventory->new(
logger => $self->{logger},
Expand Down Expand Up @@ -622,8 +623,8 @@ sub _sendMessage {
} else {
# We don't have to save control messages
return unless $content->{DEVICE};
$path .= "/netdiscovery";
mkpath($path);
$path = $self->{target}->getFullPath("netdiscovery");
mkpath($path) unless -d $path;
$ip = $content->{DEVICE}->[0]->{IP};
$file = $path . "/$ip.xml";
}
Expand Down
4 changes: 2 additions & 2 deletions lib/GLPI/Agent/Task/NetInventory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ sub _sendMessage {
if ($path eq '-') {
$handle = \*STDOUT;
} else {
$path .= "/netinventory";
mkpath($path);
$path = $self->{target}->getFullPath("netinventory");
mkpath($path) unless -d $path;
$file = $path . "/$ip.xml";
}

Expand Down
1 change: 1 addition & 0 deletions share/html/toolbox/inventory-language-en.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ netdiscovery task not installed:
netinventory task not installed:
netdiscovery and netinventory tasks not installed:
Agent folder:
Configured folder:
Target:

Created inventories:
Expand Down
1 change: 1 addition & 0 deletions share/html/toolbox/inventory-language-fr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ netdiscovery task not installed: Tâche netdiscovery non installée
netinventory task not installed: Tâche netinventory non installée
netdiscovery and netinventory tasks not installed: Tâches netdiscovery et netinventory non installées
Agent folder: Dossier de l'agent
Configured folder: Dossier configuré
Target: Cible

Created inventories: Inventaires créés
Expand Down
5 changes: 4 additions & 1 deletion share/html/toolbox/inventory-list.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ if (@jobs_order) {
my @target_tooltip;
if ($target && $targets{$target}) {
@target_tooltip = ( $targets{$target}->[0], encode('UTF-8', encode_entities($targets{$target}->[1])));
} else {
} elsif (!$default_local || $default_local eq '.') {
$target = _("Agent folder");
@target_tooltip = ( local => $target );
} else {
$target = _("Configured folder");
@target_tooltip = ( local => "<tt>$default_local</tt>" );
}
push @configuration, _("Target").":&nbsp;
<div class='with-tooltip'>$target
Expand Down

0 comments on commit 6961b4c

Please sign in to comment.