#!/bin/sh
###############################################################################
# The arkinfo.sh shell script is provided by the support team to collect 
# diagnostic information from the backup server or the client machine.
###############################################################################

# Setting variables
VERSION="5.3.10"
PS="ps axu"
OSVERSION=`uname -r`
OSALL=`uname -a`
NODENAME=`uname -n | cut -d '.' -f 1`
DF="df -k"
DF_INODE="df -i"
DMESG="dmesg"
ARKEIA_DIR=${ARKEIA_DIR:-`cat /etc/opt/arkeia/ARKEIA_DIR`}
ARKEIA_TMP="$ARKEIA_DIR"/tmp
ARKEIA_ARKEIAD="$ARKEIA_DIR"/arkeiad
ARKEIA_DBASE="$ARKEIA_DIR"/server/dbase
ARKEIA_SERVER="$ARKEIA_DIR"/server
ARKINFO_DIR="$ARKEIA_TMP"/arkinfo
OSTYPE=`uname`   
TAIL=tail

init_os_variables() {
	if [ "$OSTYPE" = "Linux" ]
	then
		PS="ps axuw"
		OSVERSION=`uname -r`
		OSALL=`uname -a`
		DF="df -k"
		DF_INODE="df -i"
	elif [ "$OSTYPE" = "IRIX" -o "$OSTYPE" = "IRIX64" ]
	then
		PS="ps -eaf"
		OSVERSION=`uname -r`
		OSALL=`uname -a`
		DF="df -k"
		DF_INODE="df -i"
		DMESG="hinv -v"
	elif [ "$OSTYPE" = "AIX" ]
	then
		PS="ps axuww"
		OSVERSION=`uname -v`.`uname -r`
		OSALL=`uname -a`
		DF="df -k"
		DF_INODE="df -i"
		DMESG="lscfg"
	elif [ "$OSTYPE" = "HP-UX" ]
	then
		PS="ps -eaf"
		OSVERSION=`uname -r`
		OSALL=`uname -a`
		DF="df -k"
		DF_INODE="df -i"
	elif [ "$OSTYPE" = "SunOS" ]
	then
		PS="ps -eaf"
		OSVERSION=`uname -r`
		OSALL=`uname -a`
		DF="df -k"
		DF_INODE="df -o i"
		TAIL="/usr/xpg4/bin/tail"
	elif [ "$OSTYPE" = "OSF1" ]
	then
		PS="ps -eaf"
		OSVERSION=`uname -r`
		OSALL=`uname -a`
		DF="df -k"
		DF_INODE="df -i"
		DMESG="uerf -R -o full"
	elif [ "$OSTYPE" = "OpenUNIX" -o "$OSTYPE" = "UnixWare" ]
	then
		PS="ps -eaf"
		OSVERSION=`uname -v`
		OSALL=`uname -a`
		DF="df -k"
		DF_INODE="df -i"
		DMESG="/sbin/sdiconfig -l"
	elif [ "$OSTYPE" = "Darwin" ]
	then
		PS="ps auxww"
	fi
}

setoutput ()
{
	output=$1
}

send ()
{
	echo "$@" 2>> $output >>$output
}

myping ()
{
	if [ "$OSTYPE" = "Linux" ]
	then
		ping -d -c1 $1 
	elif [ "$OSTYPE" = "IRIX" -o "$OSTYPE" = "IRIX64" ]
	then
		ping -d -c1 $1 
	elif [ "$OSTYPE" = "AIX" ]
	then
		ping -d -c1 $1 
	elif [ "$OSTYPE" = "HP-UX" ]
	then
		ping $1 -n 1
	elif [ "$OSTYPE" = "SunOS" ]
	then
		ping $1 1
	elif [ "$OSTYPE" = "OSF1" ]
	then
		ping -d -c1 $1 
	elif [ "$OSTYPE" = "OpenUNIX" -o "$OSTYPE" = "UnixWare" ]
	then
		ping $1 64 1
	fi
}


echo " ============================================================ "
echo "	    ARKINFO Version $VERSION Copyright Arkeia Corp 2002 ..... "
echo " ============================================================ "
echo " Arkeia directory => $ARKEIA_DIR"
echo " Arkeia tmp => $ARKEIA_TMP"
echo " Arkeia info directory => $ARKINFO_DIR"
isserver="y"

echo " Before using this script please: "
echo "     - stop all backups running"
echo "     - stop all restorations running"
echo "     - and stop all libraries ( <Libraries Management><Stop> ) "
echo ""
echo ""

rm -fr "$ARKINFO_DIR"
rm "$ARKINFO_DIR"/ARKEIA.tz 2> /dev/null

# Creating directories
mkdir "$ARKEIA_TMP" 2>/dev/null
mkdir "$ARKINFO_DIR" 2>/dev/null

cd "$ARKEIA_DIR"
init_os_variables

# =================== GENERAL INFORMATION ================

echo " ======================================================= "
echo "	    Retrieving general information "
echo " ======================================================= "
echo ""

echo ".... Retrieving operating system information"
echo `uname -a`  > "$ARKINFO_DIR"/sysversion.txt
$PS > "$ARKINFO_DIR"/process.txt
$DF > "$ARKINFO_DIR"/diskinfo.txt
$DF_INODE > "$ARKINFO_DIR"/diskinodes.txt
$DMESG > "$ARKINFO_DIR"/dmesg.txt
if [ "$OSTYPE" = "Linux" ]
then
	mkdir "$ARKINFO_DIR"/osconfig
# Get the network configuration
	cp /etc/hosts "$ARKINFO_DIR"/osconfig
	cp /etc/host.conf "$ARKINFO_DIR"/osconfig
	cp /etc/resolv.conf "$ARKINFO_DIR"/osconfig
	cp /etc/nsswitch.conf "$ARKINFO_DIR"/osconfig
	/sbin/ifconfig -a > "$ARKINFO_DIR"/osconfig/ifconfig.txt

# Get the IPC configuration
	cp /etc/sysctl.conf "$ARKINFO_DIR"/osconfig
	echo "Output of the 'ipcs -l' command: " > "$ARKINFO_DIR"/osconfig/ipcs.txt
	ipcs -l >> "$ARKINFO_DIR"/osconfig/ipcs.txt
	echo "Output of the 'ipcs -a' command: " >> "$ARKINFO_DIR"/osconfig/ipcs.txt
	ipcs -a >> "$ARKINFO_DIR"/osconfig/ipcs.txt
	echo "Output of the 'ipcs -p' command: " >> "$ARKINFO_DIR"/osconfig/ipcs.txt
	ipcs -p >> "$ARKINFO_DIR"/osconfig/ipcs.txt

# Get the GLIBC version
	/lib/libc.so.6 > "$ARKINFO_DIR"/osconfig/glibc_version.txt

# Get the linux distro name and version
	for i in				\
			"slackware-version"	\
			"debian_version"	\
			"knoppix-version"	\
			"redhat-release"	\
			"mandrake-release"	\
			"turbolinux-release"	\
			"SuSE-release"		\
			"cobalt-release"	\
			"gentoo-release"	\
			"yellowdog-release"	\
			"fedora-release"	\
			"UnitedLinux-release"; do
		if [ -f /etc/"$i" ]
		then
			cp /etc/"$i" "$ARKINFO_DIR"/osconfig/distrib_release
		fi
	done

# Get the /proc/scsi
	ls /proc/scsi > "$ARKINFO_DIR"/osconfig/proc_scsi.txt
	cat /proc/scsi/scsi >> "$ARKINFO_DIR"/osconfig/proc_scsi.txt
	grep "" /proc/scsi/*/* >> "$ARKINFO_DIR"/osconfig/proc_scsi.txt
fi

if [ "$OSTYPE" = "SunOS" ]
then
	mkdir "$ARKINFO_DIR"/osconfig
	cp /etc/system "$ARKINFO_DIR"/osconfig
	cp /etc/hosts "$ARKINFO_DIR"/osconfig
	cp /etc/resolv.conf "$ARKINFO_DIR"/osconfig
	cp /etc/netmasks "$ARKINFO_DIR"/osconfig
	cp /etc/devlink.tab "$ARKINFO_DIR"/osconfig
	cp /usr/kernel/drv/sst.conf "$ARKINFO_DIR"/osconfig
	cp /kernel/drv/st.conf "$ARKINFO_DIR"/osconfig
	ls -la /usr/kernel/drv/sparcv9/sst "$ARKINFO_DIR"/osconfig/sst.txt
fi

echo "     done ...."
echo ""


cd "$ARKEIA_DIR"
tar cf "$ARKINFO_DIR"/log.tar log
if [ "$isserver" = y ]
then
	V_ARKEIA_FILE_NAME="v_arkeia.txt"
	echo "arkeia --version: " > "$ARKINFO_DIR"/"$V_ARKEIA_FILE_NAME"
	"$ARKEIA_DIR"/bin/arkeia --version >> "$ARKINFO_DIR"/"$V_ARKEIA_FILE_NAME"
	echo "" >> "$ARKINFO_DIR"/"$V_ARKEIA_FILE_NAME"
	for str in arkfs arkeiad xarkeia arkc `ls -- "$ARKEIA_DIR"/arkeiad/plugin`; do
		if [ -f "$ARKEIA_DIR"/bin/"$str" ]
		then
			echo "$str --version: " >> "$ARKINFO_DIR"/"$V_ARKEIA_FILE_NAME"
			"$ARKEIA_DIR"/bin/"$str" --version >> "$ARKINFO_DIR"/"$V_ARKEIA_FILE_NAME" 2>&1
			echo "" >> "$ARKINFO_DIR"/"$V_ARKEIA_FILE_NAME"
		fi
	done
	echo "How much history log do you want to retrieve (6 months by default)?"
	read answer
	if [ "$answer" = "" ] ; then
		nbmonths="6"
	else
		nbmonths=$answer
	fi
	echo ".... Retrieving the last $nbmonths history logs. Please wait..."
	tar cf "$ARKINFO_DIR"/server.tar	\
		server/server.prf*		\
			`ls -rt server/server*jl2 | $TAIL -n "$nbmonths"`
	echo "     done ...."
	echo ""
fi
cat /etc/opt/arkeia/ARKEIA_DIR > "$ARKINFO_DIR"/ARKEIA_DIR
if [ "$ARKEIA_DIR" != "`cat /etc/opt/arkeia/ARKEIA_DIR`" ]
then
	echo "" >> "$ARKINFO_DIR"/ARKEIA_DIR
	echo "Real ARKEIA_DIR (environment variable): $ARKEIA_DIR" >> "$ARKINFO_DIR"/ARKEIA_DIR
fi

if [ "$isserver" = y ]
then
	echo ".... Retrieving Arkeia Database information"
	cd "$ARKEIA_SERVER"
	tar cf "$ARKINFO_DIR"/dbase.tar dbase/f3tape dbase/f3bkp dbase/f3dpk \
	dbase/f3drv dbase/f3lib dbase/f3per dbase/f3pool \
	dbase/f3sec dbase/f3spk dbase/f3tape dbase/f3dup dbase/f3alert \
	dbase/f3ndmp f3ndmpspk
	echo "     done ...."
	echo ""
	echo ".... Retrieving Arkeia Index information"
	mkdir "$ARKINFO_DIR"/o3dbtree
	cp "$ARKEIA_SERVER"/dbase/o3dbtree/o3_cpnt "$ARKINFO_DIR"/o3dbtree
	ls -al "$ARKEIA_SERVER"/dbase/o3dbtree > "$ARKINFO_DIR"/o3dbtree/ls_al
	echo "     done ...."
	echo ""
	answer="y"
	echo "Do you want to retrieve the top o3_cpnt files (if unsure, say n) (y/n)?" 
	read answer
	echo ""
	if [ "$answer" = y ]
	then
		tar cf "$ARKINFO_DIR"/o3dbtree/o3dbtree.tar `ls "$ARKEIA_DBASE"/o3dbtree/o3_cpnt "$ARKEIA_DBASE"/o3dbtree/*/o3_cpnt "$ARKEIA_DBASE"/o3dbtree/*/*/o3_cpnt`
	fi
fi

if [ -f "$ARKEIA_DIR"/bin/arkchklic ]
then
	echo ".... Checking licenses"
	"$ARKEIA_DIR"/bin/arkchklic > "$ARKINFO_DIR"/arkchklic.txt
	echo "     done ...."
	echo ""
fi

echo ".... Retrieving arkboot information"
cd "$ARKEIA_DIR"
tar cf "$ARKINFO_DIR"/arkboot.tar arkboot
echo "     done ...."
echo ""

echo ".... Retrieving arkobk information"
cd "$ARKEIA_DIR"
tar cf "$ARKINFO_DIR"/arkobk.tar arkobk/arkobk.param arkobk/arkobk.prf
echo "     done ...."
echo ""

echo ".... Retrieving arkc information"
cd "$ARKEIA_DIR"
tar cf "$ARKINFO_DIR"/arkc.tar arkc/arkc.param arkc/arkc.prf
echo "     done ...."
echo ""

echo ".... Retrieving language information"
cd "$ARKEIA_DIR"
tar cf "$ARKINFO_DIR"/lang.tar lang
echo "     done ...."
echo ""

if [ "$isserver" = y ]
then
	# ===================== TREATING DRIVE ===================

	echo " ======================================================= "
	echo "	  Checking drives ..... "
	echo " ======================================================= "
	echo ""

	cd "$ARKEIA_DIR"
	answer="y"
	echo "Do you have problem with a tape drive (y/n)?" 
	read answer
	echo ""
	if [ "$answer" = y ]
	then

		echo ".... Determining available tape drives"

		for i in `grep DRVID "$ARKEIA_DBASE"/f3drv/drvmaster.lst | awk -F\" '{print $4}'`
		do

			cd "$ARKEIA_DBASE"/f3drv/drv$i

			n=`grep NAME drv$i.lst | awk -F\" '{print $2}'`
			for t in `grep DRV_TYPE drv$i.lst | awk -F\" '{print $2}'`
			do
				if [ ! "$t" = NULL ]
				then
					if [ ! "$t" = FILE ] 
					then
						echo "Do you have a problem with your tape drive(s) " $n "? (y/n)"
						read answer
						echo ""

						if [ "$answer" = y ]
						then
							cp ./drv$i.lst "$ARKINFO_DIR"/drive_"${n}".txt 

							for d in `grep "^REWIND_DEV" drv$i.lst | awk -F\" '{print $2}'`
							do
								echo "    checking device :" $d

								setoutput "$ARKINFO_DIR"/drive_"${n}"_mt.txt
								send "==========================================================="
								send "mt result on device $d" 
								mt -f $d status 2>>"$ARKINFO_DIR"/drive_"${n}"_mt.txt 1>&2
							done

						fi
					fi
				fi
			done 
		done 
		echo "     Listing done ...."
		echo ""
	fi
fi 

if [ "$isserver" = y ]
then
	# ===================== TREATING LIBRARY ===================

	echo " ======================================================= "
	echo "	  Checking libraries ..... "
	echo " ======================================================= "
	echo ""

	cd "$ARKEIA_DIR"
	echo "Do you have problem with a library (y/n)? " 
	read answer
	echo ""
	if [ "$answer" = y ]
	then
		echo ".... Listing available libraries"
		echo ""

		for i in `grep LIBID "$ARKEIA_DBASE"/f3lib/libmaster.lst | awk -F\" '{print $4}'`
		do

			cd "$ARKEIA_DBASE"/f3lib/lib$i

			n=`grep NAME lib$i.lst | awk -F\" '{print $2}'`
			for t in `grep LIB_TYPE lib$i.lst | awk -F\" '{print $2}'`
			do
				if [ ! "$t" = FILE ] 
				then
					echo "Do you have a problem with this library " $n "? (y/n)"
					read answer
					echo ""

					if [ "$answer" = y ]
					then
						cp ./lib$i.lst "$ARKINFO_DIR"/lib_"${n}".txt

						for d in `grep ARM_DEV lib$i.lst | awk -F\" '{print $2}'`
						do
							setoutput "$ARKINFO_DIR"/lib_"${n}"_stks.txt
							echo "    checking device :" $d
							send "=============================================================="
							send " arkstks on device $d "
							"$ARKEIA_DIR"/bin/arkdetect -v -d$d -i 2>>"$ARKINFO_DIR"/lib_"${n}"_stks.txt 1>&2
						done

					fi
				fi
			done
		done
		echo "     Listing done ...."
		echo ""
	fi
fi

if [ "$isserver" = y ]
then
	# ===================== TREATING HARDWARE DETECTION ===================

	echo " ======================================================= "
	echo "	  Checking Hardware Detection ..... "
	echo " ======================================================= "
	echo ""

	cd "$ARKEIA_DIR"
	echo "Do you have problem with the backup device detection (y/n)? " 
	read answer
	echo ""
	if [ "$answer" = y ]
	then
		echo ".... performing arkdetect"
		echo ""
		"$ARKEIA_DIR"/bin/arkdetect 2> "$ARKINFO_DIR"/arkdetect.txt 1>&2
		echo "     arkdetect done ...."
		echo ""
	fi
fi

# ===================== TREATING NETWORK PROBLEM ===================

echo " ======================================================= "
echo "	  Checking network ..... "
echo " ======================================================= "
echo ""

cd "$ARKEIA_DIR"
tar cf "$ARKINFO_DIR"/arkeiad.tar arkeiad

echo "Do you have any network problem (y/n)? "
read answer
echo ""
if [ "$answer" = y ]
then

	echo ".... Starting Network check module. This operation may take time !!!" 
	"$ARKEIA_DIR"/bin/chknlp > "$ARKINFO_DIR"/chknlp.txt
	echo "     done ...."
	echo ""

	if [ "$isserver" = y ]
	then
		while [ 1 ]
		do
			echo ""
			echo "Is there a problem accessing another client (y/n) ?"
			read answer
			echo ""

			if [ ! "$answer" = y ]
			then
				break
			else
				echo "Please enter the client's hostname: "
				read host
				echo ".... Checking host "$host" this may take time !!!"
				setoutput "$ARKINFO_DIR"/network.txt
				send " ========================================================="
				send " Checking host $host "
				send " ========================================================="
				myping $host 2>>"$ARKINFO_DIR"/network.txt 1>&2
				send ""
				send ""
				echo "     done ...."
				echo ""
			fi
		done
	fi
fi

# ===================== ENDP ===================

echo " ======================================================= "
echo "	  Formating information ..... "
echo " ======================================================= "
echo ""
echo ".... Packing all information"
cd "$ARKINFO_DIR"
ARKINFO_TGZ="$ARKEIA_TMP/ARKINFO_$NODENAME""_`date +%m-%d-%Y`.tz"
tar cf - ./* | compress > "$ARKINFO_TGZ"
if [ $? != 0 ]; then
	tar cf - ./* | gzip > "$ARKINFO_TGZ"
fi
echo "     done ...."
echo ""

rm -fr "$ARKINFO_DIR"

echo ""
echo ""
echo ""
echo ""
echo " =============================================================== "
echo "    Attach the file $ARKINFO_TGZ to your incident ticket at"
echo "    http://support.arkeia.com with a complete description of your problem.  "
echo ""
echo ""
echo ""
cd "$ARKEIA_TMP"

