#!/pkg/bin/ksh
#
# Dec. 2013, Ketan Phaye
#
# Based on template from Jing Zhang (jingzh2) at:
# /spirit/issu/issudir/scripts/issu_debug_script_template
#
#  Copyright (c) 1999-2014, 2016 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
    echo "----------------------------------------------------------------"
}

# Get issudir debug information - this is called when the reason_enum option 
#is set
get_debug_info()
{
    echo "----------------------------------------------------------------"
    if [ $CALLING_PROCESS = "igmp" ] ;then 
    # Dump the IGMP trace buffers
    echo "----------------------------------------------------------------"
    echo "## `date +%H:%M:%S` Output of: show igmp trace location all "
    $BIN_DIR/igmp_show_ltrace -i all
    echo "## `date +%H:%M:%S` Output of: show igmp nsf detail "
    $BIN_DIR/igmp_show -q -d
    echo "## `date +%H:%M:%S` Output of: show igmp standby nsf detail "
    $BIN_DIR/igmp_show -q -b -d 
    echo "## `date +%H:%M:%S` Output of: show igmp nsr "
    $BIN_DIR/igmp_show -N 

else 
     # Dump the MLD trace buffers
    echo "----------------------------------------------------------------"
    echo "## `date +%H:%M:%S` Output of: show mld trace location all "
    $BIN_DIR/mld_show_ltrace -i all
    echo "## `date +%H:%M:%S` Output of: show mld nsf detail "
    $BIN_DIR/mld_show -q -d
    echo "## `date +%H:%M:%S` Output of: show mld standby nsf detail "
    $BIN_DIR/mld_show -q -b -d
    echo "## `date +%H:%M:%S` Output of: show mld nsr "
    $BIN_DIR/mld_show -N

fi 


}


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

USAGE="[-e <reason_enum>] [-p pid_to_trace] node_name calling_proc [proc_inst]"

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 [[ ($REASON_ENUM -eq 1) ]]; then
    get_debug_info
fi

# 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
