#!/bin/ksh -p
# $Id: //depot/stable/explorer/tools/t3extended#3 $
#
# Explorer Version:
EXP_VERSION=4.0
#

# Source tools functions
. ${EXP_LIB}/exp_tools

# Run if specified to do so
script=${0##*/}
which_gate_exit storage extended interactive $script default all

# defaults
T3_NAME="NOTNOISE"
T3_TIME=60
TTY=`tty`
if [ "${TTY}" = "not a tty" ]
then
    TTY=""
fi

if [ "${EXP_HOME}" = "/" ]
then
    CLIENT="${EXP_LIB}/t3client"
    TFILE="${EXP_LIB}/t3files.txt"
else
    CLIENT="${EXP_HOME}/bin/t3client.`uname -p`"
    TFILE="${EXP_ETC}/t3files.txt"
fi

collect_data () {
    # Execute commands in T3_CMDS, use small timeout to ensure this is a T3
    OPT="telnet:t3:${T3_NAME}:${T3_PASS}:5"
    get_cmd "ls -l /" ${OPT} disks/t3/${T3_NAME}/

    # Did we fail to run ls -l /?
    NUMLINES=`wc -l "${EXP_TARGET}/disks/t3/${T3_NAME}/ls_-l_@.out" |  nawk '{ print $1 }' | sed -e 's/ //g'`
    if [ "${NUMLINES}" -lt 4 ]
    then
	# Try to run it again
	rm -f "${EXP_TARGET}/disks/t3/${T3_NAME}/ls_-l_@.out"
	get_cmd "ls -l /" ${OPT} disks/t3/${T3_NAME}/
    fi

    # Still unable to get output?
    NUMLINES=`wc -l "${EXP_TARGET}/disks/t3/${T3_NAME}/ls_-l_@.out" |  nawk '{ print $1 }' | sed -e 's/ //g'`
    if [ "${NUMLINES}" -lt 4 ]
    then
	rm -rf ${EXP_TARGET}/disks/t3/${T3_NAME}/ls_-l_@.out
	TEXT=`gettext "unable to collect data for %s"`
	TEXT=`printf "${TEXT}" "${T3_NAME}"`
	logfile ERROR "${TEXT}"
	return
    fi

    # Make sure root password correct
    if egrep -s -e "Invalid password" ${EXP_TARGET}/disks/t3/${T3_NAME}/ls_-l_@.out
    then
	rm -rf ${EXP_TARGET}/disks/t3/${T3_NAME}
	TEXT=`gettext "root password for %s is incorrect"`
	TEXT=`printf "${TEXT}" "${T3_NAME}"`
	logfile ERROR "${TEXT}"
	return
    fi

    # Make sure we are talking to a T3
    if ! egrep -s -e "^T3" ${EXP_TARGET}/disks/t3/${T3_NAME}/ls_-l_@.out
    then
	rm -rf ${EXP_TARGET}/disks/t3/${T3_NAME}
	TEXT=`gettext "%s is not a StorEdge T3"`
	TEXT=`printf "${TEXT}" "${T3_NAME}"`
	logfile ERROR "${TEXT}"
	return
    fi

    OPT="telnet:t3:${T3_NAME}:${T3_PASS}:${T3_TIME}"
    get_cmd "ls -l /etc"        ${OPT} disks/t3/${T3_NAME}/
    get_cmd "ls -l /web"        ${OPT} disks/t3/${T3_NAME}/
# The following line has been commented out as it has a
# tendancy to crash t3's in certain circumstances. It will
# be re-enabled after a patch has been developed for it.
#    get_cmd "ls -l /web/snmp"   ${OPT} disks/t3/${T3_NAME}/
    get_cmd "proc list"         ${OPT} disks/t3/${T3_NAME}/
    get_cmd "fru s sys"         ${OPT} disks/t3/${T3_NAME}/
    get_cmd "sys stat"          ${OPT} disks/t3/${T3_NAME}/
    get_cmd "sys list"          ${OPT} disks/t3/${T3_NAME}/
    get_cmd "vol stat"          ${OPT} disks/t3/${T3_NAME}/
    get_cmd "vol statp"         ${OPT} disks/t3/${T3_NAME}/
    get_cmd "vol list"          ${OPT} disks/t3/${T3_NAME}/
    get_cmd "vol mode"          ${OPT} disks/t3/${T3_NAME}/
    get_cmd "fru l"             ${OPT} disks/t3/${T3_NAME}/
    get_cmd "fru st"            ${OPT} disks/t3/${T3_NAME}/
    get_cmd "fru myuid"         ${OPT} disks/t3/${T3_NAME}/
    get_cmd "date"              ${OPT} disks/t3/${T3_NAME}/
    get_cmd "tzset"             ${OPT} disks/t3/${T3_NAME}/
    get_cmd "port list"         ${OPT} disks/t3/${T3_NAME}/
    get_cmd "port listmap"      ${OPT} disks/t3/${T3_NAME}/
    get_cmd "ver"               ${OPT} disks/t3/${T3_NAME}/
    get_cmd "set"               ${OPT} disks/t3/${T3_NAME}/
    get_cmd "refresh -s"        ${OPT} disks/t3/${T3_NAME}/
    get_cmd "route -r"          ${OPT} disks/t3/${T3_NAME}/
    get_cmd ".loop stat"        ${OPT} disks/t3/${T3_NAME}/_loop_stat
    get_cmd ".set"              ${OPT} disks/t3/${T3_NAME}/_set
    get_cmd ".sys list"         ${OPT} disks/t3/${T3_NAME}/_sys_list
    get_cmd "fru s"             ${OPT} disks/t3/${T3_NAME}/

    # Collect data via token interface first
    TEXT=`gettext "Collecting data via the token interface from %s"`
    TEXT=`printf "${TEXT}" "${T3_NAME}"`
    logfile "${TEXT}"
    ${CLIENT} -h ${T3_NAME} -f ${TFILE} -b ${EXP_TARGET}/disks/t3/${T3_NAME}

    # Get enclosure count from sysprop.htm
    SYSPROP="${EXP_TARGET}/disks/t3/${T3_NAME}/sysprop.htm"
    if [ -f "${SYSPROP}" ]
    then
	COUNT=`egrep -e unitCount ${SYSPROP} | nawk -F, '{ print $3 }'`
	case "${COUNT}" in
	    1*)
		LIST="u1"
		;;
	    2*)
		LIST="u1 u2"
		;;
	    3*)
		LIST="u1 u2 u3"
		;;
	    *)
		LIST="u1 u2 u3 u4"
		;;
	esac
    else
	# Can't get unitCount from sysprop, do all
	LIST="u1 u2 u3 u4"
    fi

    TEXT=`gettext "Collecting data for enclosures %s"`
    TEXT=`printf "${TEXT}" "${LIST}"`
    logfile "${TEXT}"
    for T3_ID in ${LIST}
    do
	get_cmd "id read ${T3_ID}pcu1" \
	    ${OPT} disks/t3/${T3_NAME}/
	get_cmd "id read ${T3_ID}pcu2" \
	    ${OPT} disks/t3/${T3_NAME}/
	get_cmd "id read ${T3_ID}" \
	    ${OPT} disks/t3/${T3_NAME}/
	get_cmd "id read ${T3_ID}l1" \
	    ${OPT} disks/t3/${T3_NAME}/
	get_cmd "id read ${T3_ID}l2" \
	    ${OPT} disks/t3/${T3_NAME}/
	get_cmd "id read ${T3_ID}c1" \
	    ${OPT} disks/t3/${T3_NAME}/
	get_cmd "disk version ${T3_ID}d1-9" \
	    ${OPT} disks/t3/${T3_NAME}/
	get_cmd ".disk pathstat ${T3_ID}d1-9" \
	    ${OPT} disks/t3/${T3_NAME}/_disk_pathstat_${T3_ID}d1-9
	get_cmd ".disk linkstat ${T3_ID}d1-9 path 0" \
	    ${OPT} disks/t3/${T3_NAME}/_disk_linkstat_${T3_ID}d1-9_path_0
	get_cmd ".disk linkstat ${T3_ID}d1-9 path 1" \
	    ${OPT} disks/t3/${T3_NAME}/_disk_linkstat_${T3_ID}d1-9_path_1
	get_cmd ".disk tmon_list ${T3_ID}d1-9" \
	    ${OPT} disks/t3/${T3_NAME}/_disk_tmon_list_${T3_ID}d1-9
	get_cmd ".disk gettune ${T3_ID}d1-9" \
	    ${OPT} disks/t3/${T3_NAME}/_disk_gettune_${T3_ID}d1-9
    done

    # Run before ftp'ing files
    get_cmd "logger -dmprstlog" ${OPT} disks/t3/${T3_NAME}/

    # Get files via ftp
    get_file 'cmdlog* syslog*'    "ftp:${T3_NAME}:${T3_PASS}:/" \
	disks/t3/${T3_NAME}/files

    get_file 'hosts *.conf *.log' "ftp:${T3_NAME}:${T3_PASS}:/etc" \
	disks/t3/${T3_NAME}/files/etc
}

# Interactive?
RUN=""
which_gate interactive && RUN=notempty
which_gate t3extended  && RUN=notempty
which_gate extended    && RUN=notempty

if [ -n "${RUN}" ]
then
    logfile "Collecting extended data interactively"

    TEXT=`gettext "\nYou will be prompted for a T3 name and password.\n    If hostname cannot be resolved, please use IP address.\n\nType return to end the list.\n\n"`
    prompt "${TEXT}"
    prompt ""

    while [ -n "${T3_NAME}" ]
    do
	TEXT=`gettext "Please enter the name of the T3: "`
	prompt "${TEXT}\c"
	read T3_NAME

	if [ -n "${T3_NAME}" ]
	then
	    # Can we ping it?
	    TEXT=`gettext "Verifying connection to %s"`
	    TEXT=`printf "${TEXT}" "${T3_NAME}"`
	    logfile "${TEXT}"
	    ping ${T3_NAME} >/dev/null 2>&1
	    if [ "$?" -ne 0 ]
	    then
		TEXT=`gettext "Unable to connect to %s"`
		TEXT=`printf "${TEXT}" "${T3_NAME}"`
		logfile ERROR "${TEXT}"
		prompt ""
		continue
	    fi

	    # Get password
	    TEXT=`gettext "Please enter the root password for %s: "`
	    TEXT=`printf "${TEXT}" "${T3_NAME}"`
	    prompt "${TEXT}\c"
	    [ -n "${TTY}" ] && stty -echo
	    read T3_PASS
	    [ -n "${TTY}" ] && stty echo
	    prompt ""

	    collect_data
	    prompt ""
	fi
    done
fi

# Collect input file if secure mode selected (-w secure)
which_gate secure && SECURE=notempty
if [ -n "${SECURE}" ]
then
    get_file "${EXP_ETC}/t3input.txt" disks/t3/
fi

# Not interactive, get data if input file?
get_cmd "/bin/ls -l ${EXP_ETC}/t3input.txt" disks/t3/
if [ -r "${EXP_ETC}/t3input.txt" ]
then
    # Make sure input file is mode 0600
    if `ls -l ${EXP_ETC}/t3input.txt | nawk '{ print $1 }' | egrep -s -e '------$'`
    then
	logfile "Collecting extended data from ${EXP_ETC}/t3input.txt file"
	while read line
	do
	    # Skip comments and blank lines
	    echo $line | sed -e 's/^ *//' -e 's/ *$//' | egrep -s -e '^#' && continue
	    echo $line | egrep -s -e '^$' && continue

	    T3_NAME=`echo $line | nawk '{ print $1 }'`
	    T3_PASS=`echo $line | nawk '{ print $2 }'`

	    if [ -d "${EXP_TARGET}/disks/t3/${T3_NAME}" ]
	    then
		logfile "Data for ${T3_NAME} already collected"
	    else
		# Can we ping it?
		TEXT=`gettext "Verifying connection to %s"`
		TEXT=`printf "${TEXT}" "${T3_NAME}"`
		logfile "${TEXT}"
		ping ${T3_NAME} >/dev/null 2>&1
		if [ "$?" -ne 0 ]
		then
		    TEXT=`gettext "Unable to connect to %s"`
		    TEXT=`printf "${TEXT}" "${T3_NAME}"`
		    logfile ERROR "${TEXT}"
		    prompt ""
		    continue
		fi

		logfile "Collecting extended data for ${T3_NAME}"
		collect_data
	    fi
	done < ${EXP_ETC}/t3input.txt
    else
	logfile WARNING "${EXP_ETC}/t3input.txt file not correct mode"
    fi
fi
