Tuesday, July 17, 2012

List device tree in AIX

I just created a script to list device tree in AIX.



#!/usr/bin/ksh
#
# List devices tree in AIX
#
# Author :: Lightspeed
# 20120714
# 20120716 -- add ip/netmask, disk size/vg
#
# Limitation
# ----------
# Normal user will not get disk size if the disk is not in any vg.
#

getip () {

  interface=$( echo $1 | sed 's/t//' )

  ip=$(      odmget -q "name=$interface and attribute=netaddr" CuAt | grep value | sed 's/"$//; s/.*"//' )
  netmask=$( odmget -q "name=$interface and attribute=netmask" CuAt | grep value | sed 's/"$//; s/.*"//' )

  [ "X$ip" != "X" -a "X$netmask" != "X" ] &&  echo "$ip/$netmask"

}

getdisk() {

  vg=$(   lspv $1 2>/dev/null | head -1 | awk '{print $NF}'         )
  size=$( lspv $1 2>/dev/null | grep TOTAL | sed 's/.*(//; s/).*//; s/[^0-9]//g' | awk '{print $1}' )

  if [ "X$size" == "X" ]; then
     size=$( getconf DISK_SIZE /dev/$1 2>/dev/null )
  fi
  if [ $size -ge 1024 ]; then
     size="$(( size / 1024 )) GB"
  elif [ $size -gt 0 ]; then
     size="$size MB"
  fi

  printf "%-15s%s" "$size"  $vg

}

info() {

  ## somehow pipe doesn't work properly in recursive loop
  d=$1
  #echo $1 | grep "hdisk" > /dev/null
  #if [ $? -eq 0 ]; then
  if [ ${dev%%[0-9]*} == "hdisk" ]; then

    getdisk $d

  else
    #echo $d | grep "ent" > /dev/null
    #if [ $? -eq 0 ]; then
    if [ ${dev%%[0-9]*} == "ent" ]; then
      getip $d
    fi
  fi
}
pad () {

  s=""
  i=0
  while [ $i -lt $1 ]; do
    s=$s"  "
    i=$(( i + 1 ))
  done
  echo "$s"

}
find_dev ()
{
  dev=$1


  if [ $VERBOSE -eq 1 ]; then
    echo "$(pad $level)$dev  $(info $dev), level=$level"
  else
    echo "$(pad $level)$dev  $(info $dev)"
  fi
  list=$( lspath -p $dev -F name  2>/dev/null | sort -u -n -k 1.6 )
  if [ "X$list" == "X" ]; then
    list=$( lsdev -p $dev -F name 2>/dev/null )
  fi
  if [ "X$list" == "X" ]; then
    return
  fi
  level=$(( level + 1 ))
  for i in $list; do
    find_dev $i
  done
  level=$(( level - 1 ))
}

set -e

export ODMDIR=/etc/objrepos
export PATH=$PATH:/usr/sbin:/sbin:/usr/bin:/bin
VERBOSE=0
ROOT=sys0
level=0

if [ "X$1" != "X" ]; then

  if [ "X$1" == "X-h" ]; then
    echo
    echo "Usage :: $0 [ -l ] [ device_node ]"
    echo
    echo "   -l -- show node level"
    echo "--------------------------------------------------------------"
    echo "It displays a device tree from the device_node (default: sys0)"
    echo
    exit
  fi
  if [ "X$1" == "X-l" ]; then
    VERBOSE=1
  fi
 if [ "X$1" == "X-l" ]; then
    [ "X$2" != "X" ] && ROOT=$2
  else
    ROOT=$1
  fi
  name=$( lsdev -l $ROOT -F name )
  if [ "X$name" == "X" ]; then
    echo "$ROOT is a wrong device name."
    exit
  fi
fi

find_dev $ROOT

No comments:

Post a Comment