#!/usr/bin/ksh
# Diskinfo
# Script that displays interesting points about disks and file systems
# afx 4/95

export LANG=C
export LC_MESSAGES=en_US

DoPV=0
DoFS=0
DoLV=0
DoNA=0

if [[ $# -ge 1 ]] ; then
      for opt
      do
            case $opt in
            -d)   DoPV=1
                  ;;
            -f)   DoFS=1
                  ;;
            -l)   DoLV=1
                  ;;
            -n)   DoNA=1
                  ;;
            *)    echo "Unknown parameter $opt"
                  echo "Usage: \n$ (basename $0)  [-d] [-f] [-l] [-n]"
                  echo " -d: disks and volume groups"
                  echo " -f: file systems"
                  echo " -l: logical volumes without file systems"
                  echo " -n: unallocated disks"
                  exit 1
                  ;;
            esac
     done
else
     DoPV=1
     DoFS=1
     DoLV=1
     DoNA=1
fi

echo "Disk report for $(hostname -s) (machine id $(uname -m))\n"

disks=$(lspv | egrep -v None | cut -f1 -d\ )

[[ $DoPV -eq 1 ]] && {
      echo "Disks and Volume Groups"
      echo "                     Physical partitions"
      echo "VG        PV      size used   free Location    Description"
      for d in $disks
      do 
            (lsdev -Cl $d; lspv $d ) |
            awk '/Available/   {d=$1;loc=$3;
                        desc=substr($0,index($0,$4));
                        next}
                  /VOLUME GROUP/    {vg=$6;next}
                  /PP SIZE/         {sz=$3;next}
                  /FREE PP/         {free=$3;next}
                  /USED PP/         {used=$3;next}
                  END {printf("%-12s %-8s %4d %5d %5d %-12s %s\n",
                               vg,d,sz,used,free,loc,desc)}
                '
      done
      echo ""
}

[[ $DoNA -eq 1 ]] && {
     lspv | egrep None > /dev/null
     if [[ $? -eq 0 ]] ; then
          echo "Unallocated physical volumes:"
          for d in $(lspv | egrep None | cut -f1 -d\  )
          do
                lsdev -Cl $d
          done
     else
          echo "No unallocated physical volumes"
     fi
     echo ""
}

(( ($DoLV+$DoFS) >= 1 )) && {
echo "Volume Group Disk    LV        LPs   Distribution       Mount
point"
for d in $disks
do
      vg=$(lspv|awk "/$d/ {print \$3}")
      ( echo "$d $vg"
            [[ DoFS -eq 1 ]] && lspv -l $d | tail +3 | egrep "/"
            [[ DoLV -eq 1 ]] && lspv -l $d | tail +3 | egrep -v "/"
      ) | awk '{  if (NR==1) {
                        disk=$1
                        vg=$2
               } else {
                     printf("%-12s %-8s %-12s %-5s %-15s %s\n",
                           vg,disk,$1,$2,$4,$5)
               }
      } '
done
echo ""
}

