#!/pkg/bin/ksh
#------------------------------------------------------------------
# show tech-support yang server command
#
# April 2019, Jason Lam
#
# Copyright (c) 2019, 2021 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------

#
# Include the standard show tech-support functions
#
. /pkg/bin/show_tech_main_fragment
OS_STRING=`uname -s`
jid=`sysmgr_show -p yang_server | grep Job |  cut -f 2 -d ":" | cut -f 2 -d " "`

# get_pid function. Given the process name, it will
# echo the pid for that process
# it will have code to take care of the host OS.
function get_pid {
    if [[ ${OS_STRING} = "QNX" ]]; then
        pid=`pidin | grep $1 | cut -c1-8 | head -n 1`
    else
        pid=`ps -el | grep $1\$ | awk '{ print $4; }' | head -n 1`
    fi
    echo ${pid}
}

#
# Find the process IDs of the agent processes
#
yang_server_pid=`get_pid yang_server`

#
# Commands to run
#
common_exec[1]="show process yang_server"
common__ksh[1]='sysmgr_show -o -p yang_server'
if [[ "${OS_STRING}" = "QNX" ]]; then
    common_exec[2]="show version"
    common__ksh[2]="show_version"
    common_exec[3]="show configuration sessions"
    common__ksh[3]="cfgmgr_cmd_config_target_sessions"
    common_exec[4]="show process memory $jid"
    common__ksh[4]="malloc_dump -p $jid"
    common_exec[5]="show memory heap dllname $jid"
    common__ksh[5]="malloc_dump -A -p $jid -d"
    common_exec[6]="show context"
    common__ksh[6]="dumper_context -c 0x1 -n all"
else
    common_exec[2]="show process memory detail $yang_server_pid"
    common__ksh[2]="sh_proc_mem_cli -d -p $yang_server_pid"
    common_exec[3]="show configuration sessions"
    common__ksh[3]="cfgmgr_cmd_config_target_sessions"
    common_exec[4]="show process mem-stats summary pid $yang_server_pid"
    common__ksh[4]="malloc_dump -s $yang_server_pid"
    common_exec[5]="show process mem-stats pid $yang_server_pid"
    common__ksh[5]="malloc_dump -m $yang_server_pid"
    common_exec[6]="show version"
    common__ksh[6]='ng_show_version'
    common_exec[7]="show memory $yang_server_pid"
    common__ksh[7]="show_memory_ng -p $yang_server_pid"
    common_exec[8]="show memory heap dllname $yang_server_pid"
    common__ksh[8]="malloc_dump_wrapper -A -p $yang_server_pid -d"
    common_exec[9]="show context"
    common__ksh[9]="corehelper_context -c 0x1 -n all"
fi

yang_server_exec[1]="show yserver trace"
yang_server__ksh[1]="ys_show_trace -T ys/err -T ys/trc -T ys/dbg"
yang_server_exec[2]="show shmwin yang_server participants all-stats"
yang_server__ksh[2]="shmwin_show -N yang_server -c 0xb"
yang_server_exec[3]="show shmwin yang_server malloc-stats"
yang_server__ksh[3]="shmwin_show -N yang_server -c 0x9"

#
# Follow (attach_process) Commands
#

attach_exec[1]="follow process $yang_server_pid Delay 3 Iteration 3"
if [[ "${OS_STRING}" = "QNX" ]]; then
    attach__ksh[1]="attach_process -j $jid -d 3 -i 3"
else
    attach__ksh[1]="attach_process -p $yang_server_pid -d 3 -i 3"
fi

#
# process Commands
#
PLATFORM_TYPE=`uname -m`
block_exec[1]="show processes block"
if [[ "${PLATFORM_TYPE}" == "x86_64" ]]; then
    block__ksh[1]='sh_proc_ng_blocked'
else
    block__ksh[1]='show_processes -b'
fi

logging_exec[1]="show logging"
logging__ksh[1]='show_logging'

system_exec[1]="show redundancy"
system__ksh[1]='redcon_show'
if [[ "${PLATFORM_TYPE}" == "x86_64" ]]; then
    system_exec[2]="run lsof -U -a -p $yang_server_pid"
    system__ksh[2]="lsof -U -a -p $yang_server_pid"
    system_exec[3]='run cat /etc/build-info.txt'
    system__ksh[3]='cat /etc/build-info.txt'
    system_exec[4]='run cat /etc/os-release'
    system__ksh[4]='cat /etc/os-release'
    system_exec[5]='run cat /etc/thirdparty-release'
    system__ksh[5]='cat /etc/thirdparty-release'
    system_exec[6]='run uptime'
    system__ksh[6]='uptime'
fi

config_exec[1]="show running-config"
config__ksh[1]='nvgen -c -l 1 -t 1 -o 1'

oper_exec[1]="show mdcli trace all"
oper__ksh[1]='yfed_show_trace -T yfed/errors-mdcli -T yfed/events-mdcli -T yfed/info-mdcli -T mdcli-yfw/processor.trace -T mdcli-yfw/map.trace -T mdcli-yfw/calvados.trace -T mdcli-yfw/confd.trace -T mdcli-yfw/qt.trace -T mdcli-yfw/me.trace -T mdcli-yfw/bk.trace -T mdcli/mdcli.trace'

yang_exec[1]="run ls -ls /pkg/yang"
yang__ksh[1]="ls -ls /pkg/yang"
yang_exec[2]="run ls -ls /pkg/yang/meta"
yang__ksh[2]="ls -ls /pkg/yang/meta"

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


#
# Our display function
#
display() {
    #
    # Display a header
    #
    if [[ $shared -eq 1 ]]; then
        print_main_heading "show tech-support yang_server on shared plane"
    fi  

    if [[ "$__cardtype" == "SYS" ]]; then
        #
        # Display the nodes in the LR or system
        # Display the redundancy status of the system
        #
        exec_commands common
        exec_commands system
        exec_commands block

        #
        # run the ssh commands
        #
        exec_commands ssh

        #
        # run the yang_server commands
        #
        exec_commands yang_server

        # 
        # run yang commands
        #
        exec_commands yang
        
        #
        # run additional commands
        #
        exec_commands config
        exec_commands logging
        exec_commands oper

        #
        # follow yang_server process
        #
        exec_commands attach

        
    else
        exec_commands block
    fi

    #
    # Display a footer
    #
    print_main_heading "show tech-support yang_server complete"

}

#
# Use the standard show tech-support infra to call our display function and
# send the output to console or file
#
. /pkg/bin/show_tech_file_fragment

