IT management/strategy

Checking disk space

April 8, 2009, 11:32 AM — 

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

I like it!
Comments

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
| reply

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.
| reply

HOW TO SEND EMAILS FOR SELF?

HOW TO SEND EMAILS FOR SELF?
| reply
peer-to-peer

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?

sjvn
64-bits of protection?

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

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

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.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace