#!/bin/sh

# Copyright (c) 2005 Brocade Communication Systems, Inc.
# All rights reserved.
#
# Support show command for webtools
# 

COMP="webd"
EXECFILE="/fabos/libexec/webdconfigure"
CONFIGDIR="/fabos/webtools/bin"
HTTPROOT="/fabos/webtools/htdocs"
CERTDIR="/etc/fabos/certs"
TMPFILE="/tmp/wtss.txt"
FOSREDIROPTIONS=${FOSREDIROPTIONS:="-f $TMPFILE"}
HTTPSESSION="/fabos/libexec/webdhelper"
WEBLHELPER="/fabos/libexec/weblhelper"
MGMTAPPCMD="mgmtapp"
SUPPORTLINK="supportlink"
HTTPCFG="/fabos/libexec/webddebug"
LN=/bin/ln
TMPDIR="/tmp/webtools"
CMD=${CMD:="all"}
PIDOF=/sbin/pidof
PRINTLOGINHISTORY="mgmtapp"
HTTPDVLOG="/tmp/webtools/httpd.log"
HTTPDALERTLOG="/tmp/webtools/webtools.alert"
HTTPDERRORLOG="/var/log/save_http_error_log"
HTTPDWEBLLOG="/var/log/webldebug.txt.save"
SLLIBCURLLOG="/var/log/rest_asc_diag.txt"

standby=no;
# set standby flag
/fabos/cliexec/hashow | /bin/grep Local | /bin/grep Standby > /dev/null 2>&1
if [ $? -eq 0 ]; then
	standby=yes;
fi

log()
{
	msg=$*
	echo $msg
}

showFile()
{
	file=$1
	if [ -f $file ]; then
		log "--Begin $file"
		cat $file
		log "--End $file"
	else
		log "No such file ($file) found"
	fi
}

showHttpdConfDir()
{
	CONFFILES="httpd.conf* web.conf*"
	if [ -d $CONFIGDIR ]; then
		cd $CONFIGDIR
		for i in `ls $CONFFILES`
		do
			showFile $i
		done
	else
		log "No $CONFIGDIR found"
	fi
}

showHttpdSessionData()
{
	if [ ! -L $HTTPSESSION ]; then
		log "Creating symbolic link: $HTTPSESSION"
		$LN -s $EXECFILE $HTTPSESSION 2>/dev/null
	else
		log "Symbolic link $HTTPSESSION already exist."
	fi
	if [ -x $HTTPSESSION ]; then
		log "--Begin webd_session_data"
		$HTTPSESSION webdstore printAll 1
		$HTTPSESSION webdstore printAll 2
		log "--End webd_session_data"
	else
		log "No $HTTPSESSION command found"
	fi
	log "--Begin appLoginHistory data"
	$PRINTLOGINHISTORY --show sessionsinfo
	log "--End appLoginHistory data"

}

showRestconfData()
{
	if [ ! -L $WEBLHELPER ]; then
		log "Creating symbolic link: $WEBLHELPER"
		$LN -s $EXECFILE $WEBLHELPER 2>/dev/null
	else
		log "Symbolic link $WEBLHELPER already exist."
	fi
	if [ -x $WEBLHELPER ]; then
		log "--Begin restconf data"
		$WEBLHELPER dumprestdata
		log "--End restconf data"
	else
		log "No $WEBLHELPER command found"
	fi
	log "--Begin mgmtapp data"
	$MGMTAPPCMD --show sessionsinfo
	log "--End mgmtapp data"
	if [ -x $WEBLHELPER ]; then
		log "--Begin restconf device Throttle data"
		$WEBLHELPER dumpDeviceThrottle
		log "--End restconf device Throttle data"
	else
		log "No $WEBLHELPER command found"
	fi
}

showSLData()
{
	if [ ! -L $WEBLHELPER ]; then
		log "Creating symbolic link: $WEBLHELPER"
		$LN -s $EXECFILE $WEBLHELPER 2>/dev/null
	fi

	log "--Begin supportlink data"

	$SUPPORTLINK --show | grep -v "User name"

	log "$SUPPORTLINK --show history"

	$SUPPORTLINK --show history

	if [ -x $WEBLHELPER ]; then
		$WEBLHELPER dumpsldata
	else
		log "No $WEBLHELPER command found"
	fi

	if [ -f $SLLIBCURLLOG ]; then
		/bin/cat $SLLIBCURLLOG
	else
		log "No curl log file found"
	fi

	log "--End supportlink data"
}


showHttpdCfgData()
{
	if [ ! -L $HTTPCFG ]; then
		log "Creating symbolic link: $HTTPCFG"
		$LN -s $EXECFILE $HTTPCFG 2>/dev/null
	else
		log "Symbolic link $HTTPCFG already exist."
	fi
	if [ -x $HTTPCFG ]; then
		log "--Begin webd_dump"
		$HTTPCFG dump_all
		log "--End webd_dump"
	else
		log "No $HTTPCFG command found"
	fi
	log "--Begin weblinker backtrace"
	cat /var/log/bt.txt
	log "--End weblinker backtrace"
#	log "--Begin configshow"
#	configshow http | cat
#	configshow ssl | cat
#	log "--End configshow"
}

showHttpdTmpDir()
{
	if [ -d $TMPDIR ]; then
		cd $TMPDIR
		for i in `ls *`
		do
			showFile $i
		done
	else
		log "No $TMPDIR found"
	fi
}

showHttpdProcessData()
{
	cd /tmp
	for i in `ls $TMPFILE*`
	do
		showFile $i
	done
	log "--Begin proc details"
	ps -e -o pid,ppid,rss,osz,vsz -o pmem,pcpu -o comm | grep -E 'PID|web|http' 2> /dev/null

	# List all http process
	httplist=`$PIDOF httpd.0 httpd.1`
	log "List of http process..."
	log "$httplist"

	# List all weblinker process
	weblist=`$PIDOF 0.weblinker.fcg 1.weblinker.fcg`
	log "List of weblinker process..."
	log "$weblist"
	for i in $weblist
	do
		if [ -f /proc/$i/status ]; then
			log "Proc entry for: $i"
			showFile /proc/$i/status
		fi
	done
	log "--End proc details"
}

showHTTPDStartupLogs()     
{       
    log "--Begin HTTPD Startup logs..."
	showFile $HTTPDVLOG 
	showFile $HTTPDALERTLOG
	showFile $HTTPDERRORLOG
	showFile $HTTPDWEBLLOG
	log "--End HTTPD Startup logs"	
}

showBinFiles()
{
	log "--Begin HTTPROOT listing..."
	if [ -d $HTTPROOT ]; then
		cd $HTTPROOT
		ls -al *weblinker*
	else
		log "No such directory: $HTTPROOT"
	fi
	log "--End HTTPROOT listing..."
	log "--Begin CERTDIR listing..."
	if [ -d $CERTDIR ]; then
		cd $CERTDIR
		ls -alR *
	else
		log "No such directory: $CERTDIR"
	fi
	log "--End CERTDIR listing..."
}

usage()
{
	echo "Usage: $0 -c cmd"
	exit 0;
}

parseCmdlineArgs()
{
	while getopts o:c: opt $*
	do
		case $opt in
			c) CMD=$OPTARG;;
			\?) usage;;
		esac
	done
}

init()
{
	# First redirect component output to current control terminal
	# log "FOSREDIROPTIONS=$FOSREDIROPTIONS"
	version
	fosredirout -c $COMP $FOSREDIROPTIONS
}


execCmd()
{
# Do init
	# don't dump this information on standby cp
	if [ $standby = yes ]
	then
		return
	fi
	log "[Begin webtools data, Date: $WDATE]"
	init
	case $CMD in
		all)
			showHttpdConfDir
			showHttpdTmpDir
			showHttpdSessionData
			showHttpdCfgData
			showHttpdProcessData
			showBinFiles
			showHTTPDStartupLogs
			showRestconfData
			showSLData
		;;
		tmpdir)
			showHttpdTmpDir
		;;
		procinfo)
			showHttpdCfgData
			showHttpdSessionData
			showHttpdProcessData
		;;
		none)
		;;
	esac
	log "[End webtools data]"
}


shift `expr $OPTIND - 1`
parseCmdlineArgs $*
WDATE=`date '+%d%b%y_%H%M%S'`
execCmd $CMD
