#!/pkg/bin/ksh
#------------------------------------------------------------------
# show tech-support grpc 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 emsd | 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
#
grpc_pid=`get_pid emsd`

#
# Commands to run
#
common_exec[1]="show process emsd"
common__ksh[1]='sysmgr_show -o -p emsd'
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 $grpc_pid"
    common__ksh[3]="sh_proc_mem_cli -p $grpc_pid"
    common_exec[4]="show process mem-stats summary pid $grpc_pid"
    common__ksh[4]="malloc_dump -s $grpc_pid"
    common_exec[5]="show process mem-stats pid $grpc_pid"
    common__ksh[5]="malloc_dump -m $grpc_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 $grpc_pid"
    common__ksh[8]="show_memory_ng -p $grpc_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'

grpc_exec[1]="show grpc status"
grpc__ksh[1]='show_grpc grpc status'
grpc_exec[2]="show grpc trace ems"
grpc__ksh[2]='yfed_show_trace -T ems/errors -T ems/events -T ems/info'
grpc_exec[3]="show grpc trace yfed"
grpc__ksh[3]='yfed_show_trace -T yfed/errors-ems -T yfed/events-ems -T yfed/info-ems'
grpc_exec[4]="show grpc trace yfw"
grpc__ksh[4]='yfed_show_trace -T ems-yfw/processor.trace -T ems-yfw/map.trace -T ems-yfw/calvados.trace -T ems-yfw/qt.trace -T ems-yfw/me.trace -T ems-yfw/bk.trace'

#
# Follow (attach_process) Commands
#

attach_exec[1]="follow process $grpc_pid 1"
attach__ksh[1]="attach_process -p $grpc_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 $grpc_pid"
    system__ksh[2]="lsof -U -a -p $grpc_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 grpc 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 grpc commands
        #
        exec_commands grpc

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

