Operating System - HP-UX
1833776 Members
2345 Online
110063 Solutions
New Discussion

How to check the percentage of disk(s) free space

 
SOLVED
Go to solution
Gary L
Super Advisor

How to check the percentage of disk(s) free space

Hi

I got a task from my manager, he let me wrote a script to collect system information and performance. I have done all the parts but display percentage of disk(s) free space.
for example: server1 have 2 device (ioscan -funCdisk c1t1d0, c2t1d0). those two PV have been assigned to LV and Vg, and already created some file system on those two disks. Now, I wanna use some command(s) figure out EACH disk's PERCENTAGE of free space. Output the same like:
c1t1d0 freespace 30%
c2t1d0 freespace 50%
I have tried "bdf" and "df", but they don't work. As "bdf","df" only display the file systems that mounted, umount file systems it could not list.
I have tried "du" also, but probably "du -ks" will count whole diskuage.

whick command could get each device's total size? diskinfo? How to achieve via script. I means, whether I need run "ioscan -funCdisk" first and got the device(s) info and run diskinfo +device got the each disk total size (size: 35566480 Kbytes)

Any suggestions will be very appreciate!!!

thank you very much
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to check the percentage of disk(s) free space

I would try "vgdisplay -v /dev/vgxx" and see if a few ideas for parsing that output suggest themselves. (Don't forgat swap devices.) Of course, this works in an LVM environment. VxVM also should be considered in a full implementation. That leaves us with the most difficult case: raw disks. The meaning of data on raw disks is known only to the application so that "free" is impossible to determine outside of the context of a given application.
If it ain't broke, I can fix that.
Gary L
Super Advisor

Re: How to check the percentage of disk(s) free space

Hi A. Clay Stephenson

Thank you very much for your good suggestions.

Have a great day

-Gary
Arturo Galbiati
Esteemed Contributor

Re: How to check the percentage of disk(s) free space

Hi,
you can use these scripts to get total space available and free:

cat sduT
VolNam=${1:-""}
hostname|nslookup|grep "Name:"|cut -d":" -f2|tr -d " "
echo "======================================="
/usr/sbin/vgdisplay $VolNam |grep -E "VG Name|PE Size|Total PE"|\
awk 'BEGIN {T=0}
/VG/ {VG=$3}
/Size/ {MB=$4}
/Total/ {PE=$3;printf "%-15s # %10dMb\n",VG,MB*PE;T=T+MB*PE}
END {print "======================================="
printf "%-15s # %10dMb # %4dGb\n","Total",T,T/1024}
'


cat sduU
hostname|nslookup 2>/dev/null|grep "Name:"|cut -d":" -f2|tr -d " "
echo "======================================="
# Logical volumes unallocated space
/usr/sbin/vgdisplay|grep -E "VG Name|PE Size|Free PE"|\
awk 'BEGIN {T=0}
/VG/ {VG=$3}
/Size/ {MB=$4}
/Free/ {PE=$3;printf "%-15s # %10dMb\n",VG,MB*PE;T=T+MB*PE}
END {print "======================================="
printf "%-15s # %10dMb # %4dGb\n","Total",T,T/1024}
'
#
ARMDSP=/opt/sanmgr/commandview/client/sbin/armdsp
if [[ -x $ARMDSP ]]; then
ArmAls=$($ARMDSP -i|awk '/Alias:/ {print $2}')
UnlSpc=$($ARMDSP $ArmAls|\
awk -F: '/Unallocated/ {n=gsub("_","")
r=substr($2,1,index($2," ")-1)+.5
printf "%4i\n",r
}')
echo "======================================="
echo " Unallocated # $ArmAls # ${UnlSpc}Gb"
fi

the last gives you the space free allocated and the space not yet configured on the disk array VA7110.

HTH,
Art
Gary L
Super Advisor

Re: How to check the percentage of disk(s) free space

Hi Arturo

Thank you very much for your helps and your good script of count free space.

I will try it later.

Happy weekend.

-Gary
BTW, this Sunday is "father's day", I think your kid(s) will give you some surprise!
Geoff Wild
Honored Contributor

Re: How to check the percentage of disk(s) free space

I have a script that outputs:

# vgttl
VG Size(MB) In use Available
vg00 17668 8604 8744
vg01 12288 4 11901
---- -------------- -------------- --------------
Totl 29956 8608 20645


# cat /usr/local/bin/vgttl
\#!/bin/sh
/usr/local/bin/bdfmegs|grep vg |tr -s " " " "|cut -d" " -f 1-4|sort|
awk 'BEGIN {
prev="";
totals[0]=0;
totals[1]=0;
totals[2]=0;
gtotals[0]=0;
gtotals[1]=0;
gtotals[2]=0;
printf("%8s %14s %14s %14s\n","VG","Size(MB)","In use","Available");
}
{split($1,curr,"/");
if (prev!="" && prev!=curr[3])
{printf("%8s %14d %14d %14d\n",prev,totals[0],totals[1],totals[2]);
gtotals[0]+=totals[0];
gtotals[1]+=totals[1];
gtotals[2]+=totals[2];
totals[0]=0;
totals[1]=0;
totals[2]=0;
}
prev=curr[3];
totals[0]+=$2;
totals[1]+=$3;
totals[2]+=$4;
}
END {
gtotals[0]+=totals[0];
gtotals[1]+=totals[1];
gtotals[2]+=totals[2];
printf("%8s %14d %14d %14d\n",prev,totals[0],totals[1],totals[2]);
printf("%8s %14s %14s %14s\n","----","--------------","--------------","--------------");
printf("%8s %14d %14d %14d\n","Totl",gtotals[0],gtotals[1],gtotals[2]);
}'



Note: you also need:

# cat /usr/local/bin/bdfmegs
#!/usr/bin/sh
#
# 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 for easy reading
#
# Usage: bdfmegs [ -v ] [ -l ] [ -? | ? ]
# bdfmegs [ -? | ? ]

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

set -u

MYNAME=${0##*/}
TEMPDIR=/var/tmp/$MYNAME.$$
/usr/bin/mkdir $TEMPDIR
/usr/bin/chmod 700 $TEMPDIR
MNTLIST=$TEMPDIR/mountlist
BDFLIST=$TEMPDIR/bdflist
ERRLIST=$TEMPDIR/errlist
trap "/usr/bin/rm -rf $TEMPDIR;exit" 0 1 2 3 15

# 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 -R2 BLKSIZE
typeset -i FRAGSIZE
typeset -R6 FSTYPE
typeset -R1 FSVERSION

# Process all options

PARAMS=$#
VERBOSE=/usr/bin/false
LOCALFS=/usr/bin/false
BDFOPTS=""
while [ $PARAMS -gt 0 ]
do
case $1 in
-v ) VERBOSE=/usr/bin/true
;;
-l ) LOCALFS=/usr/bin/true
BDFOPTS="$BDFOPTS -l"
;;
-lv | -vl ) LOCALFS=/usr/bin/true
BDFOPTS="$BDFOPTS -l"
VERBOSE=/usr/bin/true
;;
\? | -\? )
echo
echo "Usage:"
echo " $MYNAME [ -v ] [ -l ]"
echo
echo "where: -v = full details"
echo " -l = local filesystems only"
exit
;;
\* ) echo "Unknown option: $1"
echo
;;
esac
shift
PARAMS=$(( PARAMS - 1 ))
done

# Get a complete list of mountpoints

/sbin/mount -p $BDFOPTS > $MNTLIST

# For bdf, process the list and paste the 2-line data together
# into a file to speed things up
#
# The mechanism is to read all the params from each line. If the
# line has been split, only the source is on line 1 so the rest
# of the params are null when read, so if $TOT is blank, read
# the next line for the missing params.

/usr/bin/bdf $BDFOPTS |
while read FS TOT USED AVAIL PERCENT MNT
do
if [ "$TOT" = "" ]
then
read TOT USED AVAIL PERCENT MNT
fi
echo $FS $TOT $USED $AVAIL $PERCENT $MNT
done > $BDFLIST

# Check if -v was specified
# If so, check if this is root? If not, fsadm can't obtain details.

if $VERBOSE
then
if [ $(/usr/bin/id -u) -ne 0 ]
then
echo
echo "$MYNAME: root required for -v option"
echo
exit 1
fi
fi

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

MAXLEN=12
cat $MNTLIST | while read SOURCE MOUNTPOINT 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=' '"

# Now get all filesystems including filesystem type
# Separate the mount point from FStype and source with a |

MYMOUNTS=$(cat $MNTLIST | /usr/bin/awk '{print $2"|"$3"|"$1}')

# 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.

echo "File System $PAD Mbytes Used Avail %Used \c"

if $VERBOSE
then
echo " Type Bk/Fg Lrg Mounted on"
else
echo "Mounted on"
fi

# Now walk through each mountpoint gathering info
# We'll search through the $BDFLIST file for
# the bdf info.

for MNTINFO in $MYMOUNTS
do
MOUNTPOINT=$(echo $MNTINFO | cut -f1 -d\|)
SOURCE=$( echo $MNTINFO | cut -f3 -d\| )
FSTYPE=$( echo $MNTINFO | cut -f2 -d\| )
grep ^$SOURCE $BDFLIST | read FS TOT USED AVAIL PERCENT MNT

# do
MTOT=$(echo $TOT \
| /usr/bin/awk '{print int($1/1024+.5)}')
MUSED=$(echo $USED \
| /usr/bin/awk '{print int($1/1024+.5)}')
MAVAIL=$(echo $AVAIL \
| /usr/bin/awk '{print int($1/1024+.5)}')
echo "$FS $MTOT $MUSED $MAVAIL $PERCENT \c"

if $VERBOSE
then

# filesystem features

BLKSIZE=$(/usr/bin/df -g $MNT \
| /usr/bin/grep "file system block" \
| /usr/bin/awk '{print $1/1024}')
FRAGSIZE=$(/usr/bin/df -g $MNT \
| /usr/bin/grep "file system block" \
| /usr/bin/awk '{print $6/1024}')
VG="${FS%/*}"
LVOL="${FS##*/}"

# Check only vxfs and hfs for features

case $FSTYPE in

# For vxfs, fsadm gets info from mountpoint but hfs needs the
# raw lvol. NOTE: fsadm may detect consistency errors so trap
# these to an error file, summarize and set an error return code.

" vxfs" )
LGFILE=$(/usr/sbin/fsadm -F $FSTYPE $MNT 2>> $ERRLIST \
| /usr/bin/grep -F largefiles)
# VxFS version
FSVERSION=$(/usr/sbin/fstyp -v $VG/r$LVOL 2>> $ERRLIST \
| /usr/bin/grep version: \
| /usr/bin/awk '{print $2}')
FSTYPE="vxfs$FSVERSION"
# largefiles?
if [ $( echo $LGFILE | /usr/bin/grep -Fc no) -gt 0 ]
then
LG=" no"
else
LG="yes"
fi
;;
" hfs" )
if [ $(/usr/sbin/fsadm -F $FSTYPE $VG/r$LVOL 2>> $ERRLIST \
| /usr/bin/grep -Fc nolargefiles) -lt 1 ]
then
LG="yes"
else
LG=" no"
fi
;;

# All others
* ) LG=" "
;;
esac
echo "$FSTYPE ${BLKSIZE}/${FRAGSIZE}K $LG $MNT"
else
echo "$MNT"
fi
done

# Any errors?

if [ -s $ERRLIST ]
then
echo " -- Errors exist (fsadm and/or fstyp):"
cat $ERRLIST | while read REPLY
do
echo " -- $REPLY"
done
exit 1
fi



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Bill Hassell
Honored Contributor

Re: How to check the percentage of disk(s) free space

Here is bdfmegs (aka, bdfgigs) version 4.5, with many, many new features including much faster performance for very large disk farms. It also handles CIFS, NFS and new filesystems for 11.31 but will run on 10.20 too. It includes a -s (sum of all displayed mountpoints) as well as flags (-p) for filesystems over a certain percentage.


Bill Hassell, sysadmin
Gary L
Super Advisor

Re: How to check the percentage of disk(s) free space

Hi Geoff Wild and Bill

You are so nice, thank you very very much for your detail scripts and bdfmegs.

Happy weekend.

-Gary