Cover V04, I02
Article
Figure 1
Figure 2
Figure 3
Listing 1

mar95.tar


Listing 1: The dusage script

#!/bin/sh
#
#       dusage
#
#       Collect disk space usage per user, sorted in descending space.
#       Show the megabytes used, the user name, and the percentage of
#       total disk capacity used by each user.  Allow a threshold
#       figure on the command line, below which no usage figures are
#       shown.  Use system names on command line if any given.
#
#       Lawrence S Reznick, 95Jan04

#
# Set default system names
#

SYSTEMS="server1 server2 system1 system2 system3 system4 system5 system6"

PATH=/bin:/usr/bin:/etc; export PATH

PROGNAME=`basename $0`

#
# HP-UX uses remsh for rsh.  Which is the local system?
# Assume SunOS is the alternative.
#

WHICHSYS=`uname -s`

if [ "`expr substr $WHICHSYS 1 5`" = "HP-UX" ]
then
RSHCMD=remsh
AWKCMD=awk
ECHOCMD=echo
else

RSHCMD=/usr/ucb/rsh
AWKCMD=nawk
ECHOCMD=/usr/5bin/echo
fi

#
# Should this program run?
#

THRESHOLD=${1:-50}

if [ "$THRESHOLD" -le 0 ]
then
$ECHOCMD "Usage $PROGNAME [threshold [systems]]\n"              1>&2
$ECHOCMD "Collects names of largest disk space users."          1>&2
$ECHOCMD "Show only users using > threshold Mbytes [defl=50]."  1>&2
$ECHOCMD "Must name threshold to name systems."                 1>&2
$ECHOCMD "Default systems: $SYSTEMS"                            1>&2
exit 1
fi

if [ "`id -u`" -ne 0 ]
then
$ECHOCMD "Can't run $PROGNAME unless you're a superuser!"       1>&2
exit 2
fi

if [ $# -lt 2 ]
then
set $SYSTEMS                            # Use default systems
else
shift                                   # Skip threshold arg
fi

#
# rlogin to each system and get big-usage stats
# System names are in positional parameters.
#

$ECHOCMD "Disk Usage Threshold ${THRESHOLD}M bytes"

for s
do
#
# HP-UX uses bdf for df.  Which is the remote system?
# Assume HP uses ksh or sh for root's rlogin shell and is
# missing /etc in its path, as documented in remsh(1).
# Assume Sun uses csh for root's rlogin shell.
#

WHICHSYS=`$RSHCMD $s uname -s`

if [ "`expr substr $WHICHSYS 1 5`" = "HP-UX" ]
then
DFCMD=bdf
TYPECMD="PATH=$PATH; type"
else
DFCMD=df
TYPECMD=/usr/ucb/which
fi

DFCMD=`$RSHCMD $s $TYPECMD $DFCMD | $AWKCMD '{print $NF}'`

#
# Find the command that shows disk usage by user
#

USECMD=`$RSHCMD $s "$TYPECMD quot" |
$AWKCMD '{print $NF}'`" -a"

#
# Collect the user stats.
#

$ECHOCMD "\n\n==== $s:"

$RSHCMD $s "$USECMD" |
$AWKCMD '
$0 ~ /dev/ {                    # Got a new filesystem name
#
# Show the mount name in remote system format
#

match($0, "\\(.*\\)");
printf("\n%s:%s\n",
SYSNAME,
substr($0, RSTART + 1, RLENGTH - 2));

#
# Determine the remote filesystem space
#

sysspace = sprintf("%s %s %s %s | egrep %s",
RSHCMD,
SYSNAME,
DFCMD,
$1,
$1);
sysspace | getline;

total = $2 / 1024;
used  = $3 / 1024;
avail = $4 / 1024;
max = avail + used;

printf("(Tot: %.2fM, Used: %.2fM, Avail: %.2fM, ",
total, used, avail);
printf("Usage: %.2f%%)\n",
(1 - (avail / max)) * 100);

close(sysspace);
next;
}

($1 / 1024) > THRESHOLD {       # Show big-usage user names
printf("%8.2fM\t%-8s\t%5.2f%% Usage\n",
$1 / 1024,
$2,
(($1 / 1024) / max) * 100);
}
' SYSNAME=$s DFCMD=$DFCMD RSHCMD=$RSHCMD THRESHOLD=$THRESHOLD
done