 #!/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 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 IPv4 arp trace buffers
     echo "----------------------------------------------------------------"
     echo "## `date +%H:%M:%S` Output of: ipv4 arp information"
     echo "++++++++++ Begin: show arp trace ++++++++++"
     $BIN_DIR/show_arp_ltrace -i all
     $BIN_DIR/show_arp_ltrace 
     exec_timeout -t 10 $BIN_DIR/show_arp_ltrace
     echo "++++++++++ End: show arp trace ++++++++++"
     echo "++++++++++ Begin: show arp ++++++++++"
     $BIN_DIR/arp_command show -A
     exec_timeout -t 10 $BIN_DIR/arp_command 
     echo "++++++++++ End: show arp ++++++++++"
     echo "++++++++++ Begin: show nvgen ++++++++++"
     /pkg/sbin/nvgen -c -l 1 -t 1 -o 1
     exec_timeout -t 10 /pkg/sbin/nvgen
     echo "++++++++++ End: show nvgen ++++++++++"
     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/recon_show
     echo "++++++++++ End: show redundancy ++++++++++"
     echo "++++++++++ Begin: show im database ++++++++++"
     /pkg/sbin/im_show_database -v 0x0 -l 0x3
     exec_timeout -t 10 /pkg/sbin/im_show_database
     echo "++++++++++ End: show im database ++++++++++"
     echo "----------------------------------------------------------------"
 }
 
 # Get issudir debug information - this is called when the reason_enum option is set
 # Not used by IPv4_arp at this point.
 get_debug_info()
 {
     echo "----------------------------------------------------------------"
 }
 
 
 ###############################################################################
 # 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
