Checking disk space
Here's a script for quickly checking on how much disk space you have on a system and how much of it is in use. It certainly beats using a calculator to tally up the columns in "df -k" output.
We're not looking for precision here. After all, disk usage changes very frequently. We don't want to see this information extended to ten decimal places. We just want a "feel" for how much space is in use and how much is still available. So, we report disk space in gigabytes and calculate a percentage. We round up both the total disk space and disk space used to the next gigabyte and the percentage used to the next percent. The generated report will look something like this:
# ./chkDiskSpace total: 308 GB used: 32 GB 11% in use
The script includes two subroutines -- one to round up a number and one to convert kilobytes to gigabytes. We could have used the "df -h" to report disk usage in GB instead of KB, but then we'd have to concern ourselves with the decimal points and occassional datapoints reported in KB because of their small size and that would complicate everything.
We squeeze strings of blanks into single blanks so that we can split the "df -k" output on the blanks in between the columns and skip over the headings before we run through each line of data.
The script also skips over swap space and NFS mounted partitions by ignoring any lines in the "df -k" output that don't start with the tell-tale "/dev". It also doesn't report on unmounted file systems; it's only looking at mounted drives.
#!/usr/bin/perl
# round up to next integer
sub roundup {
my $n = shift;
return(($n == int($n)) ? $n : int($n + 1))
}
sub conv2g {
my $n = shift;
return($n/1000000);
}
# get disk space report
open(STATS, "df -k|") || die "$!";
# init params
my $lineNo = 0;
my $totUsed = 0;
my $totSpace = 0;
while ($input = <STATS>) {
if ($lineNo++ != 0) { # Skip column headings
$input =~ s/\s+/ /g; # squeeze out blanks
next if $input !~ /^\/dev/; # skip over NFS mounts, swap
my @columns = split(/ /, $input);
$totSpace += $columns[1];
$totUsed += $columns[2];
};
};
$percent = $totUsed / $totSpace;
$pct=roundup($percent * 100);
$totSpace=roundup(conv2g($totSpace));
$totUsed=roundup(conv2g($totUsed));
print "total: $totSpace GB\n";
print " used: $totUsed GB\n";
print "$pct% in use\n";
You could set up a script like this to email you the results or email you the results only if the percentage of free disk space warrants your attention. Alternately, you might just use it when you're making plans for upgrading disks on some of your file servers and want a quick report on whether they're hurting for space or should be in good shape for the next year or two.
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
Esther Schindler
If the comments are ugly, the code is ugly
claird
SVG a graphics format for 21st century
pasmith
Take Chrome OS for a test spin
Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?
jfruh
Android fragments vs. the iPhone monolith
mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.














Need allow for output for a filesystem split over two lines
Hi,I tried this and noticed that the output figures were too low.
Having devices with names like this:
/dev/mapper/oravg-oralv
10222904 5614812 4088772 58% /oracle
I needed to allow for the df output being split over two lines or these get ignored.
So I changed the PERL:
# round up to next integer
sub roundup {
my $n = shift;
return(($n == int($n)) ? $n : int($n + 1))
}
sub conv2g {
my $n = shift;
return($n/1048576);
}
# get disk space report
open(STATS, "df -k|") || die "$!";
# init params
my $lineNo = 0;
my $totUsed = 0;
my $totSpace = 0;
my @columns;
while ($input =
if ($lineNo++ != 0) { # Skip column headings
$input =~ s/\s+/ /g; # squeeze out blanks
next if $input !~ /^\/dev/; # skip over NFS mounts, swap
@columns = split(/ /, $input);
# If only one element exists in the list read the next
# input line to get the space info
if (@columns == 1 )
{
$input =
$input =~ s/\s+/ /g; # squeeze out blanks
$input =~ s/^\s+//g; # squeeze out leading blanks
@columns = split(/ /, $input);
$totSpace += $columns[0];
$totUsed += $columns[1];
}
else
{
$totSpace += $columns[1];
$totUsed += $columns[2];
}
};
};
$percent = $totUsed / $totSpace;
$pct=roundup($percent * 100);
$totSpace=roundup(conv2g($totSpace));
$totUsed=roundup(conv2g($totUsed));
print "total: $totSpace GB\n";
print " used: $totUsed GB\n";
print "$pct% in use\n";
Regards
Pete
df -k
On Solaris, I always use the "h" switch to get output in human readable format. Space allocated, used, and available are shown in MB, GB, etc.HOW TO SEND EMAILS FOR SELF?
HOW TO SEND EMAILS FOR SELF?