App-Foca

 view release on metacpan or  search on metacpan

lib/App/Foca/Client.pm  view on Meta::CPAN


    my $command         = shift @ARGV || 'true';
    my $port            = 6666;
    my $debug           = 1;

    my $client = App::Foca::Client->new(
                port                => $port,
                debug               => $debug);

    my @hosts = qw(localhost);
    my @result = $client->run(\@hosts, $command);

    die "Not able to collect any data" unless @result;

    foreach my $host (@result) {
        my $status = $host->{'ok'} ? 'OK' : 'ERROR';
        print "$status: $host->{'hostname'}: $host->{'output'}\n";
    }

    # or..

    $client->run(\@hosts, $command, {
            on_host => \&parse_host});

    sub parse_host {
        my ($host) = @_;

        my $status = $host->{'ok'} ? 'OK' : 'ERROR';
        print "$status: $host->{'hostname'}: $host->{'output'}\n";
    }

=cut
use strict;
use warnings;
use Data::Dumper;
use FindBin;
use HTTP::Response;
use Moose;
use Parallel::ForkManager;
use WWW::Curl::Easy;
use YAML::Syck qw(LoadFile);
# Foca libs/modules
use App::Foca::Tools::Logger;

=head1 Attributes

=over 4

=item B<maxflight>

Max number of connections to do at a time. Defaults 15.

=cut
has 'maxflight' => (
    is          => 'rw',
    isa         => 'Int',
    default     => 15);

=item B<timeout>

Timeout per host in seconds. Defaults 60 seconds.

=cut
has 'timeout' => (
    is          => 'rw',
    isa         => 'Int',
    default     => 60);

=item B<connect_timeout>

TCP/connection timeout. Defaults to 5 seconds.

=cut
has 'connect_timeout' => (
    is          => 'rw',
    isa         => 'Int',
    default     => 5);

=item B<port>

TCP port where foca server is running.

=cut
has 'port' => (
    is          => 'rw',
    isa         => 'Int');

=item B<debug>

Turn on debug. Turned off by default.

=cut
has 'debug' => (
    is          => 'rw',
    isa         => 'Bool',
    default     => 0);

=back

=head1 Methods

=head2 B<run($hosts, $command, %options)>

Runs the HTTP request (C<$command>) to the given foca servers (C<$hosts>).
C<$hosts> should be an array reference, use FQDN.

By default the method returns an array of hashes. Each hash having the following
keys:

=over 4

=item B<ok> Boolean. True if command went well or false otherwise.

=item B<output> output of the command

=item B<hostname> Hostname.

=back

In addition the method offers a third parameter, C<%options> that can be used to
tie the collection of data of each host and send it to a subroutine. Options are:

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.263 second using v1.00-cache-2.02-grep-82fe00e-cpan-f5108d614456 )