#!/pkg/bin/ksh
# -----------------------------------------------------------------------------
# show_tech_ptp - Show tech-support fast script for PTP.
#
# December 2009, Matthew Edwards
#
# Copyright (c) 2009-2013, 2015-2018, 2020 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------------------

#
# This script contains only the PTP-specific portions of the show tech. This
# allows another show-tech script to include just this script, without
# including all the non-PTP-specific information which is generated by
# "show tech PTP".
#

#
# Load the script provided by show-tech infra, which provides worker functions
#
. /pkg/bin/show_tech_main_fragment

#
# Parse the arguments to the script - interface filter is the only support 
# option currently.  No need to use the default parser function, as this has 
# been done by caller.
#
internal_if="unspecified"
external_if=""
trace_only="0"
show_only="0"
ether_arg_string=""
bundle_only_arg_string=""

while [ "$#" -gt "0" ]; do
    case "$1" in
        -I) internal_if="$2"; shift 2;;
        -T) trace_only="1"; shift 1;;
        -S) show_only="1"; shift 1;;
        *) shift;;
    esac
done

if [ "$internal_if" != "unspecified" ]; then
    external_if=`convert_interface_fmt '-e' $internal_if`
fi

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

#
# Collect showtechs from neighbouring components
#
enable_techs "$arg_string" show_tech_fsync show_tech_swucode_ptp /pkg/bin/spp_show_tech_support show_tech_gsp /pkg/bin/sysdb_show_tech_support_fast
set_max_depth 1


#
# List of commands to be run.  East set must finish with a pair of empty 
# strings.  Note that it is important to use single quotes rather than double 
# quotes for the strings containing your commands.
#
# For all of these the __ksh variable is the process that will actually be 
# spawned, the _exec variable is just a string that is printed in the output to
# describe it. 
#

###############################################################################
# Show commands that run once per LR
###############################################################################
sys_exec[1]='show ptp config-errors'
sys__ksh[1]='ptp_show_config_errors'

sys_exec[2]='show configuration commit changes - for each commit ID'
sys__ksh[2]='show_tech_config_history'

sys_exec[3]=''
sys__ksh[3]=''

#
# Commands requiring interface handle
#
sys_if_exec[1]='show ptp ma database interface $external_if'
sys_if__ksh[1]='ptp_show_if_idb -i $internal_if'

sys_if_exec[2]=''
sys_if__ksh[2]=''

###############################################################################
# Show commands that run on all RPs                                           #
###############################################################################

#
# Trace commands
#
rp_trace_exec[1]='show ptp trace all location $location'
rp_trace__ksh[1]='ptp_show_ltrace'

rp_trace_exec[2]=''
rp_trace__ksh[2]=''

#
# Show commands
#
rp_show_exec[1]='show processes ptp_gl location $location'
rp_show__ksh[1]='sysmgr_show -o -p ptp_gl'

rp_show_exec[2]='show processes ptp_ctrlr location $location'
rp_show__ksh[2]='sysmgr_show -o -p ptp_ctrlr'

rp_show_exec[3]='show ptp foreign-masters best'
rp_show__ksh[3]='ptp_show_grandmaster'

rp_show_exec[4]='show ptp advertised-clock'
rp_show__ksh[4]='ptp_show_advertized'

rp_show_exec[5]='show ptp local-clock'
rp_show__ksh[5]='ptp_show_local'

rp_show_exec[6]='show ptp global event-counters'
rp_show__ksh[6]='ptp_show_global_events'

rp_show_exec[7]='show ptp global database'
rp_show__ksh[7]='ptp_show_global_db'

rp_show_exec[8]='show ptp interfaces'
rp_show__ksh[8]='ptp_show_intf'

rp_show_exec[9]='show ptp unicast-peers'
rp_show__ksh[9]='ptp_show_if_peers'

rp_show_exec[10]='show ptp ma event-counters'
rp_show__ksh[10]='ptp_show_events'

rp_show_exec[11]='show processes ptp_ma location $location'
rp_show__ksh[11]='sysmgr_show -o -p ptp_ma'

rp_show_exec[12]='show ptp summary'
rp_show__ksh[12]='ptp_show_summary'

rp_show_exec[13]='show ptp packet-counters location $location - with 5s sleep'
rp_show__ksh[13]='show_ptp_packets_difference'

rp_show_exec[14]='show ptp dataset'
rp_show__ksh[14]='ptp_show_datasets'

rp_show_exec[15]='show ptp dataset port all'
rp_show__ksh[15]='ptp_show_datasets -a'

rp_show_exec[16]='show ptp foreign-masters'
rp_show__ksh[16]='ptp_show_foreign_clocks'

rp_show_exec[17]='show ptp ma database'
rp_show__ksh[17]='ptp_show_if_idb'

rp_show_exec[18]='show ptp utc-offset detail'
rp_show__ksh[18]='ptp_show_leap_seconds -d'

rp_show_exec[19]='show ptp interop'
rp_show__ksh[19]='ptp_show_interop'

rp_show_exec[20]=''
rp_show__ksh[20]=''


###############################################################################
# Show commands that run on all LCs                                           #
###############################################################################

#
# Trace commands
#
lc_trace_exec[1]='show ptp trace all location $location'
lc_trace__ksh[1]='ptp_show_ltrace'

lc_trace_exec[2]=''
lc_trace__ksh[2]=''

#
# Show commands
#
lc_show_exec[1]='show processes ptp_ma location $location'
lc_show__ksh[1]='sysmgr_show -o -p ptp_ma'
              
lc_show_exec[2]='show ptp foreign-masters'
lc_show__ksh[2]='ptp_show_foreign_clocks'
              
lc_show_exec[3]='show ptp interfaces'
lc_show__ksh[3]='ptp_show_intf'

lc_show_exec[4]='show ptp packet-counters location $location - with 5s sleep'
lc_show__ksh[4]='show_ptp_packets_difference'

lc_show_exec[5]='show ptp summary'
lc_show__ksh[5]='ptp_show_summary'
              
lc_show_exec[6]='show ptp ma event-counters'
lc_show__ksh[6]='ptp_show_events'
              
lc_show_exec[7]='show ptp unicast-peers'
lc_show__ksh[7]='ptp_show_if_peers'

lc_show_exec[8]='show ptp ma database'
lc_show__ksh[8]='ptp_show_if_idb'

lc_show_exec[9]='show processes ptp_ctrlr location $location'
lc_show__ksh[9]='sysmgr_show -o -p ptp_ctrlr'

lc_show_exec[10]=''
lc_show__ksh[10]=''


#
# The display() function is the one that does all the work - called by us as 
# this is a worker script.
#
display() {
    print_main_heading "show tech-support ptp internal"
        
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys

        if [ "$internal_if" != "unspecified" ]; then
        exec_commands sys_if
        fi
    else
        case "$__cardtype" in
        "DRP"|"RP")
            if [ "$trace_only" = "0" ]; then
                exec_commands rp_show
            fi

            if [ "$show_only" = "0" ]; then
                exec_commands rp_trace
            fi
            ;;
        "LC")
            if [ "$trace_only" = "0" ]; then
                exec_commands lc_show
            fi

            if [ "$show_only" = "0" ]; then
                exec_commands lc_trace
            fi
            ;;
        esac
    fi
    
    print_main_heading "show tech-support ptp internal complete"
}

display
