From: Robert 'phaylon' Sedlacek Date: Thu, 10 May 2012 18:48:36 +0000 (+0000) Subject: use reusable I/O utils, more solid error handling for DiskUsage probe X-Git-Tag: v0.001_001~80 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ff3e98f6acae398971006c007d2f1bb1e7e8e04e;p=scpubgit%2FSystem-Introspector.git use reusable I/O utils, more solid error handling for DiskUsage probe --- diff --git a/lib/System/Introspector/DiskUsage.pm b/lib/System/Introspector/DiskUsage.pm index ef7e6d9..467f976 100644 --- a/lib/System/Introspector/DiskUsage.pm +++ b/lib/System/Introspector/DiskUsage.pm @@ -1,25 +1,31 @@ package System::Introspector::DiskUsage; use Moo; +use System::Introspector::Util qw( + lines_from_command + transform_exceptions +); + sub gather { my ($self) = @_; - my @lines = `df -aP`; - shift @lines; # header - chomp @lines; - my @rows; - for my $line (@lines) { - my %row; - @row{qw( - filesystem - blocks_1024 - used - available - capacity - mount_point - )} = split m{\s+}, $line; - push @rows, \%row; - } - return \@rows; + return transform_exceptions { + my @lines = lines_from_command ['df', '-aP']; + shift @lines; # header + my @rows; + for my $line (@lines) { + my %row; + @row{qw( + filesystem + blocks_1024 + used + available + capacity + mount_point + )} = split m{\s+}, $line; + push @rows, \%row; + } + return { disk_usage => \@rows }; + }; } 1; diff --git a/t/diskusage.t b/t/diskusage.t index 6499aa3..aa8b27f 100644 --- a/t/diskusage.t +++ b/t/diskusage.t @@ -4,7 +4,11 @@ use Test::More; use System::Introspector::DiskUsage; my $probe = System::Introspector::DiskUsage->new; -my $data = $probe->gather; + +my $result = $probe->gather; +ok $result, 'received data'; +my $data = $result->{disk_usage}; +ok $data, 'received disk usage data'; ok scalar(@$data), 'received data'; my @fields = qw( filesystem blocks_1024 used available capacity mount_point );