Skip to content

Commit

Permalink
make stdout target a specific local target case
Browse files Browse the repository at this point in the history
  • Loading branch information
guillomovitch committed Jul 9, 2012
1 parent 74b108f commit 0841941
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 100 deletions.
2 changes: 0 additions & 2 deletions etc/agent.cfg
Expand Up @@ -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
Expand Down
34 changes: 24 additions & 10 deletions fusioninventory-agent
Expand Up @@ -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',
Expand Down Expand Up @@ -130,12 +130,11 @@ fusioninventory-agent - FusionInventory agent For Linux/UNIX, Windows and MacOSX
=head1 SYNOPSIS
B<fusioninventory-agent> [options] [--server server|--local directory|--sdout]
B<fusioninventory-agent> [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)
Expand Down Expand Up @@ -294,10 +293,9 @@ example, the two lines below are all equivalent:
=over
=item B<-s>, B<--server>=I<URI>
Send results of tasks execution to given server.
Send the results of tasks execution to given server.
If I<URI> doesn't start with http:// or https://, the agent assume the
parameter is a hostname and rewrite it as:
Expand All @@ -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<DIR>
=item B<-l>, B<--local>=I<PATH>
Write the results of tasks execution locally.
Exact behaviour according to given path:
=over
=item *
if I<PATH> is a directory, a file will be created therein
=item *
if I<PATH> is a file, it will be used directly
Write the results of tasks execution to given directory.
=item *
=item B<--stdout>
if I<PATH> 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
Expand Down
34 changes: 12 additions & 22 deletions lib/FusionInventory/Agent.pm
Expand Up @@ -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;
Expand Down Expand Up @@ -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}) {
Expand Down
5 changes: 5 additions & 0 deletions lib/FusionInventory/Agent/Config.pm
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'};
Expand Down
38 changes: 0 additions & 38 deletions lib/FusionInventory/Agent/Target/Stdout.pm

This file was deleted.

67 changes: 39 additions & 28 deletions lib/FusionInventory/Agent/Task/Inventory.pm
Expand Up @@ -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},
Expand Down

0 comments on commit 0841941

Please sign in to comment.