#!/bin/sh
#
# ioscan_fc2.sh
#
# Gives out a comprehensive report of all agile disk devices on on HP-UX 11iv3 system
#
# N.B. This is still beta. I don't have enough 11.31 servers available to test
#      the script to its full extent.
#
# 
# (c) 2008 Olivier S. Masse, omasse ~at~ mayoxide ~dot~ com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
#
version="@(#) v0.22 2009/02/09"

verbose=0

function usage
{
	typeset myself=$(basename $0)
	echo
	echo "ioscan_fc2 ${version}"
	echo
	echo "Usage: ${myself} [-v] [-H lun_hwpath | -D agile_dsf ...]"
	echo
	echo "Examples:"
	echo "   ${myself}"
	echo "   ${myself} -H 64000/0xfa00/0xa"
	echo "   ${myself} -D /dev/disk/disk73"
	echo
	exit 1
}

if [ ! "$(uname -r)" = "B.11.31" ]
then
	echo "Tested only on B.11.31, sorry."
	exit 1	
fi

if [ "$1" ]
then
	case "$1" in
		-v) shift; verbose=1;;
		-H) shift; [ "$1" = "" ] && usage || desired_hwpath=${1};;
		-D) shift; [ "$1" = "" ] && usage || desired_disk=${1};;
		*)  usage;;
	esac
fi

scsimgr_cmd="scsimgr get_attr all_lun -a device_file -a hw_path -a state -a capacity -a block_size -a wwid -a load_bal_policy -a max_q_depth -p"
[ ! "${desired_hwpath}" = "" ] && scsimgr_cmd="scsimgr get_attr -H ${desired_hwpath} -a device_file -a hw_path -a state -a capacity -a block_size -a wwid -a load_bal_policy -a max_q_depth -p"
[ ! "${desired_disk}" = "" ] && scsimgr_cmd="scsimgr get_attr -D ${desired_disk} -a device_file -a hw_path -a state -a capacity -a block_size -a wwid -a load_bal_policy -a max_q_depth -p"

eval ${scsimgr_cmd} | grep rdisk | while IFS=":" read device_file hw_path state capacity block_size wwid load_bal_policy max_q_depth
do
	if [ "${capacity}" = "" ]   # capacity is nul if device was unpresented
	then
		size_gb="???"
	else
		echo "crap" | awk '{printf("%i\n", '"${capacity}"' * '"${block_size}"' / 1024 / 1024 / 1024);}' | read size_gb
	fi
	echo
	printf "%-20s %-34s %-10s %-20s %-7s %-12s %-12s\n" disk wwid state lun_hw_path size_gb load_bal max_q_depth
	printf "%-20s %-34s %-10s %-20s %-7s %-12s %-12s\n" "${device_file}" "${wwid}" "${state}" "${hw_path}" "${size_gb}" "${load_bal_policy}" "${max_q_depth}"

	ioscan -kFm hwpath -H ${hw_path} | while IFS=":" read crap lunpath crap
        do
		scsimgr get_attr -H ${lunpath} -a state -p | read lunpath_state
		scsimgr get_attr -H ${lunpath} -a lunid | grep "current =" | read crap crap lunpath_lun
		printf "%55s %-10s %-30s\n" "${lunpath}" "${lunpath_state}" "${lunpath_lun}"
        done | sort -k 1,1
	
	if [ ${verbose} -eq 1 ]
	then
		printf "%-55s %-40s\n" scope vg_holder
		scsimgr ddr_name -D ${device_file} rev | tail -1 | read ddr_name
		[ "${ddr_name}" = "" ] && ddr_name="(unknown)"
	
		echo ${device_file} | sed 's/rdisk/disk/g' | read cooked_device_file
		if [ -c ${cooked_device_file}_p2 ]
		then
			pvdisplay ${cooked_device_file}_p2 2>/dev/null | awk '/VG Name/ {print $3}' | read vg
		else
			pvdisplay ${cooked_device_file}_p2 2>/dev/null | awk '/VG Name/ {print $3}' | read vg
		fi
		[ "${vg}" = "" ] && vg="(unknown)"
		printf "%-55s %-40s\n" "${ddr_name}" "${vg}"
	fi
done


