The following is the latest incarnation of something I wrote a few years back, and have refined as I have learned new tricks. The output of the df -k command is filtered for a list of directories. If the free space has changed since I last ran the command, it is stored. I also store a timestamp along with it, in case I want to look at historical trends. Right now, it keeps only the latest 8 reports.
#!/usr/bin/perl
# List free space left
use strict;
use warnings;
use Storable;
my $storable_file = '/home/edi/howard/dfk.storable';
my (@reports);
@reports = @{retrieve($storable_file)} if ( -e $storable_file);
my %new = map {(split /\s+/,$_)[6,3]} grep {/home|archive|edi_store/}
+`df -k`;
$new{'timestamp'} = time;
unshift @reports, \%new;
if (print_report(@reports[0,1])) {
pop @reports if (scalar @reports > 7);
store(\@reports,$storable_file);
}
sub print_report {
my ($nhr,$ohr) = @_;
my $change_flag = defined $ohr ? 0 : 1;
foreach my $key ( keys %{$nhr}) {
next if $key eq 'timestamp';
if (defined $ohr->{$key} && $ohr->{$key} ne $nhr->{$key}) {
printf "%-17s was: %3s now: %3s\n", $key, $ohr->{$key}, $nhr->{$
+key};
$change_flag = 1;
} else {
printf "%-17s: %3s\n",$key,$nhr->{$key}
}
}
return $change_flag;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|