40 Reporting Disk Hogs


#40 Reporting Disk Hogs

Most system administrators seek the easiest way to solve a problem, and the easiest way to manage disk quotas is to extend the fquota script, Script #39, to include the ability to email warnings directly to users who are consuming too much space.

The Code

 #!/bin/sh # diskhogs - Disk quota analysis tool for Unix; assumes all user #   accounts are >= UID 100. Emails message to each violating user #   and reports a summary to the screen. MAXDISKUSAGE=20 violators="/tmp/diskhogs0.$$" trap "/bin/rm -f $violators" 0 for name in $(cut -d: -f1,3 /etc/passwd  awk -F: ' > 99 { print  }') do   echo -n "$name "   # 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) }' done  awk "$2 > $MAXDISKUSAGE { print $0 }" > $violators if [ ! -s $violators ] ; then   echo "No users exceed the disk quota of ${MAXDISKUSAGE}MB"   cat $violators   exit 0 fi while read account usage ; do    cat << EOF  fmt  mail -s "Warning: $account Exceeds Quota" $account Your disk usage is ${usage}MB, but you have been allocated only ${MAXDISKUSAGE}MB.  This means that you need to either delete some of your files, compress your files (see 'gzip' or 'bzip2' for powerful and easy-to-use compression programs), or talk with us about increasing your disk allocation. Thanks for your cooperation in this matter. Dave Taylor @ x554 EOF   echo "Account $account has $usage MB of disk space. User notified." done < $violators exit 0 

How It Works

Note the addition of the fmt command in the email pipeline:

 cat << EOF  fmt  mail -s "Warning: $account Exceeds Quota" $account 

It's a handy trick to improve the appearance of automatically generated email when fields of unknown length, like $account , are embedded in the text. The logic of the for loop in this script is slightly different from the logic of the for loop in Script #39, fquota . Because the output of the loop in this script is intended purely for the second part of the script, during each cycle it simply reports the account name and disk usage rather than a disk quota exceeded error message.

Running the Script

Like Script #39, this script has no starting arguments and should be run as root for accurate results. This can most safely be accomplished by using the sudo command.

The Results

 $  sudo diskhogs  Account linda has 39.7 MB of disk space. User notified. Account taylor has 21799.5 MB of disk space. User notified. 

If we now peek into the linda account mailbox, we'll see that a message from the script has been delivered:

 Subject: Warning: linda Exceeds Quota Your disk usage is 39.7MB, but you have been allocated only 20MB.  This means that you need to either delete some of your files, compress your files (see 'gzip' or 'bzip2' for powerful and easy-to-use compression programs), or talk with us about increasing your disk allocation. Thanks for your cooperation on this matter. Dave Taylor @ x554 

Hacking the Script

A useful refinement to this script would be to allow certain users to have larger quotas than others. This could easily be accomplished by creating a separate file that defines the disk quota for each user and by declaring in the script a default quota for users not appearing in the file. A file with account name and quota pairs can be scanned with grep and the second field extracted with a call to cut -f2 .




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