shell scripts

ygzhou518發表於2015-07-13
[sfdba@cnsz22pl0028:/dba/oracle/diag/rdbms/ecpdbsit/ecpdbsit/trace]$cat /usr/bin/sysauto_SF
#!/bin/bash
########## 0. Version Information ##########
printversion ()
{
LastModify=`cat $0 | grep '^#' | grep '# Last Modified:' | awk '{print $NF}'`
Version=`cat $0 | grep '^#' | grep '# Version:' | awk '{print $NF}'`
echo "Version: $Version"
#echo "Last Modified: $LastModify"
}


########## 1. Netcard Information ##########
getnetinfo ()
{
echo "===== Link Info ====="
ethtool $i | egrep "Speed:|Duplex:|Link detected:"
echo "===== CDPR Info ====="
if [ `ethtool $i | grep -c "Link detected: yes"` -eq 0 ]; then
  echo "Unknown!"
else
  cdpr -t 120 -d $i | egrep "Device ID|Addresses|Port ID|value:|Aborting"
fi
}


netinfomain ()
{
NETINFOLOG="/tmp/netinfo`date +%Y%m%d%H%M%S`.log"
> $NETINFOLOG


if [ $# -eq 1 ]; then
  ETHS="$1"
  [ `ifconfig -a | grep HWaddr | awk '{print $1}' | grep -wc "$ETHS"` -eq 0 ] && echo "$ETHS not exist, exit!" && exit 1
else
  ETHS=`ifconfig -a | grep HWaddr | awk '{print $1}' | grep -v bond | grep -v usb`
fi


for i in `echo $ETHS`
do
  echo "-------------------- $i --------------------"
  if [ `ifconfig | grep -wc $i` -gt 0 ];then
    getnetinfo
  else
    ifconfig $i up
    sleep 5
    getnetinfo
    ifconfig $i down
  fi
done | tee -a $NETINFOLOG
echo
echo "-- Save In: $NETINFOLOG --"
}


########## 2. HBA Information ##########
hbainfomain ()
{
[ ! -d /sys/class/fc_host/ ] && echo "No hostx found in /sys/class/fc_host/, exit!" && exit 1
for i in `ls /sys/class/fc_host/`;
do
echo "-------------------- $i --------------------"
  PCIID=`systool -c fc_host -v ${i} | grep -v 'Class Device path' | grep '/devices/pci' | head -n1 | awk -F'/' '{print $(NF-1)}' | sed 's/0000://g'`
  VENDOR=`lspci | grep $PCIID | awk -F':' '{print $3}' | awk '{print $1}'`
  [ `ls -l /sys/class/scsi_host/${i}/ | grep -c 'model'` -gt 0 ] && MODEL=`cat /sys/class/scsi_host/${i}/*model* | egrep -vi "PCI|HBA" | head -n1` || MODEL="Unknown"
  [ -z "$MODEL" ] && MODEL="Unknown"
  FIRMWARE=`cat /sys/class/scsi_host/${i}/fw* /sys/class/scsi_host/${i}/firmware* 2> /dev/null | grep '\.' | awk '{print $1}' | head -n1`
  DRIVER="Unknown"
  DRVERSION="Unknown"
  [ `echo $VENDOR | grep -ic "emulex"` -gt 0 ] && DRIVER="lpfc" && DRVERSION=`modinfo lpfc | grep -w "^version:" | awk '{print $2}'`
  [ `echo $VENDOR | grep -ic "brocade"` -gt 0 ] && DRIVER="bfa" && DRVERSION=`modinfo bfa | grep -w "^version:" | awk '{print $2}'`
  [ `echo $VENDOR | grep -ic "qlogic"` -gt 0 ] && DRIVER="qla2xxx" && DRVERSION=`modinfo qla2xxx | grep -w "^version:" | awk '{print $2}'`
  HbaWwpnString=`cat /sys/class/fc_host/${i}/port_name | cut -c3-20 | tr 'a-z' 'A-Z'`
  HbaWwpn1to2=`echo $HbaWwpnString | cut -c1-2`
  HbaWwpn3to4=`echo $HbaWwpnString | cut -c3-4`
  HbaWwpn5to6=`echo $HbaWwpnString | cut -c5-6`
  HbaWwpn7to8=`echo $HbaWwpnString | cut -c7-8`
  HbaWwpn9to10=`echo $HbaWwpnString | cut -c9-10`
  HbaWwpn11to12=`echo $HbaWwpnString | cut -c11-12`
  HbaWwpn13to14=`echo $HbaWwpnString | cut -c13-14`
  HbaWwpn15to16=`echo $HbaWwpnString | cut -c15-16`
  WWPN="${HbaWwpn1to2}:${HbaWwpn3to4}:${HbaWwpn5to6}:${HbaWwpn7to8}:${HbaWwpn9to10}:${HbaWwpn11to12}:${HbaWwpn13to14}:${HbaWwpn15to16}"
  STATE=`cat /sys/class/fc_host/${i}/port_state`
  SPEED=`cat /sys/class/fc_host/${i}/speed`
  [ "$SPEED" == "unknown" ] && SPEED="Unknown"


  if [ "$STATE" == "Online" ]; then
    if [ -x /usr/bin/lsscsi ]; then
      LsscsiLog=/tmp/.lsscsi.log
      lsscsi > $LsscsiLog
      HbaHostNum=`echo "$i" | sed 's/host//g'`
      if [ `cat $LsscsiLog | grep "\[${HbaHostNum}:" | grep -wc 'disk'` -gt 0 ]; then
        CONNECT="StorageSan"
      elif [ `cat $LsscsiLog | grep "\[${HbaHostNum}:" | grep -wc 'tape'` -gt 0 ]; then
        CONNECT="BackupSan"
      else
        CONNECT="Unknown"
      fi
    else
      multipath -l | egrep '[0-9].*:[0-9].*:[0-9].*:[0-9].*' | awk '{print $2}' | awk -F':' '{print "host"$1}' | sort -u &> /tmp/.multipath.log
      if [ `cat /tmp/.multipath.log | grep -wc $i` -gt 0 ]; then
        CONNECT="Storage SAN"
      elif [ `cat /tmp/.multipath.log | grep -wc $i` -eq 0 -a `cat /tmp/.multipath.log | grep -c host` -gt 0 ]; then
        CONNECT="Backup SAN"
      else
        CONNECT="Unknown"
      fi
    fi
  else
    CONNECT="Unknown"
  fi
    
  echo "Vendor   = $VENDOR"
  echo "Model    = $MODEL"
  echo "Firmware = $FIRMWARE"
  echo "Driver   = $DRIVER"
  echo "Version  = $DRVERSION"
  echo "Pcinum   = $PCIID"
  echo "Wwpn     = $WWPN"
  echo "State    = $STATE"
  echo "Speed    = $SPEED"
  echo "Connect  = $CONNECT"
done
}


########## 3. PATH Information ##########
pathinfomain ()
{
for i in `cat /proc/partitions | awk '{print $4}' | grep sd`
do
  [ `uname -r | egrep -c '2.6.32|2.6.39'` -gt 0 ] && echo "$i `scsi_id -g -u -d /dev/$i`" || echo "$i `scsi_id -g -u -s /block/$i`"
done > /tmp/.pathinfo.log


PATHINFOLOG="/tmp/pathinfo`date +%Y%m%d%H%M%S`.log"
> $PATHINFOLOG
echo "LUN_WWID LUN_PATH LUN_SIZE LUN_VENDOR LUN_MODEL LUN_DISK" | awk '{printf "%-35s%-10s%-15s%-15s%-20s%-30s\n",$1,$2,$3,$4,$5,$6}' > $PATHINFOLOG
echo "-------- -------- -------- ---------- --------- --------" | awk '{printf "%-35s%-10s%-15s%-15s%-20s%-30s\n",$1,$2,$3,$4,$5,$6}' >> $PATHINFOLOG
for WWID in `cat /tmp/.pathinfo.log | awk '{print $2}' | sort -u | grep -v "^$"`
do
  LUNWWID="$WWID"
  LUNPATH=`grep -wc "$WWID" /tmp/.pathinfo.log`
  LUNDISK=`grep -w "$WWID" /tmp/.pathinfo.log | awk '{printf $1" "}'`
  for DISK in `echo $LUNDISK`
  do
    SIZEDIGI=`fdisk -l /dev/$DISK 2> /dev/null | grep "^Disk" | awk '{print $3}' | head -n1`
    SIZEUNIT=`fdisk -l /dev/$DISK 2> /dev/null | grep "^Disk" | awk '{print $4}' | awk -F',' '{print $1}' | head -n1`
    LUNSIZE=`echo $SIZEDIGI $SIZEUNIT`
    LUNVENDOR=`cat /sys/block/${DISK}/device/vendor | sed 's/  //g'`
    LUNMODEL=`cat /sys/block/${DISK}/device/model | sed 's/  //g'`
    [ -n "$SIZEDIGI" -a -n "$SIZEUNIT" ] && break
  done
  echo "$LUNWWID|$LUNPATH|$LUNSIZE|$LUNVENDOR|$LUNMODEL|$LUNDISK" |awk -F'|' '{printf "%-35s%-10s%-15s%-15s%-20s%-30s\n",$1,$2,$3,$4,$5,$6}' >> $PATHINFOLOG
done
cat $PATHINFOLOG
echo
echo "-- Save In: $PATHINFOLOG --"
}


########## 4. Check lun use ##########
## 0. Functions
asmcheck ()
{
LUNDEVICE="$1"
DISKTYPE="UNKNOWN"
DISKNAME="UNKNOWN"
GROUPNAME="UNKNOWN"
GROUPSIZE="UNKNOWN"
if [ "$ASMDISKCHECK" == "yes" ]; then
  $ORACLEASM querydisk -p $LUNDEVICE &> $ORACLEASMLOG
  if [ `cat $ORACLEASMLOG | grep -c "is marked an ASM disk"` -gt 0 ]; then
    DISKTYPE="ASM"
    DISKNAME=`cat $ORACLEASMLOG | grep "is marked an ASM disk" | awk '{print $NF}' | sed 's/"//g'`
    if [ "$ASMDGCHECK" == "yes" ]; then
      if [ `cat $ASMDGDISKLOG | grep -wc "$DISKNAME"` -gt 0 ]; then
        GROUPNAME=`cat $ASMDGDISKLOG | grep -w "$DISKNAME" | awk '{print $1}'`
        GROUPSIZE=`cat $LSDGLOG | grep -w $GROUPNAME | awk '{printf "%dG\n",$(NF-6)/1024+1}'`
      else
        $KFED read $LUNDEVICE &> $KFEDLOG
        if [ `cat $KFEDLOG | grep 'hdrsts' | grep -c KFDHDR_MEMBER` -gt 0 ]; then
          GROUPNAME=`cat $KFEDLOG | grep 'grpname' | awk '{print $2}'`
          [ `cat $ASMDGDISKLOG | grep -wc "$GROUPNAME"` -gt 0 ] && GROUPNAME="UNKNOWN"
        fi
      fi
    fi
  fi
fi
}


lvmcheck ()
{
LUNDEVICE="$1"
DISKTYPE="UNKNOWN"
DISKNAME="UNKNOWN"
GROUPNAME="UNKNOWN"
GROUPSIZE="UNKNOWN"
if [ `cat $PVSCANLOG | grep -wc "$LUNDEVICE"` -gt 0 ]; then
  DISKTYPE="LVM2"
  DISKNAME=`basename "$LUNDEVICE"`
  if [ `cat $PVSCANLOG | grep -w "$LUNDEVICE" | grep -wc 'VG'` -gt 0 ]; then
    GROUPNAME=`cat $PVSCANLOG | grep -w "$LUNDEVICE" | awk '{print $4}'`
    GROUPSIZE=`cat $VGSLOG | grep -w "$GROUPNAME" | tail -n1 | awk '{print $(NF-1)}'`
  fi
fi
}


fscheck ()
{
LUNDEVICE="$1"
DISKTYPE="UNKNOWN"
DISKNAME="UNKNOWN"
GROUPNAME="UNKNOWN"
GROUPSIZE="UNKNOWN"
if [ `dumpe2fs -h $LUNDEVICE 2> /dev/null | grep -c "Filesystem UUID"` -gt 0 ]; then
  DISKTYPE="EXT3/4"
  DISKNAME=`basename "$LUNDEVICE"`
fi
}


checklunuse ()
{
## 1. Define Parameters
MULTIPATHLOG="/tmp/.multipath.log"
ORACLEASMLOG="/tmp/.oracleasm.log"
KFEDLOG="/tmp/.kfed.log"
PVSLOG="/tmp/.pvs.log"
VGSLOG="/tmp/.vgs.log"
PVSCANLOG="/tmp/.pvscan.log"
LSDGLOG="/tmp/.lsdg.log"
ASMDGDISKLOG="/tmp/.asm_dg_disk.log"
ASMDGDISKLOGTEMP="/tmp/.asm_dg_disk_temp.log"


RESULT1="/tmp/.result1.log"
RESULT2="/tmp/.result2.log"
RESULT3="/tmp/lunuseinfo`date +%Y%m%d%H%M%S`.log"


MULTIPATH="/sbin/multipath"
KPARTX="/sbin/kpartx"
ORACLEASM="/usr/sbin/oracleasm"
KFED="/dba/app/product/11.2.0/grid/bin/kfed"
PVS="pvs"
VGS="vgs"
PVSCAN="pvscan"
ASMCMD="/dba/app/product/11.2.0/grid/bin/asmcmd"


## 2. Environment Check
# Check Multipath
if [ ! -x $MULTIPATH ]; then
  echo "Multipath not installed, exit!"
  exit 1
fi
$MULTIPATH -l | grep -A1 'dm\-' &> $MULTIPATHLOG
if [ `cat $MULTIPATHLOG | grep -c 'dm\-'` -eq 0 ]; then
  echo "No multipath devices, exit!"
  exit 1
fi


# Check ASM DG
if [ ! -x $ORACLEASM ]; then
  ASMDISKCHECK="no"
else
  ASMDISKCHECK="yes"
fi
if [ ! -x $ORACLEASM -o ! -x $KFED -o ! -x $ASMCMD -o `grep -c "^grid" /etc/passwd` -eq 0 -o `ps -ef | grep -v "grep" | grep -c "asm_pmon"` -eq 0 ]; then
  ASMDGCHECK="no"
else
  ASMDGCHECK="yes"
  su - grid -c "$ASMCMD lsdg --discovery" &> $LSDGLOG
  if [ `cat $LSDGLOG | grep -wc 'MOUNTED'` -gt 0 ]; then
    > $ASMDGDISKLOG
    for i in `cat $LSDGLOG | grep -w 'MOUNTED' | awk '{print $NF}' | sed 's/\///g'`
    do
      su - grid -c "$ASMCMD lsdsk -G ${i}" &> $ASMDGDISKLOGTEMP
      cat $ASMDGDISKLOGTEMP | grep 'ORCL:' | sed -e "s/ORCL:/ /g" -e "s/^/${i}/g" >> $ASMDGDISKLOG 2>&1
    done
  fi
fi


# Detect LUN Partitions
for LUNNAME in `cat $MULTIPATHLOG | grep 'dm\-' | awk '{print $1}'`
do
  [ ! -f /dev/mapper/${LUNNAME}p1 ] && $KPARTX -a /dev/mapper/${LUNNAME} > /dev/null 2>&1
done
sleep 1


# Check LVM PV & VG
$PVSCAN &> $PVSCANLOG
$VGS &> $VGSLOG


## 3. Main
> $RESULT1
> $RESULT2
> $RESULT3


for LUNNAME in `cat $MULTIPATHLOG | grep 'dm\-' | awk '{print $1}'`
do
  LUNSIZE=`cat $MULTIPATHLOG | grep -w -A1 $LUNNAME | grep "size=" | sed -e 's/\[/ /g' -e 's/\]/ /g' -e 's/size=//g' | awk '{print $1}'`
  LUNTYPE=`cat $MULTIPATHLOG | grep -w $LUNNAME | awk '{print $4}'`
  LUNWWID=`cat $MULTIPATHLOG | grep -w $LUNNAME | awk -F'(' '{print $2}' | awk -F')' '{print $1}'`
  DISKTYPE="UNKNOWN"
  DISKNAME="UNKNOWN"
  GROUPNAME="UNKNOWN"
  GROUPSIZE="UNKNOWN"


  for LUNDEVICE in `ls -r /dev/mapper/${LUNNAME}p* 2> /dev/null` /dev/mapper/${LUNNAME}
  do
    [ "$DISKTYPE" == "UNKNOWN" ] && asmcheck "$LUNDEVICE"
    [ "$DISKTYPE" == "UNKNOWN" ] && lvmcheck "$LUNDEVICE"
    [ "$DISKTYPE" == "UNKNOWN" ] && fscheck "$LUNDEVICE"
    [ "$DISKTYPE" != "UNKNOWN" ] && break
  done


  echo "$LUNNAME|$LUNSIZE|$LUNTYPE|$LUNWWID|$DISKNAME|$GROUPNAME|$GROUPSIZE|$DISKTYPE" >> $RESULT1
done


cat $RESULT1 | awk -F'|' '{printf "%-24s%-8s%-17s%-35s%-24s%-24s%-10s%-8s\n",$1,$2,$3,$4,$5,$6,$7,$8}' | sort -k8 -k6 -k1 > $RESULT2
echo "LUN_NAME LUN_SZ STORAGE LUN_WWID ASM_DISK/LVM_PV ASM_DG/LVM_VG DG/VG_SZ TYPE" | awk '{printf "%-24s%-8s%-17s%-35s%-24s%-24s%-10s%-8s\n",$1,$2,$3,$4,$5,$6,$7,$8}' > $RESULT3
echo "-------- ------ ------- -------- --------------- ------------- -------- ----" | awk '{printf "%-24s%-8s%-17s%-35s%-24s%-24s%-10s%-8s\n",$1,$2,$3,$4,$5,$6,$7,$8}' >> $RESULT3
cat $RESULT2 | awk '($NF=="ASM"){print}' | sort -k6 -k1 >> $RESULT3
cat $RESULT2 | awk '($NF=="LVM2"){print}' | sort -k6 -k1 >> $RESULT3
cat $RESULT2 | awk '($NF=="EXT3/4"){print}' | sort -k1 >> $RESULT3
cat $RESULT2 | awk '($NF=="UNKNOWN"){print}' | sort -k2 >> $RESULT3
cat $RESULT3
echo ""
echo "-- Save In: $RESULT3 --"
}


########## 5. Check ASM DG use ##########
checkasmdguse ()
{
## 1. Define Parameters
ASMCMD="/dba/app/product/11.2.0/grid/bin/asmcmd"
CRSCTL="/dba/app/product/11.2.0/grid/bin/crsctl"
ASMCMDLOG="/tmp/.asmcmd_lsdg.log"


## 2. ASM Diskgroup Check
[ ! -f $ASMCMD ] && echo "Command asmcmd not exist!" && exit 1
[ ! -f $CRSCTL ] && echo "Command crsctl not exist!" && exit 1
[ `$CRSCTL check crs | grep -c "is online"` -lt 4 ] && echo "CRS and ASM is not running!" && exit 1
[ `cat /etc/passwd | grep -c "^grid:"` -eq 0 ] && echo "User grid not exist!" && exit 1


su - grid -c "$ASMCMD lsdg --discovery &> $ASMCMDLOG"


echo "ASMDG_NAME STATE TYPE TOTAL_GB FREE_GB USED_RATE" | awk '{printf "%-20s%-20s%-20s%-20s%-20s%-20s\n",$1,$2,$3,$4,$5,$6}'
echo "---------- ----- ---- -------- ------- ---------" | awk '{printf "%-20s%-20s%-20s%-20s%-20s%-20s\n",$1,$2,$3,$4,$5,$6}'
cat $ASMCMDLOG | grep -w MOUNTED | grep -v OCRVD | sed 's/\///g' | awk '{printf "%-20s%-20s%-20s%-20d%-20d%.2f %%\n",$NF,$1,$2,$(NF-6)/1024,$(NF-3)/1024,($(NF-6)-$(NF-3))*100/$(NF-6)}' | sort -t"_" -k1 -k2
cat $ASMCMDLOG | grep -w DISMOUNTED | sed 's/\///g' | awk '{print $NF,$1" UNKNOWN UNKNOWN UNKNOWN UNKNOWN"}' | awk '{printf "%-20s%-20s%-20s%-20s%-20s%-20s\n",$1,$2,$3,$4,$5,$6}' | sort -t"_" -k1 -k2
}


########## 6. Get DB ASMDG information ##########
########## 7. Get DB size in ASMDG information ##########
getasmcmdlsoflog ()
{
## 1. Define Parameters
ASMCMD="/dba/app/product/11.2.0/grid/bin/asmcmd"
CRSCTL="/dba/app/product/11.2.0/grid/bin/crsctl"
OLSNODES="/dba/app/product/11.2.0/grid/bin/olsnodes"


CLUSTERHOSTSLOG="/tmp/.clusterhosts.log"
ASMCMDLSDGLOG="/tmp/.asmcmd_lsdg.log"
ASMCMDLSOFSH="/tmp/.asmcmd_lsof.sh"
ASMCMDLSOFLOG="/tmp/.asmcmd_lsof.log"
ASMCMDLSOF1LOG="/tmp/.asmcmd_lsof1.log"
ASMCMDLSOF2LOG="/tmp/.asmcmd_lsof2.log"
ASMCMDLSOF3LOG="/tmp/.asmcmd_lsof3.log"


## 2. Check Environment
[ ! -f $ASMCMD ] && echo "Command asmcmd not exist!" && exit 1
[ ! -f $CRSCTL ] && echo "Command crsctl not exist!" && exit 1
[ `$CRSCTL check crs | grep -c "is online"` -lt 4 ] && echo "CRS and ASM is not running!" && exit 1
[ `cat /etc/passwd | grep -c "^grid:"` -eq 0 ] && echo "User grid not exist!" && exit 1


## 3. Get DBSID, ASMDG, DGDIR, DIRTYPE From Every Node In The Cluster
cat > $ASMCMDLSOFSH << EOF
#!/bin/bash
#
HNAME=\`hostname\`
export ORACLE_SID=\`cat /etc/oratab | grep '^+ASM' | tail -n1 | awk -F':' '{print \$1}'\`
$ASMCMD lsof | sed "s/^/\${HNAME} /g"
EOF
chmod 755 $ASMCMDLSOFSH
chown grid:oinstall $ASMCMDLSOFSH


su - grid -c "$OLSNODES &> $CLUSTERHOSTSLOG"
su - grid -c "$ASMCMD lsdg &> $ASMCMDLSDGLOG"


> $ASMCMDLSOFLOG
for HNAME in `cat $CLUSTERHOSTSLOG`
do
  su - grid -c "scp -q -o PasswordAuthentication=no -o StrictHostKeyChecking=no $ASMCMDLSOFSH $HNAME:/tmp"
  su - grid -c "ssh -q -o PasswordAuthentication=no -o StrictHostKeyChecking=no $HNAME 'bash $ASMCMDLSOFSH'" >> $ASMCMDLSOFLOG
done


cat $ASMCMDLSOFLOG | egrep -v 'Path|+ASM' | sed -e 's/+/ /g' -e 's/\// /g' | awk '{print $1,$3,$4,$5,$6}' | sort -u > $ASMCMDLSOF1LOG
cat $ASMCMDLSOF1LOG | awk '{print $1,$2,$3,tolower($4)}' | sort -u > $ASMCMDLSOF2LOG
}


dbindginfo ()
{
getasmcmdlsoflog
DBINDGLOG="/tmp/dbdginfo`date +%Y%m%d%H%M%S`.log"
> $DBINDGLOG
echo "HOSTNAME DB_SID DG_NAME TOTAL_GB FREE_GB USED_RATE" | awk '{printf "%-15s%-15s%-20s%-10s%-10s%-10s\n",$1,$2,$3,$4,$5,$6}' | tee -a $DBINDGLOG
echo "-------- ------ ------- -------- ------- ---------" | awk '{printf "%-15s%-15s%-20s%-10s%-10s%-10s\n",$1,$2,$3,$4,$5,$6}' | tee -a $DBINDGLOG
cat $ASMCMDLSOF1LOG | grep "control" | sort -u | while read LINE
do
  HNAME=`echo "$LINE" | awk '{print $1}'`
  DBSID=`echo "$LINE" | awk '{print $2}'`
  DGNAME=`echo "$LINE" | awk '{print $3}' | tr 'a-z' 'A-Z'`
  DGSIZE=`cat $ASMCMDLSDGLOG | grep -w "$DGNAME" | awk '{print $(NF-6)}'`
  DGFREE=`cat $ASMCMDLSDGLOG | grep -w "$DGNAME" | awk '{print $(NF-3)}'`
  DGRATE=`echo "$DGSIZE $DGFREE" | awk '{printf "%.2f",($1-$2)*100/$1}'`
  echo "$HNAME $DBSID $DGNAME $DGSIZE $DGFREE $DGRATE" | awk '{printf "%-15s%-15s%-20s%-10d%-10d%6s %%\n",$1,$2,$3,$4/1024,$5/1024,$6}' | tee -a $DBINDGLOG
done
echo ""
echo "-- Save In: $DBINDGLOG --"
}


dbsizeinfo ()
{
getasmcmdlsoflog
DBSIZEINDGLOG="/tmp/dbsizeinfo`date +%Y%m%d%H%M%S`.log"


if [ $# -eq 1 ]; then
  [ `cat $ASMCMDLSOF2LOG | grep -wc "$1"` -eq 0 ] && echo "DB SID $1 is not exist or not running, exit!" && exit 1
  cat $ASMCMDLSOF2LOG | grep -w "$1" > $ASMCMDLSOF3LOG
else
  cat $ASMCMDLSOF2LOG > $ASMCMDLSOF3LOG
fi


> $DBSIZEINDGLOG
echo "HOSTNAME DB_SID DIR_PATH DIR_GB DG_NAME TOTAL_GB FREE_GB USED_RATE" | awk '{printf "%-15s%-15s%-30s%-10s%-20s%-10s%-10s%9s\n",$1,$2,$3,$4,$5,$6,$7,$8}' | tee -a $DBSIZEINDGLOG
echo "-------- ------ -------- ------ ------- -------- ------- ---------" | awk '{printf "%-15s%-15s%-30s%-10s%-20s%-10s%-10s%9s\n",$1,$2,$3,$4,$5,$6,$7,$8}' | tee -a $DBSIZEINDGLOG


cat $ASMCMDLSOF3LOG | while read LINE
do
  HNAME=`echo "$LINE" | awk '{print $1}'`
  DBSID=`echo "$LINE" | awk '{print $2}'`
  DGNAME=`echo "$LINE" | awk '{print $3}' | tr 'a-z' 'A-Z'`
  DGDIR=`echo "$LINE" | awk '{print $4}' | tr 'a-z' 'A-Z'`
  DGDIRPATH=`echo "+${DGNAME}/${DGDIR}/" | tr 'A-Z' 'a-z'`
  DGDIRSIZEMB=`su - grid -c "$ASMCMD du --suppressheader $DGDIRPATH" 2> /dev/null | awk '{print $1}'`
  [ -z "$DGDIRSIZEMB" ] && DGDIRSIZEMB="0" || DGDIRSIZE=`echo "$DGDIRSIZEMB" | awk '{printf "%.2f",$1/1024}'`
  DGSIZE=`cat $ASMCMDLSDGLOG | grep -w "$DGNAME" | awk '{print $(NF-6)}'`
  DGFREE=`cat $ASMCMDLSDGLOG | grep -w "$DGNAME" | awk '{print $(NF-3)}'`
  DGRATE=`echo "$DGSIZE $DGFREE" | awk '{printf "%.2f",($1-$2)*100/$1}'`
  echo "$HNAME $DBSID $DGDIRPATH $DGDIRSIZE $DGNAME $DGSIZE $DGFREE $DGRATE" | awk '{printf "%-15s%-15s%-30s%-10s%-20s%-10d%-10d%7s %%\n",$1,$2,$3,$4,$5,$6/1024,$7/1024,$8}' | tee -a $DBSIZEINDGLOG
done
echo ""
echo "-- Save In: $DBSIZEINDGLOG --"
}


########## 8. Create CRS Resource Group ##########
resgrpconf ()
{
## 0. Parameters
INTERACTIVE="yes"
ENABLEDRESOURCE="no"
CHECKDBUSER="yes"
CHECKDBSID="no"
CHECKDBVIP="yes"
CHECKHOST="yes"
CHECKASMDG="yes"
CHECKRESGRP="yes"


CRSCTL="/dba/app/product/11.2.0/grid/bin/crsctl"
APPVIPCFG="/dba/app/product/11.2.0/grid/bin/appvipcfg"
ASMCMD="/dba/app/product/11.2.0/grid/bin/asmcmd"
CRSCTL_STAT_LOG="/tmp/.crsctl_stat.log"
CRSCTL_STAT_SERVERPOOL_LOG="/tmp/.crsctl_stat_serverpool.log"


[ "$1" == "help" ] && echo "usage: $0 " && exit 1
[ `whoami` != "root" ] && echo "Run as root, exit!" && exit 1
[ ! -f $ASMCMD ] && echo "Command asmcmd not exist!" && exit 1
[ ! -f $CRSCTL ] && echo "Command crsctl not exist!" && exit 1
[ `$CRSCTL check crs | grep -c "is online"` -lt 4 ] && echo "CRS and ASM is not running!" && exit 1
[ `cat /etc/passwd | grep -c "^grid:"` -eq 0 ] && echo "User grid not exist!" && exit 1


$CRSCTL stat res -t &> $CRSCTL_STAT_LOG
$CRSCTL stat serverpool &> $CRSCTL_STAT_SERVERPOOL_LOG


## 1. Input database information
echo "-------------------- ## 1. Input database information --------------------"
ANSWER="n"
while [ "$ANSWER" != "y" ]
do
  if [ $# -eq 7 ]; then
    DBUSER=$1
    DBSID=$2
    DBVIP=$3
    HOST1=$4
    HOST2=$5
    DATADG=$6
    FRADG=$7
  else
    DBUSERDEF=`cat /etc/passwd | awk -F':' '{print $1}' | grep "11g" | head -n1`
    read -p "Input the db user [$DBUSERDEF]: " DBUSER
    DBUSER=${DBUSER:=$DBUSERDEF}
    read -p "Input the db sid: " DBSID


    DBSIDDOMAIN="unknown"
    [ `echo $DBUSER | grep -c "^op"` -eq 1 ] && DBSIDDOMAIN="${DBSID}.db.sfdc.com.cn"
    [ `echo $DBUSER | grep -c "^os"` -eq 1 -a `hostname | grep -ic "cnsz"` -eq 1 ] && DBSIDDOMAIN="${DBSID}.dbldr.sfdc.com.cn"
    [ `echo $DBUSER | grep -c "^os"` -eq 1 -a `hostname | grep -ic "cnsh"` -eq 1 ] && DBSIDDOMAIN="${DBSID}.dbdr.sfdc.com.cn"
    [ `echo $DBUSER | grep -c "^ot"` -eq 1 ] && DBSIDDOMAIN="${DBSID}.dbstg.sfdc.com.cn"
    [ `echo $DBUSER | grep -c "^od"` -eq 1 ] && DBSIDDOMAIN="${DBSID}.dbdev.sfdc.com.cn"
    DBVIPDEF=`host $DBSIDDOMAIN | awk '{print $NF}'`
    [ `echo "$DBVIPDEF" | egrep -c "NXDOMAIN|SERVFAIL"` -gt 0 ] && DBVIPDEF=""
    read -p "Input the db vip [$DBVIPDEF]: " DBVIP
    DBVIP=${DBVIP:=$DBVIPDEF}


    read -p "Input the master host: " HOST1
    read -p "Input the slave host: " HOST2
    read -p "Input the data dg: " DATADG
    read -p "Input the fra dg: " FRADG
  fi
  echo ""
  echo "Your input db info is:"
  echo "------------------------------"
  echo "db user: $DBUSER"
  echo "db sid: $DBSID"
  echo "db vip: $DBVIP"
  echo "master host: $HOST1"
  echo "slave host: $HOST2"
  echo "data dg: $DATADG"
  echo "fra dg: $FRADG"
  echo "------------------------------"
  ANSWER="y"
  [ "$INTERACTIVE" == "yes" ] && read -p "Are you sure? (y/n/q): " ANSWER
  [ "$ANSWER" == "q" ] && exit 0
done
echo ""


## 2. Environment check
echo "-------------------- ## 2. Environment check --------------------"
if [ "$CHECKDBUSER" == "yes" ]; then
  echo -n "Checking db user: $DBUSER ... " && [ `grep -c "^${DBUSER}:" /etc/passwd` -eq 0 ] && echo "not exist, exit!" && exit 1 || echo "OK."
fi


if [ "$CHECKDBSID" == "yes" ]; then
  echo -n "Checking db sid: $DBSID ... " && [ `grep -c "^${DBSID}:" /etc/sf/shell/db_info.txt` -eq 0 ] && echo "not exist in /etc/sf/shell/db_info.txt, exit!" && exit 1 || echo "OK."
fi


if [ "$CHECKDBVIP" == "yes" ]; then
  echo -n "Checking db vip: $DBVIP ... " && [ `ping -c2 $DBVIP | grep -c "100% packet loss"` -eq 0 ] && echo "is active, exit!" && exit 1 || echo "OK."
fi


if [ "$CHECKHOST" == "yes" ]; then
  echo -n "Checking master host: $HOST1 ... " && [ `grep -wc "${HOST1}" /etc/hosts` -eq 0 ] && echo "not exist in /etc/hosts, exit!" && exit 1 || echo "OK."
  echo -n "Checking slave host: $HOST2 ... " && [ `grep -wc "${HOST2}" /etc/hosts` -eq 0 ] && echo "not exist in /etc/hosts, exit!" && exit 1 || echo "OK."
fi


if [ "$CHECKASMDG" == "yes" ]; then
  echo -n "Checking data dg: $DATADG ... " && [ `su - grid -c "$ASMCMD ls $DATADG 2> /dev/null" | grep -ic ${DBSID}` -eq 0 ] && echo "is not right, exit!" && exit 1 || echo "OK."
  echo -n "Checking fra dg: $FRADG ... " && [ `su - grid -c "$ASMCMD ls $FRADG 2> /dev/null" | grep -ic ${DBSID}` -eq 0 ] && echo "is not right, exit!" && exit 1 || echo "OK."
fi


if [ "$CHECKRESGRP" == "yes" ]; then
  echo -n "Checking db resource group: $DBSID ... " && [ `cat $CRSCTL_STAT_LOG | grep -wc "^$DBSID"` -gt 0 ] && echo "resgrp exist, exit!" && exit 1 || echo "OK."
fi


## 3. Action script check
echo "-------------------- ## 3. Action script check --------------------"
echo "---------- Checking /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgb.ksh ----------"
if [ ! -f /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgb.ksh ]; then
  ANSWER="y"
  [ "$INTERACTIVE" == "yes" ] && read "/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgb.ksh not exist, create it? (y/n): " ANSWER
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID} ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run


  if [ "$ANSWER" == "y" ]; then
    if [ -f /root/bin/act_script_template/act_rgb.ksh ]; then
      cat /root/bin/act_script_template/act_rgb.ksh | sed "s/testdb11/${DBSID}/g" > /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgb.ksh
    else
      echo "/root/bin/act_script_template/act_rgb.ksh not exist, exit!"
      exit 1
    fi
  else
    exit 1
  fi
fi


echo "---------- Checking /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_db.ksh ----------"
if [ ! -f /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_db.ksh ]; then
  ANSWER="y"
  [ "$INTERACTIVE" == "yes" ] && read -p "/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_db.ksh not exist, create it? (y/n): " ANSWER
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID} ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run


  if [ "$ANSWER" == "y" ]; then
    if [ "$DBUSER" == "os11g" ]; then
      if [ -f /root/bin/act_script_template/act_db.ksh_dr ]; then
        cat /root/bin/act_script_template/act_db.ksh_dr | sed "s/testdb11/${DBSID}/g" > /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_db.ksh
      else
        echo "/root/bin/act_script_template/act_db.ksh_dr not exist, exit!"
        exit 1
      fi
    else
      if [ -f /root/bin/act_script_template/act_db.ksh ]; then
        cat /root/bin/act_script_template/act_db.ksh | sed "s/testdb11/${DBSID}/g" > /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_db.ksh
      else
        echo "/root/bin/act_script_template/act_db.ksh not exist, exit!"
        exit 1
      fi
    fi
  else
    exit 1
  fi
fi


echo "---------- Checking /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lsnr.ksh ----------"
if [ ! -f /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lsnr.ksh ]; then
  ANSWER="y"
  [ "$INTERACTIVE" == "yes" ] && read -p "/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lsnr.ksh not exist, create it? (y/n): " ANSWER
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID} ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run


  if [ "$ANSWER" == "y" ]; then
    if [ -f /root/bin/act_script_template/act_lsnr.ksh ]; then
      if [ "$DBUSER" == "os11g" ]; then
        cat /root/bin/act_script_template/act_lsnr.ksh | sed -e "s/ORA_LISTENER_NAME=testdb11/ORA_LISTENER_NAME=stestdb11/g" -e "s/testdb11/${DBSID}/g" > /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lsnr.ksh
      else
        cat /root/bin/act_script_template/act_lsnr.ksh | sed "s/testdb11/${DBSID}/g" > /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lsnr.ksh
      fi
    else
      echo "/root/bin/act_script_template/act_lsnr.ksh not exist, exit!"
      exit 1
    fi
  else
    exit 1
  fi
fi


echo "---------- Checking /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgh.ksh ----------"
if [ ! -f /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgh.ksh ]; then
  ANSWER="y"
  [ "$INTERACTIVE" == "yes" ] && read -p "/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgh.ksh not exist, create it? (y/n): " ANSWER
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID} ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run


  if [ "$ANSWER" == "y" ]; then
    if [ -f /root/bin/act_script_template/act_rgh.ksh ]; then
      cat /root/bin/act_script_template/act_rgh.ksh | sed "s/testdb11/${DBSID}/g" > /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgh.ksh
    else
      echo "/root/bin/act_script_template/act_rgh.ksh not exist, exit!"
      exit 1
    fi
  else
    exit 1
  fi
fi


echo "---------- Checking /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lvapp.sh ----------"
if [ ! -f /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lvapp.sh ]; then
  ANSWER="y"
  [ "$INTERACTIVE" == "yes" ] && read -p "/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lvapp.sh not exist, create it? (y/n): " ANSWER
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID} ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}
  [ ! -d /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run ] && mkdir -p /dba/app/product/11.2.0/grid/crs/public/${DBSID}/.run


  if [ "$ANSWER" == "y" ]; then
    if [ -f /root/bin/act_script_template/act_lvapp.sh ]; then
      cat /root/bin/act_script_template/act_lvapp.sh | sed "s/testdb11/${DBSID}/g" > /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lvapp.sh
    else
      echo "/root/bin/act_script_template/act_lvapp.sh not exist, exit!"
      exit 1
    fi
  else
    exit 1
  fi
fi


chmod -R 755 /dba/app/product/11.2.0/grid/crs/public/${DBSID}
chown -R ${DBUSER}:oinstall /dba/app/product/11.2.0/grid/crs/public/${DBSID}
echo "----- Variables in act_rgb.ksh -----"
egrep "ORACLE_SID=|ORACLE_HOME=" /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgb.ksh
echo ""
echo "----- Variables in act_db.ksh -----"
egrep "ORACLE_SID=|ORACLE_HOME=|startup" /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_db.ksh
echo ""
echo "----- Variables in act_lsnr.ksh -----"
egrep "ORACLE_SID=|ORACLE_HOME=|ORA_LISTENER_NAME=" /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lsnr.ksh
echo ""
echo "----- Variables in act_rgh.ksh -----"
egrep "ORACLE_SID=|ORACLE_HOME=" /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgh.ksh
echo ""
echo "----- Variables in act_lvapp.sh -----"
egrep "VGNAME=|LVNAME=|MOUNTPT=" /dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lvapp.sh
echo ""
echo "----- COMMAND: ls -ld /dba/app/product/11.2.0/grid/crs/public/${DBSID} -----"
ls -ld /dba/app/product/11.2.0/grid/crs/public/${DBSID}
echo "----- COMMAND: ls -al /dba/app/product/11.2.0/grid/crs/public/${DBSID} -----"
ls -al /dba/app/product/11.2.0/grid/crs/public/${DBSID}
echo ""
ANSWER="y"
[ "$INTERACTIVE" == "yes" ] && read -p "The action scripts is right? (y/n):" ANSWER
[ "$ANSWER" == "n" ] && echo "Please manually check later, exit!" && exit 1
echo ""
[ `hostname` == "$HOST2" ] && RMTHOST="$HOST1" || RMTHOST="$HOST2"
ANSWER="y"
[ "$INTERACTIVE" == "yes" ] && read -p "Copy the /dba/app/product/11.2.0/grid/crs/public/${DBSID} directory to ${RMTHOST}? (y/n): " ANSWER
[ "$ANSWER" == "y" ] && su - $DBUSER -c "scp -r /dba/app/product/11.2.0/grid/crs/public/${DBSID} ${RMTHOST}:/dba/app/product/11.2.0/grid/crs/public/"
echo "----- ${RMTHOST}# ls -al /dba/app/product/11.2.0/grid/crs/public/${DBSID} -----"
su - $DBUSER -c "ssh ${RMTHOST} ls -al /dba/app/product/11.2.0/grid/crs/public/${DBSID}"
echo ""


## 4. Delete default resource ora..db and serverpool ora.
echo "-------------------- ## 4. Delete default resource ora..db and serverpool ora. --------------------"
if [ `grep -wc "ora.${DBSID}.db" $CRSCTL_STAT_LOG` -gt 0 ]; then
  su - $DBUSER -c "$CRSCTL delete res ora.${DBSID}.db -f; $CRSCTL delete serverpool ora.${DBSID}"
  echo "ora.${DBSID}.db and serverpool ora.${DBSID} has been deleted!"
else
  echo "ora.${DBSID}.db and serverpool ora.${DBSID} not exist!"
fi
echo ""


## 5. Create serverpool _sp
echo "-------------------- ## 5. Create serverpool _sp --------------------"
su - $DBUSER -c "$CRSCTL add serverpool ${DBSID}_sp -attr \"PARENT_POOLS=Generic, SERVER_NAMES=${HOST1} ${HOST2}\""
echo "----- COMMAND: crsctl stat serverpool ${DBSID}_sp -p -----"
crsctl stat serverpool ${DBSID}_sp -p
[ "$INTERACTIVE" == "yes" ] && read -p "-- Press Enter --" ENTER
echo ""


## 6. Create resource
echo "-------------------- ## 6. Create resource --------------------"
su - $DBUSER -c "$CRSCTL add resource ${DBSID} -type cluster_resource -attr \
\"ACTION_SCRIPT=/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgb.ksh, \
ENABLED=0, \
RESTART_ATTEMPTS=5, \
UPTIME_THRESHOLD=1h, \
FAILURE_THRESHOLD=1, \
FAILURE_INTERVAL=86400, \
CHECK_INTERVAL=10, \
OFFLINE_CHECK_INTERVAL=10, \
SCRIPT_TIMEOUT=60, \
PLACEMENT=restricted, \
SERVER_POOLS=${DBSID}_sp, \
HOSTING_MEMBERS=''\"
"
echo "----- COMMAND: crsctl stat res ${DBSID} -p -----"
crsctl stat res ${DBSID} -p
[ "$INTERACTIVE" == "yes" ] && read -p "-- Press Enter --" ENTER
echo ""


## 7. Create resource .vip
echo "-------------------- ## 7. Create resource .vip --------------------"
NETWORKRES="unknown"
ORANETLOG="/tmp/.ora_netx_network.log"


DbVipF1=`echo $DBVIP | awk -F'.' '{print $1}'`
DbVipF2=`echo $DBVIP | awk -F'.' '{print $2}'`
DbVipF3=`echo $DBVIP | awk -F'.' '{print $3}'`
DbVipF4=`echo $DBVIP | awk -F'.' '{print $4}'`


for i in `cat $CRSCTL_STAT_LOG | grep 'ora\.net.*\.network'`
do
  $CRSCTL stat res $i -p &> $ORANETLOG
  USR_ORA_IF=`cat $ORANETLOG | grep '^USR_ORA_IF=' | awk -F'=' '{print $2}'`
  USR_ORA_NETMASK=`cat $ORANETLOG | grep '^USR_ORA_NETMASK=' | awk -F'=' '{print $2}'`
  USR_ORA_SUBNET=`cat $ORANETLOG | grep '^USR_ORA_SUBNET=' | awk -F'=' '{print $2}'`


  USR_ORA_NETMASK_F1=`echo $USR_ORA_NETMASK | awk -F'.' '{print $1}'`
  USR_ORA_NETMASK_F2=`echo $USR_ORA_NETMASK | awk -F'.' '{print $2}'`
  USR_ORA_NETMASK_F3=`echo $USR_ORA_NETMASK | awk -F'.' '{print $3}'`
  USR_ORA_NETMASK_F4=`echo $USR_ORA_NETMASK | awk -F'.' '{print $4}'`


  DbVipSubnetF1=`echo "$DbVipF1 $USR_ORA_NETMASK_F1" | awk '{print and($1,$2)}'`
  DbVipSubnetF2=`echo "$DbVipF2 $USR_ORA_NETMASK_F2" | awk '{print and($1,$2)}'`
  DbVipSubnetF3=`echo "$DbVipF3 $USR_ORA_NETMASK_F3" | awk '{print and($1,$2)}'`
  DbVipSubnetF4=`echo "$DbVipF4 $USR_ORA_NETMASK_F4" | awk '{print and($1,$2)}'`
  DbVipSubnet=`echo "$DbVipSubnetF1.${DbVipSubnetF2}.${DbVipSubnetF3}.${DbVipSubnetF4}"`


  [ "$DbVipSubnet" == "$USR_ORA_SUBNET" ] && NETWORKRES=$USR_ORA_IF && break
done
[ "$NETWORKRES" == "unknown" ] && echo "No suitable ora.netx.network for DB VIP $DBVIP, exit!" && exit 1


$APPVIPCFG create \
-network=1 \
-ip=${DBVIP} \
-vipname=${DBSID}.vip \
-user=${DBUSER} \
-group=oinstall


$CRSCTL modify res ${DBSID}.vip -attr \
"DEFAULT_TEMPLATE='', \
ENABLED=0, \
RESTART_ATTEMPTS=0, \
UPTIME_THRESHOLD=1h, \
FAILURE_THRESHOLD=1, \
FAILURE_INTERVAL=86400, \
CHECK_INTERVAL=10, \
OFFLINE_CHECK_INTERVAL=10, \
CHECK_TIMEOUT=30, \
START_TIMEOUT=0, \
STOP_TIMEOUT=0, \
SCRIPT_TIMEOUT=60, \
START_DEPENDENCIES='hard(${NETWORKRES},${DBSID}) pullup(${NETWORKRES},${DBSID})', \
STOP_DEPENDENCIES='hard(${NETWORKRES},intermediate:${DBSID})', \
PLACEMENT=restricted, \
SERVER_POOLS=${DBSID}_sp, \
HOSTING_MEMBERS=''"


$CRSCTL setperm resource ${DBSID}.vip -o root
$CRSCTL setperm resource ${DBSID}.vip -u user:${DBUSER}:r-x


echo "----- COMMAND: crsctl stat res ${DBSID}.vip -p -----"
crsctl stat res ${DBSID}.vip -p
[ "$INTERACTIVE" == "yes" ] && read -p "-- Press Enter --" ENTER
echo ""


## 8. Create resource .lsnr
echo "-------------------- ## 8. Create resource .lsnr --------------------"
su - $DBUSER -c "$CRSCTL add resource ${DBSID}.lsnr -type cluster_resource -attr \
\"ACTION_SCRIPT=/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lsnr.ksh, \
ENABLED=0, \
RESTART_ATTEMPTS=0, \
UPTIME_THRESHOLD=1h, \
FAILURE_THRESHOLD=1, \
FAILURE_INTERVAL=86400, \
CHECK_INTERVAL=60, \
OFFLINE_CHECK_INTERVAL=60, \
START_TIMEOUT=180, \
STOP_TIMEOUT=0, \
SCRIPT_TIMEOUT=60, \
START_DEPENDENCIES='hard(${DBSID}.vip)pullup(${DBSID}.vip)', \
STOP_DEPENDENCIES='hard(intermediate:${DBSID}.vip)', \
PLACEMENT=restricted, \
SERVER_POOLS=${DBSID}_sp, \
HOSTING_MEMBERS=''\"
"
echo "----- COMMAND: crsctl stat res ${DBSID}.lsnr -p -----"
crsctl stat res ${DBSID}.lsnr -p
[ "$INTERACTIVE" == "yes" ] && read -p "-- Press Enter --" ENTER
echo ""


## 9. Create resource .db
echo "-------------------- ## 9. Create resource .db --------------------"
su - $DBUSER -c "$CRSCTL add resource ${DBSID}.db -type cluster_resource -attr \
\"ACTION_SCRIPT=/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_db.ksh, \
ENABLED=0, \
RESTART_ATTEMPTS=0, \
UPTIME_THRESHOLD=1h, \
FAILURE_THRESHOLD=1, \
FAILURE_INTERVAL=86400, \
CHECK_INTERVAL=60, \
OFFLINE_CHECK_INTERVAL=60, \
START_TIMEOUT=600, \
STOP_TIMEOUT=600, \
SCRIPT_TIMEOUT=60, \
START_DEPENDENCIES='hard(${DBSID},ora.${DATADG}.dg,ora.${FRADG}.dg) \
pullup(${DBSID},ora.${DATADG}.dg,ora.${FRADG}.dg)', \
STOP_DEPENDENCIES='hard(${DBSID},ora.${DATADG}.dg,ora.${FRADG}.dg)', \
PLACEMENT=restricted, \
SERVER_POOLS=${DBSID}_sp, \
HOSTING_MEMBERS=''\"
"
echo "----- COMMAND: crsctl stat res ${DBSID}.db -p -----"
crsctl stat res ${DBSID}.db -p
[ "$INTERACTIVE" == "yes" ] && read -p "-- Press Enter --" ENTER
echo ""


## 10. Create resource .head
echo "-------------------- ## 10. Create resource .head --------------------"
su - $DBUSER -c "$CRSCTL add resource ${DBSID}.head -type cluster_resource -attr \
\"ACTION_SCRIPT=/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_rgh.ksh, \
ENABLED=0, \
RESTART_ATTEMPTS=0, \
UPTIME_THRESHOLD=1h, \
FAILURE_THRESHOLD=1, \
FAILURE_INTERVAL=86400, \
CHECK_INTERVAL=10, \
OFFLINE_CHECK_INTERVAL=10, \
SCRIPT_TIMEOUT=60, \
START_DEPENDENCIES='hard(${DBSID}.lsnr,${DBSID}.db) pullup(${DBSID}.lsnr,${DBSID}.db)', \
STOP_DEPENDENCIES='hard(intermediate:${DBSID}.lsnr,${DBSID}.db)', \
PLACEMENT=restricted, \
SERVER_POOLS=${DBSID}_sp, \
HOSTING_MEMBERS=''\"
"
echo "----- COMMAND: crsctl stat res ${DBSID}.head -p -----"
crsctl stat res ${DBSID}.head -p
[ "$INTERACTIVE" == "yes" ] && read -p "-- Press Enter --" ENTER
echo ""


## 11. Create resource .lvapp
echo "-------------------- ## 11. Create resource .lvapp --------------------"
$CRSCTL add resource ${DBSID}.lvapp -type cluster_resource -attr "ACTION_SCRIPT=/dba/app/product/11.2.0/grid/crs/public/${DBSID}/act_lvapp.sh, \
ENABLED=0, \
RESTART_ATTEMPTS=0, \
UPTIME_THRESHOLD=1h, \
FAILURE_THRESHOLD=1, \
FAILURE_INTERVAL=86400, \
CHECK_INTERVAL=60, \
OFFLINE_CHECK_INTERVAL=60, \
SCRIPT_TIMEOUT=60, \
PLACEMENT=restricted, \
SERVER_POOLS=${DBSID}_sp, \
HOSTING_MEMBERS=''"


$CRSCTL setperm resource ${DBSID}.lvapp -u user:${DBUSER}:r-x
$CRSCTL setperm resource ${DBSID}.lvapp -u user:grid:r-x
$CRSCTL setperm resource ${DBSID}.lvapp -u group:oinstall:r-x


echo "----- COMMAND: crsctl stat res ${DBSID}.lvapp -p -----"
crsctl stat res ${DBSID}.lvapp -p
[ "$INTERACTIVE" == "yes" ] && read -p "-- Press Enter --" ENTER
echo ""


## 12. Complete and check
echo "-------------------- ## 12. Complete and check --------------------"
if [ "$ENABLEDRESOURCE" == "yes" ]; then
  $CRSCTL modify resource ${DBSID} -attr "ENABLED=1"
  $CRSCTL modify resource ${DBSID}.db -attr "ENABLED=1"
  $CRSCTL modify resource ${DBSID}.vip -attr "ENABLED=1"
  $CRSCTL modify resource ${DBSID}.lsnr -attr "ENABLED=1"
  $CRSCTL modify resource ${DBSID}.head -attr "ENABLED=1"
  $CRSCTL modify resource ${DBSID}.lvapp -attr "ENABLED=1"
else
  $CRSCTL modify resource ${DBSID} -attr "ENABLED=0"
  $CRSCTL modify resource ${DBSID}.db -attr "ENABLED=0"
  $CRSCTL modify resource ${DBSID}.vip -attr "ENABLED=0"
  $CRSCTL modify resource ${DBSID}.lsnr -attr "ENABLED=0"
  $CRSCTL modify resource ${DBSID}.head -attr "ENABLED=0"
  $CRSCTL modify resource ${DBSID}.lvapp -attr "ENABLED=0"
fi


$CRSCTL stat res -w "NAME st $DBSID" -t


for RES in ${DBSID} ${DBSID}.db ${DBSID}.vip ${DBSID}.lsnr ${DBSID}.head ${DBSID}.lvapp
do
  echo ""
  echo "---------- ${RES} ----------"
  crsctl stat res ${RES} -p | egrep "ACL=|ACTION_SCRIPT=|ENABLED=|START_DEPENDENCIES=|STOP_DEPENDENCIES=|USR_ORA_VIP="
done
}


########## 9. Create db app volume ##########
appvolconf ()
{
## 0. Parameters
INTERACTIVE="yes"
CHECKDBSID="no"


PVS="pvs"
VGS="vgs"
LVS="lvs"
PVCREATE="pvcreate"
VGCREATE="vgcreate"
LVCREATE="lvcreate"
VGCHANGE="/sbin/vgchange"
MULTIPATH="/sbin/multipath"
MKFSEXT3="/sbin/mkfs.ext3"
MKFSEXT4="/sbin/mkfs.ext4"


## 1. Input information
echo "-------------------- ## 1. Input information --------------------"
ANSWER="n"
while [ "$ANSWER" != "y" ]
do
  if [ $# -eq 2 ]; then
    DBSID=$1
    DBUSER=$2
  else
    read -p "Input the db sid: " DBSID
    DBUSERDEF=`cat /etc/passwd | awk -F':' '{print $1}' | grep "11g" | head -n1`
    read -p "Input the db user [$DBUSERDEF]: " DBUSER
    DBUSER=${DBUSER:=$DBUSERDEF}
  fi


  if [ -b /dev/mapper/app_${DBSID}_lun01 ]; then
    LUNNAME="app_${DBSID}_lun01"
  elif [ -b /dev/mapper/app_${DBSID}_milun01 ]; then
    LUNNAME="app_${DBSID}_milun01"
  else
    echo "app_${DBSID}_lun01 and /dev/mapper/app_${DBSID}_milun01 not exist, exit!"
    exit 1
  fi


  LUNSIZE=`$MULTIPATH -l | grep -A1 -w $LUNNAME | grep "size=" | head -n1 | sed -e 's/\[/ /g' -e 's/\]/ /g' -e 's/=/ /g' | awk '{print $2}'`
  LVSIZEUNIT=`echo $LUNSIZE | sed -e "s/[0-9]//g"` 
  LVSIZEDIGI=`echo $LUNSIZE | sed -e "s/[a-z]//g" -e "s/[A-Z]//g"`
  LVSIZEDIGI1=`expr $LVSIZEDIGI - 1`
  LVSIZE=`echo "${LVSIZEDIGI1}${LVSIZEUNIT}"`
  PVNAME="${LUNNAME}p1"
  VGNAME="VGapp_${DBSID}"
  LVNAME="LVapp_${DBSID}"
  MOUNTPT="/sf/app/${DBSID}"


  echo ""
  echo "The following app volume will be created:"
  echo "-----------------------------------------"
  echo "db sid: ${DBSID}"
  echo "db user: ${DBUSER}"
  echo "lun name: ${LUNNAME}"
  echo "lun size: ${LUNSIZE}"
  echo "pv name: ${PVNAME}"
  echo "vg name: ${VGNAME}"
  echo "lv name: ${LVNAME}"
  echo "lv size: ${LVSIZE}"
  echo "mount point: ${MOUNTPT}" 
  echo "-----------------------------------------"
  ANSWER="y"
  [ "$INTERACTIVE" == "yes" ] && read -p "Are you sure? (y/n/q): " ANSWER
  [ "$ANSWER" == "q" ] && exit 0
done
echo ""


## 2. Environment check
echo "-------------------- ## 2. Environment check --------------------"
if [ "$CHECKDBSID" == "yes" ]; then
  echo -n "Checking db sid: $DBSID ... " && [ `grep -c "^${DBSID}:" /etc/sf/shell/db_info.txt` -eq 0 ] && echo "not exist in /etc/sf/shell/db_info.txt, exit!" && exit 1 || echo "OK."
fi
echo -n "Checking db user: ${DBUSER} ... " && [ `grep -c "^${DBUSER}:" /etc/passwd` -eq 0 ] && echo "not exist, exit!" && exit 1 || echo "OK."
echo -n "Checking file system: ${MOUNTPT} ... " && [ `mount | grep -wc "${MOUNTPT}"` -gt 0 ] && echo "is exist, exit!" && exit 1 || echo "OK."
echo -n "Checking logical volume: ${LVNAME} ... " && [ `$LVS | grep -wc "${LVNAME}"` -gt 0 ] && echo "is exist, exit!" && exit 1 || echo "OK."
echo -n "Checking volume group: ${VGNAME} ... " && [ `$VGS | grep -wc "${VGNAME}"` -gt 0 ] && echo "is exist, exit!" && exit 1 || echo "OK."
echo -n "Checking physical volume: ${PVNAME} ... " && [ `$PVS | grep -c "app_${DBSID}_"` -gt 0 ] && echo "`$PVS | grep "app_${DBSID}_" | awk '{print $1}'` is exist, exit!" && exit 1 || echo "OK."
echo ""
[ "$INTERACTIVE" == "yes" ] && read -p "-- Enter --" ANSWER


## 3. Create PV
echo "-------------------- ## 3. Create PV --------------------"
if [ ! -b /dev/mapper/${LUNNAME}p1 ]; then
  echo "---------- Creating /dev/mapper/${LUNNAME}p1 ----------"
  dd if=/dev/zero of=/dev/mapper/${LUNNAME} bs=512 count=1 oflag=direct
  printf "n\np\n1\n\n\nw\n" | fdisk /dev/mapper/${LUNNAME}
  partprobe
  kpartx -d /dev/mapper/${LUNNAME}
  kpartx -a /dev/mapper/${LUNNAME}
  ls -l /dev/mapper/${LUNNAME}*
fi
echo ""


echo "---------- COMMAND: ls -l /dev/mapper/${LUNNAME}* ----------"
ls -l /dev/mapper/${LUNNAME}*
echo ""


echo "---------- COMMAND: pvcreate /dev/mapper/${LUNNAME}p1 ----------"
if [ -b /dev/mapper/${LUNNAME}p1 ]; then
  $PVCREATE /dev/mapper/${LUNNAME}p1 || exit 1
else
  echo "/dev/mapper/${LUNNAME}p1 not exist, exit!"
  exit 1
fi
echo "---------- COMMAND: pvs /dev/mapper/${LUNNAME}p1 ----------"
$PVS /dev/mapper/${LUNNAME}p1
echo ""
[ "$INTERACTIVE" == "yes" ] && read -p "-- Enter --" ANSWER


## 4. Create VG
echo "-------------------- ## 4. Create VG --------------------"
echo "---------- COMMAND: vgcreate $VGNAME /dev/mapper/${LUNNAME}p1 ----------"
$VGCREATE $VGNAME /dev/mapper/${LUNNAME}p1 || exit 1
echo "---------- COMMAND: vgs ${VGNAME} ----------"
$VGS ${VGNAME}
echo ""
[ "$INTERACTIVE" == "yes" ] && read -p "-- Enter --" ANSWER


## 5. Create LV
echo "-------------------- ## 5. Create LV --------------------"
HNAME=`hostname`
echo "---------- COMMAND: vgchange --addtag $HNAME $VGNAME ----------"
$VGCHANGE --addtag $HNAME $VGNAME || exit 1
echo ""


echo "---------- COMMAND: vgs -o vg_tags $VGNAME ----------"
$VGS -o vg_tags $VGNAME
echo ""


echo "---------- COMMAND: vgchange -ay $VGNAME ----------"
$VGCHANGE -ay $VGNAME || exit 1
echo ""


echo "---------- COMMAND: lvcreate -L $LVSIZE -n $LVNAME $VGNAME ----------"
$LVCREATE -L $LVSIZE -n $LVNAME $VGNAME || exit 1


echo "---------- COMMAND: lvs /dev/${VGNAME}/${LVNAME} ----------"
$LVS /dev/${VGNAME}/${LVNAME}


echo ""
[ "$INTERACTIVE" == "yes" ] && read -p "-- Enter --" ANSWER


## 6. Create ext3/4 filesystem
echo "-------------------- ## 6. Create ext3/4 filesystem --------------------"
if [ `uname -r | egrep -c '2.6.32|2.6.39'` -gt 0 ]; then
  echo "---------- COMMAND: mkfs.ext4 /dev/$VGNAME/$LVNAME ----------"
  $MKFSEXT4 /dev/$VGNAME/$LVNAME || exit 1
else
  echo "---------- COMMAND: mkfs.ext3 /dev/$VGNAME/$LVNAME ----------"
  $MKFSEXT3 /dev/$VGNAME/$LVNAME || exit 1
  echo ""
fi
[ "$INTERACTIVE" == "yes" ] && read -p "-- Enter --" ANSWER


## 7. Create mount point and mount
echo "-------------------- ## 7. Create mount point and mount --------------------"
echo "---------- COMMAND: mkdir -p $MOUNTPT ----------"
mkdir -p $MOUNTPT
ls -ld $MOUNTPT
echo ""


echo "---------- COMMAND: mount /dev/$VGNAME/$LVNAME $MOUNTPT ----------"
mount /dev/$VGNAME/$LVNAME $MOUNTPT
echo ""


echo "---------- COMMAND: df -h $MOUNTPT ----------"
df -h $MOUNTPT
echo ""
[ "$INTERACTIVE" == "yes" ] && read -p "-- Enter --" ANSWER


## 8. Create goldengate and quest subdirectory
echo "-------------------- ## 8. Create goldengate and quest subdirectory --------------------"
if [ `mount | grep -wc "${MOUNTPT}"` -gt 0 ]; then
  echo -n "Creating ${MOUNTPT}/goldengate and ${MOUNTPT}/quest directory..."
  mkdir ${MOUNTPT}/goldengate
  mkdir ${MOUNTPT}/quest
  chmod 755 ${MOUNTPT}/goldengate
  chmod 755 ${MOUNTPT}/quest
  chown ${DBUSER}:oinstall ${MOUNTPT}/goldengate
  chown ${DBUSER}:oinstall ${MOUNTPT}/quest
  echo "done."
  echo "---------- COMMAND: ls -l $MOUNTPT ----------"
  ls -l $MOUNTPT
else
  echo "${MOUNTPT} filesystem not exist, exit!"
  exit 1
fi
echo ""
[ "$INTERACTIVE" == "yes" ] && read -p "-- Enter --" ANSWER


## 9. Run the following commands in other nodes
echo "-------------------- ## 9. Run the following commands in other nodes --------------------"
echo "Run the following commands in other nodes in the cluster:"
echo "---------------------------------------------------------"
echo "partprobe"
echo "kpartx -d /dev/mapper/${LUNNAME}"
echo "kpartx -a /dev/mapper/${LUNNAME}"
echo "ls -l /dev/mapper/${LUNNAME}*"
echo "pvscan"
echo "mkdir -p $MOUNTPT"
echo "lvs | grep -w $LVNAME"
echo "---------------------------------------------------------"
[ "$INTERACTIVE" == "yes" ] && read -p "-- Enter --" ANSWER
}


########## 10. Add db vip manual ##########
dbvipconf ()
{
## 0. Parameters
INTERACTIVE="yes"


NSLOOKUP="/usr/bin/nslookup"
PINGLOG="/tmp/.ping.log"
IFCONFIGLOG="/tmp/.ifconfig.log"


## 1. Input information
echo "-------------------- ## 1. Input information --------------------"
ANSWER="n"
while [ "$ANSWER" != "y" ]
do
  if [ $# -eq 2 ]; then
    DBVIP=$1
    DBVIPIF=$2
  else
    read -p "Input the db vip: " DBVIP
    DBVIPIFDEF="bond0"
    BOND0NET=`/sbin/ip route | grep -w "$DBVIPIFDEF" | grep "proto kernel" | head -n1 | awk '{print $1}'`
    read -p "Input the db vip will be added to [${DBVIPIFDEF}($BOND0NET)]: " DBVIPIF
    DBVIPIF=${DBVIPIF:=$DBVIPIFDEF}
  fi


  if [ `/sbin/ifconfig | grep -wc "$DBVIPIF"` -gt 0 ]; then
    IFNETWORK=`/sbin/ip route | grep -w "$DBVIPIF" | grep "proto kernel" | head -n1 | awk '{print $1}'`
    IFNETMASK=`/sbin/ifconfig "$DBVIPIF" | grep "Mask:" | awk -F':' '{print $NF}'`
  else
    echo "Network interface $DBVIPIF not exist, exit!"
    exit 1
  fi


  echo ""
  echo "The following infomation:"
  echo "-------------------------"
  echo "db vip: ${DBVIP}"
  echo "added to: ${DBVIPIF}"
  echo "network: ${IFNETWORK}"
  echo "netmask: ${IFNETMASK}"
  echo "-------------------------"
  ANSWER="y"
  [ "$INTERACTIVE" == "yes" ] && read -p "Are you sure? (y/n/q): " ANSWER
  [ "$ANSWER" == "q" ] && exit 0
done
echo ""


## 2. Environment check
echo "-------------------- ## 2. Environment check --------------------"
> $PINGLOG
echo "---------- COMMAND: ping -c4 $DBVIP ----------"
ping -c4 $DBVIP | tee -a $PINGLOG
echo ""
echo -n "Checking db vip: $DBVIP ... " && [ `grep -c "100% packet loss" $PINGLOG` -eq 0 ] && echo "is active, exit!" && exit 1 || echo "OK."
echo ""


echo "---------- COMMAND: nslookup $DBVIP ----------"
$NSLOOKUP $DBVIP
echo ""
if [ "$INTERACTIVE" == "yes" ]; then
  ANSWER="n"
  read -p "The db vip's domain name is right? (y/n): " ANSWER
  [ "$ANSWER" != "y" ] && ANSWER="n" && read -p "Do you want to continue? (y/n): " ANSWER
  [ "$ANSWER" != "y" ] && exit 1
fi
echo ""


## 3. Add db vip and check
echo "-------------------- ## 3. Add db vip and check --------------------"
/sbin/ifconfig > $IFCONFIGLOG
STARTNUM="60"
while [ `grep -wc "${DBVIPIF}:${STARTNUM}" ${IFCONFIGLOG}` -gt 0 ]
do
  STARTNUM=`expr $STARTNUM + 1`
done


echo "---------- COMMAND: ifconfig | grep -w \"${DBVIPIF}:${STARTNUM}\" ----------"
/sbin/ifconfig | grep -w "${DBVIPIF}:${STARTNUM}"
[ `/sbin/ifconfig | grep -wc "${DBVIPIF}:${STARTNUM}"` -gt 0 ] && exit 1
echo ""


echo "---------- Will run the following commands to add vip ----------"
echo "ifconfig ${DBVIPIF}:${STARTNUM} ${DBVIP} netmask ${IFNETMASK}"
echo "arping -A -b -c 3 -I ${DBVIPIF} ${DBVIP}"
echo ""
if [ "$INTERACTIVE" == "yes" ]; then
  ANSWER="n"
  read -p "Do you want to continue? (y/n): " ANSWER
  [ "$ANSWER" != "y" ] && exit 1
  /sbin/ifconfig ${DBVIPIF}:${STARTNUM} ${DBVIP} netmask ${IFNETMASK}
  /sbin/arping -A -b -c 3 -I ${DBVIPIF} ${DBVIP}
fi
echo ""


echo "---------- COMMAND: ifconfig ${DBVIPIF}:${STARTNUM} ----------"
sleep 1
/sbin/ifconfig ${DBVIPIF}:${STARTNUM}
echo ""


echo "---------- COMMAND: ping -c4 $DBVIP ----------"
sleep 1
ping -c4 $DBVIP
echo ""
}


########## 11. Get hardware information ##########
hwinfo ()
{
echo "-------------------- Server Info --------------------"
dmidecode -t 1 | egrep "Manufacturer:|Product Name:|Serial Number:|Family:" | sed "s/\t//g"


echo "-------------------- Bios Info --------------------"
dmidecode -t 0 | egrep "Version:|Release Date:" | sed -e "s/\t//g"


echo "-------------------- CPU Info --------------------"
CpuModel=`cat /proc/cpuinfo | egrep "^model name" | sort -u | head -n1 | awk -F: '{print $2}' | awk '{for(i=1;i<=NF;i++)printf $i" "}'`
PhyCpus=`dmidecode -t 4 | grep -c "Processor Information"`
CoresPerCpu=`dmidecode -t 4 | grep "Core Count:" | head -n1 | awk '{print $NF}'`
[ -z "$CoresPerCpu" ] && CoresPerCpu=`cat /proc/cpuinfo | grep "cpu cores" | head -n1 | awk '{print $NF}'`
RunningThreads=`cat /proc/cpuinfo | grep -c "^processor"`
ThreadsPerCpu=`dmidecode -t 4 | grep "Thread Count:" | head -n1 | awk '{print $NF}'`
[ -z "$ThreadsPerCpu" ] && ThreadsPerCpu=`expr $RunningThreads \/ $PhyCpus`
TotalCores=`expr $PhyCpus \* $CoresPerCpu`
TotalThreads=`expr $PhyCpus \* $ThreadsPerCpu`


echo "CPU Model: $CpuModel"
echo "Physical CPU: $PhyCpus"
echo "Cores Per CPU: $CoresPerCpu"
echo "Threads Per CPU: $ThreadsPerCpu"
echo "Total Cores: $TotalCores"
echo "Total Threads: $TotalThreads"
echo "Running Threads: $RunningThreads"
[ "$ThreadsPerCpu" -gt "$CoresPerCpu" -a "$TotalThreads" -gt "$RunningThreads" ] && echo "Warning: HT is not open!"


echo "-------------------- Memory Info --------------------"
dmidecode -t 17 | egrep -w "MB" | uniq -c | awk '{print $2,$3,$4" * "$1" Cards"}'


echo "-------------------- Ethernet Controller Info --------------------"
lspci | grep Eth


echo "-------------------- Fibre Channel HBA Info --------------------"
lspci | grep Fib
}


########## 12. Get OS information ##########
osinfo ()
{
echo "-------------------- COMMAND: uname -a --------------------"
uname -a
echo "-------------------- COMMAND: uptime --------------------"
uptime
echo "-------------------- COMMAND: last | grep boot --------------------"
last | grep boot | head -n3
echo "-------------------- COMMAND: ifconfig --------------------"
ifconfig
echo "-------------------- COMMAND: ip route --------------------"
ip route
echo "-------------------- COMMAND: route -n --------------------"
route -n
echo "-------------------- COMMAND: df -hT --------------------"
df -hT
echo "-------------------- COMMAND: free -g --------------------"
free -g
echo "-------------------- COMMAND: ps aux | grep -v '^root' --------------------"
ps aux | egrep -v "grep|^root"
}


########## 13. Get HW and OS information ##########
sysinfo ()
{
SYSINFOLOG=/tmp/sysinfo`date +%Y%m%d%H%M%S`.log
> $SYSINFOLOG
hwinfo | tee -a $SYSINFOLOG
osinfo | tee -a $SYSINFOLOG
echo
echo "-- Save In: $SYSINFOLOG --"
}


########## 14. Get information for rsms ##########
conninfo ()
{
## 1. Global parameters.
HostName=`hostname`
ConnInfoLog=/tmp/conn_info_`date +%Y%m%d%H%M%S`.log
> $ConnInfoLog


## 2. Get ethernet information. 
IfconfigLog=/tmp/.ifconfig_a.log
ProcBondLog=/tmp/.proc_bond.log
IpRouteLog=/tmp/.ip_route.log
ConnEthLog1=/tmp/.rsms_eth_temp.log
ConnEthLog2=/tmp/rsms_eth_`date +%Y%m%d%H%M%S`.log
> $ConnEthLog1
> $ConnEthLog2


/sbin/ifconfig -a > $IfconfigLog
cat /proc/net/bonding/bond* 2> /dev/null | egrep "Slave Interface:|HW addr:" > $ProcBondLog


cat $IfconfigLog | grep "^eth" | while read LINE
do
  EthName=`echo $LINE | awk '{print $1}'`


  if [ `cat $ProcBondLog | grep -wc $EthName` -gt 0 ]; then
    EthMac=`cat $ProcBondLog | grep -A1 -w $EthName | grep "HW addr:" | awk '{print $NF}' | tr 'a-z' 'A-Z'`
    for BOND in `ls /proc/net/bonding/`
    do
      [ `cat /proc/net/bonding/$BOND | grep -wc "$EthName"` -gt 0 ] && EthBond="$BOND" && break
    done
    EthBondLan="${EthBond}-`/sbin/ip route | grep "proto kernel" | grep -w "$EthBond" | head -n1 | awk '{print $1}'`"
  else
    EthMac=`echo $LINE | awk '{print $NF}' | tr 'a-z' 'A-Z'`
    EthBond="Unknown"
    EthBondLan="Unknown"
  fi


  if [ `/sbin/ethtool $EthName | grep -c "1000base"` -gt 0 ]; then
    EthSpeedMax="1000"
  elif [ `/sbin/ethtool $EthName | grep -c "100base"` -gt 0 ]; then
    EthSpeedMax="100"
  else
    EthSpeedMax="0"
  fi


  if [ `/sbin/ethtool $EthName | grep "Speed:" | grep -c "Mb"` -gt 0 ]; then
    EthSpeedRun=`/sbin/ethtool $EthName | grep "Speed:" | awk '{print $NF}' | awk -F'Mb' '{print $1}'`
    EthConnectStatus="Connected"
  else
    EthSpeedRun="0"
    EthConnectStatus="Unknown"
  fi


  echo "$HostName $EthName $EthMac $EthSpeedMax $EthSpeedRun $EthConnectStatus $EthBondLan" | awk '{printf "%-15s%-15s%-30s%-15s%-15s%-15s%-30s\n",$1,$2,$3,$4,$5,$6,$7}' >> $ConnEthLog1
done


echo "Host_Name Eth_Name Mac_Addr Max_Speed_Mb Run_Speed_Mb Conn_Status Bond_Lan" | awk '{printf "%-15s%-15s%-30s%-15s%-15s%-15s%-30s\n",$1,$2,$3,$4,$5,$6,$7}' > $ConnEthLog2
echo "--------- -------- -------- ------------ ------------ ----------- --------" | awk '{printf "%-15s%-15s%-30s%-15s%-15s%-15s%-30s\n",$1,$2,$3,$4,$5,$6,$7}' >> $ConnEthLog2
cat $ConnEthLog1 | grep "Connected" | sort -k7 >> $ConnEthLog2
cat $ConnEthLog1 | grep -v "Connected" >> $ConnEthLog2
cat $ConnEthLog2 >> $ConnInfoLog
echo "" >> $ConnInfoLog


## 3. Get fibre channel information. 
SystoolLog=/tmp/.systool.log
MultipathHbaLog=/tmp/.multipath_hba.log
LsscsiLog=/tmp/.lsscsi.log
ConnHbaLog1=/tmp/.rsms_hba_temp.log
ConnHbaLog2=/tmp/rsms_hba_`date +%Y%m%d%H%M%S`.log
> $ConnHbaLog1
> $ConnHbaLog2


/sbin/multipath -l | grep undef | awk -F':' '{print $1}' | awk '{print "host"$NF}' | sort -u &> $MultipathHbaLog
[ -x /usr/bin/lsscsi ] && lsscsi > $LsscsiLog


[ ! -d /sys/class/fc_host/ ] && echo "No hostx found in /sys/class/fc_host/, exit!" && exit 1
for i in `ls /sys/class/fc_host/`
do
  /usr/bin/systool -c fc_host -d $i -v &> $SystoolLog
  HbaPciId=`cat $SystoolLog | grep "Device path" | grep -v "Class Device path" | grep "pci0000" | head -n1 | awk -F'/' '{print $(NF-1)}' | sed 's/0000://g'`
  HbaName="hba-$HbaPciId"


  HbaWwpnString=`cat $SystoolLog | grep "port_name" | head -n1 | awk '{print $NF}' | sed -e 's/"//g' -e 's/^0x//g' | tr 'a-z' 'A-Z'`
  HbaWwpn1to2=`echo $HbaWwpnString | cut -c1-2`
  HbaWwpn3to4=`echo $HbaWwpnString | cut -c3-4`
  HbaWwpn5to6=`echo $HbaWwpnString | cut -c5-6`
  HbaWwpn7to8=`echo $HbaWwpnString | cut -c7-8`
  HbaWwpn9to10=`echo $HbaWwpnString | cut -c9-10`
  HbaWwpn11to12=`echo $HbaWwpnString | cut -c11-12`
  HbaWwpn13to14=`echo $HbaWwpnString | cut -c13-14`
  HbaWwpn15to16=`echo $HbaWwpnString | cut -c15-16`
  HbaWwpn="${HbaWwpn1to2}:${HbaWwpn3to4}:${HbaWwpn5to6}:${HbaWwpn7to8}:${HbaWwpn9to10}:${HbaWwpn11to12}:${HbaWwpn13to14}:${HbaWwpn15to16}"


  if [ `cat $SystoolLog | grep "supported_speeds" | grep -c "8 Gbit"` -gt 0 ]; then
    HbaSpeedMax="8"
  elif [ `cat $SystoolLog | grep "supported_speeds" | grep -c "4 Gbit"` -gt 0 ]; then
    HbaSpeedMax="4"
  elif [ `cat $SystoolLog | grep "supported_speeds" | grep -c "2 Gbit"` -gt 0 ]; then
    HbaSpeedMax="2"
  elif [ `cat $SystoolLog | grep "supported_speeds" | grep -c "1 Gbit"` -gt 0 ]; then
    HbaSpeedMax="1"
  elif [ `/sbin/lspci | grep Fibre | grep "$HbaPciId" | grep -c "8Gb"` -gt 0 ]; then
    HbaSpeedMax="8"
  elif [ `/sbin/lspci | grep Fibre | grep "$HbaPciId" | grep -c "4Gb"` -gt 0 ]; then
    HbaSpeedMax="4"
  elif [ `/sbin/lspci | grep Fibre | grep "$HbaPciId" | grep -c "2Gb"` -gt 0 ]; then
    HbaSpeedMax="2"
  elif [ `/sbin/lspci | grep Fibre | grep "$HbaPciId" | grep -c "1Gb"` -gt 0 ]; then
    HbaSpeedMax="1"
  else
    HbaSpeedMax="0"
  fi


  if [ `cat $SystoolLog | grep -w "speed" | grep -c "8 Gbit"` -gt 0 ]; then
    HbaSpeedRun="8"
    HbaConnectStatus="Connected"
  elif [ `cat $SystoolLog | grep -w "speed" | grep -c "4 Gbit"` -gt 0 ]; then
    HbaSpeedRun="4"
    HbaConnectStatus="Connected"
  elif [ `cat $SystoolLog | grep -w "speed" | grep -c "2 Gbit"` -gt 0 ]; then
    HbaSpeedRun="2"
    HbaConnectStatus="Connected"
  elif [ `cat $SystoolLog | grep -w "speed" | grep -c "1 Gbit"` -gt 0 ]; then
    HbaSpeedRun="1"
    HbaConnectStatus="Connected"
  else
    HbaSpeedRun="0"
    HbaConnectStatus="Unknown"
  fi
#----------------------------------------
  if [ "$HbaConnectStatus" == "Connected" ]; then
    if [ -x /usr/bin/lsscsi ]; then
      HbaHostNum=`echo "$i" | sed 's/host//g'`
      if [ `cat $LsscsiLog | grep "\[${HbaHostNum}:" | grep -wc 'disk'` -gt 0 ]; then
        HbaConnectSan="StorageSan"
      elif [ `cat $LsscsiLog | grep "\[${HbaHostNum}:" | grep -wc 'tape'` -gt 0 ]; then
        HbaConnectSan="BackupSan"
      else
        HbaConnectSan="Unknown"
      fi
    else
      if [ `cat $MultipathHbaLog | grep -c "host"` -gt 0 ]; then
        [ `cat $MultipathHbaLog | grep -wc "$i"` -gt 0 ] && HbaConnectSan="StorageSan" || HbaConnectSan="BackupSan"
      else
        HbaConnectSan="Unknown"
      fi
    fi
  else
    HbaConnectSan="Unknown"
  fi


  [ `ls -l /sys/class/scsi_host/${i}/ | grep -c 'model'` -gt 0 ] && HbaModel=`cat /sys/class/scsi_host/${i}/*model* | egrep -vi "PCI|HBA" | head -n1` || HbaModel="Unknown"
  [ -z "$HbaModel" ] && HbaModel="Unknown"


  HbaHostName="$i"


  echo "$HostName $HbaName $HbaWwpn $HbaSpeedMax $HbaSpeedRun $HbaConnectStatus $HbaConnectSan $HbaModel $HbaHostName" | awk '{printf "%-15s%-15s%-30s%-15s%-15s%-15s%-15s%-15s%-15s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9}' >> $ConnHbaLog1
done


echo "Host_Name Hba_Name Hba_Wwpn Max_Speed_Gb Run_Speed_Gb Conn_Status Conn_San Hba_Model Hba_Host" | awk '{printf "%-15s%-15s%-30s%-15s%-15s%-15s%-15s%-15s%-15s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9}' > $ConnHbaLog2
echo "--------- -------- -------- ------------ ------------ ----------- -------- --------- --------" | awk '{printf "%-15s%-15s%-30s%-15s%-15s%-15s%-15s%-15s%-15s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9}' >> $ConnHbaLog2
cat $ConnHbaLog1 | grep "Connected" | grep "StorageSan" | sort -k7 -k2 >> $ConnHbaLog2
cat $ConnHbaLog1 | grep "Connected" | grep -v "StorageSan" | sort -k7 -k2 >> $ConnHbaLog2
cat $ConnHbaLog1 | grep -v "Connected" >> $ConnHbaLog2
cat $ConnHbaLog2 >> $ConnInfoLog
echo "" >> $ConnInfoLog
cat $ConnInfoLog
echo "-- Save In: $ConnInfoLog --"
}


########## 15. Get db cpu information ##########
dbcpuinfo ()
{
## Parameters.
DbUserSidLog="/tmp/.dbusersid.log"
TopBcTempLog="/tmp/.top_bc_temp.log"
TopBcLog="/tmp/.top_bc.log"
MpstatLog="/tmp/.mpstat.log"
ResultLog1="/tmp/.db_cpu_result1.log"
ResultLog2="/tmp/dbcpuinfo`date +%Y%m%d%H%M%S`.log"
> $ResultLog1
> $ResultLog2


## Main.
mpstat 1 1 &> $MpstatLog
top -b -c -n2 &> $TopBcTempLog
StartLine=`cat -n $TopBcTempLog | grep "load average:" | tail -n1 | awk '{print $1}'`
cat $TopBcTempLog | sed -n "${StartLine},\$p" &> $TopBcLog


[ `cat $TopBcLog | grep "_pmon_" | egrep -v "grep|ASM" | wc -l` -eq 0 ] && echo "No DB running on the server, exit!" && exit 1
cat $TopBcLog | grep "_pmon_" | egrep -v "grep|ASM" | awk '{print $2,$NF}' | sed 's/ora_pmon_//g' &> $DbUserSidLog


OsCpuUserRate=`cat $MpstatLog | grep -w "Average:" | awk '{print $3}'`
OsCpuSysRate=`cat $MpstatLog | grep -w "Average:" | awk '{print $5}'`
OsCpuIowaitRate=`cat $MpstatLog | grep -w "Average:" | awk '{print $6}'`
[ `uname -r | egrep -c '2.6.32|2.6.39'` -gt 0 ] && OsCpuIdleRate=`cat $MpstatLog | grep -w "Average:" | awk '{print $11}'` || OsCpuIdleRate=`cat $MpstatLog | grep -w "Average:" | awk '{print $10}'`
SysCpuNumber=`cat /proc/cpuinfo | grep -c "^processor"`
SysCpuValue=`echo "$SysCpuNumber" | awk '{print $1 * 100}'`


cat $DbUserSidLog | while read Line
do
  DbUser=`echo $Line | awk '{print $1}'`
  DbSid=`echo $Line | awk '{print $2}'`
  DbCpuSumValue=`cat $TopBcLog | egrep -w "ora.*_${DbSid}|oracle${DbSid}" | awk 'BEGIN{sum=0}{sum+=$9}END{print sum}'`
  DbCpuRate=`echo "$DbCpuSumValue $SysCpuValue" | awk '{print $1 * 100 / $2}'`


  echo "${DbUser} ${DbSid} ${DbCpuRate} ${OsCpuUserRate} ${OsCpuSysRate} ${OsCpuIowaitRate} ${OsCpuIdleRate}" | awk '{printf "%-20s%-20s%-20.2f%-20s%-20s%-20s%-20s\n",$1,$2,$3,$4,$5,$6,$7,$8}' >> $ResultLog1
done


echo "DB_USER DB_SID DB_CPU_USE% OS_USER_CPU_USE% OS_SYS_CPU_USE% OS_IOWAIT_CPU_USE% OS_IDLE_CPU_FREE%" | awk '{printf "%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",$1,$2,$3,$4,$5,$6,$7,$8}' > $ResultLog2
echo "------- ------ ----------- ---------------- --------------- ------------------ -----------------" | awk '{printf "%-20s%-20s%-20s%-20s%-20s%-20s%-20s\n",$1,$2,$3,$4,$5,$6,$7,$8}' >> $ResultLog2
cat $ResultLog1 | sort -k3 -nr >> $ResultLog2
cat $ResultLog2
echo
echo "-- Save In: $ResultLog2 --"
}


########## 16. Get db io information ##########
dbioinfo ()
{
## Parameters.
DbUserSidLog="/tmp/.dbusersid.log"
IotopTempLog="/tmp/.iotop_temp.log"
IotopLog="/tmp/.iotop.log"
ResultLog1="/tmp/.db_io_result1.log"
ResultLog2="/tmp/dbioinfo`date +%Y%m%d%H%M%S`.log"
> $ResultLog1
> $ResultLog2


[ ! -x /usr/bin/iotop ] && echo "Command iotop is not installed, exit!" && exit 1
iotop -k -b -n2 &> $IotopTempLog
StartLine=`cat -n $IotopTempLog | grep "Total DISK READ:" | tail -n1 | awk '{print $1}'`
cat $IotopTempLog | sed -n "${StartLine},\$p" &> $IotopLog
[ `cat $IotopLog | grep "_pmon_" | egrep -v "grep|ASM" | wc -l` -eq 0 ] && echo "No DB running on the server, exit!" && exit 1
cat $IotopLog | grep "_pmon_" | egrep -v "grep|ASM" | awk '{print $3,$NF}' | sed 's/ora_pmon_//g' &> $DbUserSidLog


OsTotalDiskReadMByte=`cat $IotopLog | grep "Total DISK READ:" | head -n1 | awk '{print $4}' | awk '{print $1 / 1024}'`
OsTotalDiskWriteMByte=`cat $IotopLog | grep "Total DISK READ:" | head -n1 | awk '{print $10}' | awk '{print $1 / 1024}'`


cat $DbUserSidLog | while read Line
do
  DbUser=`echo $Line | awk '{print $1}'`
  DbSid=`echo $Line | awk '{print $2}'`
  DbDiskReadSumKByte=`cat $IotopLog | egrep -w "ora.*_${DbSid}|oracle${DbSid}" | awk 'BEGIN{sum=0}{sum+=$4}END{print sum}'`
  DbDiskWriteSumKByte=`cat $IotopLog | egrep -w "ora.*_${DbSid}|oracle${DbSid}" | awk 'BEGIN{sum=0}{sum+=$6}END{print sum}'`
  DbDiskReadSumMByte=`echo "$DbDiskReadSumKByte" | awk '{print $1 / 1024}'`
  DbDiskWriteSumMByte=`echo "$DbDiskWriteSumKByte" | awk '{print $1 / 1024}'`


  echo "${DbUser} ${DbSid} ${DbDiskReadSumMByte} ${DbDiskWriteSumMByte} ${OsTotalDiskReadMByte} ${OsTotalDiskWriteMByte}" | awk '{printf "%-20s%-20s%-20.2f%-20.2f%-25.2f%-25.2f\n",$1,$2,$3,$4,$5,$6}' >> $ResultLog1
done


echo "DB_USER DB_SID DB_DISK_READ_MB DB_DISK_WRITE_MB OS_TOTAL_DISK_READ_MB OS_TOTAL_DISK_WRITE_MB" | awk '{printf "%-20s%-20s%-20s%-20s%-25s%-25s\n",$1,$2,$3,$4,$5,$6}' > $ResultLog2
echo "------- ------ --------------- ---------------- --------------------- ----------------------" | awk '{printf "%-20s%-20s%-20s%-20s%-25s%-25s\n",$1,$2,$3,$4,$5,$6}' >> $ResultLog2
cat $ResultLog1 | sort -k3 -nr >> $ResultLog2
cat $ResultLog2
echo
echo "-- Save In: $ResultLog2 --"
}


########## 17. Get db mem information ##########
dbmeminfo ()
{
## Parameters.
PsAuxwwLog="/tmp/.psauxww.log"
DbUserSidLog="/tmp/.dbusersid.log"
DbMemInfoTempLog="/tmp/.dbmeminfo_temp.log"
DbMemInfoLog="/tmp/dbmeminfo`date +%Y%m%d%H%M%S`.log"
> $DbMemInfoTempLog
> $DbMemInfoLog


## Main.
[ ! -f /etc/sf/shell/db_info.txt ] && echo "/etc/sf/shell/db_info.txt not exist, exit!" && exit 1
ps auxww &> $PsAuxwwLog
[ `cat $PsAuxwwLog | grep "_pmon_" | egrep -v "grep|ASM" | wc -l` -eq 0 ] && echo "No DB running on the server, exit!" && exit 1
cat $PsAuxwwLog | grep "_pmon_" | egrep -v "grep|ASM" | awk -F'[ |_]' '{print $1,$NF}' &> $DbUserSidLog


for i in `cat $DbUserSidLog | awk '{print $2}'`
do
  DbSid="$i"
  DbUser=`cat $DbUserSidLog | grep -w "$DbSid" | head -n1 | awk '{print $1}'`
  OraHome=`awk -F: '{if ($1 == "'$DbSid'") {print $2; exit}}' /etc/sf/shell/db_info.txt | tail -n1`
  SqlScript="/tmp/.script_${DbSid}.sh"
  SqlOutput="/tmp/.sql_${DbSid}.log"


  cat > $SqlScript < #!/bin/bash
export ORACLE_SID=$DbSid
export ORACLE_HOME=$OraHome
$OraHome/bin/sqlplus -S / as sysdba < set linesize 300
whenever sqlerror exit failure
show parameter memory
show parameter sga
show parameter pga
show parameter db_cache_size
SELECT NAME, PHYSICAL_READS, DB_BLOCK_GETS, CONSISTENT_GETS, 100 - (PHYSICAL_READS * 100 / (DB_BLOCK_GETS + CONSISTENT_GETS)) "Hit Ratio" FROM V\\\$BUFFER_POOL_STATISTICS;
SELECT * FROM V\\\$PGASTAT WHERE NAME='cache hit percentage';
exit
EOF1
EOF
  if [ `cat /etc/sf/shell/db_info.txt | grep -c "^${DbSid}:"` -gt 0 ]; then
    su - $DbUser -c "bash $SqlScript" &> $SqlOutput
    MemoryMaxTarget=`cat $SqlOutput | grep -w "memory_max_target" | awk '{print $NF}'`
    MemoryTarget=`cat $SqlOutput | grep -w "memory_target" | awk '{print $NF}'`
    SgaMaxSize=`cat $SqlOutput | grep -w "sga_max_size" | awk '{print $NF}'`
    SgaSize=`cat $SqlOutput | grep -w "sga_target" | awk '{print $NF}'`
    PgaAggregateTarget=`cat $SqlOutput | grep -w "pga_aggregate_target" | awk '{print $NF}'`
    DbCacheSize=`cat $SqlOutput | grep -w "db_cache_size" | awk '{print $NF}'`
    DbCacheHitRatio=`cat $SqlOutput | grep -A2 -w "PHYSICAL_READS" | grep -w "DEFAULT" | awk '{print $NF}'`
    PgaHitPercentage=`cat $SqlOutput | grep -w "cache hit percentage" | awk '{print $(NF-1)}'`
    echo "$DbUser $DbSid $MemoryMaxTarget $MemoryTarget $SgaMaxSize $SgaSize $PgaAggregateTarget $DbCacheSize $DbCacheHitRatio $PgaHitPercentage" | awk '{printf "%-10s%-12s%-19s%-15s%-14s%-12s%-22s%-15s%-15.2f%-9.2f\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10}' >> $DbMemInfoTempLog
  else
    MemoryMaxTarget="unknown"
    MemoryTarget="unknown"
    SgaMaxSize="unknown"
    SgaSize="unknown"
    PgaAggregateTarget="unknown"
    DbCacheSize="unknown"
    DbCacheHitRatio="unknown"
    PgaHitPercentage="unknown"
    echo "$DbUser $DbSid $MemoryMaxTarget $MemoryTarget $SgaMaxSize $SgaSize $PgaAggregateTarget $DbCacheSize $DbCacheHitRatio $PgaHitPercentage" | awk '{printf "%-10s%-12s%-19s%-15s%-14s%-12s%-22s%-15s%-15s%-9s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10}' >> $DbMemInfoTempLog
  fi
done


echo "DB_USER DB_SID memory_max_target memory_target sga_max_size sga_target pga_aggregate_target db_cache_size DB_CACHE_HIT% PGA_HIT%" | awk '{printf "%-10s%-12s%-19s%-15s%-14s%-12s%-22s%-15s%-15s%-9s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10}' > $DbMemInfoLog
echo "------- ------ ----------------- ------------- ------------ ---------- -------------------- ------------- ------------- --------" | awk '{printf "%-10s%-12s%-19s%-15s%-14s%-12s%-22s%-15s%-15s%-9s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10}' >> $DbMemInfoLog
cat $DbMemInfoTempLog | sort -k9 -n >> $DbMemInfoLog
cat $DbMemInfoLog
echo ""
echo "-- Save In: $DbMemInfoLog --"
}


########## 18. Check /dev/sd* device which no wwid and delete it ##########
badsdinfo ()
{
# Parameter.
IfDelBadDisk="$1"
DiskNoWwidLog="/tmp/.disknowwid.log"
DiskPathFaulty="/tmp/.diskpathfaulty.log"
BadDisksLog="/tmp/.baddisks.log"
MultipathdLog="/tmp/.multipathd_paths.log"
LsscsiLog="/tmp/.lsscsi.log"
> $DiskNoWwidLog
> $DiskPathFaulty
> $BadDisksLog


# Get data.
if [ `ls -1 /dev/ | grep -c '^sd'` -eq 0 ]; then
  echo "Disks device /dev/sd* not exist, exit!"
  exit 1
fi


for i in `ls /dev/sd* | egrep "[a-z]$" | awk -F'/' '{print $NF}'`
do
  if [ `uname -r | egrep -c '2.6.18'` -gt 0 ]; then
    WWID=`scsi_id -g -u -s /block/$i`
  elif [ `uname -r | egrep -c '2.6.32|2.6.39'` -gt 0 ]; then
    WWID=`scsi_id -g -u -d /dev/$i`
  else
    WWID="unknown"
    echo "The os version is not rhel5 or rhel6, exit!"
    exit 1
  fi
  [ -z "$WWID" ] && echo ${i} >> $DiskNoWwidLog
done


if [ -x /sbin/multipathd ]; then
  multipathd -k'show paths' > $MultipathdLog 2>&1
  cat $MultipathdLog | grep 'faulty' | awk '{print $2}' > $DiskPathFaulty
fi


cat $DiskNoWwidLog $DiskPathFaulty | sort -u > $BadDisksLog


[ -x /usr/bin/lsscsi ] && lsscsi > $LsscsiLog
if [ `cat $BadDisksLog | grep -c 'sd'` -eq 0 ]; then
  echo "All scsi disks status is ok."
elif [ "$IfDelBadDisk" == "delete" ]; then
  echo "The following scsi disks is bad:"
  echo "--------------------------------"
  if [ -x /usr/bin/lsscsi ]; then
    for i in `cat $BadDisksLog`
    do
      cat $LsscsiLog | grep -w "$i"
    done
  else
    cat $BadDisksLog
  fi
  echo ""
  echo "The disks multipath status:"
  echo "---------------------------"
  for i in `cat $BadDisksLog`
  do
    cat $MultipathdLog | grep -w "$i"
  done
  echo ""
  read -p "Are you sure to delete these disks? (y/n): " ANSWER
  if [ "$ANSWER" = "y" ]; then
    for j in `cat $BadDisksLog`
    do
      echo -n "Deleting scsi disk /dev/${j} ... "
      echo 1 > /sys/block/${j}/device/delete && echo "done!" || echo "failed!"
    done
  fi
else
  echo "The following scsi disks is bad:"
  echo "--------------------------------"
  if [ -x /usr/bin/lsscsi ]; then
    for i in `cat $BadDisksLog`
    do
      cat $LsscsiLog | grep -w "$i"
    done
  else
    cat $BadDisksLog
  fi
  echo ""
  echo "The disks multipath status:"
  echo "---------------------------"
  for i in `cat $BadDisksLog`
  do
    cat $MultipathdLog | grep -w "$i"
  done
fi
}


########## 19. Check /dev/st* device which is bad and delete it ##########
badstinfo ()
{
# Parameter.
IfDelBadTape="$1"
TapeStatusLog="/tmp/.tapestatus.log"
LsscsiLog="/tmp/.lsscsi.log"
> $TapeStatusLog


# Get data.
if [ ! -x /usr/openv/volmgr/bin/scsi_command ]; then
  echo "Command scsi_command not exist, exit!"
  exit 1
elif [ `ls /dev/ | grep -c nst` -eq 0 ]; then
  echo "Tape device /dev/nst* not exist, exit!"
  exit 1
else
  for i in `ls /dev/nst* | egrep "[0-9]$" | sort -t't' -k2 -n`
  do
    echo "$i `/usr/openv/volmgr/bin/scsi_command -d $i`" >> $TapeStatusLog
  done  
fi


[ -x /usr/bin/lsscsi ] && lsscsi > $LsscsiLog


if [ `cat $TapeStatusLog | grep -c 'inquiry failed'` -eq 0 ]; then
  echo "All scsi tapes status is ok."
elif [ "$IfDelBadTape" == "delete" ]; then
  echo "The following scsi tapes is bad:"
  echo "--------------------------------"
  cat $TapeStatusLog | grep 'inquiry failed'
  if [ -x /usr/bin/lsscsi ]; then
    echo ""
    echo "Device information details:"
    echo "---------------------------"
    for i in `cat $TapeStatusLog | grep 'inquiry failed' | awk '{print $1}' | awk -F'/' '{print $NF}' | sed 's/nst/st/g'`
    do
      cat $LsscsiLog | grep -w "$i"
    done
  fi
  echo ""
  read -p "Are you sure to delete these tapes? (y/n): " ANSWER
  if [ "$ANSWER" = "y" ]; then
    for j in `cat $TapeStatusLog | grep 'inquiry failed' | awk '{print $1}' | awk -F'/' '{print $NF}'`
    do
      echo -n "Deleting scsi tape /dev/${j} ... "
      echo 1 > /sys/class/scsi_tape/${j}/device/delete && echo "done!" || echo "failed!"
    done
  fi
else
  echo "The following scsi tapes is bad:"
  echo "--------------------------------"
  cat $TapeStatusLog | grep 'inquiry failed'
  if [ -x /usr/bin/lsscsi ]; then
    echo ""
    echo "Device information details:"
    echo "---------------------------"
    for i in `cat $TapeStatusLog | grep 'inquiry failed' | awk '{print $1}' | awk -F'/' '{print $NF}' | sed 's/nst/st/g'`
    do
      cat $LsscsiLog | grep -w "$i"
    done
  fi
fi
}


########## 20. Register to yumserver.app.sfdc.com.cn ##########
## Unregister RHN.
unreg_rhn ()
{
if [ -x /etc/init.d/rhnsd ]; then
  /etc/init.d/rhnsd stop
  /sbin/chkconfig rhnsd off
fi
if [ -x /etc/init.d/osad ]; then
  /etc/init.d/osad stop
  /sbin/chkconfig osad off
fi
if [ -f /etc/sysconfig/rhn/systemid ]; then
  rm -rf /etc/sysconfig/rhn/systemid
fi
}


## Yum Configure.
yumconf_el5 ()
{
rm -rf /etc/yum.repos.d/*
rm -rf /var/cache/yum/*
cat > /etc/yum.repos.d/yumserver.repo << EOF
[RHEL5\-basic]
name=RHEL\$releasever basic repository
baseurl=http://10.0.15.33/repo1/redhat/rhel\$releasever/U${UpdateVersion}/\$basearch/OS/Server
gpgcheck=1
enabled=1
gpgkey=http://10.0.15.33/repo1/redhat/rhel\$releasever/U${UpdateVersion}/\$basearch/OS/RPM-GPG-KEY-redhat-release
EOF
yum clean all
yum repolist
}


yumconf_el6 ()
{
rm -rf /etc/yum.repos.d/*
rm -rf /var/cache/yum/*
cat > /etc/yum.repos.d/yumserver.repo << EOF
[RHEL6\-basic]
name=RHEL\$releasever basic repository
baseurl=http://10.0.15.33/repo1/redhat/rhel\$releasever/U${UpdateVersion}/\$basearch/OS/Server
gpgcheck=1
enabled=1
gpgkey=http://10.0.15.33/repo1/redhat/rhel\$releasever/U${UpdateVersion}/\$basearch/OS/RPM-GPG-KEY-redhat-release
EOF
yum clean all
yum repolist
}


yumconf_ol6 ()
{
rm -rf /etc/yum.repos.d/*
rm -rf /var/cache/yum/*
cat > /etc/yum.repos.d/yumserver.repo << EOF
[ol${ReleaseVersion}_u${UpdateVersion}_base]
name=Oracle Linux \$releasever U${UpdateVersion} - \$basearch - base
baseurl=http://yumserver.app.sfdc.com.cn/yum/EnterpriseLinux/OL${ReleaseVersion}/${UpdateVersion}/base/\$basearch/
gpgcheck=0
enabled=1
EOF
yum clean all
yum repolist
}


## Main.
yumconf ()
{
DefaultOsVersionEL5=`cat /etc/redhat-release | awk -F'release' '{print $2}' | awk '{print $1}'`
DefaultOsVersionEL6=`cat /etc/redhat-release | awk -F'release' '{print $2}' | awk '{print $1}'`
OsVersion="$1"
if [ ! -z "$OsVersion" ]; then
  ReleaseVersion=`echo "$OsVersion" | awk -F'.' '{print $1}'`
  UpdateVersion=`echo "$OsVersion" | awk -F'.' '{print $2}'`
  [[ "$ReleaseVersion" != [2-9] ]] && echo "The input version $OsVersion is wrong, exit!" && exit 1
  [[ "$UpdateVersion" != [0-9] ]] && echo "The input version $OsVersion is wrong, exit!" && exit 1
fi


echo "The kernel version is:"
echo "----------------------"
cat /etc/redhat-release
uname -a
echo ""


if [ `cat /etc/redhat-release | grep -c 'release 5'` -gt 0 ]; then
  if [ -z "$OsVersion" ]; then
    OsVersion="$DefaultOsVersionEL5"
    ReleaseVersion=`echo "$OsVersion" | awk -F'.' '{print $1}'`
    UpdateVersion=`echo "$OsVersion" | awk -F'.' '{print $2}'`
  fi
  read -p "Will register to yum server channel ${OsVersion}, are you sure? (y/n): " ANSWER
  [ $ANSWER != "y" ] && exit 0
  wget -P /tmp http://yumserver.app.sfdc.com.cn/yum/EnterpriseLinux/EL${ReleaseVersion}/${UpdateVersion}/base/x86_64/repodata/filelists.xml.gz > /dev/null 2>&1
  if [ "$?" -eq 0 ]; then
    unreg_rhn
    yumconf_el5
  else
    echo "The yum server channel ${OsVersion} not exist, exit!"
    exit 1
  fi
elif [ `cat /etc/redhat-release | grep -c 'release 6'` -gt 0 ]; then
  if [ -z "$OsVersion" ]; then
    OsVersion="$DefaultOsVersionEL6"
    ReleaseVersion=`echo "$OsVersion" | awk -F'.' '{print $1}'`
    UpdateVersion=`echo "$OsVersion" | awk -F'.' '{print $2}'`
  fi
  read -p "Will register to yum server channel ${OsVersion}, are you sure? (y/n): " ANSWER
  [ $ANSWER != "y" ] && exit 0
  wget -P /tmp http://yumserver.app.sfdc.com.cn/yum/EnterpriseLinux/EL${ReleaseVersion}/${UpdateVersion}/base/x86_64/repodata/filelists.xml.gz > /dev/null 2>&1
  if [ "$?" -eq 0 ]; then
    unreg_rhn
    yumconf_el6
  else
    echo "The yum server channel ${OsVersion} not exist, exit!"
    exit 1
  fi
else
  echo "No suitable channel for: `cat /etc/redhat-release`"
fi
}


#################### Main ####################
### Running environment check. ###
if [ `id -u` -ne 0 ]; then
  echo "Please run as root, exit!"
  exit 110
fi


export PATH=$PATH:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/dba/app/product/11.2.0/grid/bin


### Main Options. ###
case "$1" in
  netinfo)
    [ -z "$2" ] && netinfomain || netinfomain "$2"
    ;;
  hbainfo)
    hbainfomain
    ;;
  pathinfo)
    pathinfomain
    ;;
  lunuseinfo)
    checklunuse
    ;;
  asmdginfo)
    checkasmdguse
    ;;
  dbdginfo)
    dbindginfo
    ;;
  dbsizeinfo)
    [ -z "$2" ] && dbsizeinfo || dbsizeinfo "$2"
    ;;
  dbcpuinfo)
    dbcpuinfo
    ;;
  dbioinfo)
    dbioinfo
    ;;
  dbmeminfo)
    dbmeminfo
    ;;
#  resgrpconf)
#    resgrpconf
#    ;;
#  appvolconf)
#    appvolconf
#    ;;
#  dbvipconf)
#    dbvipconf
#    ;;
  hwinfo)
    hwinfo
    ;;
  osinfo)
    osinfo
    ;;
  sysinfo)
    sysinfo
    ;;
  conninfo)
    conninfo
    ;;
  badsdinfo)
    [ -z "$2" ] && badsdinfo || badsdinfo "$2"
    ;;
  badstinfo)
    [ -z "$2" ] && badstinfo || badstinfo "$2"
    ;;
#  yumconf)
#    [ -z "$2" ] && yumconf || yumconf "$2"
#    ;;
  *)
    printversion
    echo ""
    echo "Usage: sysauto "
    echo "------------------------"
    echo "    : Show Hardware Model, SN, CPU, MEM, PCI Device."
    echo "    : Show OS Kernel, Network IP, Filesystem, Pmon Process."
    echo "   : Show Hardware And OS Informations."
    echo "   : Show Netcard Speed, Status, Switch Name, Switch Port."
    echo "   : Show HBA Vendor, WWPN, Status, Speed."
    echo "  : Show LUN WWID, Path, Size."
    echo "  : Show Netcard MAC, HBAcard WWPN, Connection Status."
    echo ": Show LUN Use Type: ASM, LVM, EXT3."
    echo " : Show ASM DG State, Type, Size, Free, Used."
    echo "  : Show DB Use ASM DG Name, Size, Free, Used."
    echo ": Show DB Use Size In ASM DG."
    echo " : Show DB Use Cpu Rate Real Time."
    echo "  : Show DB Use Io Read,Write Real Time."
    echo " : Show DB Use Memory: SGA, PGA, Hit Rate."
    echo " : Show The Bad SCSI Disks Which No WWID."
    echo " : Show The Bad SCSI Tapes Which Inquiry Failed."
#    echo ": Configure CRS Resource Group."
#    echo ": Configure APP Volume: PV, VG, LV, Mount."
#    echo " : Configure DB VIP: Add VIP To BondX Manually."
#    echo "   : Configure Yum Server: yumserver.app.sfdc.com.cn."
esac
[sfdba@cnsz22pl0028:/dba/oracle/diag/rdbms/ecpdbsit/ecpdbsit/trace]$

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24867586/viewspace-1731555/,如需轉載,請註明出處,否則將追究法律責任。

相關文章