From 6055b5b3597db0334d2af37f5cdae1e276088bd0 Mon Sep 17 00:00:00 2001 From: Pablo Fischer Date: Wed, 1 Aug 2012 02:29:17 +0000 Subject: [PATCH] Add some examples and fixes --- examples/example_commands.yaml | 10 ++++++++++ examples/foca-client | 31 +++++++++++++++++++++++++++++++ examples/foca-server | 26 ++++++++++++++++++++++++++ lib/App/Foca/Client.pm | 8 ++++---- 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 examples/example_commands.yaml create mode 100755 examples/foca-client create mode 100755 examples/foca-server diff --git a/examples/example_commands.yaml b/examples/example_commands.yaml new file mode 100644 index 0000000..93ce3c1 --- /dev/null +++ b/examples/example_commands.yaml @@ -0,0 +1,10 @@ +commands_dirs: + - /some/path/over/there/bin + +commands: + df_path: + cmd: '/bin/df {%foca_args%} | tail -n1' + uptime: + cmd: '/usr/bin/uptime' + 'true': + cmd: '/bin/true' diff --git a/examples/foca-client b/examples/foca-client new file mode 100755 index 0000000..46c2914 --- /dev/null +++ b/examples/foca-client @@ -0,0 +1,31 @@ +#!/usr/local/bin/perl +# +# foca-client +# +# Author(s): Pablo Fischer (pablo@pablo.com.mx) +# Created: 08/01/2012 12:43:34 AM UTC 12:43:34 AM + +use strict; +use warnings; +use Data::Dumper; +use FindBin; +use lib "$FindBin::RealBin/../lib/"; +use App::Foca::Client; + +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"; +} diff --git a/examples/foca-server b/examples/foca-server new file mode 100755 index 0000000..96717dd --- /dev/null +++ b/examples/foca-server @@ -0,0 +1,26 @@ +#!/usr/local/bin/perl +# +# foca-server +# +# Author(s): Pablo Fischer (pablo@pablo.com.mx) +# Created: 08/01/2012 12:43:34 AM UTC 12:43:34 AM + +use strict; +use warnings; +use Data::Dumper; +use FindBin; +use lib "$FindBin::RealBin/../lib/"; +use App::Foca::Server; + +my $port = 6666; +my $timeout = 60; +my $commands = "$FindBin::RealBin/example_commands.yaml"; +my $debug = 1; + +my $server = App::Foca::Server->new( + port => $port, + commands_file => $commands, + commands_timeout => $timeout, + debug => $debug); +$server->run_server(); + diff --git a/lib/App/Foca/Client.pm b/lib/App/Foca/Client.pm index 85b25ad..36f439c 100644 --- a/lib/App/Foca/Client.pm +++ b/lib/App/Foca/Client.pm @@ -20,6 +20,7 @@ use strict; use warnings; use Data::Dumper; use FindBin; +use HTTP::Response; use Moose; use Parallel::ForkManager; use WWW::Curl::Easy; @@ -122,8 +123,10 @@ sub run { my ($self, $hosts, $command, $options) = @_; # Some basic verification + log_die("No hosts were given") unless $hosts; + log_die("Hosts are not an array ref") unless (ref $hosts eq 'ARRAY'); log_die("No command was given") unless $command; - + # Ok, get the command args and params my ($foca_cmd, $foca_args) = ($command, ''); if ($command =~ /(.+?)\s+(.+?)$/) { @@ -177,9 +180,6 @@ sub run { ref $options->{'on_host'} eq 'CODE'; }); - my $ua = new LWP::UserAgent; - $ua->timeout($self->{'timeout'}); - foreach my $host (@{$hosts}) { $pm->start($host) and next;