How to cleanly identify available file system space
frances
created: 2006-09-05 15:58:14

I'm trying to write code that will identify the space available in a given directory on a system with several mount points an a plethora of hard links. This is simple, and works:

@result = `df -Pk $directory`;

I would be more comfortable with a solution that would not create an external system call. I know that I can get the device ID for the mount point on which $directory is stored with a stat() call, but I haven't found a way to use $dev to find more infomation about the device.

I feel that there should be a simple way to do this, but I can't find it.
Re: How to cleanly identify available file system space
created: 2006-09-05 16:01:02
Re: How to cleanly identify available file system space
created: 2006-09-05 16:10:43
#!/usr/bin/perl -w

use strict;
use warnings;

#usage: diskspace 

my $diskuse = $ARGV[0];
my $recipient = 'support@example.com'; #who gets the report
my $dfrep = `df -k`; #get partition report
my @partitions = split(/\n/,$dfrep); # split report
my (@full, @ele, $usage, $x, $i);
my $hostname = `hostname`;
chomp($hostname);

#check syntax
unless ($diskuse > 0 || $diskuse < 100) {
        print "\nUsage:  spaceuse \n";
        print "Where n is the maximum percentage of space, in use by ";
        print "any disk partition\n";
}

#check for partitions whose percent full is
#greater than or equal to the percent entered
#by the user.
foreach $i (@partitions) {
        $_ = $i;
        if (m/(\d{1,2})%/) {$usage = $1}
        if ($diskuse <= $usage) { push(@full,$x) }
        $x++;
}

#create and mail report
if ($#full != 0) {

        open(MAIL, "|mail -s \"Diskspace Warning\" $recipient");

        print MAIL "\nWarning from $hostname\n";
        print MAIL "These Partitions are dangerously full:\n\n";

        for (@full) {
                $_++;
                $_--; #converts scalar to numberic
                $partitions[$_] =~ s/\s+/\t/g;
                @ele = split(/\t/, $partitions[$_]);
                printf MAIL "%13s %11s %11s %11s %4s %13s\n", $ele[0], $ele[1], $ele[2], $ele[3], $ele[4], $ele[5];

        }
        close(MAIL);
}

Neil Watson
watson-wilson.ca

Re: How to cleanly identify available file system space
created: 2006-09-05 16:12:12

Re: disk space utilization

Re^2: How to cleanly identify available file system space
created: 2006-09-05 18:04:16
ambrus: Thanks! That's a perfect and simple solution.

perlmonks.org content © perlmonks.org and ambrus, Fletch, frances, neilwatson

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03