This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusioninventory-injector
executable file
·174 lines (140 loc) · 3.79 KB
/
fusioninventory-injector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/perl -w
###############################################################################
##Copyleft Pascal DANEK 2005
##Copyleft Goneri Le Bouder 2006
##Copyleft FusionInventory Project 2010-2011
##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
################################################################################
if ($ENV{REALLIB}) {
@INC = split(/:/,$ENV{REALLIB});
}
eval "
use LWP::UserAgent;
use XML::Simple;
use Compress::Zlib;
use Getopt::Long;
use strict;";
my $help;
my $directory;
my $file;
my $url;
my $useragent;
my $remove;
my $verbose;
my $stdin;
sub loadfile {
$file = shift;
unless ( -r $file ) {
print STDERR "Can't read $file\n";
return;
}
print "Loading $file..." if $verbose;
unless ( open( FILE, "$file" )) {
print STDERR "Failed to access $file : $!";
return;
}
local $/;
my $content = <FILE>;
close FILE or die "Can't close file $file: $!";
sendContent($content);
}
sub loaddirectory {
my $directory = shift;
unless ( -r $directory ) {
print STDERR "Can't read $directory: $!\n";
return;
}
opendir( DIR, $directory ) || die "can't opendir $directory: $!";
foreach ( readdir(DIR) ) {
loadfile("$directory/$_") if (/\.ocs$/);
}
closedir DIR;
}
sub loadstdin {
my $content;
undef $/;
$content = <STDIN>;
sendContent($content);
}
sub sendContent {
my $content = shift;
my $ua = LWP::UserAgent->new;
$ua->agent($useragent);
my $request = HTTP::Request->new( POST => $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 $verbose;
print STDERR "Can't remove $file: $!\n"
if ($remove && (!unlink $file));
}else{
if($verbose){
print "ERROR: ";
print $res->status_line(), "\n";
}
}
}
sub usage {
print STDERR <<EOF;
DESCRIPTION:
A command line tools to import .ocs file in an OCS Inventry server.
USAGE:
-h --help : this menu
-d --directory: load every .ocs files from a directory
-f --file : load a speficic file
-u --url : ocsinventory backend URL
-r --remove : remove succesfuly injected files
-v --verbose : verbose mode
--stdin : read data from STDIN
You must specify a --file or a --directory or STDIN.
Example :
\$export https_proxy=http://www-proxy:8080
\$fusioninventory-injector -v -f /tmp/toto-2010-09-10-11-42-22.ocs --url https://login:pw\@yourserver/ocsinventory
This tool is part of the FusionInventory distribution.
EOF
exit 1;
}
GetOptions(
'h|help' => \$help,
'd|directory=s' => \$directory,
'f|file=s' => \$file,
'u|url=s' => \$url,
'useragent=s' => \$useragent,
'r|remove' => \$remove,
'v|verbose' => \$verbose,
'stdin' => \$stdin,
);
# Default values
$useragent = 'FusionInventory-Injector' unless $useragent;
###
$|=1;
usage() if $help;
if ($file && -f $file) {
loadfile($file);
} elsif ($stdin) {
loadstdin();
} elsif ($directory) {
die("Directory does not exist. Abort.") unless -d $directory;
loaddirectory($directory);
} else {
usage();
}
__END__
=head1 NAME
fusioninventory-injector - A tool to push inventory in an OCS Inventory or compatible server.
=head1 DESCRIPTION
This tool can be used to test your server, do benchmark or push inventory from off-line machine.
=head1 SYNOPSIS
Please see:
B<fusioninventory-injector> S<--help>