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

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


restconf_exec[1]="show restconf trace all"
restconf__ksh[1]='rest_trace -T restconf/fcgi.trace -T restconf/restconf.trace -T yfed/info-restconf -T yfed/events-restconf -T yfed/errors-restconf -T restconf-yfw/qt.trace  -T restconf-yfw/bk.trace -T restconf-yfw/me.trace -T restconf-yfw/processor.trace -T restconf-yfw/map.trace'

restconf_exec[5]="show restconf statistic all"
restconf__ksh[5]='show_restconf_agent stats all'
restconf_exec[6]="show restconf statistic connections"
restconf__ksh[6]='show_restconf_agent stats connections'
restconf_exec[7]="show restconf statistic http"
restconf__ksh[7]='show_restconf_agent stats http'
restconf_exec[8]="show restconf statistic service"
restconf__ksh[8]='show_restconf_agent stats service'
restconf_exec[9]="show restconf statistic sessions"
restconf__ksh[9]='show_restconf_agent stats sessions'
restconf_exec[10]="show process lighttpd_yang"
restconf__ksh[10]='sysmgr_show -o -p lighttpd_yang'
restconf_exec[11]="show running-config web server"
restconf__ksh[11]='nvgen -c -q gl/httpD/server/'
restconf_exec[12]="show running-config interface"
restconf__ksh[12]='nvgen -c -q if/act/;if/pre/'

#
# Follow (attach_process) Commands
#

attach_exec[1]="follow process $restconf_pid 1"
attach__ksh[1]="attach_process -p $restconf_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]="admin show platform"
    system__ksh[2]='shelfmgr_show_hfr -a'
else
    system_exec[2]="run lsof -U -a -p $restconf_pid"
    system__ksh[2]="lsof -U -a -p $restconf_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 restconf 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 restconf commands
        #
        exec_commands restconf

    else
        #
        # Collect follow restconf process
        #
        print_command_heading "follow restconf 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 restconf 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

