#!/pkg/bin/ksh
#------------------------------------------------------------------
# show tech-support netconf command
#
# Copyright (c) 2016-2017 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 netconf | 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
#
netconf_pid=`get_pid netconf`

#
# Commands to run
#
common_exec[1]="show process netconf"
common__ksh[1]='sysmgr_show -o -p netconf'
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"
else
    common_exec[2]="show process memory $netconf_pid"
    common__ksh[2]="sh_proc_mem_cli -p $netconf_pid"
    common_exec[3]="show configuration sessions"
    common__ksh[3]="cfgmgr_cmd_config_target_sessions"
    common_exec[4]="show process mem-stats summary pid $netconf_pid"
    common__ksh[4]="malloc_dump -s $netconf_pid"
    common_exec[5]="show process mem-stats pid $netconf_pid"
    common__ksh[5]="malloc_dump -m $netconf_pid"
    common_exec[6]="show version"
    common__ksh[6]='ng_show_version'
    common_exec[7]="show memory $netconf_pid"
    common__ksh[7]="show_memory_ng -p $netconf_pid"
fi

ssh_exec[1]="show ssh"
ssh__ksh[1]='show_ssh_session'
ssh_exec[2]="show ssh session details"
ssh__ksh[2]='show_ssh_alginfo'

netconf_exec[1]="show netconf-yang trace"
netconf__ksh[1]='yfwtrace -T netconf-yfw/processor.trace -T netconf-yfw/map.trace -T netconf-yfw/calvados.trace -T netconf-yfw/qt.trace -T netconf-yfw/me.trace -T netconf-yfw/bk.trace -T netconf/netconf.trace -T netconf/proxy_client.trace'
netconf_exec[2]="show netconf-yang clients"
netconf__ksh[2]='show_netconf_agent clients'
netconf_exec[3]="show netconf-yang rate-limit"
netconf__ksh[3]='show_netconf_agent rate-limit'
netconf_exec[4]="show netconf-yang statistics"
netconf__ksh[4]='show_netconf_agent stats'

#
# Follow (attach_process) Commands
#

attach_exec[1]="follow process $netconf_pid 1"
attach__ksh[1]="attach_process -p $netconf_pid -i 1"

#
# 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 $netconf_pid"
    system__ksh[2]="lsof -U -a -p $netconf_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

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 netconf 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

        #
        # run the ssh commands
        #
        exec_commands ssh

        #
        # run the netconf commands
        #
        exec_commands netconf

    else
        #
        # Collect follow netconf process
        #
        print_command_heading "follow netconf processe"
        cat <<EOF
These commands are run once now and once towards the end of the
command. This is to avoid the delay between iterations of follow
EOF

        #
        # attach_process
        #
        exec_commands attach

    fi

    #
    # Display a footer
    #
    print_main_heading "show tech-support netconf 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

