#!/bin/bash
# -----------------------------------------------------------------------------
# tech_install
#
#  Copyright (c) 2015-2018 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

shopt -s nullglob

#
# 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"
host_ip="0"
ctrl_vrf_cmd=""
ssh_options="-o UserKnownHostsFile=/dev/null -o LogLevel=quiet"
redir_stderr="2>&1"
virsh_dumpxml="virsh list --all | awk '{print \$2}' | while read line; do echo VM:\$line; virsh dumpxml \$line; done"
pvscan_err="[ -f /tmp/pvscan_err.log ] && cat /tmp/pvscan_err.log "
host_pxe_log="/var/log/pxe_install.sh.flow.log"
host_user_log="/var/log/user.log"
dest_host_log_path="/var/log/host_user.log"
dest_pxe_log_path="/var/log"

platform=$(/usr/bin/xr_sysctl kernel.boot.platform | cut -d "=" -f2)
boardtype=$(/usr/bin/xr_sysctl kernel.boot.boardtype | cut -d "=" -f2)

get_host_ip host_ip
get_vrf_command ctrl_vrf_cmd CTRL_VRF

dev_name=$(ssh $ssh_options $host_ip "pvscan" | awk '{print $2;exit}')

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

if [ "$__cardtype" != "SYS" ]; then
    __cardtype=$__node_type
fi

touch="/bin/touch"
#error file part of sh-tech o/p, redirect all errors to this file
error_file="/var/log/install/sh_tech_errors.txt"
if [ ! -f "$error_file" ] ; then
	# if not create the file
    $touch $error_file
fi
#
# List of commands to be run.  Each 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. 
#

# ................................................................................ 
# General 
# ................................................................................ 

sys_show_exec[1]='show processes location all'
sys_show__ksh[1]='/opt/cisco/calvados/bin/show_cmd "show processes location all | nomore"'

sys_show_exec[2]='show running-config'
sys_show__ksh[2]='/opt/cisco/calvados/bin/show_cmd "show running-config | nomore"'

sys_show_exec[3]='show platform'
sys_show__ksh[3]='/opt/cisco/calvados/bin/show_cmd "show platform | nomore"'

sys_show_exec[4]='show system-service-inventory' 
sys_show__ksh[4]='/opt/cisco/calvados/bin/show_cmd "show system-service-inventory | nomore"'

sys_show_exec[5]='show logging'
sys_show__ksh[5]='/opt/cisco/calvados/bin/show_cmd "show logging | nomore"'

sys_show_exec[6]='show install repository all'
sys_show__ksh[6]='/opt/cisco/calvados/bin/show_cmd "show install repository all | nomore"'

sys_show_exec[7]='show install active'
sys_show__ksh[7]='/opt/cisco/calvados/bin/show_cmd "show install active | nomore"'

sys_show_exec[8]='show install inactive'
sys_show__ksh[8]='/opt/cisco/calvados/bin/show_cmd "show install inactive | nomore"'

sys_show_exec[9]='show install prepare'
sys_show__ksh[9]='/opt/cisco/calvados/bin/show_cmd "show install prepare | nomore"'

sys_show_exec[10]='show install committed'
sys_show__ksh[10]='/opt/cisco/calvados/bin/show_cmd "show install committed | nomore"'

sys_show_exec[11]='show install log'
sys_show__ksh[11]='/opt/cisco/calvados/bin/show_cmd "show install log | nomore"'

sys_show_exec[12]='show version'
sys_show__ksh[12]='/opt/cisco/calvados/bin/show_cmd "show version | nomore"' 

sys_show_exec[13]='show reboot-history card'
sys_show__ksh[13]='/opt/cisco/calvados/bin/show_cmd "show reboot-history card | nomore"' 

sys_show_exec[14]='show reboot-history admin-vm'
sys_show__ksh[14]='/opt/cisco/calvados/bin/show_cmd "show reboot-history admin-vm | nomore"' 

sys_show_exec[15]='show sdr'
sys_show__ksh[15]='/opt/cisco/calvados/bin/show_cmd "show sdr | nomore"'

sys_show_exec[16]='show context'
sys_show__ksh[16]='/opt/cisco/calvados/bin/show_cmd "show context"'

sys_show_exec[17]='show processes inst_mgr location all'
sys_show__ksh[17]='/opt/cisco/calvados/bin/show_cmd "show processes inst_mgr location all | nomore"'

sys_show_exec[18]='show processes inst_agent location all'
sys_show__ksh[18]='/opt/cisco/calvados/bin/show_cmd "show processes inst_agent location all | nomore"'

sys_show_exec[19]='show install superseded'
sys_show__ksh[19]='/opt/cisco/calvados/bin/show_cmd "show install superseded | nomore"'

sys_show__ksh[20]=''

build_info_exec[1]='run cat /etc/build-info.txt'
build_info__ksh[1]='cat /etc/build-info.txt'
build_info__ksh[2]=''


dump_logfiles_system() {
    a=(/etc/build-info.txt)
    if [ -f "$a" ]; then
        exec_commands build_info
    fi

    b=(/install_repo/gl/instdb/log/)
    if [ -d "$b" ]; then
        c=(/install_repo/gl/instdb/log/*.log)
        if [ -f "$c" ]; then
            ls -tr /install_repo/gl/instdb/log/ > tmpXX.txt
            chmod 777 tmpXX.txt
            while read line; do
                if [ -n "$line" ]; then
                    cat "/install_repo/gl/instdb/log/"$line
                fi
            done < tmpXX.txt
            rm tmpXX.txt
        fi
    fi
}

#This will backup local rpm database
dump_local_rpmdb()
{
	echo "***backing up local rpm database ***"
	rpmdb_path=`rpm --eval '%{_dbpath}'`
	dst=$__tar_file_directory_on_node"/local"
	mkdir $dst
	`cp -rf  ${rpmdb_path}/* $dst/`
}

#
# This will check if the XR VM is offline and 
# recover log files generated by rpm command if so.
#
dump_rpmdb_system()
{
	ssh_options="-o UserKnownHostsFile=/dev/null -o LogLevel=quiet "

	get_host_ip host_ip
	get_vrf_command ctrl_vrf_cmd CTRL_VRF
	ssh_prefix=$ctrl_vrf_cmd" ssh "$ssh_options$host_ip
	scp_prefix=$ctrl_vrf_cmd" scp "$host_ip
	SDRs=`${ssh_prefix} "source /etc/profile; virsh list --all |grep sdr " | awk '{print $2}'`
	SDRlist=($SDRs)
	for sdr in "${SDRlist[@]}"
	do
		# for each sdr do the following
		dst=$__tar_file_directory_on_node"/"$sdr
		mkdir -p $dst/rpmdb
		mkdir -p $dst/logs
		lvname=`${ssh_prefix} "source /etc/profile; virsh dumpxml ${sdr} | grep xr_lv"|awk -F"'"  '{print $2}' |grep dev`
		dlvname=`${ssh_prefix} "source /etc/profile; virsh dumpxml ${sdr} | grep xr_data_lv"|awk -F"'"  '{print $2}' |grep dev`
       		#if lvname is NULL, then there is no XR so skip
       		if [ -z "$lvname" ]; then
       		         echo "LV  missing from $sdr. Skipping backup of rpmdb";
       		         continue;
       		fi
       		if [ -z "$dlvname" ]; then
       		         echo "data LV  missing from $sdr.Skipping backup";
       		         continue;
       		fi
		unitcmd="${ssh_prefix} fdisk -u -l ${lvname} 2>/dev/null |grep Units"
		stcmd="${ssh_prefix} fdisk -u -l ${lvname} 2>/dev/null |grep ${lvname}p1"
		units=`$unitcmd | awk '{print $(NF -1) }'`
                check_op=`$stcmd`
                if [ -z "$check_op" ]; then
                    #For multi-SDR, the disk will be missing in some cases
                    continue
                fi

		start=`$stcmd | awk '{print $2}'`
		if [ "$start" == "*" ]; then
			# handle boot partitions
			start=`$stcmd | awk '{print $3}'`
		fi
		u1=$((units))
		u2=$((start))
		offset=$((u1*u2))
		unitcmd="${ssh_prefix} fdisk -u -l ${dlvname} |grep Units"
		stcmd="${ssh_prefix} fdisk -u -l ${dlvname} |grep ${dlvname}p1"
		units=`$unitcmd | awk '{print $(NF -1) }'`
		start=`$stcmd | awk '{print $2}'`
		if [ "$start" == "*" ]; then
			# handle boot partitions
			start=`$stcmd | awk '{print $3}'`
		fi
		u1=$((units))
		u2=$((start))
		doffset=$((u1*u2))
		dbpathcmd="${ssh_prefix} rpm --eval '%{_dbpath}'"
		rpmdb_path=`$dbpathcmd`
       		rpmdb_cmds[1]="${ssh_prefix} umount /tmp/dbpath 2>/dev/null"
       		rpmdb_cmds[2]="${ssh_prefix} rm -rf /tmp/dbpath"
       		rpmdb_cmds[3]="${ssh_prefix} mkdir /tmp/dbpath"
       		rpmdb_cmds[4]="${ssh_prefix} mount -o offset=${offset} ${lvname} /tmp/dbpath"
       		rpmdb_cmds[5]="${ssh_prefix} ls -tr /tmp/dbpath${rpmdb_path} > tmpXX.txt"
		rpmdb_cmds[6]="${scp_prefix}:tmpXX.txt ."
       		rpmdb_cmds[7]="${ssh_prefix} umount /tmp/logs 2>/dev/null"
       		rpmdb_cmds[8]="${ssh_prefix} rm -rf /tmp/logs"
       		rpmdb_cmds[9]="${ssh_prefix} mkdir /tmp/logs"
       		rpmdb_cmds[10]="${ssh_prefix} mount -o offset=${doffset} ${dlvname} /tmp/logs"
       		rpmdb_cmds[11]="${ssh_prefix} ls -tr /tmp/logs/install/ > tmpYY.txt"
		rpmdb_cmds[12]="${scp_prefix}:tmpYY.txt ."
        	xr_shut=`${ssh_prefix} "source /etc/profile; virsh list --all |grep $sdr |grep shut"`
       		if [ -n "$xr_shut" ]; then
	                for i in "${rpmdb_cmds[@]}"
       		        do
       		                 `$i`
       		        done
			while read line; do
       		        	`${scp_prefix}:${rpmdb_path}/${line} ${dst}/rpmdb`
               		done < tmpXX.txt
	                `${ssh_prefix} umount /tmp/dbpath`
			while read line; do
                       	 `${scp_prefix}:/tmp/logs/install/${line} ${dst}/logs 2>/dev/null`
             		done < tmpYY.txt
           		`${ssh_prefix} umount /tmp/logs`
		else
			echo "skipping backup of $sdr as it is not in shut state"
	        fi
	done
}

# -------------------------------------------------------------------------------
# Commands to be executed for all nodes
# -------------------------------------------------------------------------------

all_show_exec[1]='run ps -ef'
all_show__ksh[1]='ps -ef'
all_show__ksh[2]=''

core_command_exec[1]='run ls -l /misc/scratch/core/*core*'    
core_command__ksh[1]='ls -l /misc/scratch/core/*core*'
core_command__ksh[2]=''

var_log_command_exec[1]='run ls -l /var/log/*log*'
var_log_command__ksh[1]='ls -l /var/log/*log*'
var_log_command__ksh[2]=''

install_log_command_exec[1]='run ls -l /install/*log*'
install_log_command__ksh[1]='ls -l /install/*log*'
install_log_command__ksh[2]=''

insttmp_log_command_exec[1]='run ls -l /install/tmp/*log*'
insttmp_log_command__ksh[1]='ls -l /install/tmp/*log*'
insttmp_log_command__ksh[2]=''

smu_acti_log_command_exec[1]='run cat /var/log/smu_acti_cmd_log'
smu_acti_log_command__ksh[1]='cat /var/log/smu_acti_cmd_log'
smu_acti_log_command__ksh[2]=''

smu_deacti_log_command_exec[1]='run cat /var/log/smu_deacti_cmd_log'
smu_deacti_log_command__ksh[1]='cat /var/log/smu_deacti_cmd_log'
smu_deacti_log_command__ksh[2]=''

calvados_info_command_exec[1]='run rpm -qa'
calvados_info_command__ksh[1]='rpm -qa'

calvados_info_command_exec[2]='run df -h --block-size=M'
calvados_info_command__ksh[2]='df -h --block-size=M'

if [ -d /misc/disk1 ]; then
    calvados_info_command_exec[3]='run df -kh /misc/disk1'
    calvados_info_command__ksh[3]='df -kh /misc/disk1'
fi

calvados_info_command_exec[4]='run du -chx --max-depth=1 /'
calvados_info_command__ksh[4]='du -chx --max-depth=1 /'

calvados_info_command_exec[5]='run du -chx --max-depth=1 /opt/cisco'
calvados_info_command__ksh[5]='du -chx --max-depth=1 /opt/cisco'

calvados_info_command_exec[6]='run mount'
calvados_info_command__ksh[6]='mount'

calvados_info_command_exec[7]='run free -m'
calvados_info_command__ksh[7]='free -m'

calvados_info_command_exec[8]='run smap /proc/*'
calvados_info_command__ksh[8]='smap /proc/*'

calvados_info_command_exec[9]='run eval ls -l /opt/cisco/calvados/bin/inst_mgr'
calvados_info_command__ksh[9]='eval ls -l /opt/cisco/calvados/bin/inst_mgr $redir_stderr'

calvados_info_command_exec[10]='run eval ls -l /opt/cisco/calvados/bin/inst_agent'
calvados_info_command__ksh[10]='eval ls -l /opt/cisco/calvados/bin/inst_agent $redir_stderr'

calvados_info_command_exec[11]='run eval losetup -a'
calvados_info_command__ksh[11]='eval losetup -a $redir_stderr'

calvados_info_command_exec[12]='run ifconfig'
calvados_info_command__ksh[12]='eval ifconfig $redir_stderr'

calvados_info_command_exec[13]='run $ctrl_vrf_cmd ifconfig'
calvados_info_command__ksh[13]='eval $ctrl_vrf_cmd ifconfig $redir_stderr'

calvados_info_command__ksh[14]=''

if [ "$__node_type" == "XC" -o "$__node_type" == "FC" ]; then
host_info_command_exec[1]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip grep -e logger -e hushd /var/log/messages'
host_info_command__ksh[1]='$ctrl_vrf_cmd ssh $ssh_options $host_ip grep -e logger -e hushd /var/log/messages'

host_info_command_exec[2]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip rpm -qa'
host_info_command__ksh[2]='$ctrl_vrf_cmd ssh $ssh_options $host_ip rpm -qa'
host_info_command__ksh[3]=''

else

host_info_command_exec[1]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip ls -l /dev/panini_vol_grp 2>/dev/null'
host_info_command__ksh[1]='$ctrl_vrf_cmd ssh $ssh_options $host_ip ls -l /dev/panini_vol_grp 2>/dev/null'

host_info_command_exec[2]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip grep -e logger -e hushd /var/log/messages'
host_info_command__ksh[2]='$ctrl_vrf_cmd ssh $ssh_options $host_ip grep -e logger -e hushd /var/log/messages'

host_info_command_exec[3]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip rpm -qa'
host_info_command__ksh[3]='$ctrl_vrf_cmd ssh $ssh_options $host_ip rpm -qa'

host_info_command_exec[4]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip lvscan'
host_info_command__ksh[4]='$ctrl_vrf_cmd ssh $ssh_options $host_ip lvscan $redir_stderr'

host_info_command_exec[5]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip vgdisplay'
host_info_command__ksh[5]='$ctrl_vrf_cmd ssh $ssh_options $host_ip vgdisplay $redir_stderr'

host_info_command_exec[6]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip pvscan'
host_info_command__ksh[6]='$ctrl_vrf_cmd ssh $ssh_options $host_ip pvscan $redir_stderr'

host_info_command_exec[7]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip ls -l /dev/pci_disk1'
host_info_command__ksh[7]='$ctrl_vrf_cmd ssh $ssh_options $host_ip ls -l /dev/pci_disk1 $redir_stderr'

host_info_command_exec[8]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip /usr/bin/xr_sysctl -n kernel.boot.cmdline'
host_info_command__ksh[8]='$ctrl_vrf_cmd ssh $ssh_options $host_ip /usr/bin/xr_sysctl -n kernel.boot.cmdline'

host_info_command_exec[9]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip lvdisplay'
host_info_command__ksh[9]='$ctrl_vrf_cmd ssh $ssh_options $host_ip lvdisplay $redir_stderr'

host_info_command_exec[10]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip ls /misc/config/'
host_info_command__ksh[10]='$ctrl_vrf_cmd ssh $ssh_options $host_ip ls /misc/config/ $redir_stderr'

host_info_command_exec[11]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip hdparm -tT $dev_name'
host_info_command__ksh[11]='$ctrl_vrf_cmd ssh $ssh_options $host_ip hdparm -tT $dev_name'

host_info_command_exec[12]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip "ps aux | grep qemu"'
host_info_command__ksh[12]='$ctrl_vrf_cmd ssh $ssh_options $host_ip "ps aux | grep qemu"'

host_info_command_exec[13]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip virsh list --all'
host_info_command__ksh[13]='$ctrl_vrf_cmd ssh $ssh_options $host_ip virsh list --all $redir_stderr'

host_info_command_exec[14]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip $virsh_dumpxml'
host_info_command__ksh[14]='$ctrl_vrf_cmd ssh $ssh_options $host_ip $virsh_dumpxml $redir_stderr'

host_info_command_exec[15]='run $ctrl_vrf_cmd ssh $ssh_options $host_ip $pvscan_err'
host_info_command__ksh[15]='$ctrl_vrf_cmd ssh $ssh_options $host_ip $pvscan_err'
fi

if ssh $ssh_options $host_ip "[ -f $host_user_log ] "; then
    host_info_command_exec[16]='run $ctrl_vrf_cmd scp $ssh_options $host_ip:$host_user_log $dest_host_log_path'
    host_info_command__ksh[16]='$ctrl_vrf_cmd scp $ssh_options $host_ip:$host_user_log $dest_host_log_path'
    if ssh $ssh_options $host_ip "[ -f $host_pxe_log ] "; then
        host_info_command_exec[17]='run $ctrl_vrf_cmd scp $ssh_options $host_ip:$host_pxe_log $dest_pxe_log_path'
        host_info_command__ksh[17]='$ctrl_vrf_cmd scp $ssh_options $host_ip:$host_pxe_log $dest_pxe_log_path'
        host_info_command__ksh[18]=''
    else
        host_info_command__ksh[17]=''
    fi
else
    if ssh $ssh_options $host_ip "[ -f $host_pxe_log ] "; then
        host_info_command_exec[16]='run $ctrl_vrf_cmd scp $ssh_options $host_ip:$host_pxe_log $dest_pxe_log_path'
        host_info_command__ksh[16]='$ctrl_vrf_cmd scp $ssh_options $host_ip:$host_pxe_log $dest_pxe_log_path'
        host_info_command__ksh[17]=''
    else
        host_info_command__ksh[16]=''
    fi
fi

dump_logfiles_allnodes() {
    a=(/misc/scratch/core/*core*)
    if [ -n "$a" ]; then
        exec_commands core_command
    fi

    b=(/var/log/*log*)
    if [ -n "$b" ]; then
        exec_commands var_log_command
    fi

    c=(/install/*log*)
    if [ -n "$c" ]; then
        exec_commands install_log_command
    fi

    d=(/install/tmp/*log*)
    if [ -n "$d" ]; then
        exec_commands insttmp_log_command 
    fi

    e="/var/log/smu_acti_cmd_log"
    if [ -f $e ]; then
        exec_commands smu_acti_log_command
    fi

    f="/var/log/smu_deacti_cmd_log"
    if [ -f $f ]; then
        exec_commands smu_deacti_log_command
    fi

    exec_commands calvados_info_command
    exec_commands host_info_command
}

# ................................................................................
# Install show commands
# ................................................................................

rp_pstack_instmgr_exec[1]='run pstack `pgrep inst_mgr`'
rp_pstack_instmgr__ksh[1]='pstack `pgrep inst_mgr`'
rp_pstack_instmgr__ksh[2]=''

all_pstack_instagt_exec[1]='run pstack `pgrep inst_agent`'
all_pstack_instagt__ksh[1]='pstack `pgrep inst_agent`'
all_pstack_instagt__ksh[2]=''

dump_instmgr_pstack() {
    pid=`pgrep inst_mgr`
    if [ -n "$pid" ]; then
        exec_commands rp_pstack_instmgr
	else
		echo "pid of inst_mgr is NULL" >> $error_file
    fi
       
    a=(/var/log/ctrace/instmgr/)
    if [ -d "$a" ]; then
        exec_commands rp_instmgr_ctrace
	else
		echo "pid of inst_agnt is NULL" >> $error_file
    fi
}

dump_instagt_pstack() {
    pid=`pgrep inst_agent`
    if [ -n "$pid" ]; then
        exec_commands all_pstack_instagt
    fi       
}

#
# The display() function is the one that does all the work - called by us as 
# this is a worker script.
#
display() {
    if [ "$__cardtype" = "SYS" ]; then
        if [ "$trace_only" = "0" ]; then
            exec_commands sys_show
        fi
        dump_logfiles_system
	dump_local_rpmdb
	dump_rpmdb_system
	echo "copied files to "$__tar_file_directory_on_node
    else
        case "$__cardtype" in
        "DRP"|"RP"|"CC")
                if [ "$trace_only" = "0" ]; then
                    dump_instmgr_pstack
                fi
                ;;

              "LC")
                ;;
        esac
        if [ "$show_only" = "0" ]; then
            dump_logfiles_allnodes
        fi

        if [ "$trace_only" = "0" ]; then
            exec_commands all_show
            dump_instagt_pstack
        fi
    fi
}

display
