====== How can I report on all home directory use? ======
If you are a **system administrator**, you can get a CSV listing of all home directory use, including status of the owners of home directories with the following script:
#!/bin/bash
# Read a list of usernames (or directory names)
# and run quota on them.
#
# Falls back to (slow) 'du' if a quota is not set on the user
# or the user id is non-existent.
#
# This is used to get the size of home directories
# of current users, as well as those users who are no longer present.
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USERNAMES=$SCRIPT_DIR/usernames.txt
QUOTA_CMD="quota"
TARGET_DIR="/mnt/nfs/home"
TARGET_MNT="nfs01-ib:/home"
echo "Username,User_Active,Directory_Present,Directory_Owner,Directory_Group,Space_Calculation_Type,Space_Used,Directory,Dir_Perms"
cat $USERNAMES | while read U
do
# Is this an active username
uid=`id -u $U 2>/dev/null`
if [ "$?" == "0" ]
then
user_status="Yes"
else
user_status="No"
fi
# Look up home directory space utilisation
if [ -d "$TARGET_DIR/$U" ]
then
# Get owner and group of home directory
L=`ls -ld $TARGET_DIR/$U`
user_owner=`echo $L | awk '{print $3}'`
user_group=`echo $L | awk '{print $4}'`
dir_perms=`echo $L | awk '{print $1}'`
quota_size=`$QUOTA_CMD -u $U 2>/dev/null | grep $TARGET_MNT | awk '{print $2}'`
if [ "$quota_size" == "" ]
then
# No quota set ... try again with the UID
quota_size=`$QUOTA_CMD -u $user_owner 2>/dev/null | grep $TARGET_MNT | awk '{print $2}'`
if [ "$quota_size" == "" ]
then
# no quota set ... should we fall back to 'du' ???
quota_size=`du -s "$TARGET_DIR/$U" | awk '{print $1}'`
echo "$U,$user_status,Found,$user_owner,$user_group,du,$quota_size,$TARGET_DIR/$U,$dir_perms"
else
# Found quota by UID
quota_size=`echo $quota_size | sed 's/\*//g'`
echo "$U,$user_status,Found,$user_owner,$user_group,quota_uid,$quota_size,$TARGET_DIR/$U,$dir_perms"
fi
else
# Found quota by username
quota_size=`echo $quota_size | sed 's/\*//g'`
echo "$U,$user_status,Found,$user_owner,$user_group,quota_username,$quota_size,$TARGET_DIR/$U,$dir_perms"
fi
else
# Unable to find user home directory
echo "$U,$user_status,Missing,NA,NA,NA,0,$TARGET_DIR/$U,NA"
fi
done
You will need to set the following variables:
* ''QUOTA_CMD'' - The command to run to get a disk quota report. This may need to be ''quota'' for normal Linux filesystems/mounts, and ''lfs quota'' for Lustre filesystem mounts.
* ''TARGET_DIR'' - Where your home directories are located in your filesystem; e.g. ''/mnt/home'', ''/home'' or similar.
* ''TARGET_MNT'' - The hostname and export on which your home directory filesystem is mounted and on which quotas are enabled; e.g. ''server:/home''
You will need to supply a file ''usernames.txt'' which has a list of usernames/home directory names, one per line, e.g.
bob
john
emma
testuser
These will be assumed to be the directories under ''TARGET_DIR'' which are then checked, i.e. ''$TARGET_DIR/bob'', ''$TARGET_DIR/testuser'', etc.
* To determine if a user is active, we try to use ''id'' to return their UID. If it is found, then we assume the account is valid.
* If we have an active username then we pass that username to ''$QUOTA_CMD -u''.
* If a username is //not// active, then we try to call ''$QUOTA_CMD -u'' using the leftover UID which the directory belongs to.
* If that //still// fails, then we finally resort to calculating directory size using ''du''. This is the slowest method and we only use it in the last case.
This script will __not__ run as a normal user - the calls to ''quota'', ''lfs quota'' and ''du'' will fail.
=== Output ===
The output from the script will be a single line per username/home directory named in ''usernames.txt'', with the following fields:
* Username
* Is user active? - If not, then it shows the user account has expired
* Is home directory present?
* Username or UID of the home directory owner - If this doesn't match username, then we have a mismatch in home directory name and assigned owner
* Group name or GID of the home directory group
* Method used to calculate space (''quota'', ''quota'' using ''UID'' only, or ''du'')
* Space used, in Kilobytes
* Directory path
* Directory permissions
----
[[:faq:index|Back to FAQ]]