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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
#!/usr/bin/perl -w
use strict;
use lib 'lib';
use File::Path;
use FusionInventory::Agent::Config;
# DEB/RPM/... maintainers, turn this to '1'
my $packaged = 1;
my $config;
my @cacert;
my $randomtime;
my $cron_line;
sub loadModules {
my @modules = @_;
foreach (@modules) {
eval "use $_;";
if ($@) {
print STDERR "Failed to load $_. Please install it and ".
"restart the fusioninventory-agent-config script.\n";
exit 1;
}
}
}
sub ask_yn {
my $promptUser = shift;
my $default = shift;
die unless $default =~ /^(y|n)$/;
my $cpt = 5;
while (1) {
my $line = prompt("$promptUser\nPlease enter 'y' or 'n'?>", $default);
return 1 if $line =~ /^y$/;
return if $line =~ /^n$/;
if ($cpt-- < 0) {
print STDERR "to much user input, exit...\n";
exit(0);
}
}
}
sub promptUser {
my ($promptUser, $default, $regex, $notice) = @_;
my $string = $promptUser;
$string .= "?>";
my $line;
my $cpt = 5;
while (1) {
$line = prompt($string, $default);
if ($regex && $line !~ /$regex/) {
print STDERR $notice."\n";
} else {
last;
}
if ($cpt-- < 0) {
print STDERR "to much user input, exit...\n";
exit(0);
}
}
return $line;
}
sub pickConfigdir {
my ($args) = @_;
my $dirs = $args->{dirs};
my $files = $args->{files};
foreach my $dir (@$dirs) {
foreach my $file (@$files) {
my $t = $dir.'/'.$file;
if (-f $t) {
return $dir."/".$file;
}
}
}
}
sub mkFullServerUrl {
my $server = shift;
my $ret = 'http://' unless $server =~ /^http(s|):\/\//;
$ret .= $server;
if ($server !~ /http(|s):\/\/\S+\/\S+/) {
$ret .= '/ocsinventory';
}
return $ret;
}
####################################################
################### main ###########################
####################################################
loadModules (qw/XML::Simple ExtUtils::MakeMaker/);
if (!ask_yn("Do you want to configure the agent", 'y')) {
exit 0;
}
my $defaultConfigDir = "/etc/fusioninventory";
my $defaultCfgFile = "agent.cfg";
my $newConfigFile = pickConfigdir ({
dirs => [
$defaultConfigDir,
"/usr/local/etc/fusioninventory"
],
files => [$defaultCfgFile]
});
my $ocsConfigFile = pickConfigdir ({
dirs => [
"/etc/ocsinventory",
"/usr/local/etc/ocsinventory",
"/etc/ocsinventory-agent"
],
files => ['ocsinventory-agent.cfg']
});
my $oldOcsConfigdir = "/etc/ocsinventory-client";
if (!-d $defaultConfigDir) {
if (ask_yn ("Do you want to create the directory ".
$defaultConfigDir."?", 'y')) {
if (!mkdir $defaultConfigDir) {
print "Failed to create ".$defaultConfigDir.". Are you root?\n";
exit 1;
}
} else {
print "Please create the ".$defaultConfigDir." directory first.\n";
exit 1;
}
}
if (-f $oldOcsConfigdir.'/ocsinv.conf' && ask_yn("Should the old linux_agent settings be imported?", 'y')) {
my $ocsinv = XMLin($oldOcsConfigdir.'/ocsinv.conf');
$config->{server} = mkFullServerUrl($ocsinv->{'OCSFSERVER'});
if (-f $oldOcsConfigdir.'/cacert.pem') {
open CACERT, $oldOcsConfigdir.'/cacert.pem' or die "Can'i import the CA certificat: ".$!;
@cacert = <CACERT>;
close CACERT;
}
my $admcontent = '';
if (-f "$oldOcsConfigdir/ocsinv.adm") {
if (!open(ADM, "<:encoding(iso-8859-1)", "$oldOcsConfigdir/ocsinv.adm")) {
warn "Can't open $oldOcsConfigdir/ocsinv.adm";
} else {
$admcontent .= $_ foreach (<ADM>);
close ADM;
my $admdata = XMLin($admcontent) or die;
if (ref ($admdata->{ACCOUNTINFO}) eq 'ARRAY') {
foreach (@{$admdata->{ACCOUNTINFO}}) {
$config->{tag} = $_->{KEYVALUE} if $_->{KEYNAME} =~ /^TAG$/;
}
} elsif (
exists($admdata->{ACCOUNTINFO}->{KEYNAME}) &&
exists($admdata->{ACCOUNTINFO}->{KEYVALUE}) &&
$admdata->{ACCOUNTINFO}->{KEYNAME} eq 'TAG'
) {
print $admdata->{ACCOUNTINFO}->{KEYVALUE}."\n";
$config->{tag} = $admdata->{ACCOUNTINFO}->{KEYVALUE};
}
}
}
}
foreach ($newConfigFile, $ocsConfigFile) {
if (-f $_) {
open (CONFIG, "<".$_) or
die "Can't open ".$_.": ".$!;
print "Load settings from $_\n";
foreach (<CONFIG>) {
s/#.+//;
if (/(\w+)\s*=\s*(.+)/) {
my $key = $1;
my $val = $2;
# Remove the quotes
$val =~ s/\s+$//;
$val =~ s/^'(.*)'$/$1/;
$val =~ s/^"(.*)"$/$1/;
$config->{$key} = $val;
}
}
close CONFIG;
last;
}
}
print "[info] The config file will be written in $defaultConfigDir/$defaultCfgFile,\n";
my $tmp = promptUser('What is the address of your ocs server', exists
($config->{server})?$config->{server}:'http://ocsinventory-ng/ocsinventory');
$config->{server} = mkFullServerUrl($tmp);
if (!$config->{server}) {
print "Server is empty. Leaving...\n";
exit 1;
}
if (ask_yn ("Do you need credential for the server? (You probably don't)", 'n')) {
$config->{user} = promptUser("user", $config->{user});
$config->{password} = promptUser("password");
print "[info] The realm can be found in the login popup of your Internet browser.\n[info] In general, it's something like 'Restricted Area'.\n";
$config->{realm} = promptUser("realm");
} else {
delete ($config->{user});
delete ($config->{password});
delete ($config->{realm});
}
if (ask_yn('Do you want to apply an administrative tag on this machine', 'y')) {
$config->{tag} = promptUser("tag", $config->{tag});
} else {
delete($config->{tag});
}
chomp(my $binpath = `which fusioninventory-agent 2>/dev/null`);
print $binpath."\n";
if (! -x $binpath) {
# Packaged version with perl and agent ?
$binpath = $^X;
$binpath =~ s/perl/fusioninventory-agent/;
}
if (! -x $binpath) {
print "sorry, can't find fusioninventory-agent in \$PATH\n";
exit 1;
} else {
print "FusionInventory Agent found: $binpath\n";
}
if (!$packaged) {
$randomtime = int(rand(60)).' '.int(rand(24));
$cron_line = $randomtime." * * * root $binpath --lazy > /dev/null 2>&1\n";
if ($^O =~ /solaris/) {
if (ask_yn("Do yo want to install the cron task in current user crontab ?", 'y')) {
my $crontab = `crontab -l`;
# Let's suppress Linux cron/anacron user column
$cron_line =~ s/ root / /;
$crontab .= $cron_line;
open CRONP, "| crontab" || die "Can't run crontab: $!";
print CRONP $crontab;
close(CRONP);
}
}
elsif (-d "/etc/cron.d") {
if (ask_yn("Do yo want to install the cron task in /etc/cron.d", 'y')) {
open DEST, '>/etc/cron.d/fusioninventory-agent' or die $!;
# Save the root PATH
print DEST "PATH=".$ENV{PATH}."\n";
print DEST $randomtime." * * * root $binpath --lazy > /dev/null 2>&1\n";
close DEST;
}
}
my $old_vardir;
my $default_vardir = $config->{basevardir};
if ($^O =~ /solaris/) {
$default_vardir = '/var/opt/fusioninventory-agent';
$old_vardir = '/var/opt/ocsinventory-agent';
} else {
$default_vardir = '/var/lib/fusioninventory-agent';
$old_vardir = '/var/lib/ocsinventory-agent';
}
$config->{basevardir} = promptUser('Where do you want the agent to store its files? (You probably don\'t need to change it)', exists ($config->{basevardir})?$config->{basevardir}:$default_vardir, '^\/\w+', 'The location must begin with /');
if (!-d $config->{basevardir}) {
if (ask_yn ("Do you want to create the ".$config->{basevardir}." directory?\n", 'y')) {
mkdir $config->{basevardir} or die $!;
} else {
print "Please create the ".$config->{basevardir}." directory\n";
exit 1;
}
}
if (-d $config->{basevardir} && !-d $default_vardir && ask_yn("Do yo want to ".
"migration $old_vardir for FusionInventory Agent", 'y')) {
system('mv', $old_vardir, $default_vardir);
}
my @oldFiles = qw(
/etc/ocsinventory-client
/etc/logrotate.d/ocsinventory-agent
/etc/logrotate.d/ocsinventory-client
/usr/sbin/ocsinventory-agent
/usr/bin/ocsinventory-agent
/usr/sbin/ocsinventory-client.pl
/etc/cron.d/ocsinventory-client
/bin/ocsinv);
my $doCleanUp;
foreach (@oldFiles) {
if (-f||-d||-l) {
$doCleanUp=1;
last;
}
}
if ($doCleanUp && ask_yn ("Should I remove the Ocsinventory Agent files", 'n')) {
foreach (@oldFiles) {
next unless -f;
print $_."\n";
rmdir if -d;
unlink if -f || -l;
}
print "done\n"
}
# Create the vardirectory for this server
my $dir = $config->{server};
$dir =~ s/\//_/g;
$dir =~ s/:/../g if $^O =~ /^MSWin/; # Conditional because there is
my $vardir = $config->{basevardir}."/".$dir;
if (!-d $vardir && !mkpath($vardir)) {
die "Can't create $vardir!";
}
if (@cacert) { # we need to migrate the certificat
open CACERT, ">".$vardir."/cacert.pem" or die "Can't open ".$vardir.'/cacert.pem: '.$!;
print CACERT foreach (@cacert);
close CACERT;
print "Certificat copied in ".$vardir."/cacert.pem\n";
}
}
open CONFIG, ">$defaultConfigDir/$defaultCfgFile" or die "Can't write the config file in $defaultConfigDir/$defaultCfgFile: ".$!;
print CONFIG $_."=".$config->{$_}."\n" foreach (keys %$config);
close CONFIG;
chmod 0600, "$defaultConfigDir/$defaultCfgFile";
if (ask_yn("Do you want to send an inventory of this machine?", 'y')) {
system("$binpath --force");
}
print "################################\n";
print "New settings written! Thank you for using FusionInventory!\n";
print " http://www.FusionInventory.org\n";
__END__
=head1 NAME
fusioninventory-agent-config - FusionInventory Agent configuration script
=head1 SYNOPSIS
B<fusioninventory-agent-config>
|