#!/bin/sh
#************************************************************************* 
#   	Source Id:	 @(#)ts_client.make	1.1.1.1		07/16/98
#*************************************************************************

##	Generate Tech Support Information for BP Client
##	Copyright (C) 1997  UniTrends Software Corportation
##	All Rights Reserved
##
##	Author: Scott Gregory
##
##	Script to examine an unknown system and give information
##	useful to Technical support Staff
##
##	Usage: ts
##


#************************************************************************* 
#   	Source Id:	 @(#)ts.inc	1.1.1.3		06/25/98
#*************************************************************************






































# ************************************************************************* 
#    	Source Id:	 @(#)bpsh.inc	1.1.1.3		08/24/98
# *************************************************************************

## BP Shell Init

[ -n "$PRODDIR" ] && BPDIR=$PRODDIR
export BPDIR




PSCMD="ps ax"











MAILCMD=mail


set_ECHO() { 
	[ -d /usr/5bin ] && { PATH="/usr/5bin:${PATH}"; export PATH; }
  ECHO="echo -e"; 
  echo -e "hello" | grep "e h" >/dev/null && ECHO="echo"
  OS=`uname 2>/dev/null`
  case "$OS" in
      SunOS) [ -x /usr/5bin/echo ] && ECHO=/usr/5bin/echo ;;
  esac
  unset OS;
}

set_ECHO; export ECHO

bpsh_init()
{
	RTN=0

	eval `id  |  sed 's/[^a-z0-9=].*//'`
	




	
	# Bug 7515 Enable core dump

CHECK_SCO_OS=`uname 2>/dev/null`
if [ "$CHECK_SCO_OS" != "SCO_SV" ]
then
        ulimit -S -c unlimited
else
        ulimit -f 512
fi

	umask 022

	if [ -z "$BPDIR" ]; then
		if [ ! -f /etc/default/bp.ini ]; then
        id | grep "\(root\)" > /dev/null
        if [ $? -eq 0 ]; then
    		   $ECHO "\n  * File \"/etc/default/bp.ini\" does not exist!\n  * Consider reinstalling the product."
        else
           $ECHO "\n  * Unable to access file \"/etc/default/bp.ini\" "
           $ECHO "\n  * Please check your permissions\n"
        fi
    		RTN=1
		else
			# The brackets contain a space and a tab
			eval `egrep '^[ 	]*BPDIR=' /etc/default/bp.ini 2>/dev/null`
			if [ ! -d "$BPDIR" ]; then
    			$ECHO "\n  * Can't determine product directory.\n  * Try setting BPDIR to the install directory in \"/etc/default/bp.ini\""
    			RTN=1
			fi
		fi
	else
		if [ ! -d "$BPDIR" ]; then
			$ECHO "\n  * Invalid product directory from BPDIR environment variable."
			RTN=1
		fi
	fi

	BPUTIL="$BPDIR/bin/bputil"
	MASTER_INI="$BPDIR/bpinit/master.ini"

	return $RTN
}


[ -d /usr/5bin ] && { PATH="/usr/5bin:${PATH}"; export PATH; }
PATH="$PATH:/usr/sbin"

TMP1=/tmp/rm1_$$
TMP2=/tmp/rm2_$$
TMP=$TMP1

TLINE="=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
NLINE=$TLINE
SLINE="-----------------------------------------------------------"
: ${PAGER:=more}
: ${OURVOICE:=1-803-454-0300}
: ${OURFAX:=1-803-454-0308}
: ${OUREMAIL:="support@unitrends.com"}
: ${BOTTOM:="\n\n"}
: ${PRINTER_CMD:=lpr}
: ${YOURDEALER:="Unitrends Software Corporation"}



: ${YOUREMAIL:="${USER}@`hostname`"}

: ${MLINE_U:="    \t*************************************************"}
: ${MLINE_L:="    \t*************************************************"}
: ${v:="** "}
LINE="$NLINE"
BPINFO=/tmp/bpfax_info
BPPROB=/tmp/bpfax_prob
YOURNAME=
TELNO=
FAXNO=
export LINE NLINE CATLIST BPINFO BPPROB TMP1 TMP2  YOURNAME FAXNO TELNO
export OURVOICE OURFAX OUR_EMAIL YOUR_DEALER YOUR_EMAIL TLINE

BP_PORT=1743
export BP_PORT

bpsh_init; [ $? -ne 0 ] && ${ECHO} $?

BPBIN=$BPDIR/bin; export BPBIN
BPUTIL=$BPBIN/bputil; export BPUTIL





PSCMD="ps -ax"



TOPCMD="top -n 1 b"









RCMD="rsh"


##
## source a section of an INI file
## Usage: source_ini_section <section> <file>
##
## Please Note: Any illegal characters in the variables will be removed:
##	ie. Start-Dir=/tmp/foo  ->  StartDir=/tmp/foo
##
source_ini_section()
{
	tmpf=/tmp/bpvar.$$
	
	# The sed removes:
	#	illegal chars in variable names.
	#	whitespace before/after the equal(=) sign.
	#	leading whitespace.
	#	puts quotes around value of variable
	$BPUTIL -g "$1" NULL "" "$2" | grep = | sed -e 's/-\(.*=\)/\1/;s/[ 	]*=[ 	]*/=/;s/=\(.*\)/=\"\1\"/' -e 's/^[ 	]*//' > $tmpf
	eval `cat $tmpf`
	export `cat $tmpf | awk -F= '{print $1}'`

	rm -f $tmpf
}

## get tech support info straight
##
source_ini_section "Tech Support Information" $BPDIR/bpinit/master.ini
[ -z "$OurVoice" ] && OurVoice="$OURVOICE"
[ -z "$OurFaxno" ] && OurFaxno="$OURFAX"
[ -z "$OurEmail" ] && OurEmail="$OUREMAIL"
[ -z "$YourDealer" ] && YourDealer="$YOURDEALER"
[ -z "$YourName" ] && YourName="$YOURNAME"
[ -z "$YourVoice" ] && YourVoice="$YOURVOICE"
[ -z "$YourFaxno" ] && YourFaxno="$YOURFAX"
[ -z "$YourEmail" ] && YourEmail="$YOUREMAIL"
export OurVoice OurFaxno OurEmail YourDealer YourName YourVoice YourFaxno

locale_info()
{
	${ECHO} "LANG=${LANG}"
	${ECHO} "LC_CTYPE=\"${LC_CTYPE}\""
	${ECHO} "LC_COLLATE=\"${LC_COLLATE}\""
	${ECHO} "LC_TIME=\"${LC_TIME}\""
	${ECHO} "LC_NUMERIC=\"${LC_NUMERIC}\""
	${ECHO} "LC_MONETARY=\"${LC_MONETARY}\""
	${ECHO} "LC_MESSAGES=\"${LC_MESSAGES}\""
	${ECHO} "LC_ALL=${LC_ALL}"
}

check_exclaimation()
{
	if [ `grep -c "^[         ]*![A-z]" ${BPDIR:=/usr/bp}/bpinit/master.ini 2>/dev/null >/dev/null` ]; then
		${ECHO} "Master configuration file (master.ini) has leading exclamation points!!". These must be removed"
		${ECHO} "Check to be sure your ed editor is present."
	fi
}

net_info()
{
	showNIS=0
	${ECHO} "\nNetwork Information:"
	${ECHO} "$TLINE"
	${ECHO} "/etc/services\n--------------------"
	sl=`grep '^bpd[ 	]' /etc/services`
	BP_PORT=`${ECHO} $sl | awk '{print $2}' | sed -e 's/\/.*//'`
	${ECHO} $sl
	g=`grep "^+" /etc/services`
	if [ -n "$g" ]; then
		${ECHO} "Services Uses NIS!"
		showNIS=1
	fi
	${ECHO} "/etc/inetd.conf\n--------------------"
	grep bp /etc/inetd.conf 2>/dev/null
	g=`grep "^+" /etc/inetd.conf 2>/dev/null`
	if [ -n "$g" ]; then
		${ECHO} "Inetd.conf Uses NIS!"
		showNIS=1
	fi
	${ECHO} "Host information\n--------------------"
	$BPUTIL -t
	g=`grep "^+" /etc/hosts`
	if [ -n "$g" ]; then
		${ECHO} "Hosts Uses NIS!"
	fi
	g=`$PSCMD | grep -v "grep" | grep named`
	if [ -n "$g" ]; then
		${ECHO} "Host IS a NameServer:\n    $g"
	fi
	if [ -f /etc/resolv.conf ]; then
		${ECHO} "Nameserver configuration:"
		pr -t -o 4 /etc/resolv.conf
	fi
	
	if [ $showNIS -eq 1 ]; then
		${ECHO} "NIS Info\n--------------------"
		domain=`domainname`
		g=`$PSCMD | grep -v grep | grep ypbind`
		if [ -n "$g" -a -n "$domain" ]; then
			sl=`ypcat services | grep bp`
			ypcat inetd.conf | grep bp
		else
			${ECHO} "NIS commands or domainname not set, so NIS is not used."
		fi
	fi
	
	connect_to_bp `$BPUTIL -n` $BP_PORT
}

myping()
{
	tmpf=/tmp/myping.$$
	



	ping -c 3 $1 > $tmpf 2>&1


	status=$?
	rm -f $tmpf
	
	return $status
}

## Exercize the connection to the BP service on some machine
## Usage: connect_to_bp <client> <port>
##
connect_to_bp()
{
	client=$1
	port=$2
	
	${ECHO} "Trying to connect to $client on port ${port}... \c"
	telnet $client $port << EOF > $TMP1 2>&1
q
EOF
	if [ $? -ne 0 ]; then
		${ECHO} "FAILED!"
		pr -t -o 4 $TMP1
	else
		${ECHO} "SUCCESS!"
	fi
}

file_select() # Usage: file_select DIR PATTERN CONTAINER
{
	DIR=${1:-/}
	PATTERN=$2
	CONTAINER=$3
	PROMPT=$4
	FLIST=
	TMP=/tmp/ls.$$
	TMP2=/tmp/Rm.$$
	export DIR PATTERN
	if [ ! -d "$DIR" ] ; then
		${ECHO} "$DIR is not a directory"
		return 1
	fi 
	SELECTED=NO
	
	while [ $SELECTED = NO ]
	do
	clear
	(cd $DIR; ls $PATTERN || ${ECHO} "No files to choose from") | pr -t -n\)1 |
		sed -n '/[0-9])/s/)/) /p'|tee $TMP| pr -t -4a -w80
	
	if [ -z "$PROMPT" ];
	then
	${ECHO} "\nEnter choices separated by spaces.  You can enter by number\nor name or both.  Wild cards '*' and '?' are allowed. Enter 'q' to quit"
	${ECHO} "\nEnter Selection(s): \c"
	else
		${ECHO} "$PROMPT \c"
	fi
	read TSEL ; : ${TSEL:=q}
	SELECTIONS=`${ECHO} "$TSEL" | sed '/\./s//\\\\./g;/\*/s//.*/g;/?/s//./g'`
	set -f # Disable filename expansion
	for sel in $SELECTIONS
	do
	${ECHO} ".\c"
	case "${sel}" in
		q|Q) break 2
		;;
		"\\.") 
		[ -n "$FLIST" ]  && FLIST="${FLIST}\n"
		FLIST="${FLIST}."
		;;
		[0-9]*) 
		[ -n "$FLIST" ]  && FLIST="${FLIST}\n"
		FLIST="${FLIST}`sed -n "${sel}p" $TMP | sed 's/[0-9].*) //'`"
		;;
		[A-z\*\?\.]*) 
		grep -y ") $sel\$" $TMP 2>/dev/null >/dev/null
		if [ $? -eq 0 ]
		then
			grep -y ") $sel\$" $TMP 2>/dev/null | sed 's/[0-9].* //' > $TMP2
			[ -n "$FLIST" ]  && FLIST="${FLIST}\n"
			FLIST="${FLIST}`cat $TMP2`"
		else
			rm -f $TMP2
			${ECHO} "\nYour selection: \"$sel\" does not match anything"
			${ECHO} "Please try again!"
			sleep 2
			continue
		fi
		;;
		#  * means <RETURN> was hit or another character
		*)  ${ECHO} "Bad response"
			sleep 2
			continue
		;;
	esac
	done  #  Read and process each arg loop 
	
	set +f  # Re-enable filename expansion
		show_list Selection "$FLIST"
		is_correct 
		if [ $? -eq 0 ]
		then
			SELECTED=YES
		else
			FLIST=
		fi
	done
	
	FLIST="`${ECHO} "$FLIST" | tr '\012' ' '`"
	LFLIST=
	for file in $FLIST
	do
		[ -n "$LFLIST" ]  && LFLIST="${LFLIST} "
		LFLIST="${LFLIST}${DIR}/$file"
	done
	[ $SELECTED = YES ] && eval "$CONTAINER=\"$LFLIST\""
	rm -f $TMP $TMP2
	return 0
}

system_info()
{
	${ECHO} "Backup Professional" "10.5.9-2.20211216_1209"
	${ECHO} "Copyright (C) 1994-2010, Unitrends Software Corp.  All rights reserved."
	${ECHO} ""
	
	uname -a
	
	${ECHO} "Locale Information:"
	locale_info | pr -t -o 4
}

common_info()
{
	${ECHO} "\nCapturing output of df command:" 
	${ECHO} "$TLINE"
	df

	${ECHO} "\nLooking for common binaries: \c" 
	BINARIES="what" # General interest only
	# Needed for ctmenu
	BINARIES2="awk sort sed pr comm tee tr wc basename dirname find ed"

	BINARIES=""
	BINARIES3="ksh more pg grep crontab tput du mt"



	>$TMP1
	for file in $BINARIES $BINARIES2 $BINARIES3
	do
		${ECHO} ".\c"
		type $file >> $TMP1 2>&1
	done
	grep found $TMP1 > $TMP2
	if [ -s $TMP2 ]
	then
		${ECHO} "\nWHOOPS!! You are missing:"
		cat $TMP2| sed '/^/s//	*==>FILE /;/$/s//!!/;'
		${ECHO} $TLINE
	else
		${ECHO} " All present!!"
	fi
	rm -f $TMP2

	${ECHO} "\nRunning Processes:"
	${ECHO} "$TLINE"
	$PSCMD | grep bpserverd | grep -v grep
	$PSCMD | grep bpclientd | grep -v grep
	$PSCMD | grep tasker | grep -v grep
	$PSCMD | grep bkup | grep -v grep
	
	# Special Problems of Interest
	# Mail -s not found
	${ECHO} "BP: fax_prep:\nTesting to see if the mail supports the subject (-s) option\nYou can safely delete this message"|
		mail -s "test" root >/dev/null 2>/dev/null
	if  [ $? -ne 0 ]
	then
		${ECHO} "PROBLEM\nNo Subject (-s) Option allowed for mail"
	fi

	${ECHO} hello | grep -y HELLO >/dev/null 2>/dev/null
	[ $? -ne 0 ] && ${ECHO} "\nGrep does not support the '-y' option; use '-i' instead"

	if grep "wc " $TMP1 >/dev/null 
	then
		${ECHO} "This should appear \c" >$TMP1
		${ECHO}"all on one line!! (if not '\\ c' option is not supported) " >> $TMP1
		[ `wc -l < $TMP1` -ne 1 ] &&
			${ECHO} "The echo  command does not support the '\\c' option"
		${ECHO} -n "This should appear "  > $TMP1
		${ECHO}  "all on one line!! (if not '-n' to echo is not supported) ">>$TMP1
		[ `wc -l < $TMP1` -ne 1 ] &&
			${ECHO} "The echo  command does not support the '\\c' option"
	else
		${ECHO}
		${ECHO} "This should appear \c" 
		${ECHO} "all on one line!! (if not '\\ c' option is not supported) " 
		${ECHO}
		${ECHO} -n "This should appear "  
		${ECHO} "all on one line!! (if not '-n' to echo is not supported) "
	fi
}

system_check()
{
  vpath=/var
  tmpf=/tmp


  R=`grep -c 0xcb $vpath/log/messages`








  if [ $R ]; then
	then
     ${ECHO} "$R 0xcb errors found for 3ware card"
  else
		 ${ECHO} "No 0xcb errors found for 3ware card"
  fi


  cat $vpath/dmesg | grep "device driver"



  ${ECHO} "\nOutput of top command which includes uptime + free :"
  ${ECHO} "------------------------------------------------------"
  $TOPCMD > $tmpf/dputop 2>&1
  head -30 $tmpf/dputop
  ${ECHO} " "

  P=`grep -c xbpmenu $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P xbpmenu processes currently running on system"
  fi
  P=`grep -c xpromon $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P xpromon processes currently running on system"
  fi
  P=`grep -c tasker $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P taskers currently running on system"
  fi
  P=`grep -c bpserverd $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P bpserverd processes currently running on system"
  fi
  P=`grep -c updatedb $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P updatedb processes currently running on system"
  fi
  rm -f $tmpf/dputop
																				 
  P=`$PSCMD | grep -c -i tarantella`
  if [ "$P" -gt 0 ]; then
    ${ECHO} "\n$P instances of tarantella services found on system"
  fi
			
  ${ECHO} "\nVirtual memory info:"
  ${ECHO} "---------------------"
  vmstat
  ${ECHO} " "
}


dpu_check()
{
  vpath=/var/log
  tmpf=/tmp







	R=`grep -c 0xcb $vpath/messages` 
  if [ $R ]; then
	then
     ${ECHO} "$R 0xcb errors found for 3ware card"
  else
		 ${ECHO} "No 0xcb errors found for 3ware card"
  fi

  cat $vpath/dmesg | grep "device driver"

	if [ `lspci | grep -c 17b4:0011` ]; then
		if [ `lsmod | grep -c rx9` ]; then
       ${ECHO} "DPU has RX9 card installed with driver"
		   cat $vpath/messages | grep "RX9 driver version"
	  else
       ${ECHO} "DPU has RX9 card but driver is not loaded"
	else
    ${ECHO} "No RX9 card found in DPU."
  fi

  ${ECHO} "\nOutput of top command which includes uptime + free :"
  ${ECHO} "------------------------------------------------------"
  $TOPCMD > $tmpf/dputop 2>&1
  head -30 $tmpf/dputop
  ${ECHO} " "

  P=`grep -c xbpmenu $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P xbpmenu processes currently running on DPU"
  fi
  P=`grep -c xpromon $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P xpromon processes currently running on DPU"
  fi
  P=`grep -c tasker $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P taskers currently running on DPU"
  fi
  P=`grep -c bpserverd $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P bpserverd processes currently running on DPU"
  fi
  P=`grep -c updatedb $tmpf/dputop`
  if [ "$P" -gt 1 ]; then
    ${ECHO} "$P updatedb processes currently running on DPU"
  fi
  rm -f $tmpf/dputop
																				 
  P=`$PSCMD | grep -c -i tarantella`
  if [ "$P" -gt 0 ]; then
    ${ECHO} "\n$P instances of tarantella services found on DPU"
  fi

  ${ECHO} "\nVirtual memory info:"
  ${ECHO} "---------------------"
  vmstat
  ${ECHO} " "
}


license_info()
{
	if [ -f $BPBIN/tasker ]; then
		BP_LM=$BPBIN/tasker
	else
		BP_LM=$BPBIN/bpclientd
	fi
	${ECHO} "\nLicense Information: ($BP_LM)"
	${ECHO} "$TLINE"
	if [ -x $BPBIN/lmmgr ]
	then
		$BPBIN/lmmgr -info $BP_LM | pr -t -o 4
		$BPBIN/lmmgr -hostid 2>/dev/null | pr -t -o 4
#		LTYPE=`$BPBIN/lmmgr -info %t $BP_LM 2>/dev/null`
#		case "$LTYPE" in
#			DEMO)
#			$BPBIN/lmmgr -info "%t %p Serial#%s (%c) [%r days]\n" $BP_LM 2>/dev/null
#				;;
#			*) #FULL
#			#$BPBIN/lmmgr -info "%t %p Serial#%s (%c)\n" $BP_LM 2>/dev/null
#			$BPBIN/lmmgr -info $BP_LM 2>/dev/null
#				;;
#		esac
	else
		${ECHO} 'Cant find lmmgr?'
	fi
}

HAVE_CONTACT_INFO=0

get_contact_info()
{
	if [ $HAVE_CONTACT_INFO -eq 1 ]; then
		return
	fi
	HAVE_CONTACT_INFO=1
	
	promptfor YourName "Enter your full name: \c"
	promptfor YourFax "Enter your fax number: \c"
	promptfor YourPhone "Enter your voice phone number: \c"
	promptfor YourEmail "Enter your email address: \c"
	export YourName YourFax YourPhone YourEmail
	
	$BPUTIL -p "Tech Support Information" "YourName" "$YourName" $BPDIR/bpinit/master.ini
	$BPUTIL -p "Tech Support Information" "YourFax" "$YourFax" $BPDIR/bpinit/master.ini
	$BPUTIL -p "Tech Support Information" "YourPhone" "$YourPhone" $BPDIR/bpinit/master.ini
	$BPUTIL -p "Tech Support Information" "YourEmail" "$YourEmail" $BPDIR/bpinit/master.ini
}

put_contact_info()
{
	${ECHO} "Contact Person........: $YourName"
	${ECHO} "Fax number............: $YourFax"
	${ECHO} "Voice Telephone Number: $YourPhone"
	${ECHO} "Email Address.........: $YourEmail"
	${ECHO} ""
}


OPT_Q=0

BPINFO=/tmp/bpfax_info

get_contact_info

put_contact_info | tee $BPINFO
system_info 2>&1 | tee -a $BPINFO
common_info 2>&1 | tee -a $BPINFO
license_info 2>&1 | tee -a $BPINFO
net_info 2>&1 | tee -a $BPINFO

graph_stop
rm -f $TMP1 $tmp2
exit 0
