#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_timing - Shared show tech-support for RSP4 timing
# Cisco_user: Gaurav Sharma(gaurash6)
# Copyright (c) 2016-2021 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

# Load the script fragment containing 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

# Parse the arguments to the script.
__cardtype="unspecified"

while [ "$#" -gt "0" ]; do
   case "$1" in
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

if [ "$__filename" = "unspecified" ]; then
    echo "$0: output file not specified"
    exit
fi

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

################################################################################
# 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.
###############################################################################

#####################################################################
#  Commands that run on SYS node (i.e. the node where the show tech
#  command is issued from).
#####################################################################


cmd_index=1
sys_exec[$cmd_index]='show platform'
sys__ksh[$cmd_index]='show_platform_sysdb'
((cmd_index++))


sys_exec[$cmd_index]='show spp node'
sys__ksh[$cmd_index]='spp_sysdb_get -M node_stats'
((cmd_index++))

sys_exec[$cmd_index]='show spp graph'
sys__ksh[$cmd_index]='spp_sysdb_get -M node_graph '
((cmd_index++))

sys_exec[$cmd_index]='show spp offload lib trace location all'
sys__ksh[$cmd_index]='swoff_show_trace -i all'
((cmd_index++))

sys_exec[$cmd_index]='show frequency synchronization selection'
sys__ksh[$cmd_index]='fsync_show_selection'
((cmd_index++))

sys_exec[$cmd_index]='show frequency synchronization interfaces'
sys__ksh[$cmd_index]='fsync_show_interface'
((cmd_index++))

sys_exec[$cmd_index]='show frequency synchronization clock-interfaces'
sys__ksh[$cmd_index]='fsync_show_clock'
((cmd_index++))

sys_exec[$cmd_index]='show ptp platform servo'
sys__ksh[$cmd_index]='ptp_pd_show_servo 0'
((cmd_index++))

sys_exec[$cmd_index]='show ptp platform timestamps logger-info'
sys__ksh[$cmd_index]='ptp_pd_show_servo 1'
((cmd_index++))

sys_exec[$cmd_index]='show ptp dataset time-properties'
sys__ksh[$cmd_index]='ptp_show_datasets -t '
((cmd_index++))


sys_exec[$cmd_index]='show ptp dataset parent'
sys__ksh[$cmd_index]='ptp_show_datasets -p'
((cmd_index++))

sys_exec[$cmd_index]='show ptp dataset current'
sys__ksh[$cmd_index]='ptp_show_datasets -c'
((cmd_index++))

sys_exec[$cmd_index]='show ptp dataset default'
sys__ksh[$cmd_index]='ptp_show_datasets -d'
((cmd_index++))

sys_exec[$cmd_index]='show ptp local-clock'
sys__ksh[$cmd_index]='ptp_show_local'
((cmd_index++))

sys_exec[$cmd_index]='show ptp advertised-clock'
sys__ksh[$cmd_index]='ptp_show_advertized'
((cmd_index++))

sys_exec[$cmd_index]='show controllers timing controller clock'
sys__ksh[$cmd_index]='syncctrl_show clock'
((cmd_index++))

sys_exec[$cmd_index]='show controllers timing controller src'
sys__ksh[$cmd_index]='syncctrl_show src'
((cmd_index++))

sys_exec[$cmd_index]='show logging'
sys__ksh[$cmd_index]='show_logging'
((cmd_index++))

sys_exec[$cmd_index]='show ptp platform servo iter2'
sys__ksh[$cmd_index]='ptp_pd_show_servo 0'
((cmd_index++))

sys_exec[$cmd_index]='show ptp platform timestamps logger-info iter2'
sys__ksh[$cmd_index]='ptp_pd_show_servo 1'
((cmd_index++))

sys_exec[$cmd_index]=''
sys__ksh[$cmd_index]=''

#RPLC commands
cmd_index=1
rplc_exec[$cmd_index]='show spp node-counters location $location'
rplc__ksh[$cmd_index]='spp_sysdb_get -L $fq_nodeid node_counters'
((cmd_index++))

rplc_exec[$cmd_index]='show ptp trace location $location'
rplc__ksh[$cmd_index]='ptp_show_ltrace -i $fq_nodeid'
((cmd_index++))

rplc_exec[$cmd_index]='show ptp packet-counters location $location'
rplc__ksh[$cmd_index]='ptp_show_packets -h $fq_nodeid'
((cmd_index++))

rplc_exec[$cmd_index]='show controllers timing sync-agent trace all location $location'
rplc__ksh[$cmd_index]='show_sync_common_trace -E -h $fq_nodeid'
((cmd_index++))

rplc_exec[$cmd_index]=''
rplc__ksh[$cmd_index]=''

#RP only commands
cmd_index=1
rp_exec[$cmd_index]='show ptp platform trace location $location'
rp__ksh[$cmd_index]='ptp_pd_show_ltrace -i $fq_nodeid'
((cmd_index++))

rp_exec[$cmd_index]='show controllers timing input-clock status location $location'
rp__ksh[$cmd_index]='tmgctrl_core_show_ctrl -C -H $fq_nodeid'
((cmd_index++))

rp_exec[$cmd_index]='show controllers timing pll status location $location'
rp__ksh[$cmd_index]='tmgctrl_core_show_ctrl -P -H $fq_nodeid'
((cmd_index++))

rp_exec[$cmd_index]='show controllers timing tod snapshot location $location'
rp__ksh[$cmd_index]='tmgctrl_core_show_ctrl -T -H $fq_nodeid'
((cmd_index++))

rp_exec[$cmd_index]='show controllers timing controller trace all location $location'
rp__ksh[$cmd_index]='show_sync_common_trace -D -h $fq_nodeid'
((cmd_index++))

rp_exec[$cmd_index]='show controllers gnssea ltrace location $location'
rp__ksh[$cmd_index]='show_gnss_ea_ltrace -I .* -i $fq_nodeid -T .*'
((cmd_index++))

rp_exec[$cmd_index]='show gnss-receiver 0 location $location'
rp__ksh[$cmd_index]='gnssmgr_show_receiver -r 0 -h $fq_nodeid'
((cmd_index++))

rp_exec[$cmd_index]=''
rp__ksh[$cmd_index]=''

platform_specific_clis() {
    if [ -f /pkg/bin/show_tech_platform_specific_timing ]; then
        enable_techs ""  /pkg/bin/show_tech_platform_specific_timing
    fi
}


#Collect additional files on disk into the show-tech
bundle_rplc_files() {

    #Expect files to be present either on RP or on LC as
    # timing is distributed across platforms
    if [ -d $__tar_file_directory_on_node ]; then

        #APR logs - Collect the trace file
        apr_log='/harddisk:/ptp_trace_file.log'
        dest_file="ptp_trace_file_node_${fq_nodeid}.gz"
        if [ -e $apr_log ]; then
            gzip -c $apr_log >$__tar_file_directory_on_node/$dest_file
        fi

        #Collect the existing memory-log file
        mem_log='/harddisk:/ptp_mem_logger_trace_file.log'
        dest_file="ptp_mem_logger_trace_file_OLD_node_${fq_nodeid}.gz"
        #Collect the running timestamps
        if [ -e $mem_log ]; then
            gzip -c $mem_log >$__tar_file_directory_on_node/$dest_file
        fi

        #Dump the new memory-log file
        if [ -e /pkg/bin/ptp_pd_show_servo ]; then
            dest_file="ptp_mem_logger_trace_file_RUNNING_node_${fq_nodeid}.gz"
            rm -rf $mem_log
            /pkg/bin/ptp_pd_show_servo "2"  
            if [ -e $mem_log ]; then
                gzip -c $mem_log >$__tar_file_directory_on_node/$dest_file
            fi
        fi

        #Collect rotating apr logs
        rotating_log='/harddisk:/ptp_rotating_trace_file*.log'
        dest_file="ptp_trace_rotating_files.tgz"
        if ls $rotating_log >/dev/null 2>&1; then
            tar -cvzf $__tar_file_directory_on_node/$dest_file $rotating_log
        fi

        #SPP timestamps
        spp_ts_locations="/harddisk: /disk0:"
        dest_file="ptp_timestamps_${fq_nodeid}.gz"
        for loc in $spp_ts_locations; do
            spp_ts="$loc/ptp_timestamps"
            if [ -e $spp_ts ]; then
                gzip -c $spp_ts >$__tar_file_directory_on_node/$dest_file
            fi
        done

        #Sets traces
        for var in `ls /harddisk:/zl*_trace_file.log`; do
            dest_file="`basename $var`_${fq_nodeid}.gz"
            gzip -c $var >$__tar_file_directory_on_node/$dest_file
        done
    fi
}

##################################################################
# Run show tech commands
##################################################################
display() {

    print_main_heading "show tech-support  platform timing"

    # Get the card_type: SYS|RP|DRP|LC.
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
        platform_specific_clis
    else
        case "$__cardtype" in
        "DRP"|"RP")
            exec_commands rplc
            exec_commands rp
            platform_specific_clis
            bundle_rplc_files
            ;;
        "LC")
            exec_commands rplc
            platform_specific_clis
            bundle_rplc_files
            ;;
        esac
    fi

    print_main_heading "show tech-support  platform timing complete"
}

# This function calls the display() function and sends the output to file if
# the file option has been set.
. /pkg/bin/show_tech_file_fragment
