From: Robert 'phaylon' Sedlacek Date: Thu, 10 May 2012 18:58:12 +0000 (+0000) Subject: use reusable I/O utils, more solid error handling for Nagios::CheckMkAgent probe X-Git-Tag: v0.001_001~78 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7b3254fd50d487e5e2551f9e2ed695910bf070e2;p=scpubgit%2FSystem-Introspector.git use reusable I/O utils, more solid error handling for Nagios::CheckMkAgent probe --- diff --git a/lib/System/Introspector/Nagios/CheckMkAgent.pm b/lib/System/Introspector/Nagios/CheckMkAgent.pm index f50f821..5362457 100644 --- a/lib/System/Introspector/Nagios/CheckMkAgent.pm +++ b/lib/System/Introspector/Nagios/CheckMkAgent.pm @@ -1,25 +1,32 @@ package System::Introspector::Nagios::CheckMkAgent; use Moo; +use System::Introspector::Util qw( + lines_from_command + transform_exceptions +); + sub gather { my ($self) = @_; - my @lines = $self->_get_check_mk_agent_output; - chomp @lines; - my %plugin; - my $current; - for my $line (@lines) { - if ($line =~ m{^<<<(.+)>>>$}) { - $plugin{ $1 } = $current = []; - next; + return transform_exceptions { + my @lines = $self->_get_check_mk_agent_output; + chomp @lines; + my %plugin; + my $current; + for my $line (@lines) { + if ($line =~ m{^<<<(.+)>>>$}) { + $plugin{ $1 } = $current = []; + next; + } + next unless $current; + push @$current, $line; } - next unless $current; - push @$current, $line; - } - return \%plugin; + return { nagios_check_mk_agent => \%plugin }; + }; } sub _get_check_mk_agent_output { - return `check_mk_agent`; + return lines_from_command ['check_mk_agent']; } 1; diff --git a/t/nagios-checkmkagent.t b/t/nagios-checkmkagent.t index 0ffcc08..2fee140 100644 --- a/t/nagios-checkmkagent.t +++ b/t/nagios-checkmkagent.t @@ -18,7 +18,11 @@ my $probe = System::Introspector::Nagios::CheckMkAgent->new; my $data = $probe->gather; is_deeply $data, - { foo => [qw( bar baz )], bar => [], baz => [qw( qux )] }, + { nagios_check_mk_agent => { + foo => [qw( bar baz )], + bar => [], + baz => [qw( qux )], + } }, 'output parsing worked'; done_testing;