#!/bin/bash
# $Id: hat_collect_d7.lnx,v 1.10 2018/02/19 07:11:11 cozcan Exp $ Copyright (c) NewNet
###############################################################################
#                                                                             #
# Copyright (c) NewNet Communication Technologies, LLC. (NewNet)              #
# All rights reserved.                                                        #
#                                                                             #
# This document contains confidential and proprietary information in which    #
# any reproduction, disclosure, or use in whole or in part is expressly       #
# prohibited, except as may be specifically authorized by prior written       #
# agreement or permission of NewNet.                                          #
#                                                                             #
###############################################################################
# VERSION      : $Revision: 1.10 $
# DATE         : $Date: 2018/02/19 07:11:11 $
#
# MODULE NAME  : $RCSfile: hat_collect_d7.lnx,v $
# AUTHOR       :
# DESCRIPTION  :
# DATE 1st REL :
# REV.HIST.    :
#
# Date      Owner  Description
# ========  =====  ===========================================================
#
#
###############################################################################
#                                                                             #
#                     RESTRICTED RIGHTS LEGEND                                #
# Use, duplication, or disclosure by Government Is Subject to restrictions as #
# set forth in subparagraph (c)(1)(ii) of the Rights in Technical Data and    #
# Computer Software clause at DFARS 252.227-7013                              #
#                                                                             #
###############################################################################

if [ -z "$EBSHOME" ]
then
        echo "EBSHOME environment variable is not set!"
        exit 1
fi

LIGHT=0
ALL=0

if [ $# -lt 1 -o $# -gt 4 ]
then
	ERR=1
else
	if [ $1 != "-" -a $1 != "+" -a $1 != "txt" -a $1 != "all" ]
	then
		ERR=1
	else
		EXT=$1
		TPC=.

		if [ $1 = "all" ]
		then
			ALL=1
		fi

		if [ $1 = "txt" ]
		then
			LIGHT=1
		fi

		if [ $# -ge 2 ]
		then
			TPC=${TPC}TP$2.
		fi

		if [ $# -ge 3 ]
		then
			TPC=${TPC}TC$3.
		fi

		if [ $# -ge 4 ]
		then
			OPT=$4
		fi

		ERR=0
	fi
fi

if [ $ERR = 1 ]
then
	echo "Usage: hat_collect [all] or [-/+] [tp] [tc]"
	exit 2
fi

BIN=$EBSHOME/access/bin
RUN=$EBSHOME/access/RUN
OUT=/tmp

if [ $ALL = 1 ]
then
	FOLDERNAME="newnet_logs_$(date +%m%d%y-%H-%M-%S)"
	TMPFOLD="$OUT/$FOLDERNAME"

        mkdir $TMPFOLD
	if [ $? -ne 0 ]
	then
		echo "$?"
		exit 1
	fi

	mkdir $TMPFOLD/mlog
	mkdir $TMPFOLD/alarmlog
	mkdir $TMPFOLD/messages
	mkdir $TMPFOLD/db2text
	mkdir $TMPFOLD/hat_collect
        mkdir $TMPFOLD/license

	cp $EBSHOME/access/RUN/mlog/MLog.* $TMPFOLD/mlog
        if [ $? -ne 0 ]
        then
                echo "$?"
		rm -rf $TMPFOLD
                exit 1
        fi

        cp $EBSHOME/access/etc/* $TMPFOLD/license
        if [ $? -ne 0 ]
        then
                echo "$?"
                rm -rf $TMPFOLD
                exit 1
        fi


	cp $EBSHOME/access/RUN/alarmlog/* $TMPFOLD/alarmlog
	cp /var/log/messages* $TMPFOLD/messages
	(cd $TMPFOLD/db2text && db2text . . > /dev/null)
	
	hat_collect + tp tc all > /dev/null
        if [ $? -ne 0 ]
        then
                echo "$?"
		rm -rf $TMPFOLD
                exit 1
        fi

	mv /tmp/LOG.$(hostname).TPtp.TCtc.+  $TMPFOLD/hat_collect
	
	(cd /tmp && tar -zcvf $FOLDERNAME.tar.gz $FOLDERNAME > /dev/null)
        if [ $? -ne 0 ]
        then
                echo "$?"
		rm -rf $TMPFOLD
                exit 1
        fi

	rm -rf $TMPFOLD		

	echo -e "\nhat_collect output file is created under $OUT with name $FOLDERNAME.tar.gz"
	echo -e "Please share the tar file with support@newnet.com\n"
exit 0
fi

if [ $LIGHT = 1 ]
then
	OUT=$EBSHOME
fi

TCL_LIBRARY=$EBSHOME/access/lib/tcl_lib
TK_LIBRARY=$EBSHOME/access/lib/tk_lib

SYS=`uname -a | awk '{ print $2 };'`

LOG=$OUT/LOG.$SYS$TPC$EXT
rm -rf $LOG >/dev/null 2>&1

#
# retrieve date of execution
#

CMD="date"
echo -e "\nCMD -> $CMD\n" >> $LOG
$CMD >> $LOG

#
# retrieve machine name, os patch level, ip address, etc.
#

if [ -x /bin/uname ]
then
	CMD="uname -a"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi


if [ -a /etc/redhat-release ]
then
        CMD="cat /etc/redhat-release"
        echo -e "\nCMD -> $CMD\n" >> $LOG
        $CMD >> $LOG
fi


if [ -x $BIN/ebs_sysinfo ]
then
	CMD="$BIN/ebs_sysinfo -x"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG

	CMD="$BIN/ebs_sysinfo -xl"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

#
# retrieve version, cpu, memory related info
#

if [ -f /proc/version ]
then
	CMD="cat /proc/version"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

if [ -f /proc/cpuinfo ]
then
	CMD="cat /proc/cpuinfo"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

if [ -f /proc/meminfo ]
then
	CMD="cat /proc/meminfo"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

#
# retrieve device related info
#

CMD="lspci"
echo -e "\nCMD -> $CMD\n" >> $LOG
$CMD >> $LOG

if [ -f /proc/pci ]
then
	CMD="cat /proc/pci"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

#
# retrieve release/version info [if any]
#

if [ -x $BIN/ebs_setrelease ]
then
	CMD="$BIN/ebs_setrelease -i"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

#
# retrieve contents of license.dat
#

if [ -a $EBSHOME/access/etc/license.dat ]
then
	CMD="cat $EBSHOME/access/etc/license.dat"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

#
# retrieve platform specific info
#

if [ -x $BIN/ebs_ps ]
then
	CMD="$BIN/ebs_ps -ax"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG

	CMD="$BIN/ebs_ps -dx"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG

	CMD="$BIN/ebs_ps -rx"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG

	CMD="$BIN/ebs_ps -mx"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

if [ -x $BIN/ebs_qlist ]
then
	CMD="$BIN/ebs_qlist -x"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

if [ -x $BIN/ebs_qstat ]
then
	CMD="$BIN/ebs_qstat -x"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

if [ -x $BIN/ebs_qinfo ]
then
	CMD="$BIN/ebs_qinfo -mx"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG

	CMD="$BIN/ebs_qinfo -sx"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

if [ -x $BIN/ebs_tasklist ]
then
	CMD="$BIN/ebs_tasklist -x"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi

if [ -x $BIN/ebs_showlink ]
then
	CMD="$BIN/ebs_showlink -x"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi


CMD="netstat -a | grep netdbase"
echo -e "\nCMD -> $CMD\n" >> $LOG
netstat -a | grep netdbase >> $LOG

#
# retrieve network configurations
#

CMD="ifconfig -a"
echo -e "\nCMD -> $CMD\n" >> $LOG
ifconfig -a >> $LOG

#
# retrieve firewall info/status
#

CMD="ps aux | grep firewall"
echo -e "\nCMD -> $CMD\n" >> $LOG
ps aux | grep firewall >> $LOG

# 
# retrieve mml display info
#

SPMD="DISPLAY-HOST:;
DISPLAY-LINE:;
DISPLAY-LINEHIST:;
DISPLAY-LINESTAT:;
DISPLAY-NTWK:;
DISPLAY-PMLINK:;
DISPLAY-PORT:;
DISPLAY-SS7BOARD:;
DISPLAY-TCPCON:;
DISPLAY-TIMESLOT:;
"

UPMD="DISPLAY-ALIAS:;
DISPLAY-L2CS:;
DISPLAY-L2FLOW:;
DISPLAY-L2TIMER:;
DISPLAY-L3TIMER:;
DISPLAY-LINK:;
DISPLAY-LINKSTAT:;
DISPLAY-LSET:;
DISPLAY-LSETSTAT:;
DISPLAY-MTP:;
DISPLAY-ROUTE:;
DISPLAY-RTSET:;
DISPLAY-SLTIMER:;
DISPLAY-SP:;
"

SCMD="DISPLAY-CONNECTION:;
DISPLAY-CPC:;
DISPLAY-CTBUS:;
DISPLAY-GT:;
DISPLAY-GTENTRY:;
DISPLAY-LOCALSUBSYS:;
DISPLAY-MATE:;
DISPLAY-SCCP:;
DISPLAY-SNSP:;
"

ISUPD="DISPLAY-ISUP:;
DISPLAY-ISUPNODE:;
DISPLAY-ISUPTMR:;
"

ASPD="DISPLAY-SGCAS:;
DISPLAY-SGCASP:;
DISPLAY-SGCASTFC:;
DISPLAY-SGCDPC:;
DISPLAY-SGCIPAS:;
DISPLAY-SGCIPSP:;
DISPLAY-SGCRK:;
DISPLAY-SGCRKRNG:;
DISPLAY-SGCSG:;
DISPLAY-SGCSGP:;
DISPLAY-SGCSPNA:;
"

SGPD="DISPLAY-SGAS:;
DISPLAY-SGASP:;
DISPLAY-SGASTFC:;
DISPLAY-SGDPC:;
DISPLAY-SGPSG:;
DISPLAY-SGPSGP:;
DISPLAY-SGRK:;
DISPLAY-SGRKRNG:;
DISPLAY-SGSGP:;
DISPLAY-SGSPMC:;
DISPLAY-SGSPNA:;
"

PARAMS=""


pgrep spmd > /dev/null
if [ $? != "1" ]
then
        PARAMS=$PARAMS$SPMD
fi

pgrep upmd > /dev/null
if [ $? != "1" ]
then
        PARAMS=$PARAMS$UPMD
fi

pgrep scmd > /dev/null
if [ $? != "1" ]
then
        PARAMS=$PARAMS$SCMD
fi

pgrep isupd > /dev/null
if [ $? != "1" ]
then
        PARAMS=$PARAMS$ISUPD
fi

pgrep aspd > /dev/null
if [ $? != "1" ]
then
        PARAMS=$PARAMS$ASPD
fi

pgrep sgpd > /dev/null
if [ $? != "1" ]
then
        PARAMS=$PARAMS$SGPD
fi


# Command sequence for getting displays from mml

# command
# parameters
# exit:;	cancels out giving commands to mml
# EOF 		exits mml completely
# !!! please do NOT put indentation in below loop. Indentation causes error because of EOF usage.

lines=( $PARAMS )

for dis_command in "${lines[@]}"
do

CMD="mml 0 >> $LOG 2>&1 <<-EOF\n"$dis_command"exit:;\nEOF"
echo -e "\nCMD -> $CMD\n" >> $LOG
mml 0 >> $LOG 2>&1 <<-EOF  
$dis_command
exit:;
EOF

done


#
# retrieve crash and/or driver related messages
#

if [ -s /var/log/messages ]
then
	CMD="tail -30 /var/log/messages"
	echo -e "\nCMD -> $CMD\n" >> $LOG
	$CMD >> $LOG
fi


if [ -d /var/crash ]
then
        CMD="ls -la /var/crash"
        echo -e "\nCMD -> $CMD\n" >> $LOG
        $CMD >> $LOG
fi


#
# retrieve dkm related info
#

ebs_ps -ax | grep dkmd >/dev/null
if [ $LIGHT = 0 -a $? = 0 ]
then
	if [ -x $BIN/dkm_list ]
	then
		CMD="$BIN/dkm_list -h"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -s"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -l"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -m"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -x"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -q"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -u"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -hd"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -sd"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -ld"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -md"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dkm_list -xd"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG
	fi

	if [ -x $BIN/dkm_dump ]
	then
		CMD="$BIN/dkm_dump -i0 -n"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG
	fi

	if [ -x $BIN/dratest ]
	then
	        SUBCMD="echo dra_stat_get"
		CMD="$BIN/dratest"
		echo -e "\nCMD -> $SUBCMD | $CMD\n" >> $LOG
		$SUBCMD | $CMD  >> $LOG
	fi
fi

#
# retrieve dsm related info
#

ebs_ps -ax | grep dsmd >/dev/null
if [ $LIGHT = 0 -a $? = 0 ]
then
	if [ -x $BIN/dsm_list ]
	then
		CMD="$BIN/dsm_list -m"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dsm_list -s"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dsm_list -l"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dsm_list -r"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dsm_list -a"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dsm_list -md"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dsm_list -sd"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dsm_list -ld"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dsm_list -rd"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/dsm_list -ad"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG
	fi
fi

#
# retrieve tcap related info [if any]
#

ebs_ps -ax | grep tcmd >/dev/null
if [ $LIGHT = 0 -a $? = 0 ]
then
	modinfo d7_streams_tcap | grep tcap >/dev/null
	if [ $? = 0 ]
	then
		if [ -x $BIN/tcm_list ]
		then
			CMD="$BIN/tcm_list -u"
			echo -e "\nCMD -> $CMD\n" >> $LOG
			$CMD >> $LOG

			CMD="$BIN/tcm_list -p"
			echo -e "\nCMD -> $CMD\n" >> $LOG
			$CMD >> $LOG

			CMD="$BIN/tcm_list -s"
			echo -e "\nCMD -> $CMD\n" >> $LOG
			$CMD >> $LOG

			CMD="$BIN/tcm_list -t"
			echo -e "\nCMD -> $CMD\n" >> $LOG
			$CMD >> $LOG

			CMD="$BIN/tcm_list -ud"
			echo -e "\nCMD -> $CMD\n" >> $LOG
			$CMD >> $LOG

			CMD="$BIN/tcm_list -pd"
			echo -e "\nCMD -> $CMD\n" >> $LOG
			$CMD >> $LOG

			CMD="$BIN/tcm_list -sd"
			echo -e "\nCMD -> $CMD\n" >> $LOG
			$CMD >> $LOG
		fi

		if [ -x $BIN/tcm_tune ]
		then
			CMD="$BIN/tcm_tune"
			echo -e "\nCMD -> $CMD\n" >> $LOG
			$CMD >> $LOG
		fi
	fi
fi

#
# retrieve remote tcap info [if any]
#

modinfo d7_streams_rtcmod | grep rtcmod >/dev/null
if [ $LIGHT = 0 -a $? = 0 ]
then
	if [ -x $BIN/rtc_dump ]
	then
		CMD="$BIN/rtc_dump -p"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG

		CMD="$BIN/rtc_dump -t"
		echo -e "\nCMD -> $CMD\n" >> $LOG
		$CMD >> $LOG
	fi
fi

#
# retrieve alarm/mlog info
#

LATEST=`ls -t $RUN/alarmlog | awk '{ print $1; exit };'`
if [ -n "$LATEST" ]
then
	CMD="tail -50 $RUN/alarmlog/$LATEST"
	echo -e "\nCMD -> $CMD\n\n" >> $LOG
	$CMD >> $LOG
fi

MMDDYY=`date '+%m%d%y'`

if [ -f $RUN/mlog/MLog.$MMDDYY ]
then
	CMD="tail -50 $RUN/mlog/MLog.$MMDDYY"
	echo -e "\nCMD -> $CMD\n\n" >> $LOG
	$CMD >> $LOG
fi

if [[ "$OPT" = "all" ]]
then
KERNEL=$(uname -r)
CRASH_TOOL=crash
VMLINUX=/usr/lib/debug/lib/modules/${KERNEL}/vmlinux
SYSTEM_MAP=/boot/System.map-${KERNEL}

hash ${CRASH_TOOL} 2>/dev/null || {
echo "crash tool not installed"
CRASH_TOOL=
}

if ! [ -f ${SYSTEM_MAP} ]; then
echo "debuginfo and debuginfo-common packages should be installed"
SYSTEM_MAP=
fi

if ! [ -f ${VMLINUX} ]; then
echo "debuginfo and debuginfo-common packages should be installed"
VMLINUX=
fi
        if [[ -n ${CRASH_TOOL} ]] && [[ -n ${SYSTEM_MAP} ]] && [[ -n ${VMLINUX} ]]; then
                CMD="${CRASH_TOOL} ${VMLINUX} ${SYSTEM_MAP}"
                echo -e "\nCMD -> $CMD\n" >> $LOG
                $CMD >> $LOG <<-EOF
                        \foreach bt
                        \q
EOF
        fi

fi

echo "hat_collect output saved in $LOG on $SYS"

exit 0
