#!/usr/bin/perl use strict; use warnings; use debug; my @headers = qw(name size used free capacity mount); &debug::predebugdumplist("\@headers", \@headers); my @df = &readDFfile('test.dat'); &debug::predebugdumplist("\@df", \@df); shift @df; # get rid of the header &debug::predebugdumplist("\@df", \@df); my %devices; &debug::predebugdumplist("\%devices", \%devices); for my $line (@df) { &debug::predebug("\$line = '$line'\n"); my %info; &debug::predebugdumplist("\%info", \%info); @info{@headers} = split /\s+/, $line; # note the hash slice &debug::predebugdumplist("\%info", \%info); $info{capacity} = _percentage_to_decimal($info{capacity}); &debug::predebugdumplist("\%info", \%info); $devices{ $info{mount} } = \%info; &debug::predebugdumplist("\%devices", \%devices); } # Change 12.3% to .123 sub _percentage_to_decimal { my $percentage = shift; &debug::predebug("\$percentage = '$percentage'\n"); $percentage =~ s{%}{}; &debug::predebug("\$percentage = '$percentage'\n"); return $percentage / 100; } # Now the information for each device is in a hash of hashes. # Show how much space is free in device /dev/ad4s1e print $devices{"/production/log"}{free} ; print "\n"; for my $info (values %devices) { &debug::predebug("\$info = '$info'\n"); # Skip to the next device if its capacity is not over 60%. next unless $info->{capacity} > .10; &debug::predebug("Found \$info->{capacity} to be > .10\n"); # Print some info about each device printf "%s is at %d%% with %dK remaining.\n", $info->{mount}, $info->{capacity}*100, $info->{free}; } exit; sub readDFfile { my ($rdofnm, @arglst) = @_; if (!defined $rdofnm) { $rdofnm = ''; } # -----[ Was command, now reading from file for testing purposes ]-------------- # my @df = `df -k`; # ------------------------------------------------------------------------------ my @df = (); if (open RDOFIL, '<', $rdofnm) { @df = ; close RDOFIL; } # ------------------------------------------------------------------------------ return @df; } __END__