#!/usr/bin/perl

# In order to be able to relocate the librairie location we can't
# load them with 'use' here.

my $devlib;
foreach (@ARGV) {
    if (/^--devlib$/) {
        print STDERR "[info] --devlib are found, Developer mode enabled\n";
        unshift @INC, 'lib';
    } elsif (/^--disable-perllib-envvar$/) {
        $ENV{PERL5LIB}=undef;
        $ENV{PERLLIB}=undef;
    }
}

if ($ENV{REALLIB}) {
    @INC = split(/:/,$ENV{REALLIB});
}


eval 'use English qw(-no_match_vars); 1;';

if ($OSNAME =~ /MSWin/ && $EXECUTABLE_NAME =~ /(^.*(\\|\/))/) {
    eval 'use Configs; 1';
    $ENV{PATH} .= $Config::Config{path_sep}.$1;
}
eval {
    require FusionInventory::Agent;
};
if ($EVAL_ERROR) {
    print STDERR "Failed to load FusionInventory::Agent, $EVAL_ERROR\n";
    print STDERR "you can run the agent directly from its source directory ";
    print STDERR "with --devlib\n";
    exit 1;
}

eval {
    my $agent = new FusionInventory::Agent;
    $agent->main();
};

if ($EVAL_ERROR) {
    print STDERR "Execution failed. Are you sure the software is fully ";
    print STDERR "installed\n";
    print STDERR "and an old version of the agent not already present on the ";
    print STDERR "system?\n";
    print STDERR "___\n";
    print STDERR $EVAL_ERROR;
    exit 1;
}
__END__

=head1 NAME

fusioninventory-agent - FusionInventory agent For Linux/UNIX, Windows and MacOSX

=head1 SYNOPSIS

B<fusioninventory-agent> S<[ ... ]> S<[ I<--server server> | I<--local /tmp> ]>...

=head1 EXAMPLES

    % fusioninventory-agent --server localhost
    # send an inventory to the OCS server

    % fusioninventory-agent --server http://localhost/ocsinventory2
    # send an inventory over http to a server with a non standard
    # virtual directory

    % fusioninventory-agent --server https://localhost/ocsinventory
    # send an inventory over https to the OCS server

    % fusioninventory-agent --local /tmp
    # write an inventory in the /tmp directory

    % fusioninventory-agent --local /tmp --html
    # Use the --html parameter to generate a HTML file

    % fusioninventory-agent --server localhost --user=toto --password=pw --realm="Restricted Area"
    # send a report to a server protected by a basic authentication
    % fusioninventory-agent --lazy
    # send an inventory only if a random delay between 0 and PROLOG_FREQ had been run over.

    % fusioninventory-agent --delaytime 60 -d
    # If NO PROLOG_FREQ has been preset, pick a time between execution and --delaytime for the agent to contact the server [default is 3600 seconds]

=head1 DESCRIPTION

F<fusioninventory-agent> is an agent for OCS Inventory server and
FusionInventory for GLPI servers. It creates local inventory of the machines
(hardware and software) and send it to the server. It can also write it
in a local XML file.
This agent is the successor of the former linux_agent which was released with
OCS 1.01 and prior. It also replaces the Solaris/AIX/BSD unofficial agents.

Supported systems:

=over 4

=item F<Windows> (since Windows 2000)

=item F<GNU/Linux>

=item F<MacOSX>

=item F<Solaris>

=item F<FreeBSD>

=item F<NetBSD>

=item F<OpenBSD>

=item F<AIX>

=item F<HP-UX>

=item F<GNU/kFreeBSD>

=back

=head1 INSTALLATION

=head2 PREREQUISITES

Minimum perl version: 5.8

Additional mandatory perl modules:

=over

=item Digest::MD5

=item XML::Simple

=item LWP

=item Net::IP

=item UNIVERSAL::require

=item Mac::SysProfile (MacOs only)

=back

Additional optional perl modules:

=over

=item Compress::Zlib, for message compression

=item HTTP::Daemon, for web interface

=item Net::CUPS (v0.60 minimum), for printers detection 

=item File::ShareDir, for automatic data directory determination

=item Proc::Daemon, for daemon mode (Unix only)

=item Proc::PID::File, for daemon mode (Unix only)

=back

Additional optional programs:

=over

=item nmap or ipdiscover, for network discovery

=item dmidecode (linux or bsd) for hardware inventory

=item lspci (linux or bsd) for hardware inventory

=back

=head2 PROCEDURE

Once the archive is unpacked, use these commands:

    perl Makefile.PL
    make
    make install

You can also run the agent from the tarball directory. In this case, use
the --devlib flag to load the library from the local directory.

=head2 SYSTEM-SPECIFIC INFORMATIONS

=head3 Solaris

Sun Studio seems to be needed to build the dependency. The generated Makefile
needs gmake to be executed. The default installation will install the binary in
/usr/perl5/5.XXXXX/bin, set you $PATH variable according to that.

On Solaris/SPARC, you must install sneep and record the Serial Number with
it.
Download it from L<http://www.sun.com/download/products.xml?id=4304155a>

=head3 Windows

If you don't want to use the installer for Windows you can follow the
installation process avalaible there:
L<http://forge.fusioninventory.org/projects/fusioninventory-agent/wiki/RunAgentWin32>

=head2 POST-INSTALLATION

Run ./fusioninventory-agent-config for automatic configuration.

=head1 OCS INVENTORY PROTOCOL

FusionInventory Agent uses OCS Inventory protocol and is compatible with OCS
Inventory server. The agent uses the same protocol with the FusionInventory
For GLPI plugin.

The OCS protocol is basicly an exchange of XML file done over HTTP(S).

=head2 First, The PROLOG

The agent send a first message to give it's ID (DEVICEID). The
server send back to the agent an XML file with a list of parameter. It's the
PROLOG RESP. You can see it if you're in Debug mode.
In this PROLOG, the server send the PROLOG_FREQ. The agent will use it to
know when it need to contact the server for the next time.

The agent compute the next query date this way:

     PROLOG_FREQ/2+RANDOM(PROLOG_FREQ/2)


=head2 Module execution

The agent will launch each modules and pass the list of options found in the
PROLOG RESP as parameter. The module can send information directly to the
server (inventory, SNMP scan result, etc)

=head2 Windows service or Deamon mode

In deamon mode, the agent wait during a period between the last PROLOG and
PROLOG_FREQ. If PROLOG_FREQ is not set, it will use --delaytime value.
The default value is 3600.

=head1 OPTIONS

Most of the options are available in a I<short> form and a I<long> form.  For
example, the two lines below are all equivalent:

    % fusioninventory-agent -s localhost
    % fusioninventory-agent --server localhost

=over 4

=item B<--backend-collect-timeout=SECONDS_BEFORE_KILL>

Time before the agent kills modules processing which don't end before the
timeout.

=item B<--basevardir>=I<DIR>

Indicate the place where the agent should store its files.

=item B<--color>

Display color on the terminal. This feature is disabled on Windows.

=item B<-d>, B<--daemon>

Launch fusioninventory-agent as daemon in background. Proc::Daemon is needed.

=item B<-D>, B<--daemon-no-fork>

Launch fusioninventory-agent in daemon but do fork in background.

=item B<--ca-cert-dir>=I<PATH>

The directory where public SSL certificates are stored. In general it's
/etc/ssl/certs.

You don't need to copy a public certificate on very agent if your private
key is valide and was been signed by a valide certificate authority.

=item B<--ca-cert-file>=I<FILE>

The path to the public certificate (e.g: /etc/ssl/certs/cacert.pem). If
--ca-cert-file is used, --ca-cert-dir is ignored.

You don't need to copy a public certificate on very agent if your private
key is valide and was been signed by a valide certificate authority.

=item B<--conf-file>=I<FILE>

Load the configuration file from the given location. Ignored on Windows.

On Windows the settings are loaded from the registry. The information are
stored there:
HKEY_LOCAL_MACHINE\SOFTWARE\FusionInventory-Agent

Or on Windows 64bit machine:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\FusionInventory-Agent

=item B<--debug>

Turn the debug mode on.

=item B<--devlib>

This option is designed for the developer. With it enabled,
fusioninventory-agent won't try to load the Backend module installed
on the system. Instead it will scan the ./lib directory.
It may also be useful if you don't want to install the agent on
your system.

This option can only be used as command line argument. It will be
ignored if you set it in a config file or in Windows registry.

=item B<--delaytime=SECONDS_TO_WAIT>

This option defaults to waiting a random() time between 0 and 3600 before
initially contacting the server assuming NO PROLOG_FREQ has been set.
Once PROLOG_FREQ has been set, it uses that number at the top end of
it's random setting. Useful for pre-setting a deployed agent's initial
start time (so you don't jam the server all at once, but don't have to
wait an hour to make sure the deployment worked).

=item B<--disable-perllib-envvar>

Do not use PERL5LIB and PERLLIB environment variable to find Perl library
path.

This option can only be used as command line argument. It will be
ignored if you set it in a config file or in Windows registry.

=item B<-f>, B<--force>

The agent ask first the server if it should send an inventoroy of the
machine. If the server doesn't know the machine or has outdated
informations, it will request it.
With this option, the agent ignore the server anwser and send an inventory.

=item B<--html>

Save the local inventory requested by B<--local> in HTML.

=item B<-i>, B<--info>

Turn the verbose mode on. The flag is ignored if B<--debug> is enable.

=item B<--lazy>

Do not contact the server more than one time during the PROLOG_FREQ and do
an exit if there is nothing to do. Useful if you want to start the agent
script regulary from the crontab but don't want to overload the server.

=item B<-l>, B<--local>=I<DIR>

Write an inventory in the I<DIR> directory. A new file will be created if needed.

=item B<--logfile>=I<FILE>

Log message in I<FILE> and turn off STDERR

=item B<--logfile-maxsize>=I<FILE>

Max logfile size in MB, default is unlimited. When the max size is reached, the
file is truncated. This is only useful if there is no log rotation mechanism on
the system.

=item B<--logger>=I<LOGGER:>

The logger you want to use. It can be:

=over 4

=item

Stderr: print the messages directly in the console. Messages are lost in
daemon mode (B<--daemon>). You can use B<--color> to turn color mode on.

=item

File: log the error in a log file. Please use B<--logfile> to set the
location of your log file. B<--logfile> turns this option on automatically

=item

Syslog: use the local syslog server to log the messages.

=back

You can use more than one parameter if you use ',' as delimiter. E.g:

 % fusioinventory-agent --logger=Syslog,Stderr --local=/tmp

=item B<--no-deploy>

Disable the software deployment (new module).

=item B<--no-esx>

Disable the ESX inventory task.

=item B<--no-ocsdeploy>

Disable the software deployment (OCS compatible module).

=item B<--no-inventory>

Do not generate inventory.

=item B<--no-wakeonlan>

Do not use the wakeonlan.

=item B<--no-printer>

Do not inventory the printer.

=item B<--no-software>

Do not inventory the software installed on the machine.

=item B<--no-p2p>

For OCS Inventory software deployment. Do not use peer 2 peer to download files.

=item B<-p>, B<--password>=I<PASSWORD>

Use I<PASSWORD> for an HTTP authentification with the server.

=item B<-P>, B<--proxy>=I<PROXY>

Use I<PROXY> to specify a proxy HTTP server. By default, the agent uses
HTTP_PROXY environment variable.

=item B<-r>, B<--realm>=I<REALM>

Use I<REALM> for an HTTP authentification with the server. For example, the
value can be 'Restricted Area'. You can find it in the login popup of your
Internet browser.

if no realm is passed and the auth fails, the agent will retry an authentification
with the realm returned by the server.

=item B<--rpc-ip>=I<IP>

The IP address of the interface to use for the P2P exchange. The default
is 'all' the IP addresses with a 255.255.255.0 mask except 127.0.0.1.

=item B<--rpc-port>=I<PORT>

The PORT of the interface to use for the embedded HTTP server used for the
peer to peer data exchange and remote control. The default
is '62354'.

=item B<--rpc-trust-localhost>

Allow local users to access to http://127.0.0.1:62354/now to force an
inventory immediately.

=item B<--scan-homedirs>

Should the agent scan the user directories to inventory their virtual machines ?.

=item B<-s>, B<--server>=I<URI>

The uri of the server. If I<URI> doesn't start with http:// or https://, the
agent assume the parameter is a hostname and rewrite it like that:

    % --server=http://servername/ocsinventory

If you want to use https or another virtual directory (e.g: /ocsinventory2)
you need to enter the full path.

You can declare more than one server if you use the ',' delimiter.

    % --server=http://servername/ocsinventory,server2

In general, OCS Inventory server URL have this format:

    http://servername/ocsinventory

and FusionInventory for GLPI this one:

    http://servername/glpi/plugins/fusioninventory/front/plugin_fusioninventory.communication.php

B<--server> is ignored if B<--local> is in use.

=item B<--share-dir>

The directory where are stored the shared files. You probably don't
need this option unless you don't want to use the Makefile.PL installation
procedure or if File::ShareDir is not avalaible.

=item B<--stdout>

Print the inventory on stdout.

    % fusioninventory-agent --stdout > /tmp/report.xml
    # prepare an inventory and write it in the /tmp/report.xml file.
    # A file will be created.

=item B<--scan-homedirs>

Authorize the agent to scan home directories to help with the Virtual Machines
inventory.

=item B<--tag>=I<TAG>

Mark the machine with the I<TAG> tag. Once the initial inventory
is accepted by the server this value is ignored and you've to
change the information directly on the server. The server do so
in order to centralize the administration of the machine.

=item B<--no-ssl-check>

Do now validate HTTPS certificat with the servers. Please never use this
option in production. This is just a very bad idea.

Hint: you can declare HTTPS_DEBUG to get SSL error messages in the console.

=item B<-u> I<USER>, B<--user>=I<USER>

Use I<USER> for the server authentication.

=item B<--version>

Print the version and exit.

=item B<-w> I<DURATION>, B<--wait>=I<DURATION>

Wait during a random period between 0 and DURATION second before initializing
the connexion with the server.

    % fusioninventory-agent --wait 5 --server localhost

=back


=head1 CONFIG FILE

The agent try to initialize the settings from the B<agent.cfg>
config file.

It looks for the file in these location:

=over

=item

/etc/fusioninventory/agent.cfg

=item

/usr/local/etc/fusioninventory/agent.cfg

=back

You can use the B<--conf-file=XXX> parameter to load you own configuration
file from a specific location.
On Windows the settings are loaded from the Registry. The configuration file
is ignored.

The command line parameters overwrite the configuration file.

=head1 SECURITY

Some modules like SNMPQuery needs to sent credential from the agent to the
agent. We recommend to enable HTTPS support on the OCS or FusionInventory
For GLPI server. We also recommend to use only trusted machine as agent in
this case.

=head1 AUTHORS

The FusionInventory project L<fusioninventory-devel@lists.alioth.debian.org>

The maintainer is Goneri LE BOUDER <goneri@rulezlan.org>

Please read the AUTHORS, Changes and THANKS files to see who is behind
FusionInventory.

=head1 SEE ALSO

=over 4

=item

FusionInventory website: L<http://www.FusionInventory.org/>

=item

project Forge: L<http://Forge.FusionInventory.org>

=item

The source code of the agent is available on:

=over

=item

Gitorious: L<http://gitorious.org/fusioninventory>

=item

Github: L<http://github.com/fusinv/fusioninventory-agent>

=back

=item

The mailing lists:

=over

=item

L<http://lists.alioth.debian.org/mailman/listinfo/fusioninventory-devel>

=item

L<http://lists.alioth.debian.org/mailman/listinfo/fusioninventory-user>

=back

=item

IRC: #FusionInventory on FreeNode IRC Network

=back

=head1 BUGS

Please, use the mailing lists as much as possible. You can also use the bugtracker on
http://forge.fusionInventory.org


=head1 COPYRIGHT

Copyright (C) 2006-2010 OCS Inventory contributors
Copyright (C) 2010-2011 FusionInventory Team

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The memconf script is maintained by Tom Schmidt
http://myweb.cableone.net/4schmidts/memconf.html
Copyright © 1996-2009 Tom Schmidt

memconf is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

FusionInventory::Agent::Backend::Virtualization::Vmsystem uses code from
imvirt:

   Authors:
     Thomas Liske <liske@ibh.de>

   Copyright Holder:
     2008 (C) IBH IT-Service GmbH [http://www.ibh.de/]

   License:
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this package; if not, write to the Free Software
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
     USA


=cut
