From 08419411601d9fb80f7a130d299e3f0682ba8cd1 Mon Sep 17 00:00:00 2001 From: Guillaume Rousse Date: Mon, 9 Jul 2012 09:05:37 +0200 Subject: [PATCH] make stdout target a specific local target case --- etc/agent.cfg | 2 - fusioninventory-agent | 34 ++++++++--- lib/FusionInventory/Agent.pm | 34 ++++------- lib/FusionInventory/Agent/Config.pm | 5 ++ lib/FusionInventory/Agent/Target/Stdout.pm | 38 ------------ lib/FusionInventory/Agent/Task/Inventory.pm | 67 ++++++++++++--------- 6 files changed, 80 insertions(+), 100 deletions(-) delete mode 100644 lib/FusionInventory/Agent/Target/Stdout.pm diff --git a/etc/agent.cfg b/etc/agent.cfg index d84d8bcf84..6504fc59c9 100644 --- a/etc/agent.cfg +++ b/etc/agent.cfg @@ -14,8 +14,6 @@ #server = http://server.domain.com/glpi/plugins/fusioninventory/ # write tasks results in a directory #local = /tmp -# write tasks results on stdout -#stdout = 1 # # Task definition options diff --git a/fusioninventory-agent b/fusioninventory-agent index da74a1eec5..03bfe52e18 100755 --- a/fusioninventory-agent +++ b/fusioninventory-agent @@ -49,13 +49,13 @@ GetOptions( 'httpd-trust=s', 'scan-homedirs', 'server|s=s', - 'stdout', 'tag|t=s', 'timeout=i', 'user|u=s', 'version', 'wait|w=s', # deprecated options + 'stdout', 'daemon-no-fork|D', 'devlib', 'no-ocsdeploy', @@ -130,12 +130,11 @@ fusioninventory-agent - FusionInventory agent For Linux/UNIX, Windows and MacOSX =head1 SYNOPSIS -B [options] [--server server|--local directory|--sdout] +B [options] [--server server|--local path] Target definition options -s --server=URI send tasks result to a server - -l --local=DIR write tasks results in a directory - --stdout write tasks result on STDOUT + -l --local=PATH write tasks results locally Target scheduling options --delaytime=DURATION maximum initial delay before first target, in seconds (3600) @@ -294,10 +293,9 @@ example, the two lines below are all equivalent: =over - =item B<-s>, B<--server>=I -Send results of tasks execution to given server. +Send the results of tasks execution to given server. If I doesn't start with http:// or https://, the agent assume the parameter is a hostname and rewrite it as: @@ -314,13 +312,29 @@ and FusionInventory for GLPI this one: Multiple values can be specified, using comma as a separator. -=item B<-l>, B<--local>=I +=item B<-l>, B<--local>=I + +Write the results of tasks execution locally. + +Exact behaviour according to given path: + +=over + +=item * + +if I is a directory, a file will be created therein + +=item * + +if I is a file, it will be used directly -Write the results of tasks execution to given directory. +=item * -=item B<--stdout> +if I is '-', STDOUT will be used -Write the results of tasks execution on stdout. +=back + +Multiple values can be specified, using comma as a separator. =back diff --git a/lib/FusionInventory/Agent.pm b/lib/FusionInventory/Agent.pm index b5c5ba1cf9..2af336b894 100644 --- a/lib/FusionInventory/Agent.pm +++ b/lib/FusionInventory/Agent.pm @@ -17,7 +17,6 @@ use FusionInventory::Agent::Storage; use FusionInventory::Agent::Task; use FusionInventory::Agent::Target::Local; use FusionInventory::Agent::Target::Server; -use FusionInventory::Agent::Target::Stdout; use FusionInventory::Agent::Tools; use FusionInventory::Agent::Tools::Hostname; use FusionInventory::Agent::XML::Query::Prolog; @@ -90,28 +89,19 @@ sub init { my $scheduler = $self->{scheduler}; # create target list - if ($config->{stdout}) { - $scheduler->addTarget( - FusionInventory::Agent::Target::Stdout->new( - logger => $logger, - deviceid => $self->{deviceid}, - delaytime => $config->{delaytime}, - basevardir => $self->{vardir}, - ) - ); - } - if ($config->{local}) { - $scheduler->addTarget( - FusionInventory::Agent::Target::Local->new( - logger => $logger, - deviceid => $self->{deviceid}, - delaytime => $config->{delaytime}, - basevardir => $self->{vardir}, - path => $config->{local}, - html => $config->{html}, - ) - ); + foreach my $path (@{$config->{local}}) { + $scheduler->addTarget( + FusionInventory::Agent::Target::Local->new( + logger => $logger, + deviceid => $self->{deviceid}, + delaytime => $config->{delaytime}, + basevardir => $self->{vardir}, + path => $path, + html => $config->{html}, + ) + ); + } } if ($config->{server}) { diff --git a/lib/FusionInventory/Agent/Config.pm b/lib/FusionInventory/Agent/Config.pm index ee49231fa3..a30fa81e47 100644 --- a/lib/FusionInventory/Agent/Config.pm +++ b/lib/FusionInventory/Agent/Config.pm @@ -116,6 +116,10 @@ my $deprecated = { message => 'use --no-category software option instead', new => { 'no-category' => 'software' } }, + 'stdout' => { + message => 'use --local - option instead', + new => { 'local' => '-' } + }, }; sub new { @@ -294,6 +298,7 @@ sub _checkContent { # multi-values options $self->{logger} = [ split(/,/, $self->{logger}) ] if $self->{logger}; + $self->{local} = [ split(/,/, $self->{local}) ] if $self->{local}; $self->{server} = [ split(/,/, $self->{server}) ] if $self->{server}; $self->{'no-task'} = [ split(/,/, $self->{'no-task'}) ] if $self->{'no-task'}; diff --git a/lib/FusionInventory/Agent/Target/Stdout.pm b/lib/FusionInventory/Agent/Target/Stdout.pm deleted file mode 100644 index 75fd914848..0000000000 --- a/lib/FusionInventory/Agent/Target/Stdout.pm +++ /dev/null @@ -1,38 +0,0 @@ -package FusionInventory::Agent::Target::Stdout; - -use strict; -use warnings; -use base 'FusionInventory::Agent::Target'; - -use English qw(-no_match_vars); - -my $count = 0; - -sub new { - my ($class, %params) = @_; - - my $self = $class->SUPER::new(%params); - - $self->_init( - id => 'stdout' . $count++, - vardir => $params{basevardir} . '/__LOCAL__', - ); - - return $self; -} - -sub getDescription { - my ($self) = @_; - - return "stdout"; -} - -1; - -1; - -__END__ - -=head1 NAME - -FusionInventory::Agent::Target::Stdout - Stdout target diff --git a/lib/FusionInventory/Agent/Task/Inventory.pm b/lib/FusionInventory/Agent/Task/Inventory.pm index fac3090bc3..8bbe4b30ec 100644 --- a/lib/FusionInventory/Agent/Task/Inventory.pm +++ b/lib/FusionInventory/Agent/Task/Inventory.pm @@ -73,40 +73,51 @@ sub run { $self->_initModulesList(\%disabled); $self->_feedInventory($inventory, \%disabled); - if ($self->{target}->isa('FusionInventory::Agent::Target::Stdout')) { - $self->_printInventory( - inventory => $inventory, - handle => \*STDOUT, - format => 'xml' - ); - } elsif ($self->{target}->isa('FusionInventory::Agent::Target::Local')) { + if ($self->{target}->isa('FusionInventory::Agent::Target::Local')) { + my $path = $self->{target}->getPath(); my $format = $self->{target}->{format}; + my ($file, $handle); + + SWITCH: { + if ($path eq '-') { + $handle = \*STDOUT; + last SWITCH; + } - my $extension = $format eq 'xml' ? '.ocs' : '.html'; - my $file = - $self->{config}->{local} . - "/" . - $self->{deviceid} . - $extension; - - my $handle; - if (Win32::Unicode::File->require()) { - $handle = Win32::Unicode::File->new('w', $file); - } else { - open($handle, '>', $file); + if (-f $path) { + $file = $path; + last SWITCH; + } + + if (-d $path) { + $file = + $path . "/" . $self->{deviceid} . + $format eq 'xml' ? '.ocs' : '.html'; + last SWITCH; + } } - if ($handle) { - $self->_printInventory( - inventory => $inventory, - handle => $handle, - format => $format - ); - close $handle; + if ($file) { + if (Win32::Unicode::File->require()) { + $handle = Win32::Unicode::File->new('w', $file); + } else { + open($handle, '>', $file); + } + $self->{logger}->error("Can't write to $file: $ERRNO") + unless $handle; + } + + $self->_printInventory( + inventory => $inventory, + handle => $handle, + format => $format + ); + + if ($file) { $self->{logger}->info("Inventory saved in $file"); - } else { - $self->{logger}->error("Can't write to $file: $ERRNO"); + close $handle; } + } elsif ($self->{target}->isa('FusionInventory::Agent::Target::Server')) { my $client = FusionInventory::Agent::HTTP::Client::OCS->new( logger => $self->{logger},