Wednesday, August 20, 2008

Script to know the file system size in GB.

Adding to the below post there is one more script which is very useful.This actually works as an command if you copy it in /usr/bin/. The script is created by Mr.Bill Hassell. Great work is done by Bill which helps all HP-UX admins. Copy the below text in a file and save it as bdfmegs.

#!/usr/bin/sh
# Bill Hassell Nov 2006

# ###### ###### ####### # # ####### ##### #####
# # # # # # ## ## # # # # #
# # # # # # # # # # # # #
# ###### # # ##### # # # ##### # #### #####
# # # # # # # # # # # #
# # # # # # # # # # # # #
# ###### ###### # # # ####### ##### #####
#

# Ver 3.8 - Cleaned up help text.
# Added -q to suppress the header line for data collection
# Ver 3.7 - Added vxfs filesystem version to -v. Requires read
# permission on character dev file
# Ver 3.6 - Changed to add -g for gigabytes
# Added dir(s) or file(s) to match bdf options
# Removed blk/frg and vxfs version and obtain
# largefiles flag from df -g so anyone can use -v
# Ver 3.5 - Changed to use getopts for options
# Ver 3.4 - Changed to ignore autofs entries in mount -p
# Ver 3.3 - Changed formats to handle terabyte lvols
# Changed option processing to getopts
# Ver 3.2 - Simplify code with local $PATH
# Ver 3.1 - Trap fsadm errors and change return code. Allow -vl
# or -lv combinations.
# Ver 3.0 - Rewrote using temp files to speed up code about 10:1
# Added -l option and ? or -? help
# Ver 2.1 - Added vxfs filesystem layout verrsion (2,3,4,etc)
# Added non-root check for -v option fsadm needs root
# permission for fsadm to read lvols directly.
# Ver 2.0 - Change Blks to blk/frag size and add largefile status
# Made: FSTYPE blk/frag and largefile optional with -v
# to reduce the width of the line
# Ver 1.2 - Expanded filesystem type to handle autofs
# Ver 1.1 - Added blocksize column for filesystem blocks
# Ver 1.0 - Original
# -----------------------------------------------------------------------

# Show bdf in megs or gigs for easy reading
#
# Usage: bdfmegs [ -vlg [ file(s) or dir(s) ]
# -l = local (no NFS)
# -v = verbose
# -q = suppress header line
# -g = show gigabytes, otherwise megabytes

# After the options, path(s) or file(s) may be added to limit output.

# The width of the source filesystem will be adjusted to the
# longest path (ie, NFS or other long device filename). All
# fields are in megs (defined as Kbytes/1024) or gigs. Field
# widths are 7 digits for multi-terabyte capability.

set -u
PATH=/usr/bin:/usr/sbin

MYNAME=${0##*/}
TEMPDIR=/var/tmp/$MYNAME.$$
mkdir $TEMPDIR
chmod 700 $TEMPDIR
BDFLIST=$TEMPDIR/bdflist
trap "rm -rf $TEMPDIR;exit" 0 1 2 3 15
HPUXVER=$(uname -r | cut -d. -f2-3 | tr -d .)
[ $HPUXVER -ge 1123 ] && FASTDF="-s" || FASTDF=""

# These values are tied to the widths if the fields and the title.

typeset -R7 MTOT
typeset -R7 MUSED
typeset -R7 MAVAIL
typeset -R5 PERCENT
typeset -R4 FSTYPE
typeset -L2 FSVER

NODEVREAD=false # flag in case -v can't read devfile ver

# Process all options

VERBOSE="false"
LOCALFS=""
GIGABYTES="false"
NOHEADER="false"
MAGNITUDE=1024

while getopts ":vlg" OPTCHAR
do
case $OPTCHAR in
v) VERBOSE=true ;;
l) LOCALFS="-l" ;;
q) NOHEADER="true" ;;
g) let MAGNITUDE=$MAGNITUDE*1024
GIGABYTES=true
;;
: | ?) cat << v =" verbose" q =" suppress" l =" local" g =" gigabytes" specify="$*" specify=""> $BDFLIST

# Find the longest source string with a sweep through $BDFLIST
# Minimum length is 12 so typeset will pad on the right when
# needed.


MAXLEN=12
cat $BDFLIST | while read SOURCE DUMMY
do
[ ${#SOURCE} -gt $MAXLEN ] && MAXLEN=${#SOURCE}
done

# Variable width typesets here
#
# By using eval, a variable typeset instruction can be created
# and then executed as part of the inline script.

# First is for the filesystem source string
# Second is to pad the title. Note that PAD must be typeset to
# a value greater than 0, so subtract 11 for the evaluation.
# (the minimum width for a source directory is 12 chars)

eval "typeset -L$MAXLEN FS"
eval "typeset -L$(( MAXLEN - 11 )) PAD=' '"

# Print the title line. $PAD is used to prorvide proper spacing for
# short to long source filesystem names. This must match the
# evaluated typeset value for $FS above. We'll split the line
# at %Used in order to have plain and verbose versions.

[ $GIGABYTES = "true" ] && BYTES=Gbytes || BYTES=Mbytes
[ $NOHEADER = "true" ] || \
echo "File-System $PAD $BYTES Used Avail %Used \c"

if [ $NOHEADER = "false" ]
then
if $VERBOSE
then
echo "Type Vr Lrg Mounted on"
else
echo "Mounted on"
fi
fi

###################################################################
# Walk through each mountpoint gathering info
# We'll ask df -g for the filesystem type and largefile option.
# For 11.23 and loater, bdf and df have a -s (no sync) option
# to speed up getting static data. FASTDF is null prior to
# 11.23 here.

cat $BDFLIST | while read FS TOT USED AVAIL PERCENT MNT
do

# Find FSTYPE df -g

FSTYPE=$(df -g $FASTDF $MNT \
| grep "system type" \
| awk '{print $5}')

# Compute megs or gigs with $MAGNITUDE (1024 or 1024*1024)

MTOT=$(echo $TOT \
| awk '{print int($1/'$MAGNITUDE'+.5)}')
MUSED=$(echo $USED \
| awk '{print int($1/'$MAGNITUDE'+.5)}')
MAVAIL=$(echo $AVAIL \
| awk '{print int($1/'$MAGNITUDE'+.5)}')

echo "$FS $MTOT $MUSED $MAVAIL $PERCENT \c"

if $VERBOSE
then

# VxFS filesystem version is found with fstyp but requires the source
# device file be readable. Otherwise, print na and add a note.

if [ "$FSTYPE" = "vxfs" ]
then
if [ -r $FS ]
then
FSVER=$(fstyp -v $FS | awk '/version:/ {print $2}')
else
FSVER="na"
NODEVREAD=true
fi
else
FSVER=" "
fi

# filesystem features (from /usr/include/sys/statvfs.h)
# df -g will return these flags
# ST_RDONLY 0x01 /* read only vfs */
# ST_NOSUID 0x02 /* someone is waiting for lock */
# ST_EXPORTED 0x04 /* file system is exported (NFS) */
# ST_QUOTA 0x08 /* filesystem with quotas */
# ST_LARGEFILES 0x10 /* filesystem supports largefiles */
#
# For now, just test that the features flag is greater
# than 0x10 or 16. Use bc to convert the flag into
# a decimal number. printf has a problem with %.0f
# prior to 11.23) bc requires uppercase chars so
# use typeset -u


typeset -u HEX
HEX=$(df -g $FASTDF $MNT \
| tr -d x | awk '/ flags / {print $1}')
DECFLG=$(echo "ibase=16\n $HEX" | bc)
[ $DECFLG -ge 16 ] && LG="yes" || LG=" no"

echo "$FSTYPE $FSVER $LG $MNT"
else
echo "$MNT"
fi
done

# In case -v was specified and one or more device files were not
# readable by the current user, add a note

if $NODEVREAD
then
echo "\nNote: na means devicefile not readable by $(id -un)"
fi

Once you copy the above text give execute permission on bdfmegs file. Move this file to /usr/bin/.

Thats it now you are ready to use this script as an command.

Simple # bdfmegs command will display the file system size in MB.

# bdfmegs used with -g option will display file system in GB.

No comments: