From 36a7bd6c788e2b0e3ad019a5f54ccf3907c68b5b Mon Sep 17 00:00:00 2001 From: dormando Date: Fri, 28 Oct 2011 16:44:14 -0700 Subject: [PATCH] improve usage file errors for mogadm if usage file is corrupt, try to tell the user a little more about why and hide spurious errors. --- mogadm | 58 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/mogadm b/mogadm index 3c125a9..5c8e3d5 100755 --- a/mogadm +++ b/mogadm @@ -468,7 +468,7 @@ sub cmd_check { foreach my $hosturl (@urls) { my ($hostid, $url) = @$hosturl; my $devs = $devices->{$hostid}; - foreach my $devid (sort { $a <=> $b } keys %$devs) { + DEV: foreach my $devid (sort { $a <=> $b } keys %$devs) { my $dev = $devs->{$devid}; my $status = $dev->{status} || "??"; next if $status eq "dead"; @@ -476,31 +476,41 @@ sub cmd_check { printf " [%2d] %-7s", $hostid, "dev$devid"; my $usage = get($url . "/dev$devid/usage"); - if (defined $usage) { - my %data = ( map { split(/:\s+/, $_) } split(/\r?\n/, $usage) ); - foreach (qw(time used total avail)) { - $data{$_} = 0 if (!$data{$_} || - $data{$_} !~ /\A\d+(\.\d+)?\Z/); + if (! defined $usage) { + print "REQUEST FAILURE FETCHING: $url" . "dev$devid/usage\n"; + next; + } + if (length($usage) < 1) { + print "USAGE FILE BROKEN OR EMPTY: $url" . "dev$devid/usage\n"; + next; + } + my %data = ( map { split(/:\s+/, $_) } split(/\r?\n/, $usage) ); + foreach (qw(time used total avail)) { + $data{$_} = 0 if (!$data{$_} || + $data{$_} !~ /\A\d+(\.\d+)?\Z/); + } + foreach (qw(available device disk)) { + if (! exists $data{$_} || !$data{$_}) { + print "MISSING FIELD ($_) FROM USAGE FILE: $url" . "dev$devid/usage\n"; + next DEV; } - $data{age} = $now - $data{time}; - $data{used} /= 1024**2; - $data{total} /= 1024**2; - $data{available} /= 1024**2; - $data{avail} = $data{available}; - my $pct = 100 - $data{available}/$data{total}*100; - $total{used} += $data{used}; - $total{avail} += $data{avail}; - $total{total} += $data{total}; - - $dev->{utilization} = 0 if (!defined($dev->{utilization}) || - $dev->{utilization} !~ /\A\d+(\.\d+)?\Z/); - printf(" %10.3f %10.3f %10.3f %6.2f%% %-7s %5.1f\n", - (map { $data{$_} } qw(total used avail)), - $pct, ($dev->{observed_state} || "?"), - $dev->{utilization}); - } else { - print "REQUEST FAILURE FETCHING: $url" . "/dev$devid/usage\n"; } + $data{age} = $now - $data{time}; + $data{used} /= 1024**2; + $data{total} /= 1024**2; + $data{available} /= 1024**2; + $data{avail} = $data{available}; + my $pct = 100 - $data{available}/$data{total}*100; + $total{used} += $data{used}; + $total{avail} += $data{avail}; + $total{total} += $data{total}; + + $dev->{utilization} = 0 if (!defined($dev->{utilization}) || + $dev->{utilization} !~ /\A\d+(\.\d+)?\Z/); + printf(" %10.3f %10.3f %10.3f %6.2f%% %-7s %5.1f\n", + (map { $data{$_} } qw(total used avail)), + $pct, ($dev->{observed_state} || "?"), + $dev->{utilization}); } } my $pct = 0;