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

# Source tools functions
. ${EXP_LIB}/exp_tools

# System configuration information is critical, always run

# Get system information
get_cmd "/usr/sbin/sysdef"            sysconfig/sysdef
get_cmd "/usr/sbin/sysdef -d"         sysconfig/sysdef-d
get_cmd "/usr/sbin/prtconf -v"        sysconfig/prtconf-v
get_cmd "/usr/sbin/prtpicl -v"        sysconfig/prtpicl-v
get_cmd "/usr/sbin/prtconf -vp"       sysconfig/prtconf-vp
get_cmd "/usr/bin/uptime"             sysconfig/uptime
get_cmd "/usr/sbin/psrinfo -v"        sysconfig/psrinfo-v
get_cmd "/usr/sbin/ifconfig -a"       sysconfig/ifconfig-a
get_cmd "/usr/sbin/ifconfig -a modlist"	sysconfig/ifconfig-a-modlist
get_cmd "/usr/sbin/eeprom"            sysconfig/eeprom
get_cmd "/usr/bin/ipcs -a"            sysconfig/ipcs-a
get_cmd "/usr/bin/ps -ef"             sysconfig/ps-ef
get_cmd "/usr/bin/ps -acefl"          sysconfig/ps-acefl
get_cmd "/usr/bin/last -100"          sysconfig/last-100-login
get_cmd "/usr/bin/last -20 reboot"    sysconfig/last-20-reboot
get_cmd "/usr/bin/uname -a"           sysconfig/uname-a
get_cmd "/bin/ls -lR /kernel/drv"     sysconfig/ls-lR_kernel_drv
get_cmd "/bin/ls -lR /usr/kernel/drv" sysconfig/ls-lR_usr_kernel_drv
get_cmd "/usr/sbin/dumpadm"           sysconfig/dumpadm
get_cmd "/usr/sbin/cfgadm -alv"	      sysconfig/cfgadm-alv
get_cmd "/usr/sbin/pmadm -L"          sysconfig/pmadm-L
get_cmd "/usr/sbin/sacadm -L"         sysconfig/sacadm-L
get_cmd "/usr/bin/isainfo"            sysconfig/isainfo
get_cmd "/usr/bin/isainfo -kv"        sysconfig/isainfo-kv
get_cmd "/usr/bin/coreadm"            sysconfig/coreadm
get_cmd "/usr/sbin/lockstat sleep 5"  sysconfig/lockstat-sleep-5
get_cmd "/usr/proc/bin/ptree root"    sysconfig/ptree-root
get_cmd "/usr/bin/priocntl -l"        sysconfig/priocntl-l
get_cmd "/usr/bin/locale"	      sysconfig/locale
get_cmd "/usr/bin/who -b"	      sysconfig/who-b
get_cmd "/opt/SUNWut/sbin/utadm -p"   sysconfig/utadm-p

# Collect kernel config files
get_file "/kernel/drv/*.conf"     sysconfig/drv/
get_file "/usr/kernel/drv/*.conf" sysconfig/drv/

# NEW
get_cmd "/usr/bin/vmstat 3 3" sysconfig/

# kernel msgbuf of the currently running system
# COMMENTED OUT due to SunAlert 27552
# echo '$<msgbuf' | get_cmd "/usr/bin/adb -k /dev/ksyms /dev/mem" sysconfig/kernel-msgbuf

# nm on /dev/ksyms for values listed in /etc/system
cat /etc/system | egrep -e "^set" | sed 's/set //' | sed 's/ //g' | cut -d= -f1 | cut -d: -f2 |
while read mod
do
    get_cmd "/usr/ccs/bin/nm /dev/ksyms | egrep -e $mod" sysconfig/nm.ksyms=$mod
done

# Don't execute prtconf -V if it returns error, i.e. illegal option.
# -V option works only on Solaris 2.5 and higher. It reports OBP rev level.
if [ -z "`prtconf -V 2>&1 | egrep -e 'illegal option'`" ]
then
    get_cmd "/usr/sbin/prtconf -V" sysconfig/prtconf-V
fi

# Don't execute uname -X if it returns error, i.e. illegal option.
# -X option works only on Solaris 2.6 and higher.
if [ -z "`uname -X 2>&1 | egrep -e 'illegal option'`" ]
then
    get_cmd "/usr/bin/uname -X" sysconfig/uname-X
fi

# Get prtdiag info if it exists, also there has been a change to
# where prtdiag is located in Solaris 2.5 and later.
#
# SUNW - is for Sun HW
# CYRS - is for Cray HW
#
# Update to use machine hardware name (class) from uname -m
#
ARCH=`uname -m`
SYSNAME="`prtconf | egrep -e '^SUNW,'`"

if [ -z "$SYSNAME" ]
then
    SYSNAME="`prtconf | egrep -e '^CYRS,'`"
fi

# /platform exist only on Solaris 2.5 and above, for 2.3&2.4 use /kernel
if [ -d /platform ]
then
    # kernel is generally not under SYSNAME but under ARCH
    # get_cmd "/bin/ls -l /platform/"$SYSNAME"/kernel" sysconfig/kernel_ls-l
    get_cmd "/bin/ls -l /platform/${ARCH}/kernel" sysconfig/kernel_ls-l
elif [ -d /kernel ]
then
    get_cmd "/bin/ls -l /kernel" sysconfig/kernel_ls-l
fi

# If sbin/prtdiag not under ARCH, try SYSNAME before /usr/kvm/prtdiag
if [ -x "/usr/platform/${ARCH}/sbin/prtdiag" ]
then
    get_cmd "/usr/platform/${ARCH}/sbin/prtdiag -v" sysconfig/prtdiag-v
elif [ -x "/usr/platform/${SYSNAME}/sbin/prtdiag" ]
then
    get_cmd "/usr/platform/${SYSNAME}/sbin/prtdiag -v" sysconfig/prtdiag-v
elif [ -x /usr/kvm/prtdiag ]
then
    get_cmd "/usr/kvm/prtdiag -v" sysconfig/prtdiag-v
fi

# Run core check script
get_cmd "${EXP_HOME}/bin/core_check.sh" sysconfig/crash/core_check

# check for dumpadm and set appropriate CRASHDIR
if [ -x /usr/sbin/dumpadm ]
then
    CRASHDIR=`dumpadm | egrep -e "Savecore directory" | awk '{print $3}' | sed 's/ //g'`
else
    TEMPDIR=`egrep -e savecore /etc/init.d/sysetup | sed -e '/^ *#/d' -e 's/^ *savecore //g'`
    CRASHDIR=`eval echo ${TEMPDIR}`
fi

# Make sure CRASHDIR isn't empty
if [ -z "${CRASHDIR}" ]
then
    CRASHDIR=/var/crash/`uname -n`
fi

# List CRASHDIR, in case it's a link
# Check for minfree file in output and remove
DIR=`echo ${CRASHDIR} | sed -e 's/\//_/g'`

get_cmd "/bin/ls -al ${CRASHDIR}" sysconfig/crash/ls-al${DIR}

if [ -d "${CRASHDIR}" ]
then
    # See if we can find out which core file is the last crash
    if [ -f "${CRASHDIR}/bounds" ]
    then
	NEXT=`cat ${CRASHDIR}/bounds`
	LAST=`expr ${NEXT} - 1`
    else
	FILE=`ls ${CRASHDIR}/vmcore.* 2>/dev/null | sort -rn | head -1`
	LAST=`basename ${FILE} | sed 's/vmcore.//'`
    fi

    # Check all cores
    LIST=`ls ${CRASHDIR}/vmcore.* 2>/dev/null`
    for core in ${LIST}
    do
	file=`basename $core`
	get_cmd "/usr/bin/strings $core | head" sysconfig/crash/strings-$file
    done

    # Check for ACT crash dump analysis
    get_file "${CRASHDIR}/act.*" sysconfig/crash/act

    # Check for ACT, and get analysis of last crash
    if [ -x /opt/CTEact/bin/act -a -f ${CRASHDIR}/vmcore.${LAST} -a -f ${CRASHDIR}/unix.${LAST} ]
    then
	get_cmd "/opt/CTEact/bin/act -d ${CRASHDIR}/vmcore.${LAST} -n ${CRASHDIR}/unix.${LAST}" \
	    sysconfig/crash/act/act-d_vmcore.${LAST}
    elif [ -x /opt/CETactx/bin/act -a -f ${CRASHDIR}/vmcore.${LAST} -a -f ${CRASHDIR}/unix.${LAST} ]
    then
	get_cmd "/opt/CTEactx/bin/act -d ${CRASHDIR}/vmcore.${LAST} -n ${CRASHDIR}/unix.${LAST}" \
	    sysconfig/crash/act/act-d_vmcore.${LAST}
    fi
fi

# Run crash interactively
# COMMENTED OUT due to SunAlert 27552
# get_cmd "/usr/sbin/crash" sysconfig/crash/crash <<END
# status
# proc
# kmastat
# pty
# quit
# END

# pty doesn't run on Solaris 8, so use mdb
# COMMENTED OUT due to SunAlert 27552
# get_cmd "/usr/bin/mdb -k" sysconfig/crash/mdb_-k <<END
# ::walk pty_map | ::ptms
# END

# exec dispadmin to list the dispatch tables
get_cmd "/usr/sbin/dispadmin -l" sysconfig/dispadmin-l

# obtain the dispatch table for class names
CLASSES=`/usr/sbin/dispadmin -l | nawk '$1 ~ /^[A-Z]+$/ && $2 ~ /^\(/ { print $1 }'`
export CLASSES

# for each class found ...
for CLASS in ${CLASSES}
do
	# get the class' dispatch table
	get_cmd "/usr/sbin/dispadmin -c ${CLASS} -g" sysconfig/dispadmin-c_${CLASS}
done

# AP info, exit if no SUNWapu package
TEXT=`gettext "AP not installed"`
require "pkginfo -q SUNWapu" "${TEXT}"
get_cmd "/usr/sbin/apconfig -D"    sysconfig/ap/apconfig-D
get_cmd "/usr/sbin/apconfig -N"    sysconfig/ap/apconfig-N
get_cmd "/usr/sbin/apconfig -N -u" sysconfig/ap/apconfig-N-u
get_cmd "/usr/sbin/apconfig -S"    sysconfig/ap/apconfig-S
get_cmd "/usr/sbin/apconfig -S -u" sysconfig/ap/apconfig-S-u
get_cmd "/usr/sbin/apinst"         sysconfig/ap/apinst
