39 Analyzing Disk Usage


#39 Analyzing Disk Usage

Even with the advent of very large disks and their continual drop in price, system administrators seem to perpetually be tasked with keeping an eye on disk usage to ensure that the system doesn't fill up.

The most common monitoring technique is to look at the /users or /home directory, using the du command to ascertain the disk usage of all the subdirectories, and then reporting the top five or ten users therein. The problem with this approach, however, is that it doesn't take into account space usage elsewhere on the hard disk(s). If you have some users who have additional archive space on a second drive, or sneaky types who keep MPEGs in a dot directory in /tmp or in an unused and accidentally opened directory in the ftp area, they'll escape detection. Also, if you have home directories spread across multiple devices (e.g., disks), searching each /home isn't necessarily optimal.

Instead, a better solution is to get all the account names directly from the /etc/passwd file and then to search the file systems for files owned by each account, as shown in this script.

The Code

 #!/bin/sh # fquota - Disk quota analysis tool for Unix. #          Assumes that all user accounts are >= UID 100. MAXDISKUSAGE=20 for name in $(cut -d: -f1,3 /etc/passwd  awk -F: ' > 99 {print }') do   echo -n "User $name exceeds disk quota. Disk usage is: "   # You might need to modify the following list of directories to match   # the layout of your disk. Most likely change: /Users to /home   find / /usr /var /Users -user $name -xdev -type f -ls  \       awk '{ sum +=  } END { print sum / (1024*1024) " Mbytes" }' done  awk "$9 > $MAXDISKUSAGE { print $0 }" exit 0 

How It Works

By convention, uids 1 through 99 are for system daemons and administrative tasks , while 100 and above are for user accounts. Unix administrators tend to be a fairly organized bunch, and this script takes advantage of that, skipping all accounts that have a uid of less than 100.

The -xdev argument to the find command ensures that find doesn't go through all file systems, preventing it from slogging through system areas, read-only source directories, removable devices, the /proc directory of running processes (on Linux), and similar areas.

It may seem at first glance that this script outputs an exceeds disk quota message for each and every account, but the awk statement after the loop allows reporting of this message only for accounts with usage greater than the predefined MAXDISKUSAGE .

Running the Script

This script has no arguments and should be run as root to ensure access to all directories and file systems. The smart way to do this is by using the helpful sudo command (see man sudo for more details). Why is sudo helpful? Because it allows you to execute one command as root , after which you are back to being a regular user. Each time you want to run an administrative command, you have to consciously use sudo to do so; using su - root , by contrast, makes you root for all subsequent commands until you exit the subshell, and when you get distracted it's all too easy to forget you are root and then type a command that can lead to disaster.

Note  

You will likely have to modify the directories listed in the find command to match the corresponding directories in your own disk topography .

The Results

Because it's searching across file systems, it should be no surprise that this script takes rather a while to run. On a large system it could easily take somewhere between a cup of tea and a lunch with your significant other. Here are the results:

 $  sudo fquota  User linda exceeds disk quota. Disk usage is: 39.7 Mbytes User taylor exceeds disk quota. Disk usage is: 21799.4 Mbytes 

You can see that taylor is way out of control with his disk usage! That's 21GB. Sheesh.

Hacking the Script

A complete script of this nature should have some sort of automated email capability to warn the scofflaws that they're hogging disk space. This enhancement is demonstrated in the very next script.




Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net