#!/pkg/bin/ksh
#
#  Mar 2014 John Geevarghese
#
#  Copyright (c) 1999-2015, 2021 by cisco Systems, Inc.
#  All rights reserved.
#
# Take a snapshot of the application and store it on persistent storage before the node
# is shut down on ISSU Abort. The snapshot contains basic debug information and additional
# application specified debug information based on error type


# Get generic information - this will always be called by the script.                  
get_general_info()
{
    # Dump the local syslog buffer
    echo "----------------------------------------------------------------"
    echo "## `date +%H:%M:%S` Output of sysmgr_log -f /tmp/syslog.local.log"
    $LOG_CMD -f /dev/shm/tmp/syslog.local.log

    # Dump the ltrace buffer
    echo "----------------------------------------------------------------"
    echo "## `date +%H:%M:%S`

    NODE1=`echo $NODE | awk '{ split($0, strarry, "_")} END { print strarry[2]}'`
    NODE2=$((${NODE1}*256))

    echo "$NODE----capturing FIA ISSU logs "
    echo "$NODE----capturing on LC : $NODE1"
    echo "$NODE----capturing on LC node id : $NODE2"

    eval $BIN_DIR/fia_show_ltrace   -A -i all
       # show asic errors  fia all detail
    echo "$NODE----capturing asic-errors-all on LC node id : $NODE2"
    eval $BIN_DIR/asic_errors_show  -n 0 -a 0x0 -s -x 0x1 -t 0x0 -m 0x0 
       # show asic errors fia <inst> all  loc
    echo "$NODE----capturing asic-errors-fia on LC node id : $NODE  instance 0 "
    eval $BIN_DIR/asic_errors_show  -n $NODE2 -a 0x4 -i 0x0  -e 0x7 -s -t 0x0 -m 0x0
    echo "$NODE----capturing asic-errors-fia on LC node id : $NODE  instance 1 "
    eval $BIN_DIR/asic_errors_show  -n $NODE2 -a 0x4 -i 0x1  -e 0x7 -s -t 0x0 -m 0x0
    echo "$NODE----capturing asic-errors-fia on LC node id : $NODE  instance 2 "
    eval $BIN_DIR/asic_errors_show  -n $NODE2 -a 0x4 -i 0x2  -e 0x7 -s -t 0x0 -m 0x0
    echo "$NODE----capturing asic-errors-fia on LC node id : $NODE  instance 3 "
    eval $BIN_DIR/asic_errors_show  -n $NODE2 -a 0x4 -i 0x3  -e 0x7 -s -t 0x0 -m 0x0
    echo "$NODE----capturing asic-errors-fia on LC node id : $NODE  instance 4 "
    eval $BIN_DIR/asic_errors_show  -n $NODE2 -a 0x4 -i 0x4  -e 0x7 -s -t 0x0 -m 0x0
    echo "$NODE----capturing asic-errors-fia on LC node id : $NODE  instance 5 "
    eval $BIN_DIR/asic_errors_show  -n $NODE2 -a 0x4 -i 0x5  -e 0x7 -s -t 0x0 -m 0x0
       # show asic errors trace all
    echo "$NODE----capturing ael_show_ltrace on LC node id : $NODE2 "
    eval $BIN_DIR/ael_show_ltrace   -i $NODE2 -R -P -E -C 0x4  
    echo "$NODE----capturing fia_driver_show on LC node id : $NODE2 "
    eval $BIN_DIR/fia_driver_show   -d d -b 0x24  -f 0x24 -i 0x10 -n $NODE2 -t n -p 0xffffffff
    eval cat /var/log/messages 
    
}


###############################################################################
# Start of main script
###############################################################################

USAGE="[-e <reason_enum>] [-p pid_to_trace] node_name calling_process [process_instance]"

unset PID
unset REASON_ENUM 

LOG_CMD=/pkg/sbin/sysmgr_log
BIN_DIR=/pkg/bin
DETAIL_LOG=0

while getopts p:e: c
do
    case $c in
    p) PID=$OPTARG;;
    e) REASON_ENUM=$OPTARG;;
    *) echo "Usage: $0 $USAGE" >&2; exit 1;;
    esac
done

# Move forward in the argument list
shift $(($OPTIND - 1))

# Make the arguments up to 'calling process' compulsory
if [ -z $2 ]
then
    echo "Usage: $0 $USAGE" >&2
    exit 1
fi

NODE=$1
CALLING_PROCESS=$2

# Get optional argument 'process_instance'
if [ -n $3 ]
then
    PROC_INST=$3
fi

# Ensure there are no trailing extra arguments after 'opt_tag'
if [ -n "$4" ]
then
    echo "Usage: $0 $USAGE" >&2
    exit 1
fi

# Find the application storage device, using 'showtech' for now as issudir is not
# defined as one of the appmedia_type currently
BOOTDEV=`/pkg/sbin/getappmedia showtech`

# Select log device by first available in order of priority.
# If we are on a LC, first attempt to pick a device on the RP.
for device in harddisk: $BOOTDEV bootflash:
do 
    if [ -e /$device ]; then
        ISSU_DEBUG_DEV=$device; break; 
    fi
done

# If there is no device available for logging, exit
if [ -z $ISSU_DEBUG_DEV ]; then
    echo "Logging device is not available; exiting..." >&2; exit 1
fi

# set up debug directory on the local log device based on environment variable 
# ISSU_TRACE_DIR inheritted from the parent process spawning this script.
# DO NOT CHANGE THE ENVIRONMENT VARIABLE !!!
ISSU_DEBUG_DIR=/$ISSU_DEBUG_DEV/$ISSU_TRACE_DIR

# Decide whether the script will log in detail or not
# If the available device is bootflash; make it short
# because of space.
if [ $device != "bootflash:" ]; then
    DETAIL_LOG=1
fi

# Create directory if they don't exist
if [ ! -d $ISSU_DEBUG_DIR ]; then
    mkdir $ISSU_DEBUG_DIR
    if [ $? -ne 0 ]; then
	exit 1
    fi
fi

if [ -z $PROC_INST ]
then
    ISSU_LOG_FILE=$NODE.$CALLING_PROCESS.log
else
    ISSU_LOG_FILE=$NODE.$CALLING_PROCESS.$PROC_INST.log
fi

ISSU_DEBUG_LOG=$ISSU_DEBUG_DIR/$ISSU_LOG_FILE

# Generate the snap
(
echo "$0 invoked by ($CALLING_PROCESS) at `iosclock -d 0x0`."

get_general_info

# If we have a pid, get the attach_process output
if [ -n "$PID" ]
then
    echo "------------------------------------------------------------"
    echo "Output of attach_process -p $PID -i 1"
    $BIN_DIR/attach_process -p $PID -i 1 -v -f
fi

) > $ISSU_DEBUG_LOG 2>&1

ISSU_DEBUG_TAR_DIR=/harddisk:/$ISSU_TAR_DIR
if [ -e $ISSU_DEBUG_LOG ]; then
    scp -o StrictHostKeychecking=no $ISSU_DEBUG_LOG root@$LEAD_IPADDR:$ISSU_DEBUG_TAR_DIR/$ISSU_LOG_FILE
fi





