#START

# Global variables/functions ----------------------------------------------

version=4.0
name=$(echo $0 | cut -c 3-)
mainDIR="$name-$(hostname)-$(date +%H%M%d%m%Y)"
rootDir=$(pwd)

stty_old=$(stty -g)
trap 'wrapper' 1 2 3 15

cleanuptemp()
{
        rm -r $mainDIR
	kill -9 $kdbpid > /dev/null 2> /dev/null
}

#******************** SIG_TERM Trap Handler *******************************

wrapper() {
echo "User terminated Adepto at $date" >> $mainDIR/AdeptoLOG
#stty $stty_old
echo ""
echo Premature termination of Adepto detected. Finalizing...
tar cf $mainDIR.tar $mainDIR/
gzip --best $mainDIR.tar
cleanuptemp
echo "Partial file generated: $mainDIR.tar.gz"
echo ""
exit 1
} 2> /dev/null

#**************************************************************************

set -A Spinner 10 20 30 40 50 60 70 80 90 100
Spinnerpos=0

update_spinner()
{
    printf ${Spinner[$Spinnerpos]}
    printf "\% complete"
    (( Spinnerpos=(Spinnerpos +1)%10 ))
}

#--------------------------------------------------------------------------

mkdir $mainDIR
touch $mainDIR/AdeptoLOG
echo "Adepto run on $(date)" > $mainDIR/AdeptoLOG

echo
echo Starting $name ... 
echo Time to complete will vary depending on system configuration and age.
echo

if [ $1 = "-mp" ] 2> /dev/null
then
	echo "Gathering MP/GSP details" >> $mainDIR/AdeptoLOG
	echo "Running MP extended mode"
	echo "MP/GSP Username and Password is usually Admin/Admin or Oper/Oper"
	echo ""
	echo "MP/GSP IP address: \c"; read ipaddr
        userStr=$(echo $ipaddr | sed -e "s/[0-9]//g" | sed -e "s/\.//g" | sed -e "s/ //g")

        until [ $(echo $ipaddr | sed -e "s/\.\./\*\*/g" | grep "\*\*" | wc -l ) -lt "1" -a $(echo $userStr | wc -c) -eq "1" -a $(echo $ipaddr | sed -e "s/[0-9]//g" | wc -c) -eq "4" ]
        do
                echo "Invalid IP address. IPv4 addresses only. Please try again."
                echo "MP/GSP IP address: \c"; read ipaddr
                userStr=$(echo $ipaddr | sed -e "s/[0-9]//g" | sed -e "s/\.//g" | sed -e "s/ //g")
        done

        echo "Username: \c"; read user
        stty -echo
        echo "Password: \c"; read pass
        stty $stty_old
	echo ""
fi

crashinfo()
{
	crashDir=$(ls -d /var/adm/crash/cra* | tail -1)
	absPath=$(echo $PWD)
	echo $absPath

	cd $crashDir
	zcat /usr/contrib/lib/Q4Lib.tar.Z | tar -xvf -
	/usr/contrib/bin/gunzip vmunix.gz
	/usr/contrib/bin/q4pxdb vmunix
	cp /usr/contrib/lib/Q4/sample.q4rc.pl ~/.q4rc.pl

/usr/contrib/bin/q4 -p << EOF
include analyze.pl
trace event 0
run Analyze AU > $absPath/$mainDIR/CRASH/Q4Analysis.out
include whathappened.pl
run WhatHappened > $absPath/$mainDIR/CRASH/Q4WhatHappened.out
EOF

	cd $absPath
	
	tar cf $mainDIR/CRASH/Q4Analysis.out.tar $mainDIR/CRASH/Q4Analysis.out
	gzip $mainDIR/CRASH/Q4Analysis.out.tar
	rm $mainDIR/CRASH/Q4Analysis.out.tar
	rm $mainDIR/CRASH/Q4Analysis.out
	rm -r q4lib
	rm .q4_hist

} > /dev/null 2> /dev/null

if [ $(ls -1 /var/adm/crash | wc -l) -gt  "0" ] > /dev/null
then
	mkdir $mainDIR/CRASH
	if [ $1 = "-c" ] 2> /dev/null
	then	
		echo Running Crash Analysis
		crashinfo
	fi
else
	mkdir $mainDIR/CRASH
fi

date > $mainDIR/Sys_Info

ioscanSummary()
{
	echo "Summarizing IO scan" >> $mainDIR/AdeptoLOG
	echo Ioscan Summary > $mainDIR/Summary
	echo >> $mainDIR/Summary
	cat $mainDIR/Hardware/Ioscan-fn | grep -i no_hw >> $mainDIR/Summary
	cat $mainDIR/Hardware/Ioscan-fn | grep -i unclaimed >> $mainDIR/Summary
	echo ----------------------------------------------------------------------------------------------------- >> $mainDIR/Summary
	echo >> $mainDIR/Summary
}

syslogSummary()
{
	echo "Summarizing Syslog and Syslog.OLD" >> $mainDIR/AdeptoLOG
	echo Syslog.log Summary >> $mainDIR/Summary
	echo >> $mainDIR/Summary
	cat $mainDIR/Logs/Syslog | grep -i -e fail -e unable>> $mainDIR/Summary
	cat $mainDIR/Logs/Syslog | grep -i error >> $mainDIR/Summary
	cat $mainDIR/Logs/Syslog | grep -i "not responding" >> $mainDIR/Summary
	echo >> $mainDIR/Summary

	echo ----------------------------------------------------------------------------------------------------- >> $mainDIR/Summary
	echo OLDSyslog.log Summary >> $mainDIR/Summary
	cat $mainDIR/Logs/OLDSyslog | grep -i -e fail -e unable>> $mainDIR/Summary
	cat $mainDIR/Logs/OLDSyslog | grep -i error>> $mainDIR/Summary
	cat $mainDIR/Logs/OLDSyslog | grep -i "not responding" >> $mainDIR/Summary
	echo >> $mainDIR/Summary
}

ifroot()
{
	if [[ $(id -un) != root ]]
	then
		echo "ADEPTO IS BEING RUN WITHOUT ROOT PRIVILEGES" >> $mainDIR/AdeptoLOG
		echo "This account does not have root privileges."
		echo "This was run without root privileges" > $mainDIR/warnings
	fi
}

ifroot

createHTML()
{
echo "Creating HTML" >> $mainDIR/AdeptoLOG
cd $mainDIR
mkdir HTML
echo "<HTML><BODY></BODY></HTML>" >> HTML/Blank.html

echo "<HTML>
<frameset rows="70,*" meborder="0" border="0" framespacing="0" >
	<frame src="Header.html" name="header" scrolling="no" >
	<frameset cols="310,*" meborder="0" border="0" framespacing="0" >
	<frame src="Main.html" name="menu" scrolling="no" >
	<frame src="Blank.html" name="main" >
	</frameset>
</HTML>" >> HTML/index.html

echo "<HTML><BODY><body bgcolor="#CFECEC">
<TABLE width="860"><TR>
	<TD><H2>${0##*/}</H2></TD>
	<TD align="center"><H3>Located on Host: $(hostname)</H3></TD>
	<TD align="center"><H3>Run on: $(date)</H3></TD>
</TR></TABLE>
</BODY></HTML>" >> HTML/Header.html

dirs=$(ls -Fd * | grep "/" | grep -v "HTML" | sed -e "s/\///g")
other=$(ls -Fd * | grep -v "/" | sed -e 's/\*//g')

echo "<HTML>
<BODY>
<body bgcolor="#CFECEC">
<H3>Main Menu</H3>
<BR>" >> HTML/Main.html

for page in $dirs
do
	echo "<A HREF = "'"'$page.html'"' target='"'menu'"' "> $page </A><BR>" >> HTML/Main.html
done

for page in $other
do
        echo "<A HREF = "'"'..\/$page'"' target='"'main'"' "> File: $page </A><BR>"  >> HTML/Main.html
done

echo "<BR>" >> HTML/Main.html
echo "</BODY>
</HTML>" >> HTML/Main.html

for page in $dirs
do
        echo "<HTML>
        <BODY>
        <body bgcolor="#CFECEC">
        <b>$page Menu</b><BR>
        <BR>" >> HTML/$page.html

	for pg in $(ls -p $page | grep -v HTML | grep "/" | sed -e "s/\///g" )
        do
               	echo "<A HREF = "'"'.\/$pg.html'"' target='"'menu'"' "> /$pg </A><BR>"  >> HTML/$page.html
        done

	for pg in $(ls -p $page | grep -v HTML | grep -v "/")
        do
            	echo "<A HREF = "'"'..\/$page\/$pg'"' target='"'main'"' "> File: $pg </A><BR>"  >> HTML/$page.html
        done

	echo "<BR><BR><A HREF = "'"'.\/Main.html'"' target='"'menu'"' "> Main Menu </A><BR>" >> HTML/$page.html

	for pgtree in $(ls -R $page | grep ":" | sed -e "s/://g")
	do
		strip=${pgtree##*/}	
		echo "<HTML>
       		<BODY>
		<body bgcolor="#CFECEC">
       		<b>$pgtree Menu</b><BR>
		<BR>" >> HTML/$strip.html

		temp=$(ls -p $pgtree | grep "/" | sed -e "s/\///g" )
		for pg in $temp
		do
				echo "<A HREF = "'"'.\/$pgtree\/$pg.html'"' target='"'menu'"' "> /$pg </A><BR>"  >> HTML/$strip.html
		done

		temp=$(ls -p $pgtree | grep -v "/" )
                for pg in $temp
                do
                                echo "<A HREF = "'"'..\/$pgtree\/$pg'"' target='"'main'"' "> File: $pg </A><BR>"  >> HTML/$strip.html
                done

		pd=HTML/$pgtree
		pdnm=${pd##*/}
		backPage=$(echo $pd | awk -F'/' '{print $(NF -1)}')
		pdr=$(echo $pd | sed -e "s/\/$pdnm//g")
		echo "<BR><A HREF = "'"'.\/$backPage.html'"' target='"'menu'"' "> Back </A><BR>" >> HTML/$strip.html
		echo "<BR><A HREF = "'"'.\/Main.html'"' target='"'menu'"' "> Main Menu </A><BR>" >> HTML/$strip.html

		echo "</BODY>
		</HTML>" >> HTML/$strip.html
	done

	echo "<BR>" >> HTML/$page.html
	echo "</BODY>
	</HTML>" >> HTML/$page.html
done
cd $rootDir
}

# MAIN --------------------------------------------------------------------

divider()
{
	print - "@@@@@@ $1"
}

{

sleep 2
update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"

#main routine
{
	echo "Running Single File Mode" >> $mainDIR/AdeptoLOG
	echo "Run on" $(date +%d-%m-%Y)
	echo ""
	divider uname
	uname -a
	divider model
	/usr/bin/model
	divider ioscanF
	/usr/sbin/ioscan -F
	divider ioscanfn
	/usr/sbin/ioscan -fn
	divider cstm

	echo "iop reportinfo;gop cstm cat;gop stop no;selall;info;wait;il"| /usr/sbin/cstm

	divider eisa
	/sbin/eisa_config 2> /dev/null <<- EOF
		q
	EOF

	divider armdsp
	if [ -s /opt/sanmgr/commandview/client/sbin/armdsp -i ] > /dev/null 2> /dev/null
	then
        for arms in $(/opt/sanmgr/commandview/client/sbin/armdsp -i | grep Alias | sed -e "s/ //g" | cut -f 2,2 -d ":")

        do
                /opt/sanmgr/commandview/client/sbin/armdsp -a $arms 
        done
	fi

	divider amdsp
	if [ -s /opt/hparray/bin/amdsp -i ] > /dev/null 2> /dev/null
	then
        	for ams in $(/opt/hparray/bin/amdsp -i | sed -e "s/ //g" | grep Alias | cut -f 3,3 -d ":")

        	do
                	/opt/hparray/bin/amdsp -a $ams
        	done
	fi

	divider arraydsp
	if [ -s /opt/hparray/bin/arraydsp -i ] > /dev/null 2> /dev/null
	then
        	for arr in $(/opt/hparray/bin/arraydsp -i | grep Alias | sed -e "s/ //g" | cut -f 3,3 -d ":")

        	do
                	/opt/hparray/bin/arraydsp -a $arr 
        	done
	fi

	divider swlist
	/usr/sbin/swlist -v

	/usr/sbin/swlist -l fileset
	
	divider print_manifest
	if [ -s /opt/ignite/bin/print_manifest ] > /dev/null 2> /dev/null
	then
        	/opt/ignite/bin/print_manifest
	fi		

	divider lvmtab
	strings /etc/lvmtab
	
	divider mnttab
	strings /etc/mnttab

	divider fstab
	strings /etc/fstab

	divider inittab
	strings /etc/inittab

	divider vgdisplay
	vgdisplay -v /dev/vg00

	divider sautil
	if [ -s /opt/raidsa/bin/sautil ] > /dev/null 2> /dev/null
	then
        	echo "Fetching sasmgr output" >> $mainDIR/AdeptoLOG
        	for dev in $(ls /dev/ciss*)
        	do
                	/opt/raidsa/bin/sautil $dev
                	/opt/raidsa/bin/sautil $dev scan
        	done
	fi

	divider machinfo
	machinfo

	if [ -s /usr/sbin/parstatus ] > /dev/null 2>/dev/null
	then
		divider parstatus
		/usr/sbin/parstatus
	fi

} >>$mainDIR/mvss.$(hostname) 2> /dev/null

update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"

date > $mainDIR/Sys_Info
uname -a >> $mainDIR/Sys_Info
model >> $mainDIR/Sys_Info
echo "" >> $mainDIR/Sys_Info
echo "Uptime:" >> $mainDIR/Sys_Info
uptime >> $mainDIR/Sys_Info

echo "Fetching contents of CRASH directory" >> $mainDIR/AdeptoLOG
ll /var/adm/crash > $mainDIR/CRASH/CrashFileList
cat /var/tombstones/ts99 > $mainDIR/CRASH/TS99

mkdir $mainDIR/Hardware
echo "Performing HARDWARE information gathering" >> $mainDIR/AdeptoLOG
echo "Fetching IO scan" >> $mainDIR/AdeptoLOG
/usr/sbin/ioscan -fn > $mainDIR/Hardware/Ioscan-fn
echo "Fetching CSTM" >> $mainDIR/AdeptoLOG
echo "sall;info;wait;il" | /usr/sbin/cstm > $mainDIR/Hardware/CSTM  

if [ $(/usr/sbin/icod_stat | wc -l) -gt "1" ] > /dev/null
then
	echo "Fetching icod_stat" >> $mainDIR/AdeptoLOG
	/usr/sbin/icod_stat > $mainDIR/Hardware/ICOD
fi

if [ -s /opt/sas/bin/sasmgr ] > /dev/null 2> /dev/null
then
	echo "Fetching sasmgr output" >> $mainDIR/AdeptoLOG
	for dev in $(ls /dev/sasd*)
	do
		echo "Fetching details for device" $dev >> $mainDIR/Hardware/SasMgrOUT
		/opt/sas/bin/sasmgr get_info $dev -q raid >> $mainDIR/Hardware/SasMgrOUT
		echo >> $mainDIR/Hardware/SasMgrOUT
	done
fi

if [ -s /opt/raidsa/bin/sautil ] > /dev/null 2> /dev/null
then
        echo "Fetching sautil output" >> $mainDIR/AdeptoLOG
        for dev in $(ls /dev/ciss*)
        do
                echo "Fetching details for devicae" $dev >> $mainDIR/Hardware/SautilOUT
                /opt/raidsa/bin/sautil $dev >> $mainDIR/Hardware/SautilOUT
		/opt/raidsa/bin/sautil $dev scan >> $mainDIR/Hardware/SautilOUT
        done   
fi

update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"
 
mkdir $mainDIR/Software 
echo "Performing SOFTWARE information gathering" >> $mainDIR/AdeptoLOG
echo "Fetching swlist" >> $mainDIR/AdeptoLOG
/usr/sbin/swlist -l bundle > $mainDIR/Software/SWBundle  
ll /etc > $mainDIR/Software/ETCls-l
/usr/sbin/swlist -l product > $mainDIR/Software/SWProduct  
ps -ef | grep defunct > $mainDIR/Software/ProcsDefunct

if [ -s /opt/hparray/bin/amdsp -i ] > /dev/null 2> /dev/null
then
	echo "Fetching amdsp information" >> $mainDIR/AdeptoLOG
	mkdir -p $mainDIR/Arrays 
	/opt/hparray/bin/amdsp -i > $mainDIR/Arrays/Amdsp
	for ams in $(/opt/hparray/bin/amdsp -i | sed -e "s/ //g" | grep Alias | cut -f 3,3 -d ":") 
	do
		/opt/hparray/bin/amdsp -a $ams >> $mainDIR/Arrays/Amdsp
	done
fi

if [ -s /opt/hparray/bin/arraydsp -i ] > /dev/null 2> /dev/null
then
	echo "Fetching arraydsp information" >> $mainDIR/AdeptoLOG
	mkdir -p $mainDIR/Arrays 
	/opt/hparray/bin/arraydsp -i > $mainDIR/Arrays/Arraydsp 
	for arr in $(/opt/hparray/bin/arraydsp -i | grep Alias | sed -e "s/ //g" | cut -f 3,3 -d ":") 
	do
		/opt/hparray/bin/arraydsp -a $arr >> $mainDIR/Arrays/Arraydsp
	done
fi

if [ -s /opt/sanmgr/commandview/client/sbin/armdsp -i ] > /dev/null 2> /dev/null
then
	echo "Fetching armdsp information" >> $mainDIR/AdeptoLOG
	mkdir -p $mainDIR/Arrays 
	armdsp -i > $mainDIR/Arrays/Armdsp
	for arms in $(/opt/sanmgr/commandview/client/sbin/armdsp -i | grep Alias | sed -e "s/ //g" | cut -f 2,2 -d ":") 
	do
		/opt/sanmgr/commandview/client/sbin/armdsp -a $arms >> $mainDIR/Arrays/Armdsp
	done
fi

update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"

mkdir $mainDIR/Config
echo "Performing CONFIG mapping" >> $mainDIR/AdeptoLOG
echo "Fetching lvmtab" >> $mainDIR/AdeptoLOG
strings /etc/lvmtab > $mainDIR/Config/Lvmtab
echo "Fetching kmtune" >> $mainDIR/AdeptoLOG
/usr/sbin/kmtune > $mainDIR/Config/Kmtune
echo "Fetching mnttab" >> $mainDIR/AdeptoLOG
strings /etc/mnttab > $mainDIR/Config/Mnttab
echo "Fetching fstab" >> $mainDIR/AdeptoLOG 
strings /etc/fstab > $mainDIR/Config/Fstab
echo "Fetching inittab" >> $mainDIR/AdeptoLOG
strings /etc/inittab > $mainDIR/Config/Inittab
echo "Fetching inetd.conf" >> $mainDIR/AdeptoLOG
cat /etc/inetd.conf > $mainDIR/Config/Inetd.conf
echo "Fetching syslog.conf" >> $mainDIR/AdeptoLOG
cat /etc/syslog.conf > $mainDIR/Config/Syslog.conf
echo "Fetching setboot" >> $mainDIR/AdeptoLOG
/usr/sbin/setboot > $mainDIR/Config/Setboot
echo "Fetching lvlnboot" >> $mainDIR/AdeptoLOG
/usr/sbin/lvlnboot > $mainDIR/Config/Lvlnboot -v
echo "Fetching bdf" >> $mainDIR/AdeptoLOG
/usr/bin/bdf > $mainDIR/Config/BDF
echo "Fetching vgdisplay" >> $mainDIR/AdeptoLOG
/usr/sbin/vgdisplay -v > $mainDIR/Config/VGDisplay 
echo "Analyzing memory configuration" >> $mainDIR/AdeptoLOG
echo "selclass qualifier memory;info;wait;infolog"|cstm > $mainDIR/Config/MemConfig
echo "Fetching crontab" >> $mainDIR/AdeptoLOG
/usr/bin/crontab -l > $mainDIR/Config/Crontab
echo "Attempting to fecth RAD" >> $mainDIR/AdeptoLOG

update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"

chmod 755 kdbin
{ ./kdbin $mainDIR & } 2> $mainDIR/kdbPID
kdbpid=$(cat $mainDIR/kdbPID | cut -d "]" -f 2- | sed -e "s/[^a-zA-Z0-9]//g")
rm $mainDIR/kdbPID

#if (echo 'itick_per_usec/D' | adb -k /stand/vmunix /dev/kmem)> /dev/null 
#then
#	echo "Fetching processor speed" >> $mainDIR/AdeptoLOG
#	echo 'itick_per_usec/D' | adb -k /stand/vmunix /dev/kmem >> $mainDIR/Config/CPUSpeed
#	echo "Fetching kernel location" >> $mainDIR/AdeptoLOG
#	echo 'boot_string/S' | adb /stand/vmunix /dev/kmem >> $mainDIR/Config/KernelLocation
#	echo "Fetching size of physical memory" >> $mainDIR/AdeptoLOG
#	echo 'phys_mem_pages/D' | adb /stand/vmunix /dev/kmem >> $mainDIR/Config/PhysicalMem
#fi

if [ $(ls /dev/td** | wc -l) -gt 0 ]
then
	echo "Fetching FCMSUTIL for all FC devices" >> /$mainDIR/AdeptoLOG
        for fdev in $(ls /dev/td**)
        do      
		echo Probing $fdev >> $mainDIR/Config/FCMSUtilStat
                /opt/fcms/bin/fcmsutil $fdev >> $mainDIR/Config/FCMSUtilStat
		/opt/fcms/bin/fcmsutil $fdev get remote all >> $mainDIR/Config/FCMSUtilNports
        done
fi

if [ -s /opt/hpvm/bin/hpvmstatus ] > /dev/null 2> /dev/null
then
	mkdir $mainDIR/Config/VM
	echo "Fetching VMstatus for all Virtual Machines" >> $mainDIR/AdeptoLOG
	for vm in $(/opt/hpvm/bin/hpvmstatus | tail +4 | cut -d " " -f 1)
	do
		echo "******* Fetching info for VM: $vm ********" >> $mainDIR/Config/VM/VMStatus
		/opt/hpvm/bin/hpvmstatus -P $vm	>> $mainDIR/Config/VM/VMStatus
		echo >> $mainDIR/Config/VM/VMStatus
	done

	for sw in $(/opt/hpvm/bin/hpvmnet | tail +3 | cut -d " " -f 1)
        do
                echo "Fetching info for VM switch: $sw" >> $mainDIR/Config/VM/VMSwitchInfo
                /opt/hpvm/bin/hpvmnet -S $sw >> $mainDIR/Config/VM/VMSwitchInfo
                echo >> $mainDIR/Config/VM/VMSwitchInfo
        done    
	
	/opt/hpvm/bin/hpvmstatus -M >> $mainDIR/Config/VM/VMStatusM
	cat /etc/rc.config.d/hpvmconf >> $mainDIR/Config/VM/VMConfFile
fi

if [ -s /opt/ignite/bin/print_manifest ] > /dev/null 2>> $mainDIR/Config/PrintManifest
then
        echo "Fetching print_manifest" >> $mainDIR/AdeptoLOG
        /opt/ignite/bin/print_manifest > $mainDIR/Config/PrintManifest
fi

echo "Fetching pvdisplay of all disks" >> $mainDIR/AdeptoLOG
for disk in $(strings /etc/lvmtab | grep dsk)
do
	/usr/sbin/pvdisplay -v $disk >> $mainDIR/Config/PVDisplay; echo >> $mainDIR/Config/PVDisplay
done

echo "Fetching diskinfo of all disks" >> $mainDIR/AdeptoLOG
for disk in $(strings /etc/lvmtab | grep dsk | sed -e "s/dev\//dev\/r/g")
do
	/usr/sbin/diskinfo $disk >> $mainDIR/Config/DiskInfo; echo >> $mainDIR/Config/DiskInfo
done

echo "Fetching lvdisplay of all lvols" >> $mainDIR/AdeptoLOG
for volume in $(find /dev -name group | cut -f 3,3 -d "/") 
do
	for lvs in $(ls /dev/$volume/*)
	do
		if (file $lvs | grep block) > /dev/null
		then
                	/usr/sbin/lvdisplay -v $lvs >> $mainDIR/Config/LVDisplay 
		fi
	done
done

if [ -s /usr/contrib/bin/machinfo ] > /dev/null 2> /dev/null
then
        echo "Fetching machinfo" >> $mainDIR/AdeptoLOG
        /usr/contrib/bin/machinfo >> $mainDIR/Config/Machinfo
fi

if [ -s /usr/bin/rad ]> /dev/null 2> /dev/null
then
	echo "Fetching RAD Output" >> $mainDIR/AdeptoLOG
	/usr/bin/rad -q > $mainDIR/Config/RADqOut
elif [ -s /usr/bin/olrad ] > /dev/null 2> /dev/null
then
	echo "Fetching OLRAD Output" >> $mainDIR/AdeptoLOG
	/usr/bin/olrad -q > $mainDIR/Config/OLRADq
fi

update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"

mkdir $mainDIR/Network
echo "Performing NETWORK information gathering" >> $mainDIR/AdeptoLOG
echo "Fetching netstat" >> $mainDIR/AdeptoLOG
/usr/bin/netstat -rn > $mainDIR/Network/NetstatRN
/usr/bin/netstat -an > $mainDIR/Network/NetstatAN
/usr/bin/netstat -in > $mainDIR/Network/NetstatIN
echo "Fetching lanscan" >> $mainDIR/AdeptoLOG
/usr/sbin/lanscan -v > $mainDIR/Network/LanscanV
/usr/sbin/lanscan > $mainDIR/Network/Lanscan
/usr/bin/netstat -an | grep -e Proto -e 2049 > $mainDir/Network/NetstatUDPbufferSize
/usr/bin/netstat  -s | grep overflow > $mainDir/Network/NetstatUDPoverflow
echo "Fetching network services" >> $mainDIR/AdeptoLOG
cat /etc/services > $mainDIR/Network/NetServices
echo "Fetching inetd.sec" >> $mainDIR/AdeptoLOG
cat /var/adm/inetd.sec > $mainDIR/Network/InetdSec
echo "Fetching rc.config.d stats" >> $mainDIR/AdeptoLOG
cat /etc/rc.config.d/netdaemons > $mainDIR/Network/NetDaemons
/usr/bin/netstat > $mainDIR/Network/NetStat
cat /etc/rc.config.d/hp* > $mainDIR/Network/HPBall 
#echo "Fetching etc hosts" >> $mainDIR/AdeptoLOG
#cat /etc/hosts > $mainDIR/Network/Hosts
echo "Fetching nsswitch" >> $mainDIR/AdeptoLOG
cat /etc/nsswitch.* > $mainDIR/Network/NsswitchAll
echo "Fetching netfmt logs" >> $mainDIR/AdeptoLOG
/usr/sbin/netfmt /var/adm/nettl.LOG0* > $mainDIR/Network/NettlLogs
if [ -s /etc/rc.config.d/hp_apaconf ]
then
	echo "Fetching hp_apconf" >> $mainDIR/AdeptoLOG
	cat /etc/rc.config.d/hp_apaconf > $mainDIR/Network/AutoPortAggregation
fi

mkdir $mainDIR/Network/NFS
echo "Analyzing NFS structure" >> $mainDIR/AdeptoLOG
echo "Fetching nfsconf" >> $mainDIR/AdeptoLOG
cat /etc/rc.config.d/nfsconf > $mainDIR/Network/NFS/NFSconf
echo "Fetching etc exports" >> $mainDIR/AdeptoLOG
cat /etc/exports > $mainDIR/Network/NFS/Exports
echo "Fetching showmount" >> $mainDIR/AdeptoLOG
/usr/sbin/showmount -a > $mainDIR/Network/NFS/ShowmountA
echo "Fetching rpcinfo" >> $mainDIR/AdeptoLOG
/usr/bin/rpcinfo > $mainDIR/Network/NFS/Rpcinfo
/usr/bin/rpcinfo -p localhost | grep -i nfs >  $mainDIR/Network/NFS/NFSVersion
/usr/bin/rpcinfo -s > $mainDIR/Network/NFS/Rpcinfos
/usr/bin/rpcinfo -u localhost mountd > $mainDIR/Network/NFS/Rpcinfo_mountd
echo "Fetching nfsstat" >> $mainDIR/AdeptoLOG
/usr/bin/nfsstat -m > $mainDIR/Network/NFS/NFSstatM
/usr/bin/nfsstat -c > $mainDIR/Network/NFS/NFSstatC
/usr/bin/nfsstat -n > $mainDIR/Network/NFS/NFSstatN
/usr/bin/nfsstat -r > $mainDIR/Network/NFS/NFSstatR
/usr/bin/nfsstat -s > $mainDIR/Network/NFS/NFSstatS
echo "Fetching xtab" >>$mainDIR/AdeptoLOG
cat /etc/xtab > $mainDIR/Network/NFS/Xtab
echo "Fetching rmtab" >> $mainDIR/AdeptoLOG
cat /etc/rmtab > $mainDIR/Network/NFS/Rmtab

mkdir $mainDIR/Sar
echo "Fetching SAR output" >> $mainDIR/AdeptoLOG
/usr/sbin/sar -v 1 10 > $mainDIR/Sar/SarV
/usr/sbin/sar -d 1 10 > $mainDIR/Sar/SarD

update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"

if [ -s /usr/sbin/parstatus ]> /dev/null 2> /dev/null
then
	echo "Fetching parstatus" >> $mainDIR/AdeptoLOG
	mkdir -p $mainDIR/PartitionINFO
	/usr/sbin/parstatus > $mainDIR/PartitionINFO/Parstatus
fi

if [ -s /usr/sbin/vparstatus ] > /dev/null 2> /dev/null
then
	echo "Fetching vPar status" >> $mainDIR/AdeptoLOG
	mkdir -p $mainDIR/PartitionINFO
	/usr/sbin/vparstatus > $mainDIR/PartitionINFO/VParstatus
fi

mkdir -p $mainDIR/Logs/Other
echo "Obtaining other logs setup in syslog.conf" >> $mainDIR/AdeptoLOG

for file in $(cat /etc/syslog.conf | grep / | grep -v console | grep -v /var/adm/syslog/syslog.log | cut -d / -f 2-)
do
	if [ -s /$file ]
	then 
        	cat /$file > $mainDIR/Logs/Other/$(basename $file)
	fi
done

echo "Fetching machine serial" >> $mainDIR/AdeptoLOG
getconf CS_MACHINE_SERIAL > $mainDir/Logs/serial
echo "Fetching syslog" >> $mainDIR/AdeptoLOG
cat /var/adm/syslog/syslog.log > $mainDIR/Logs/Syslog
echo "Fetching syslog.OLD" >> $mainDIR/AdeptoLOG
cat /var/adm/syslog/OLDsyslog.log > $mainDIR/Logs/OLDSyslog
echo "Fetching rc.log" >> $mainDIR/AdeptoLOG
cat /etc/rc.log > $mainDIR/Logs/RCLog
echo "Fetching automount.log" >> $mainDIR/AdeptoLOG
cat /var/adm/automount.log > $mainDIR/Logs/Automount
echo "Fetching bdf" >> $mainDIR/AdeptoLOG
/usr/bin/bdf > $mainDIR/Logs/BDF
echo "Fetching manifest.info" >> $mainDIR/AdeptoLOG
cat /var/opt/ignite/local/manifest/manifest.info > $mainDIR/Logs/mainfestLog
echo "Fetching machine serial" >> $mainDIR/AdeptoLOG
getconf MACHINE_SERIAL >> $mainDir/Logs/serial
echo "Fetching ts99" >> $mainDIR/AdeptoLOG
cat /var/tombstones/ts99 > $mainDIR/Logs/TS99
echo "Fetching dmesg" >> $mainDIR/AdeptoLOG
/usr/sbin/dmesg > $mainDIR/Logs/Dmesg
echo "Fetching user.log" >> $mainDIR/AdeptoLOG
cat /var/opt/soe/log/user.log > $mainDIR/Logs/UserLogs
echo "Fetching ts98" >> $mainDIR/AdeptoLOG
cat /var/tombstones/ts98 > $mainDIR/Logs/TS98
echo "Fetching EMS" >> $mainDIR/AdeptoLOG
cat /var/opt/resmon/log/event.log > $mainDIR/Logs/EMSLog
echo "Fetching shutdownlog" >> $mainDIR/AdeptoLOG
cat /etc/shutdownlog > $mainDIR/Logs/Shutdownlog
echo "Fetching ps list" >> $mainDIR/AdeptoLOG
/usr/bin/ps -ef > $mainDIR/Logs/ps-ef
echo "Fetching processor CPU usage" >> $mainDIR/AdeptoLOG
UNIX95= ps -e -o pcpu -o ruser -o args | sort -nr | grep -v %CPU | head -10 > $mainDIR/Logs/ProcCPUusage
echo "Fetching processor memory usuage" >> $mainDIR/AdeptoLOG
UNIX95= ps -e -o ruser,vsz,pid,ppid,args | sort -rnk2 | head -10 > $mainDIR/Logs/ProcMEMusage
echo "Fetching SAM Log" >> $mainDIR/AdeptoLOG
cat /var/sam/log/samlog >> $mainDIR/Logs/SAMLog
echo "Fetching swapinfo" >> $mainDIR/AdeptoLOG
/usr/sbin/swapinfo -mt >> $mainDIR/Logs/Swapinfo
echo "Fetching IPCS" >> $mainDIR/AdeptoLOG
/usr/bin/ipcs -mpb >> $mainDIR/Logs/SharedMem
echo "Fetching KCUsage" >> $mainDIR/AdeptoLOG
/usr/sbin/kcusage >> $mainDIR/Logs/KCUsage
echo "\n\n\nOutut of kcusage -th" >> $mainDIR/Logs/KCUsage
/usr/sbin/kcusage -th >> $mainDIR/Logs/KCUsage
echo "\n\n\nOutut of kcusage -td" >> $mainDIR/Logs/KCUsage
/usr/sbin/kcusage -td >> $mainDIR/Logs/KCUsage
echo "\n\n\nOutut of kcusage -tm" >> $mainDIR/Logs/KCUsage
/usr/sbin/kcusage -tm >> $mainDIR/Logs/KCUsage

update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"

#******* TEST IF HP-UX 11.3x ****************************

if [ $(uname -r | awk '{print substr($0,length - 1)}' | sed -e 's/[0-9]$//g') = 3 ]
then
	echo " Performing HP-UX 11.31 disk name conversion" >> $mainDIR/AdeptoLOG
	/usr/sbin/ioscan -m dsf > $mainDIR/Hardware/DiskTranslation
fi 

#******* TEST IF PART OF CLUSTER ************************

if [ -s /etc/cmcluster/cmclnodelist ]
then
	echo "Fetching cluster configuration" >> $mainDIR/AdeptoLOG
	mkdir $mainDIR/Cluster
	/usr/sbin/cmviewcl -v > $mainDIR/Cluster/Cmviewcl 2> /dev/null 
	/usr/sbin/cmscancl -o $mainDIR/Cluster/Cmscancl 2> /dev/null > /dev/null
fi

#******* MP/GSP Log fetch *******************************

mod=$(model | cut -d / -f 3) 2> /dev/null
modchar=$(model | cut -d / -f 3 | sed -e "s/[a-zA-Z]//g" | cut -c 1,2) 2> /dev/null

rp44() {
sleep 4;echo $user
sleep 2;echo $pass
sleep 4;echo cm
sleep 2;echo ps
sleep 2;echo ss
sleep 2;echo sysrev
sleep 2; echo '\003'
}

rp84() {
sleep 4;echo $user
sleep 2;echo $pass
sleep 4;echo cm
sleep 2;echo cp
sleep 2;echo df
sleep 2;echo d
sleep 2;echo a
sleep 12;echo s
sleep 9;echo sysrev
sleep 2; echo '\003'
}

sdome() {
sleep 4;echo $user
sleep 2;echo $pass
sleep 4;echo cm
sleep 2;echo df
sleep 2;echo d
sleep 2;echo a
sleep 2;echo 0
sleep 12;echo s
sleep 12;echo cp
sleep 2;echo io
sleep 2;echo sysrev
sleep 2; echo '\003'
}

analyzeSD() {
echo "Analyzing Superdome layout" >> $mainDIR/AdeptoLOG

chs=$(cat $mainDIR/MP/mpOut | grep "IO Chas" | sed -e "s/[A-Z][a-z]*//g" | sed -e "s/ //g" | sed -e "s/\|//g")
bay=$(cat $mainDIR/MP/mpOut | grep "IO Bay" | sed -e "s/[A-Z][a-z]*//g" | sed -e "s/ //g" | sed -e "s/\|//g")

length=$(echo $chs | wc -c)
count=0
pos=0

while [ $count -le $length ]
do
        count=$(expr $count + 1)
        char=$(echo $chs | cut -c $count)

        if [ "$char" -eq "1" -o "$char" -eq "3" ]
        then
                positions[$pos]=$count
                pos=$(expr $pos + 1)
        fi
done
length=${#positions[*]}

for ps in ${positions[*]}
do
        echo "" >> $mainDIR/MP/SDLayout
        echo "Cellboard in slot $ps is connected to:" >> $mainDIR/MP/SDLayout
        echo "IO Bay $(echo $bay | cut -c $ps)" >> $mainDIR/MP/SDLayout
        echo "IO Chassis $(echo $chs | cut -c $ps)" >> $mainDIR/MP/SDLayout
done

strt=$(cat $mainDIR/MP/mpOut | grep -n "MP:CM> cp" | cut -d : -f 1)
end=$(cat $mainDIR/MP/mpOut | grep -n "MP:CM> io" | cut -d : -f 1)

cat $mainDIR/MP/mpOut | sed -n "$strt,$end p" | grep "X" > mpcp
cat mpcp | sed -e "s/\|/*/g" | cut -d \* -f 1 > names
pos=1

while [ $pos -le $(cat names | wc -l) ]
do
        parname=$(cat names | sed -n "$pos,$pos p")

        parstr=$(cat mpcp | grep -i "$parname" | sed -e "s/$parname//g" | sed -e "s/ //g" | sed -e "s/\|//g")
        parstrlen=$(echo $parstr | wc -c)

        count=0
	
	echo ""
        echo "Partition $parname contains:" >> $mainDIR/MP/SDLayout

        while [ $count -le $parstrlen ]
        do
               count=$(expr $count + 1)
                char=$(echo $parstr | cut -c $count)

                if [ "$char" = "X" ]
                then
                if [ "$count" -le 8 ]   
                        then    
                                echo "Cabinet 0, Cellboard $count" >> $mainDIR/MP/SDLayout
                        else    
                                echo "Cabinet 1, Cellboard $(expr $count - 8)" >> $mainDIR/MP/SDLayout  
                        fi      

                fi
        done

        pos=$(expr $pos + 1)
done

rm mpcp 2> /dev/null
rm names 2> /dev/null
}

if [ $1 = "-mp" ] 2> /dev/null
then
	echo "Performing MP connect and crawl" >> $mainDIR/AdeptoLOG
	mkdir $mainDIR/MP

        if [ $mod = "Superdome" -o $modchar = "32" ]
        then
                sdome | telnet -e c $ipaddr > $mainDIR/MP/mpOut
		analyzeSD
        elif [ $modchar = "44" -o $modchar = "31" -o $modchar = "34" ]
        then
                rp44 | telnet -e c $ipaddr > $mainDIR/MP/mpOut
        elif [ $modchar = "7" -o $modchar = "16" ]
        then
                rp84 | telnet -e c $ipaddr > $mainDIR/MP/mpOut
        else
                echo "Some other model found" > $mainDIR/MP/mpOut
        fi
fi

chmod 755 swainv
./swainv
echo "Generating inventory.xml" >> $mainDIR/AdeptoLOG
mv inventory.xml $mainDIR

chmod 755 getsn
./getsn > $mainDIR/serialNumber

update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"

ioscanSummary
syslogSummary
createHTML

} 2> $mainDIR/RunTimeErrors

# Zip the directory using tar and gzip
echo "Performing tar and gzip on generated directory" >> $mainDIR/AdeptoLOG
echo "Adepto completed on $(date)" >> $mainDIR/AdeptoLOG

 tar cf $mainDIR.tar $mainDIR/
 gzip --best $mainDIR.tar 

# Run Cleanup and remove duplicate files

echo "Performing cleanup" >> $mainDIR/AdeptoLOG
 cleanuptemp

update_spinner
printf "\b\b\b\b\b\b\b\b\b\b\b\b"

echo ""
echo "File Generated: $mainDIR.tar.gz"
#END