#!/pkg/bin/ksh
#----------------------------------------------------------------------------
# appmgr_show_tech - Appmgr tech-support
#
# September 2020, Arshan Hashemi
#
# Copyright (c) 2020-2024 by Cisco Systems, Inc.
# All rights reserved.
#----------------------------------------------------------------------------

. /pkg/bin/show_tech_main_fragment

__cardtype="unspecified"


_docker="/usr/bin/docker"
if [ -S "/misc/app_host/docker.sock" ]; then
     export DOCKER_HOST=unix:///misc/app_host/docker.sock
fi


#----------------------------------------------------------------------------
# Parse the arguments to the script.
#

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

if [ "$__cardtype" == "unspecified" ]; then
    __cardtype=`rp_type`
fi


#----------------------------------------------------------------------------
# Commands run once per system
#

# General Commands
idx=1
sys_gen_exec[idx]='show logging'
sys_gen__ksh[idx]='show_logging'
((idx++))

sys_gen_exec[idx]='show context'
sys_gen__ksh[idx]='corehelper_context -c 0x1 -n all'
((idx++))

sys_gen_exec[idx]='show platform'
if [[ "$platform" == "viking" ]]; then
     sys_gen__ksh[idx]='show_platform_vkg -e'
elif [[ "$platform" == "panini" ]]; then
     sys_gen__ksh[idx]='show_platform_sysdb -v'
elif [[ "$platform" == "hfr" ]]; then
     sys_gen__ksh[idx]='shelfmgr_show_hfr -e'
else
     sys_gen__ksh[idx]='show_platform -a'
fi
((idx++))

sys_gen_exec[idx]='show version'
if [[ "$platform" == "panini" || "$platform" == "viking" ]]; then
     sys_gen__ksh[idx]='ng_show_version'
else
     sys_gen__ksh[idx]='show_version'
fi
((idx++))

if [[ "$platform" == "panini"  || "$platform" == "viking" ]]; then
     sys_gen_exec[idx]='show install active'
     sys_gen__ksh[idx]='sdr_instcmd show install active'
else
     sys_gen_exec[idx]='show install active summary'
     sys_gen__ksh[idx]='instcmd show install active summary'
fi
((idx++))

sys_gen_exec[idx]='show redundancy'
sys_gen__ksh[idx]='show_rmf_sysdb'
((idx++))

sys_gen_exec[idx]='show running-config'
sys_gen__ksh[idx]='nvgen -c -l 1 -t 1 -i1 -o 1'
((idx++))

sys_gen_exec[idx]='show processes blocked location all'
sys_gen__ksh[idx]='sh_proc_ng_blocked -l all'
((idx++))

sys_gen_exec[idx]='show processes location all'
sys_gen__ksh[idx]='show_processes -l all'
((idx++))

if [ -f /opt/cisco/thinxr/am_i_thinxr ]; then
	sys_gen_exec[idx]='show reboot history detail'
	sys_gen__ksh[idx]='show_platform_sysdb "-r" "-d"'
else
	sys_gen_exec[idx]='show reboot history location $location'
	sys_gen__ksh[idx]='show_reboot_ng -h -n $fq_nodeid'
fi
((idx++))

sys_gen_exec[idx]=''
sys_gen__ksh[idx]=''
((idx++))

# Appmgr operational data
idx=1
sys_oper_exec[idx]='show appmgr application-table'
sys_oper__ksh[idx]='appmgr-cli show-app-table'
((idx++))

sys_oper_exec[idx]='show processes appmgr '
sys_oper__ksh[idx]='sysmgr_show "-o" "-p" "appmgr"'
((idx++))

sys_oper_exec[idx]='show appmgr source-table'
sys_oper__ksh[idx]='appmgr-cli show-source-table'
((idx++))

sys_oper_exec[idx]='show appmgr packages installed'
sys_oper__ksh[idx]='appmgr-cli show-installed-packages'
((idx++))

sys_oper_exec[idx]=''
sys_oper__ksh[idx]=''
((idx++))

#----------------------------------------------------------------------------
# Commands run once per node
#

# Since Appmgr does not run on LCs this is deferred to RP/DRP Section

#----------------------------------------------------------------------------
# RP/DRP-only commands
#

# Run top as soon as possible to try and capture the true state of the system
idx=1
rp_gen_exec[idx]='run top -bn1'
rp_gen__ksh[idx]='top -bn1'
((idx++))

rp_gen_exec[idx]=''
rp_gen__ksh[idx]=''
((idx++))

# Logs and trace
idx=1
rp_logs_exec[idx]='run appmgr_show_trace --all'
rp_logs__ksh[idx]='appmgr_show_trace --all'
((idx++))

rp_logs_exec[idx]=''
rp_logs__ksh[idx]=''
((idx++))

# Persistent files
idx=1
if [ -d "/misc/app_host" ]; then
    rp_files_exec[idx]='run tree -a /misc/app_host/appmgr/'
    rp_files__ksh[idx]='tree -a /misc/app_host/appmgr/'
    ((idx++))
    rp_files_exec[idx]='run awk 1 /misc/app_host/appmgr/.internal/state/apps/*.app '
    rp_files__ksh[idx]='awk 1 /misc/app_host/appmgr/.internal/state/apps/*.app '
    ((idx++))
    rp_files_exec[idx]='run awk 1 /misc/app_host/appmgr/.internal/state/internal-apps/*.app '
    rp_files__ksh[idx]='awk 1 /misc/app_host/appmgr/.internal/state/internal-apps/*.app '
    ((idx++))
    rp_files_exec[idx]='run awk 1 /misc/app_host/appmgr/.internal/state/clients/*.client '
    rp_files__ksh[idx]='awk 1 /misc/app_host/appmgr/.internal/state/clients/*.client '
    ((idx++))
else
    rp_files_exec[idx]='run tree -a /var/lib/docker/appmgr/'
    rp_files__ksh[idx]='tree -a /var/lib/docker/appmgr/'
    ((idx++))
    rp_files_exec[idx]='run awk 1 /var/lib/docker/appmgr/.internal/state/apps/*.app '
    rp_files__ksh[idx]='awk 1 /var/lib/docker/appmgr/.internal/state/apps/*.app '
    ((idx++))
    rp_files_exec[idx]='run awk 1 /var/lib/docker/appmgr/.internal/state/internal-apps/*.app '
    rp_files__ksh[idx]='awk 1 /var/lib/docker/appmgr/.internal/state/internal-apps/*.app '
    ((idx++))
    rp_files_exec[idx]='run awk 1 /var/lib/docker/appmgr/.internal/state/clients/*.client '
    rp_files__ksh[idx]='awk 1 /var/lib/docker/appmgr/.internal/state/clients/*.client '
    ((idx++))
fi

rp_files_exec[idx]=''
rp_files__ksh[idx]=''
((idx++))

# Misc
rp_misc_exec[idx]='run /usr/bin/uptime'
rp_misc__ksh[idx]='/usr/bin/uptime'
((idx++))

rp_misc_exec[idx]='run cat /var/log/messages'
rp_misc__ksh[idx]='cat /var/log/messages'
((idx++))

# Filesystem & LVM state

rp_misc_exec[idx]='run df -h'
rp_misc__ksh[idx]='df -h'
((idx++))

rp_misc_exec[idx]='run mount'
rp_misc__ksh[idx]='mount'
((idx++))

rp_misc_exec[idx]='run cat /etc/fstab'
rp_misc__ksh[idx]='cat /etc/fstab'
((idx++))

# Certificates
if [ -d "/misc/app_host" ]; then
    rp_misc_exec[idx]='run rpm  --dbpath /misc/app_host/appmgr/rpm -qi gpg-pubkey-\*'
    rp_misc__ksh[idx]='rpm  --dbpath /misc/app_host/appmgr/rpm -qi gpg-pubkey-\*'
else
    rp_misc_exec[idx]='run rpm --dbpath /var/lib/docker/appmgr/rpm -qi gpg-pubkey-\*'
    rp_misc__ksh[idx]='rpm --dbpath /var/lib/docker/appmgr/rpm -qi gpg-pubkey-\*'

fi
((idx++))

# Packages
if [ -d "/misc/app_host" ]; then
    rp_files_exec[idx]='run rpm -qa --dbpath /misc/app_host/appmgr/rpm --last'
    rp_files__ksh[idx]='rpm -qa --dbpath /misc/app_host/appmgr/rpm --last'
    ((idx++))
else
    rp_files_exec[idx]='run rpm -qa --dbpath /var/lib/docker/appmgr/rpm --last'
    rp_files__ksh[idx]='rpm -qa --dbpath /var/lib/docker/appmgr/rpm --last'
    ((idx++))
fi

rp_misc_exec[idx]=''
rp_misc__ksh[idx]=''
((idx++))

# Docker
idx=1
rp_docker_exec[idx]='run '"$_docker"' version'
rp_docker__ksh[idx]="$_docker"' version'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' info'
rp_docker__ksh[idx]="$_docker"' info'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' system df'
rp_docker__ksh[idx]="$_docker"' system df'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' ps -a'
rp_docker__ksh[idx]="$_docker"' ps -a'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' inspect $('"$_docker"' ps -aq)'
rp_docker__ksh[idx]="$_docker"' inspect $('"$_docker"' ps -aq)'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' --no-stream --no-trunc stats $('"$_docker"' ps -aq)'
rp_docker__ksh[idx]="$_docker"' stats  --no-stream --no-trunc $('"$_docker"' ps -aq)'
((idx++))

rp_docker_exec[idx]='run appmgr_get_container_logs'
rp_docker__ksh[idx]='appmgr_get_container_logs'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' images -a'
rp_docker__ksh[idx]="$_docker"' images -a'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' inspect $('"$_docker"' images -aq)'
rp_docker__ksh[idx]="$_docker"' inspect $('"$_docker"' images -aq)'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' network ls'
rp_docker__ksh[idx]="$_docker"' network ls'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' inspect $('"$_docker"' network ls -q)'
rp_docker__ksh[idx]="$_docker"' inspect $('"$_docker"' network ls -q)'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' volume ls'
rp_docker__ksh[idx]="$_docker"' volume ls'
((idx++))

rp_docker_exec[idx]='run '"$_docker"' inspect $('"$_docker"' volume ls -q)'
rp_docker__ksh[idx]="$_docker"' inspect $('"$_docker"' volume ls -q)'
((idx++))

if [ -f /opt/cisco/thinxr/am_i_thinxr ]; then
    rp_docker_exec[idx]='run journalctl --no-pager -u docker'
    rp_docker__ksh[idx]='journalctl --no-pager -u docker'
    ((idx++))
fi

rp_docker_exec[idx]='run cat /var/log/iosxr/appmgr/save_dm_containers.log'
rp_docker__ksh[idx]='cat /var/log/iosxr/appmgr/save_dm_containers.log'
((idx++))

rp_docker_exec[idx]='run cat /var/log/iosxr/appmgr/remove_sysctl_run_opts.log'
rp_docker__ksh[idx]='cat /var/log/iosxr/appmgr/remove_sysctl_run_opts.log'
((idx++))

rp_docker_exec[idx]='show cli history detail'
rp_docker__ksh[idx]='show_parser_history "-h" "0x2" '
((idx++))

rp_docker_exec[idx]=''
rp_docker__ksh[idx]=''
((idx++))

#----------------------------------------------------------------------------
# Script
#

display() {
    print_main_heading "show tech-support appmgr"

    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys_gen
        exec_commands sys_oper
    else
        case "$__cardtype" in
        "RP")
            exec_commands rp_gen
            exec_commands rp_logs
            exec_commands rp_files
            exec_commands rp_misc
            exec_commands rp_docker
            ;;
        "DRP")
            exec_commands rp_gen
            exec_commands rp_logs
            exec_commands rp_files
            exec_commands rp_misc
            exec_commands rp_docker
            ;;
        esac
    fi

    print_main_heading "show tech-support appmgr complete"
}

# Run the appropriate function depending on the node specified and if a
# file is specified write the output to that file.
. /pkg/bin/show_tech_file_fragment
