#! /bin/sh
# Display iscsi targets

HOST_PATH=/sys/class/iscsi_host
SCSI_HOST=/sys/class/scsi_host
CLASS_PATH=/sys/class/iscsi_transport_class

print_usage()
{
	echo
        echo "Usage: iscsi-ls [-c] [-l] [-i host_id]"
        echo "-c : iSCSI target configuration parameters"
        echo "-i : Details regarding the specific host ID"
        echo "-l : Lun details available on the target"
}

is_num()
{
	case "$1" in
	-*[!0-9]*) rval=0;;
	*) rval=1;;
	esac
}

find_host_ids()
{
	i=0
	if [ $opt_i ]; then
		hosts=host$opt_i
		if [ ! -e $SCSI_HOST/$hosts ]; then
			echo "Host $opt_i does not exist"
			exit 1
		fi
	else
		hosts=`ls -1 $SCSI_HOST/`
	fi
	for host in $hosts; do
		host=$SCSI_HOST/$host
		proc_name=`cat $host/proc_name`
		if [ "$proc_name" = "iscsi-sfnet" ]; then
			host_no=${host:${#SCSI_HOST} + 5}
			host_ids[$i]=$host_no
			let "i += 1"
		elif [ $opt_i ]; then
			echo "Device is not an iSCSI device"
		fi
	done
}

show_session_status()
{
	session_alive=`cat $SCSI_HOST/host$1/session_alive`
	if [ $session_alive -eq 0 ]; then
		drop_time=`cat $SCSI_HOST/host$1/session_drop_time`
		echo -n "SESSION STATUS          : CONNECTION DROPPED AT "
		date -d ''$drop_time' seconds ago'
	else
		est_time=`cat $SCSI_HOST/host$1/session_established_time`
		echo -n "SESSION STATUS          : CONNECTION ESTABLISHED AT "
		date -d ''$est_time' seconds ago'
	fi
}

print_common_info()
{
	echo "TARGET NAME             :" `cat $1/target_name`
	echo "TARGET ALIAS            :" `cat $1/target_alias`
	echo "HOST NO                 : $2" 
	echo -n "TARGET ADDRESS          : "
	echo `cat $1/ip_address`:`cat $1/port`,`cat $1/tpgt`
	show_session_status $2
	echo "SESSION ID              : ISID" `cat $1/isid`
	echo "                          TSIH" `cat $1/tsih`
}

print_lun_info()
{
	luns=`ls -d $1/device/$2:0:0* 2> /dev/null`
	echo "DEVICE DETAILS:"
	echo "---------------"
	for lun in $luns; do
		lun_no=${lun##[/]*[:]}
		echo -n "LUN NO: $lun_no               : "
		unset device
		if [ -h $lun/block ]; then
			device=`ls -l $lun/block`
		elif [ -h $lun/tape ]; then
			device=`ls -l $lun/tape`
		fi
		if [ "$device" ]; then
			device=`echo $device | awk -F ' ' '{print $NF}'`
			device=${device##[.]*[/]}
			echo $device
		else
			echo " "
		fi
		echo "  Vendor                :" `cat $lun/vendor`
		echo "  Model                 :" `cat $lun/model`
		echo "  Rev                   :" `cat $lun/rev`
	done
}

print_config_info()
{
	echo "TARGET CONFIGURATION KEYS :"
	echo "-------------------------"
	echo "INITIAL R2T             :" `cat $1/initial_r2t`
	echo "IMMEDIATE DATA          :" `cat $1/immediate_data`
	echo "HEADERDIGEST            :" `cat $1/header_digest`
	echo "DATA DIGEST             :" `cat $1/data_digest`
	echo "FIRSTBURSTLENGTH        :" `cat $1/first_burst_len`
	echo "MAXBURSTLENGTH          :" `cat $1/max_burst_len`
	echo -n "MAXRECVDATASEGMENTLENGTH: initiator - "
	cat $1/max_recv_data_segment_len
	echo "LOGIN TIMEOUT           :" `cat $SCSI_HOST/host$2/login_timeout`
	echo "AUTH TIMEOUT            :" `cat $SCSI_HOST/host$2/auth_timeout`
	echo "ACTIVE TIMEOUT          :" `cat $SCSI_HOST/host$2/active_timeout`
	echo "IDLE TIMEOUT            :" `cat $SCSI_HOST/host$2/idle_timeout`
	echo "PING TIMEOUT            :" `cat $SCSI_HOST/host$2/ping_timeout`
}

while [ -n "$1" ]; do
case $1 in
    -c)
	opt_c=1
	shift 1;;
    -i)
	if [ $2 ];then
		is_num $2
		if [ $rval -eq  1 ];then
			opt_i=$2
			shift 2
		else
			print_usage
			exit 1
		fi
	else
		print_usage
		exit 1
	fi;;
    -l)
	opt_l=1
	shift 1;;
    *)
	print_usage
	exit 1;;
esac
done

cat /proc/devices | grep -q iscsictl
if [ $? -ne 0 ]; then
	echo "###############################################################################"
	echo "iSCSI driver is not loaded"
	echo "###############################################################################"
	exit
fi

echo "*******************************************************************************"
echo -n "SFNet iSCSI Driver Version ..."
iscsid -v | awk '{ print $3 $4 $5 }'
echo "*******************************************************************************"

declare -a host_ids
find_host_ids
no_hosts=${#host_ids[*]}

i=0
while [ $i -lt $no_hosts ]; do
	target=$CLASS_PATH/target${host_ids[$i]}:0:0

	if [ -e $target ]; then
		print_common_info $target ${host_ids[$i]}

		if [ $opt_l ]; then
			print_lun_info $target ${host_ids[$i]}
		fi

		if [ $opt_c ]; then
			print_config_info $target ${host_ids[$i]}
		fi
		echo "-------------------------------------------------------------------------------"
	fi
	let "i += 1"
done
