Codebase list fusioninventory-agent / debian/2.2.3-8 fusioninventory-injector
debian/2.2.3-8

Tree @debian/2.2.3-8 (Download .tar.gz)

fusioninventory-injector @debian/2.2.3-8raw · history · blame

#!/usr/bin/perl
###############################################################################
##Copyleft Pascal DANEK 2005
##Copyleft Goneri Le Bouder 2006
##Copyleft FusionInventory Project 2010-2012
##Web : http://www.FusionInventory.org
##
##This code is open source and may be copied and modified as long as the source
##code is always made freely available.
##Please refer to the General Public Licence http://www.gnu.org/ or Licence.txt
################################################################################

use strict;
use warnings;

use Compress::Zlib;
use English qw(-no_match_vars);
use Fcntl qw/:flock/;
use Getopt::Long;
use LWP::UserAgent;
use Pod::Usage;

my %options = (
    useragent => 'FusionInventory-Injector'
);

sub loadfile {
    my $file = shift;

    die "can't read file $file" unless -r $file;

    print "Loading $file..." if $options{verbose};

    open (my $fh, '<', $file) or die "can't open file $file: $ERRNO";
    ## no critic (ProhibitBitwise)
    flock ($fh, LOCK_EX | LOCK_NB) or die "can't lock file $file: $ERRNO";
    local $RS;
    my $content = <$fh>;
    close $fh or die "Can't close file $file: $ERRNO";

    sendContent($content);
}

sub loaddirectory {
    my $directory = shift;

    die "can't read directory $directory" unless -r $directory;

    opendir (my $dh, $directory) or die "can't open directory $directory: $ERRNO";
    foreach ( readdir($dh) ) {
        loadfile("$directory/$_") if (/\.ocs$/);
    }
    closedir $dh;
}

sub loadstdin {
    my $content;
    undef $RS;
    $content = <STDIN>;
    sendContent($content);
}

sub sendContent {
    my $content = shift;

    my $ua = LWP::UserAgent->new;
    $ua->agent($options{useragent});
    my $request = HTTP::Request->new( POST => $options{url} );
    $request->header(
        'Pragma' => 'no-cache',
        'Content-type', 'Application/x-compress'
    );
    if (uncompress($content)) {
        $content = uncompress($content);
    }
    $request->content(compress($content));
    my $res = $ua->request($request);

    if ($res->is_success()) {
        print "OK\n" if $options{verbose};
        print STDERR "Can't remove $options{file}: $ERRNO\n"
            if ($options{remove} && (!unlink $options{file}));
    } else {
        if ($options{verbose}) {
            print "ERROR: ";
            print $res->status_line(), "\n";
        }
    }
}

GetOptions(
    \%options,
    'help|h',
    'directory|d=s',
    'file|f=s',
    'url|u=s',
    'useragent=s',
    'remove|r',
    'verbose|v',
    'stdin',
);

$OUTPUT_AUTOFLUSH = 1;
pod2usage() if $options{help};

if ($options{file}) {
    die "file $options{file} does not exist" unless -f $options{file};
    loadfile($options{file});
} elsif ($options{stdin}) {
    loadstdin();
} elsif ($options{directory}) {
    die "directory $options{directory} does not exist" unless -d $options{directory};
   loaddirectory($options{directory});
} else {
    pod2usage();
}

exit(0);

__END__

=head1 NAME

fusioninventory-injector - A tool to push inventory in an OCS Inventory or compatible server.

=head1 SYNOPSIS

fusioninventory-injector [options] [--file <file>|--directory <directory|--stdin]

  Options:
    -h --help      this menu
    -d --directory load every .ocs files from a directory
    -f --file      load a speficic file
    -u --url       server URL
    -r --remove    remove succesfuly injected files
    -v --verbose   verbose mode
    --stdin        read data from STDIN

  Examples:
    fusioninventory-injector -v -f /tmp/toto-2010-09-10-11-42-22.ocs --url https://login:pw@server/ocsinventory

=head1 DESCRIPTION

This tool can be used to test your server, do benchmark or push inventory from
off-line machine.