#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_lldp - LLDP show tech commands
#
# February 2023
#
# Copyright (c) 2023 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

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

# Initialise any variables used.
__cardtype="unspecified"

# Parse the arguments to the script.
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

node_name_internal=`uname -n`
node_number=`node_conversion -i $node_name_internal`

###############################################################################
# Show commands that run once per LR                                          #
###############################################################################
sys_exec[1]='show version'
if [ "$platform" == "panini" ]; then
    sys__ksh[1]='ng_show_version'
else
    sys__ksh[1]='show_version'
fi

sys_exec[2]='show running-config'
sys__ksh[2]='nvgen -c -l 1 -t 1 -o 1'

sys_exec[3]='show install active'
if [[ "$platform" = "panini" ]]; then
    sys__ksh[3]='sdr_instcmd show install active'
else
    sys__ksh[3]='instcmd show install active'
fi

sys_exec[4]='show platform'
if [ "$platform" == "panini" ]; then
    sys__ksh[4]='show_platform_sysdb'
elif [ "$platform" == "viking" ]; then
    sys__ksh[4]='show_platform_vkg -e'
elif [ "$platform" == "hfr" ]; then
    sys__ksh[4]='shelfmgr_show_hfr -e'
else
    sys__ksh[4]='show_platform'
fi

sys_exec[5]='show platform vm'
if [ "$platform" == "panini" ]; then
    sys__ksh[5]='show_platform_sysdb -v'
elif [ "$platform" == "viking" ]; then
    sys__ksh[5]='show_platform_vkg -e'
elif [ "$platform" == "hfr" ]; then
    sys__ksh[5]='shelfmgr_show_hfr -e'
else
    sys__ksh[5]='show_platform'
fi

sys_exec[6]='show logging'
sys__ksh[6]='show_logging'

sys_exec[7]='show context location all'
if [[ "$platform" = "panini" ]]; then
    sys__ksh[7]='corehelper_context -c 0x1 -n all'
else
    sys__ksh[7]='dumper_context -c 0x1'
fi

sys_exec[8]='show redundancy location all'
sys__ksh[8]='redcon_show -n all'

sys_exec[9]='show process lldp_mgr'
sys__ksh[9]='sysmgr_show -o -p lldp_mgr'

sys_exec[10]='show process lldp_agent location all'
sys__ksh[10]='sysmgr_show -o -p lldp_agent -n all'

sys_exec[11]='show process ifmgr location all'
sys__ksh[11]='sysmgr_show -o -p ifmgr -n all'

sys_exec[12]=''
sys__ksh[12]=''

###############################################################################
# Show commands that run on every RP                                          #
###############################################################################

# misc info
rp_exec[1]='show cli history detail'
rp__ksh[1]='show_parser_history -h 0x2'

#LLDP specific
rp_exec[2]='show lldp'
rp__ksh[2]='lldp_command -s -g '

rp_exec[3]='show lldp interface location $location'
rp__ksh[3]='lldp_command -s -i -L $location'

rp_exec[4]='show lldp neighbors location $location detail'
rp__ksh[4]='lldp_command -s -n -L $location -d '

rp_exec[5]='show lldp errors location $location'
rp__ksh[5]='lldp_command -s -r -L $location'

rp_exec[6]='show lldp trace config'
rp__ksh[6]='show_lldp_ltrace -C '

rp_exec[7]='show lldp trace state'
rp__ksh[7]='show_lldp_ltrace -S '

rp_exec[8]='show lldp trace error'
rp__ksh[8]='show_lldp_ltrace -M '

rp_exec[9]='show lldp trace all'
rp__ksh[9]='show_lldp_ltrace -A '

rp_exec[10]=''
rp__ksh[10]=''

mem_exec[1]='sh processes cpu'
if [ "$platform" == "panini" ]; then
    mem__ksh[1]='show_watchdog -p'
else
    mem__ksh[1]='show_proc_cpu -c'
fi

mem_exec[2]='sh memory summary detail'
if [ "$platform" == "panini" ]; then
    mem__ksh[2]='show_memory_ng -s -d'
else
    mem__ksh[2]='show_memory -s -d'
fi

mem_exec[3]='sh memory heap summary all'
mem__ksh[3]='malloc_dump -A -s'

mem_exec[4]=''
mem__ksh[4]=''

# A function called display() must be provided that calls the functions to
# run the required show commands. The display() function will be called in
# the /pkg/bin/show_tech_comp_file_frag

display() {

   # Print the output heading
   print_main_heading "show tech-support lldp"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        case "$__cardtype" in
        "DRP"|"RP"|"LC")
            exec_commands rp
            exec_commands mem
            sleep 10
            print_main_heading "Collecting memory outputs after 10 seconds"
            exec_commands mem
            ;;
        esac
    fi

    enable_techs "" /pkg/bin/show_tech_pfi
    enable_techs "" /pkg/bin/show_tech_spio

    set_max_depth 3

    # Print the closing heading.
    print_main_heading "show tech-support lldp 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
