Unix: Tracking disk space usage

No matter how much disk space you add to a system, sooner or later you're going to run short. But watching disk space get slowly used up is a lot like watching grass grow. How do you avoid a disk space crisis and avoid being lulled into a data stupor?

By  

Watching over available disk space is one of a Unix systems administrator's most basic responsibilities. It doesn’t matter if you manage a single server or thousands of them. If your users run out of the disk space they need to work, they won't get their work done and you will be the one who will be expected to somehow make everything right -- even if you have no real authority over how your users manage their files. So …

  • what are the most useful commands for monitoring disk space usage?
  • how can you track disk space "growth" over time?
  • when should you worry?
  • how should you respond when disk space gets too tight for comfort?

What are the most useful commands for monitoring disk space usage?

The basic commands for evaluating disk space usage are the df and du (disk usage) commands. For example, you might look at the disk space used by /home or the directories within /home with one of these commands:

# du /home
# du /home/*

With df -h, you can looks at how full particular file systems are. For example, maybe on issuing this command, you will see something like this:

$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       34G   28G  4.7G  86% /
/dev/sda1              99M   16M   79M  17% /boot
/dev/sda2            11.0G  9.8G  1.2G  89% /home
tmpfs                 7.9G  200M  7.7G   3% /dev/shm

This output would tell you that you are near to having only 10% of the home file system available for growth. What it doesn't tell you is how quickly you are likely to get there. If your users are adding a lot of content and a week ago, the file system was only 70% full, this might be cause for concern.

The du /home command will display the disk space used in the /home directory (or partition). Add /* and it will display how much space is used by each home directory within /home. This will give you an idea of who among your user population are the “disk hogs” and who are more modest disk space users.

You can run the du command as yourself, but you are unlikely to have read access to the content of other people's home directories, so you are likely to get many "Permission denied" errors if you do. And, if you can read some but not all of a particular directory's contents (see kc below), du will only tell you about the parts of it that you have read access to – not the entire home directory. That's not likely to tell you what you need to know.

$ du -sk /home/*
du: cannot read directory `/home/admin': Permission denied
56      /home/jdoe
du: cannot read directory `/home/kc/docs': Permission denied
64      /home/kc

As root, however, this command will give you the size of every home directory in /home.

# du -sk /home/*
800     /home/admin
5678423 /home/jdoe
86390   /home/kc
3392396 /home/oracle
292032  /home/shs
...

Tracking growth can take a number of forms. You can store data on disk usage for many weeks and calculate how much each file system has changed in size from one week to the next or you could just send yourself weekly reports on your key servers and gauge which of them are reaching the 90% or 95% figures that might set off alarms in your head.

This script simplifies the df output and adds the current date to each line. It also anticipates Logical Volume Manager entries that often wrap around to a second line.

#!/bin/bash

dt=`date +%D`

df -h > /tmp/du$$

while read line
do
    fields=`echo $line | awk '{print NF}'`
    case $fields in
    1) echo -n "$dt $line ";;
    5) echo -n "$dt "
        echo $line | awk '{print $4}';;
    6) echo -n "$dt "
        echo $line | awk '{print $1,$5}';;
    esac
done < /tmp/du$$

rm /tmp/du$$

The output from this script might look like this:

09/15/13 /dev/mapper/VolGroup00-LogVol00 09/15/13 86%
09/15/13 /dev/sda1 17%
09/15/13 /dev/sda2 89%
09/15/13 tmpfs 3%

Or you might prefer to display mount points rather than “/dev” values:

#!/bin/bash

dt=`date +%D`

df -h > /tmp/du$$

while read line
do
    fields=`echo $line | awk '{print NF}'`
    case $fields in
    5) echo -n "$dt "
        echo $line | awk '{print $5,$4}';;
    6) echo -n "$dt "
        echo $line | awk '{print $6,$5}';;
    esac
done < /tmp/du$$

rm /tmp/du$$

If so, your output might look like this:

09/15/13 / 86%
09/15/13 /boot 17%
09/15/13 /home 89%
09/15/13 /dev/shm 3%

How can you track disk space "growth" over time?

Unix sysadmins might look at data like this on a daily basis, but email like this can become so routine that projecting when disk space might be a hot issue gets lost in the detail. If, on the other hand, you add this kind of information to a log file and send yourself the last X lines (enough to see growth trends), you'd get a much better idea how disk usage is changing over time – even better if you can somehow turn the numbers into graphs.

In this script, we’re adding the df information into a growing log file which would allow us to look at disk usage over time.

#!/bin/bash

dt=`date +%D`
log="/var/tmp/du.log"

df -h > /tmp/du$$

while read line
do
    fields=`echo $line | awk '{print NF}'`
    case $fields in
    5) echo -n "$dt " >> $log
        echo $line | awk '{print $5,$4}' >> $log;;
    6) echo -n "$dt " >> $log
        echo $line | awk '{print $6,$5}' >> $log;;
    esac
done < /tmp/du$$

rm /tmp/du$$

Run this once a day and you can soon start sending yourself reports that show how quickly your disk space usage is growing. By using a tail command (e.g., tail -28 /var/tmp/du.log) and sending the output to yourself, you would see seven days’ worth of data for a system with four file systems.

When should you worry?

That's hard to predict, but when you see your disk space growing so quickly that your options for releasing disk space or adding disk storage would take more time than your disk is like to fill up.

How should you respond when disk space gets too tight for comfort?

By using the find command to locate especially large files and compressing them, especially of you can coordinate this action with the users in question. By using the du command to locate the users or directories that are using an unusually large amount of the available space. Users are often careless about cleaning up but, in my experience, will often jump into cleaning up their old files if asked. And, with enough lead time, you might just be prepared to add disk space when it’s needed.

Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.

Photo Credit: 

flickr / matsuyuki

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

Ask a Question