#!/pkg/bin/ksh
# Dec 2016, imrankh3 
#
# Copyright (c) 2014-2019 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

# This script fragment contains the code for the following functions
# - print_main_heading
# - print_command_heading
# - run_single_command
# - run_commands
# - run_single_command_on_all_nodes
# - run_commands_on_all_nodes
# - default_parser_function
. /pkg/bin/show_tech_main_fragment

# List each set of show commands to be run. Each set must finish with an empty
# string. Note that it is important to use single quotes rather than double
# quotes for the strings containing your commands.

__cardtype="unspecified"
__coherent_detail="unspecified"

# Set the default values of the file name for which show tech-support macsec 
# is to run and the file it is to write to.
# Read in the arguments to the script, setting node_required and filename
# according to these arguments.
# Note that it is important for security reasons that users can only enter
# alphanumeric filenames and nodes and that anywhere calling this script must
# enforce this.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -t) __cardtype="$2"; shift 2;;
        -d) __coherent_detail=1; shift 1;;
         *)  default_parser_function "$@"; shift $#;;
    esac
done

if [ "$__cardtype" == "unspecified" ]; then
    __cardtype=`node_type`
fi

 # global commands on RP


###############################################################################
# Show commands that run once per LR                                          #
###############################################################################


sys_exec[1]='show install active'
sys__ksh[1]='sdr_instcmd show install active'

sys_exec[2]='show version'
sys__ksh[2]='ng_show_version'

sys_exec[3]='show platform'
sys__ksh[3]='show_platform_sysdb -v'

sys_exec[4]=''
sys__ksh[4]=''


rp_exec[1]='show ip int brief'
rp__ksh[1]='show_ip_interface -b -v all'

rp_exec[2]='show im database detail'
rp__ksh[2]='/pkg/bin/im_show database -l 0x3'

rp_exec[3]='show context location all'
rp__ksh[3]='corehelper_context -c 0x1 -n all'

rp_exec[4]='show running-config'
rp__ksh[4]='nvgen -c -l 1 -t 1 -o 1'

rp_exec[5]=''
rp__ksh[5]=''

index=5

coherent_add_header() {
    touch /root/logs
    echo "" >> $2
    date_str=`date +"%k:%M:%S.%3N UTC %a %b %d %Y"`
    echo "++++++++++++++++ $1  [$date_str] ++++++++++++++++" >> $2
    echo "++++++++++++++++ $1  [$date_str] ++++++++++++++++" >> /root/logs 
    echo "" >> $2
}

coherent_add_footer() {
    date_str=`date +"%k:%M:%S.%3N UTC %a %b %d %Y"`
    echo "" >> $2
    echo "--------------- $1  [$date_str] ----------------" >> $2
    echo "" >> $2
}

# Higher level logs
coherent_driver_state() {
    outfile=$1/coherent_health
    rp_ip=$2
    node_id=$3
    node_str=$4
    card_type=$5

    coherent_add_header "show coherent driver summary location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/coherent_show_health -o 3 -p -1 -L $node_id" >> $outfile
    coherent_add_footer "show coherent Driver summary location $node_str" $outfile

    coherent_add_header "show coherent driver internal location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/coherent_show_health -o 5 -p -1 -L $node_id" >> $outfile
    coherent_add_footer "show coherent driver internal location $node_str" $outfile

    coherent_add_header "show coherent driver linkstate location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/coherent_show_health -o 6 -p -1 -L $node_id" >> $outfile
    coherent_add_footer "show coherent driver linkstate location $node_str" $outfile

    coherent_add_header "show coherent driver timestats location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/coherent_show_health -o 2 -p -1 -L $node_id" >> $outfile
    coherent_add_footer "show coherent driver timestats location $node_str" $outfile

    info=`/pkg/bin/im_show database -l 0x1 -h $node_id  | grep Co | cut -d" " -f2 | sed -e 's/Co//' | xargs echo`

    for rsip in $info; do
        handle=`echo $rsip | sed -e 's/\//_/g'`
        coherent_add_header "show coherent driver detail optics $rsip location $node_str" $outfile
        #coherent_show_health -o 4 -p Optics0_0_2_2 -L 0
        /usr/bin/ssh $rp_ip "/pkg/bin/coherent_show_health -o 4 -p Optics$handle -L $node_id" >> $outfile
    	coherent_add_footer "show coherent driver detail optics $rsip location $node_str" $outfile
    done

    rack_num=`echo $node_str | awk -F / '{print $1}'`
    slot_num=`echo $node_str | awk -F / '{print $2}'`
    inst=`echo $node_str | awk -F / '{print $3}'`
    coherent_add_header "show alarms brief card location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/show_operational -a Alarms Brief BriefCard BriefLocationTable BriefLocation/NodeID/Rack=$rack_num;Slot=$slot_num;Instance=$inst" >> $outfile
    /user/bin/ssh 192.0.0.4 "/pkg/bin/show_operational -a Alarms Brief BriefCard BriefLocationTable BriefLocation/NodeID/Rack=0;Slot=0;Instance=CPU0"
    coherent_add_footer "show alarms brief card location $node_str" $outfile

    coherent_add_header "show processes coherent_driver location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/sbin/sysmgr_show -o -p coherent_driver -n $node_id" >> $outfile
    coherent_add_footer "show processes coherent_driver location $node_str" $outfile
}

coherent_controller_state() {
    outdir=$1   
    rp_ip=$2
    node_id=$3
    node_str=$4
    card_type=$5
    outfile=$outdir/coherent_interface_logs

    info=`/pkg/bin/im_show database -l 0x1 -h $node_id  | grep Co | cut -d" " -f2 | sed -e 's/Co//' | xargs echo`

    touch /root/logs
    echo "/pkg/bin/im_show database -l 0x1 -h $node_id  | grep Co | cut -d" " -f2 | sed -e 's/Co//" >> /root/logs
    echo "will fetch $info from rp_ip $rp_ip" >> /root/logs

    for rsip in $info; do
        handle=`echo $rsip | sed -e 's/\//_/g'`
        coherent_add_header "Show controller optics $rsip" $outfile
        /usr/bin/ssh $rp_ip "/pkg/sbin/show_controller_optics -a Optics$handle" >> $outfile
        coherent_add_footer "Show controller optics $rsip" $outfile

        coherent_add_header "show controllers optics $rsip pm current 30-sec optics 1" $outfile
        /usr/bin/ssh $rp_ip "/pkg/sbin/show_controller_optics_pm -n Optics$handle -T -o -z 0x1" >> $outfile
        coherent_add_footer "show controllers optics $rsip pm current 30-sec optics 1" $outfile

        if [ "$card_type" = "27012" ];then
            coherent_add_header "python /pkg/bin/coherent_config.py Optics $node_str $rsip" $1/coherent_config
            /usr/bin/ssh $rp_ip "python /pkg/bin/coherent_config.py Optics $node_str $rsip" >> $1/coherent_config
            coherent_add_footer "run python /pkg/bin/coherent_config.py Optics $node_str $rsip" $1/coherent_config
        fi

        coherent_add_header "Show controller CoherentDSP $rsip" $outfile
        /usr/bin/ssh $rp_ip "/pkg/bin/intf_recovered  -a CoherentDSP$handle" >> $outfile
        coherent_add_footer "Show controller CoherentDSP $rsip" $outfile

        coherent_add_header "show controllers coherentDSP $rsip pm current 30-sec otn" $outfile
        /usr/bin/ssh $rp_ip "/pkg/sbin/show_controller_otu_pm -n CoherentDSP$handle -T -g" >> $outfile
        coherent_add_footer "show controllers coherentDSP $rsip pm current 30-sec otn" $outfile

        coherent_add_header "show controllers coherentDSP $rsip pm current 30-sec fec" $outfile
        /usr/bin/ssh $rp_ip "/pkg/sbin/show_controller_otu_pm -n CoherentDSP$handle -T -f" >> $outfile
        coherent_add_footer "show controllers coherentDSP $rsip pm current 30-sec fec" $outfile
    done
}

coherent_save_core () {
    outdir=$1
    rp_ip=$2
    nodeid=$3
    node_str=$4
    card_type=$5
    nodename=$6

    core_string=`/usr/bin/ssh $rp_ip "/pkg/bin/corehelper_context -c 0x1 -n $nodeid" | grep "coherent_driver" | grep "Core for process:"`

    if [ "$core_string" != "" ]; then
        corefile=${core_string/Core for process: /}
        if [ "$corefile" != "" ]; then
            /bin/mkdir $outdir/core
            /usr/bin/scp root@$rp_ip:/harddisk\\:/$corefile $outdir/core
        fi
    fi
}


# MA EA logs
coherent_ma_ea_logs() {
    outfile=$1/ma_ea_logs
    rp_ip=$2
    node_id=$3
    node_str=$4
    card_type=$5

    coherent_add_header "show processes optics_ma location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/sbin/sysmgr_show -o -p optics_ma -n $node_id" >> $outfile
    coherent_add_footer "show processes optics_ma location $node_str" $outfile

    coherent_add_header "show processes optics_ea location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/sbin/sysmgr_show -o -p optics_ea -n $node_id" >> $outfile
    coherent_add_footer "show processes optics_ea location $node_str" $outfile

    coherent_add_header "show processes eth_intf_ma location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/sbin/sysmgr_show -o -p eth_intf_ma -n $node_id" >> $outfile
    coherent_add_footer "show processes eth_intf_ma location $node_str" $outfile

    coherent_add_header "show processes eth_intf_ea location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/sbin/sysmgr_show -o -p eth_intf_ea -n $node_id" >> $outfile
    coherent_add_footer "show processes eth_intf_ea location $node_str" $outfile

    coherent_add_header "show processes otn_ma location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/sbin/sysmgr_show -o -p otn_ma -n $node_id" >> $outfile
    coherent_add_footer "show processes otn_ma location $node_str" $outfile

    coherent_add_header "show processes otn_ea location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/sbin/sysmgr_show -o -p otn_ea -n $node_id" >> $outfile
    coherent_add_footer "show processes otn_ea location $node_str" $outfile
}


# PTAH trace logs for coherent driver
coherent_ptah_trace_logs() {
    outfile=$1/ptah_trace_logs
    rp_ip=$2
    node_id=$3
    node_str=$4
    card_type=$5

    job_id_str=`/usr/bin/ssh $rp_ip "/pkg/sbin/sysmgr_show -o -p coherent_driver -n $node_id | grep -m 1 Job\ Id"`
    job_id=`echo $job_id_str | awk -F':' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//'` 

    coherent_add_header "show ptah trace all jid $job_id location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/ptah_show_ltrace -B all -P $job_id -i $node_id" >> $outfile
    coherent_add_footer "show ptah trace all jid $job_id location $node_str" $outfile
}


# FPD logs
coherent_fpd_logs() {
    outfile=$1/fpd_logs
    rp_ip=$2

    coherent_add_header "show hw-module fpd" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/show_fpd_loc_ng fpd" >> $outfile
    coherent_add_footer "show hw-module fpd" $outfile

    coherent_add_header "show fpd package" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/show_fpd_pkg_ng" >> $outfile
    coherent_add_footer "show fpd package" $outfile

    coherent_add_header "fpdmgr pi client traces" $outfile
    /pkg/bin/ctracedec -gTtkAsdwp coherent/fpdmgr >> $outfile
    coherent_add_footer "fpdmgr pi client traces" $outfile
}

# VETHER and DPA trace logs
vether_dpa_trace() {
    outfile=$1/vether_dpa_trace
    rp_ip=$2
    node_id=$3
    node_str=$4
    card_type=$5

    coherent_add_header "show dpa trace location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/dpa_show_ltrace -i $node_id" >> $outfile
    coherent_add_footer "show dpa trace location $node_str" $outfile

    coherent_add_header "show vether-ea trace all location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/dpa_vether_ea_show_ltrace -i $node_id -E -V" >> $outfile
    coherent_add_footer "show vether-ea trace all location $node_str" $outfile

    coherent_add_header "show vether-driver server event location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/easy_debug_ltrace -i $node_id -W 0 -X 0x1 -Y 0x0 -Z libvether_server" >> $outfile
    coherent_add_footer "show vether-driver server event location $node_str" $outfile

    coherent_add_header "show vether-client client event location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/easy_debug_ltrace -i $node_id -W 0 -X 0x0 -Y 0x0 -Z libvether_client" >> $outfile
    coherent_add_footer "show vether-client client event location $node_str" $outfile

    coherent_add_header "show vether-client client error location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/easy_debug_ltrace -i $node_id -W 0 -X 0x1 -Y 0x0 -Z libvether_client" >> $outfile
    coherent_add_footer "show vether-client client error location $node_str" $outfile

    coherent_add_header "show controllers fia trace all location $node_str" $outfile
    /usr/bin/ssh $rp_ip "/pkg/bin/fia_show_ltrace -A -i $node_id" >> $outfile
    coherent_add_footer "show controllers fia trace all location $node_str" $outfile
}


# Coherent Ctrace logs
coherent_driver_logs() {
    dir=$1
    rp_ip=$2
    node_id=$3
    node_str=$4
    card_type=$5

    ea_buffers="coherent/fretta_eapd_optics coherent/eapd_dsp coherent/eapd_optics coherent/ea_client_optics coherent/ea_client_dsp"
    core_buffers="coherent/coh_error coherent/coh_info coherent/coh_debug " 
    buffers="$ea_buffers $core_buffers"

    if [ "$card_type" = "27012" ];then
        coherent_add_header "show coherent traces driver all location $node_str"  $dir/coherent_driver_ctrace_logs 
        /pkg/bin/ctracedec -Akdtgp  $buffers >> $dir/coherent_driver_ctrace_logs
        coherent_add_footer "show coherent traces driver all location $node_str" $dir/coherent_driver_ctrace_logs 
    else
        add_buffers="$buffers coherent/coh_dco_hal" 
        coherent_add_header "show coherent traces driver all location $node_str"  $dir/coherent_driver_ctrace_logs 
        /pkg/bin/ctracedec -Akdtgp  $add_buffers >> $dir/coherent_driver_ctrace_logs
        coherent_add_footer "show coherent traces driver all location $node_str" $dir/coherent_driver_ctrace_logs 
    fi

    coherent_add_header "show coherent traces driver periodic location $node_str" $dir/coherent_driver_ctrace_logs 
    /pkg/bin/ctracedec -Akdtgp coherent/coh_periodic >> $dir/coherent_driver_periodic_ctrace_logs
    coherent_add_footer "show coherent traces driver periodic location $node_str" $dir/coherent_driver_ctrace_logs 
}

# Add SDK, Denali, Arm logs
coherent_sdk_logs () {
    dir=$1
    rp_ip=$2
    nodeid=$3
    node_str=$4
    card_type=$5
    
    if [ "$card_type" = "27012" ];then
        coherent_add_header "show coherent traces sdk coh400g all location $node_str"  $dir/coherent_sdk_logs
        /pkg/bin/coh_show_sdk_coh400g_trace -APIDE -h $nodeid >> $dir/coherent_sdk_logs
        coherent_add_footer "show coherent traces sdk coh400g all location $node_str"  $dir/coherent_sdk_logs

        coherent_add_header "show coherent traces denali instance 0 location $node_str"  $dir/coherent_denali_logs 
        /usr/bin/ssh $rp_ip "/pkg/bin/coh_show_denali_trace -d 0 -i $node_id" >>  $dir/coherent_denali_logs 
        coherent_add_footer "show coherent traces denali instance 0 location $node_str"  $dir/coherent_denali_logs 

        coherent_add_header "show coherent hardware denali instance 0 location $node_str"  $dir/coherent_denali_logs
        /pkg/bin/coherent_show_denali_subsys  -d 0 -h $nodeid >> $dir/coherent_denali_logs
        coherent_add_footer "show coherent hardware denali instance 0 location $node_str"  $dir/coherent_denali_logs

        coherent_add_header "show coherent traces denali instance 1 location $node_str"  $dir/coherent_denali_logs 
        /usr/bin/ssh $rp_ip "/pkg/bin/coh_show_denali_trace -d 1 -i $node_id" >>  $dir/coherent_denali_logs 
        coherent_add_footer "show coherent traces denali instance 1 location $node_str"  $dir/coherent_denali_logs 

        coherent_add_header "show coherent hardware denali instance 1 location $node_str"  $dir/coherent_denali_logs
        /pkg/bin/coherent_show_denali_subsys  -d 1 -h $nodeid >> $dir/coherent_denali_logs
        coherent_add_footer "show coherent hardware denali instance 1 location $node_str"  $dir/coherent_denali_logs

        coherent_add_header "show coherent traces denali instance 2 location $node_str"  $dir/coherent_denali_logs 
        /usr/bin/ssh $rp_ip "/pkg/bin/coh_show_denali_trace -d 2 -i $node_id" >>  $dir/coherent_denali_logs 
        coherent_add_footer "show coherent traces denali instance 2 location $node_str"  $dir/coherent_denali_logs 

        coherent_add_header "show coherent hardware denali instance 2 location $node_str"  $dir/coherent_denali_logs
        /pkg/bin/coherent_show_denali_subsys  -d 2 -h $nodeid >> $dir/coherent_denali_logs
        coherent_add_footer "show coherent hardware denali instance 2 location $node_str"  $dir/coherent_denali_logs

        coherent_add_header "show coherent traces arm all location $node_str" $dir/coherent_arm_logs
        /pkg/bin/coh_show_arm_trace -A  >> $dir/coherent_arm_logs
        coherent_add_footer "show coherent traces arm all location $node_str" $dir/coherent_arm_logs
    else
        coherent_add_header "show coherent traces sdk flexcoh all location $node_str" $dir/coherent_sdk_logs
        /usr/bin/ssh $rp_ip "/pkg/bin/coh_show_sdk_flex_ctrace -zscvbnmdg -h $node_id" >>  $dir/coherent_sdk_logs
        coherent_add_footer "show coherent traces sdk flexcoh all location $node_str" $dir/coherent_sdk_logs
    fi
}

save_platform() {
    node_subdir=$__tar_file_directory_on_node
    /pkg/bin/show_platform_sysdb >> $node_subdir/platform
}

save_show_logging() {
    node_subdir=$__tar_file_directory_on_node
    show_logging >> $node_subdir/show_logging
}

save_boot_py_logs() {
    node_subdir=$__tar_file_directory_on_node
    python /pkg/bin/boot.py >> $node_subdir/boot_py_logs
}

# Parse the arguments to the script.
# Usage:
#
#sys.exit()
# A function called display() must be provided that calls the functions to
# Print the output heading
do_show_command() {
     print_main_heading "show tech-support coherent $ext_node_name"
     print_main_heading `node_type`

# This is the script
    if [ "$__cardtype" == "SYS" ]; then
# $__location is not defined in case of fretta, so we would not know 
# which location user wants to run show-tech. So system-tech will have
#  NCS-55A2-MOD-SE-S<1610744067>
#  NCS-55A2-MOD-HD-S<1610744069>
#  NCS-55A2-MOD-S<1610744068>
#  NC55-MOD-A-S<27034>
#  NC55-MOD-A-SE-S<27035>
#  NC55-6X200-DWDM-S<27012>
# all coherent cards info.
        #touch /root/logs
        #echo "save_platform(1)" >> /root/logs
        save_platform
        exec_commands sys
        exec_commands rp
        save_show_logging
        save_boot_py_logs
    else
         case "$__cardtype" in
         "RP"|"DRP")
             #Nothing to be done in RP as of now.
             ;;
         "LC")
            touch /root/logs
            card_type=`cat -v /dev/xr_bootstrap | grep PD_CARD_TYPE | awk -F "=" '{ print $2}'`
            echo "card = $card_type" >> /root/logs
	        if [ "$card_type" = "27012" -o "$card_type" = "27034" -o "$card_type" = "1610744068" -o "$card_type" = "27035" -o "$card_type" = "1610744067" -o "$card_type" = "1610744069" ]; then
                node_name=`uname -n`
                node_str=`uname -n | sed -e 's/xr-vm_node//' | sed -e 's/_/\//g'`
                node_subdir=$__tar_file_directory_on_node/$node_name
                if [ ! -d "$node_subdir" ]; then
                    /bin/mkdir $node_subdir
                fi

                # A bit wierd way of getting active RP IP
                ip=`/pkg/bin/show_platform_sysdb -v | grep \`/pkg/bin/node_list_generation -f DLRSC\` | awk '{print $7}'`
                # Node id of the current LC - needed for some CLIs
                my_nodeid=`node_conversion -i ${node_name/xr-vm_/}`
                #get the node name like 0/N/CPU0
                my_node=`node_conversion -E "${node_name/xr-vm_/}"`

                coherent_controller_state $node_subdir $ip $my_nodeid $node_str $card_type
                coherent_driver_state     $node_subdir $ip $my_nodeid $node_str $card_type
                coherent_save_core        $node_subdir $ip $my_nodeid $node_str $card_type $my_node 

                coherent_driver_logs      $node_subdir $ip $my_nodeid $node_str $card_type
                coherent_sdk_logs         $node_subdir $ip $my_nodeid $node_str $card_type
		        coherent_fpd_logs	      $node_subdir $ip $my_nodeid $node_str $card_type 
		        coherent_ptah_trace_logs  $node_subdir $ip $my_nodeid $node_str $card_type
		        coherent_ma_ea_logs	      $node_subdir $ip $my_nodeid $node_str $card_type
                #FIA/Vether Logs have seperate showtech, we should not club this here
		        #vether_dpa_trace	      $node_subdir $ip $my_nodeid $node_str $card_type
	        fi
	     ;;
         esac
     fi

     # Print the closing heading.
     print_main_heading "show tech-support coherent complete"
}

# The display function.
display() {
        do_show_command
}
  # the file option has been set.
. /pkg/bin/show_tech_file_fragment


