#!/pkg/bin/ksh
#
# May. 2013, Leena Jeevan
#
# Based on template from Jing Zhang (jingzh2) at:
# /spirit/issu/issudir/scripts/issu_debug_script_template
#
#  Copyright (c) 1999-2014, 2018 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 IPv6 trace buffers
    echo "----------------------------------------------------------------"
    echo "## `date +%H:%M:%S` Output of: ipv6 ma information"
    echo "++++++++++ Begin: show sysmgr ++++++++++"
    $BIN_DIR/show_ipv6_interface -v  all
    $BIN_DIR/show_ipv6_interface -u -v all
    exec_timeout -t 10 $BIN_DIR/show_ipv6_interface
    echo "++++++++++ End: show sysmgr ++++++++++"
    echo "+++++ Begin: show system statistics component ipv6-ma +++++"
    $BIN_DIR/ship_show -c ipv6-ma
    exec_timeout -t 10 $BIN_DIR/ship_show
    echo "+++++ End: show system statistics component ipv6-ma ++++++++"
    echo "++++++++++ Begin: show ipv6 ma event ++++++++++"
    $BIN_DIR/show_ipv6_ma_evts
    exec_timeout -t 10 $BIN_DIR/show_ipv6_ma_evts
    echo "++++++++++ End: show ipv6 ma event ++++++++++"
    echo "++++++++++ Begin: show ipv6 traffic brief ++++++++++"
    $BIN_DIR/show_ipv6_traffic -b -l 
    exec_timeout -t 10 $BIN_DIR/show_ipv6_traffic
    echo "++++++++++ End: show ipv6 traffic brief ++++++++++"
    echo "++++++++++ Begin: show sysmgr ++++++++++"
    /pkg/sbin/sysmgr_show -o -p ipv6_ma
    /pkg/sbin/sysmgr_show -o -p ipv6_io
    /pkg/sbin/sysmgr_show -o -p ipv6_ea
    exec_timeout -t 10 /pkg/sbin/sysmgr_show
    echo "++++++++++ End: show sysmgr ++++++++++"
    echo "++++++++++ Begin: show nvgen trace client ++++++++++"
    /pkg/sbin/nvgen -c  -l 1 -t 1 -o 1
    exec_timeout -t 10 /pkg/sbin/nvgen
    echo "++++++++++ End: show nvgen trace client ++++++++++"
    echo "++++++++++ Begin: show logging ++++++++++"
    /pkg/sbin/show_logging
    exec_timeout -t 10 /pkg/sbin/show_logging
    echo "++++++++++ End: show logging ++++++++++"
    echo "++++++++++ Begin: show platform ++++++++++"
    $BIN_DIR/show_platform
    exec_timeout -t 10 $BIN_DIR/show_platform
    echo "++++++++++ End: show platform ++++++++++"
    echo "++++++++++ Begin: show redundancy ++++++++++"
    $BIN_DIR/redcon_show
    exec_timeout -t 10 $BIN_DIR/redcon_show
    echo "++++++++++ End: show redundancy ++++++++++"
    echo "++++++++++ Begin: show proccessed blocked ++++++++++"
    $BIN_DIR/show_processes -b 
    exec_timeout -t 10 $BIN_DIR/show_processes
    echo "++++++++++ End: show proccessed blocked ++++++++++"
    echo "++++++++++ Begin: show ipv6 trace ma ++++++++++"
    $BIN_DIR/show_ltrace_ma  
    $BIN_DIR/show_ltrace_ma -i  all
    exec_timeout -t 10 $BIN_DIR/show_ltrace_ma
    echo "++++++++++ End: show ipv6 trace ma ++++++++++"
    echo "++++++++++ Begin: show ipv6 trace io ++++++++++"
    $BIN_DIR/show_ltrace_io  
    $BIN_DIR/show_ltrace_io -i  all
    exec_timeout -t 10 $BIN_DIR/show_ltrace_io
    echo "++++++++++ End: show ipv6 trace io ++++++++++"
    echo "++++++++++ Begin: show ipv6 trace ea ++++++++++"
    $BIN_DIR/ipv6_ea_pi_show_ltrace  
    $BIN_DIR/ipv6_ea_pi_show_ltrace -i  all
    exec_timeout -t 10 $BIN_DIR/ipv6_ea_pi_show_ltrace
    echo "++++++++++ End: show ipv6 trace ea ++++++++++"
    echo "++++++++++ Begin: show ipv6 trace netio ++++++++++"
    $BIN_DIR/show_ltrace_netio  
    $BIN_DIR/show_ltrace_netio -i  all 
    exec_timeout -t 10 $BIN_DIR/show_ltrace_netio
    echo "++++++++++ End: show ipv6 trace netio ++++++++++"
    echo "++++++++++ Begin: show im database detail ++++++++++"
    $BIN_DIR/im_show_database -v 0x0 -l 0x3
    exec_timeout -t 10 $BIN_DIR/im_show_database
    echo "++++++++++ End: show im database detail ++++++++++"
    echo "++++++++++ Begin: show ipv6 router global-address ++++++++++"
    $BIN_DIR/show_ipv6_global_addr -a
    exec_timeout -t 10 $BIN_DIR/show_ipv6_global_addr
    echo "++++++++++ End: show ipv6 router global-address ++++++++++"
    echo "++++++++++ Begin: show ipv6 router global-address clients ++++++++++"
    $BIN_DIR/show_ipv6_global_addr -c
    exec_timeout -t 10 $BIN_DIR/show_ipv6_global_addr
    echo "++++++++++ End: show ipv6 router global-address clients ++++++++++"
    echo "----------------------------------------------------------------"
}

# Get issudir debug information - this is called when the reason_enum option is set
# Not used by IPv6_ma at this point.
get_debug_info()
{
    echo "----------------------------------------------------------------"
    echo "## `date +%H:%M:%S` Output of: issumgr_dbg"
    eval $BIN_DIR/issumgr_dbg $REASON_ENUM

}


###############################################################################
# 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 [[ ($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
