#!/bin/bash
# -----------------------------------------------------------------------------
# tech_envmon                     
#                                      
# Copyright (c) 2014-2016, 2020 by cisco Systems, Inc.
# All rights reserved.
#------------------------------------------------------------------------------

#
# Load the script provided by show-tech infra, which provides worker functions
#
source /opt/cisco/calvados/script/show_tech_main_fragment

#
# Parse the arguments to the script - card type and interface filter are the 
# only support options currently.  No need to use the default parser function,
# as this has been done by caller.
#
__cardtype="unspecified"
trace_only="0"
show_only="0"
not_sim="0"

while [ "$#" -gt "0" ]; do
    case "$1" in
        -T) trace_only="1"; shift 1;;
        -S) show_only="1"; shift 1;;
        -t) __cardtype="$2"; shift 2;;
        *) shift;;
    esac
done
if [ "$__cardtype" != "SYS" ]; then
    __cardtype=$__node_type
fi

not_sim=`lspci | grep "Ethernet controller: Intel" | wc -l`

#
# List of commands to be run.  East set must finish with a pair of empty 
# strings.  Note that it is important to use single quotes rather than double 
# quotes for the strings containing your commands.
#
# For all of these the __ksh variable is the process that will actually be 
# spawned, the _exec variable is just a string that is printed in the output to
# describe it. 
#

###############################################################################
# Show commands that run on all RP or LC                                      #
###############################################################################

#
# misc show RPLC
#
rplc_show_exec[1]='show version'
rplc_show__ksh[1]='cat /etc/show_version.txt'

rplc_show_exec[2]='show envmon log'
rplc_show__ksh[2]='cat /var/log/envmon.log'


rplc_show_exec[3]='pstack envmon'
rplc_show__ksh[3]='pstack `pidof envmon`'

rplc_show_exec[4]='pstack shelf_mgr'
rplc_show__ksh[4]='pstack `pidof shelf_mgr`'

rplc_show_exec[5]='pstack led_mgr'
rplc_show__ksh[5]='pstack `pidof led_mgr`'

rplc_show_exec[6]='show envmon data dump'
rplc_show__ksh[6]='cat /tmp/envmon_dump'

#
# Show commands
#
sys_show_exec[1]='show environment'
sys_show__ksh[1]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show environment"'

sys_show_exec[2]='show running-config'
sys_show__ksh[2]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show running-config"'

sys_show_exec[3]='show platform'
sys_show__ksh[3]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show platform"'

sys_show_exec[4]='show platform detail'
sys_show__ksh[4]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show platform detail"'

sys_show_exec[5]='show inventory all'
sys_show__ksh[5]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show inventory all"'

sys_show_exec[6]='show alarms'
sys_show__ksh[6]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show alarms"'

sys_show_exec[7]='show diag detail'
sys_show__ksh[7]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show diag detail"'

sys_show_exec[8]='show led'
sys_show__ksh[8]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show led"'

sys_show_exec[8]='show hw-moudle fpd'
sys_show__ksh[8]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show hw-module fpd"'

sys_show_exec[9]='show System-statistics ENVMON_CDUI'
sys_show__ksh[9]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show System-statistics Component-name ENVMON_CDUI"'

sys_show_exec[10]='show System-statistics ENVMON_CPMIRM'
sys_show__ksh[10]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show System-statistics Component-name ENVMON_CPMIRM"'

sys_show_exec[11]='show System-statistics ENVMON_CPMINM'
sys_show__ksh[11]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show System-statistics Component-name ENVMON_CPMINM"'

sys_show_exec[12]='show System-statistics ENVMON_PLAT_DRV' 
sys_show__ksh[12]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show System-statistics Component-name ENVMON_PLAT_DRV"'

sys_show_exec[13]='show System-statistics ENVMON_FAN_ALGO'
sys_show__ksh[13]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show System-statistics Component-name ENVMON_FAN_ALGO"'

sys_show_exec[14]='show System-statistics ENVMON_PWRM'
sys_show__ksh[14]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show System-statistics Component-name ENVMON_PWRM"'

sys_show_exec[15]='show processes envmon location all'
sys_show__ksh[15]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show processes envmon location all"'

sys_show_exec[16]='show processes shelf_mgr location all'
sys_show__ksh[16]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show processes shelf_mgr location all"'

sys_show_exec[17]='show processes calv_alarm_mgr location all'
sys_show__ksh[17]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show processes calv_alarm_mgr location all"'

sys_show_exec[18]='show processes led_mgr location all'
sys_show__ksh[18]='/opt/cisco/calvados/bin/show_cmd "terminal length 0; show processes led_mgr location all"'

#
# command only supported in real HW
#

#
# The display() function is the one that does all the work - called by us as 
# this is a worker script.
#
display() {
        
    run_module show_tech_envmon_pd -t $__cardtype 

    print_main_heading "General envmon tech-support info"

    if [ "$__cardtype" = "SYS" ]; then
        if [ "$trace_only" = "0" ]; then
                exec_commands sys_show
        fi

    else
        case "$__cardtype" in
        "RP")
            exec_commands rplc_show
            ;;

        "LC")
            exec_commands rplc_show
            ;;

        "FC")
            exec_commands rplc_show 
            ;;

        "XC")
            exec_commands rplc_show 
            ;;

        esac
    fi
    
    print_main_heading "General enmvon tech-support info complete"
}

display
