#!/bin/bash
# INTEL CONFIDENTIAL
# Copyright (c) 2008-2012  Intel Corporation All Rights Reserved.
# The source code contained or described herein and all documents related
# to the source code ("Material") are owned by Intel Corporation or its
# suppliers or licensors. Title to the Material remains with Intel Corporation
# or its suppliers and licensors. The Material contains trade secrets and
# proprietary and confidential information of Intel or its suppliers and
# licensors. The Material is protected by worldwide copyright and trade
# secret laws and treaty provisions. No part of the Material may be used,
# copied, reproduced, modified, published, uploaded, posted, transmitted,
# distributed, or disclosed in any way without Intel's prior express
# written permission.
#
# No license under any patent, copyright, trade secret or other intellectual
# property right is granted to or conferred upon you by disclosure or delivery
# of the Materials, either expressly, by implication, inducement, estoppel or
# otherwise. Any license under such intellectual property rights must be
# express and approved by Intel in writing.

# [ICS VERSION STRING: @(#) ./bin/iba_capture 7_4_3_0_2 [01/26/18 09:02]

# Usage: iba_capture output_file_name
# captures system information for IntelIB problem reporting

Usage_full()
{
	echo "Usage: iba_capture [-d detail] output_tar_file" >&2
	echo "              or" >&2
	echo "       iba_capture --help" >&2
	echo "   --help - produce full help text" >&2
	echo "   -d detail - level of detail of capture" >&2
	echo "               1-Local 2-Fabric 3-Fabric+FDB 4-Analysis" >&2
	echo "This will capture critical system information into a zipped tar file" >&2
	echo "The resulting tar file should be sent to Customer Support along with any" 2>&1
	echo "IntelIB problem report regarding this system" >&2
}

Usage()
{
	Usage_full
	exit 2
}

if [ `basename $0` = ics_capture ]
then
	echo "warning: ics_capture is depricated, use iba_capture" >&2
fi

if [ x"$1" = "x--help" ]
then
	Usage_full
	exit 0
fi

if [ -f /opt/iba/ib_tools/defaults ]
then
	. /opt/iba/ib_tools/defaults
fi

if [ -f /opt/iba/ib_tools/ff_funcs ]
then
	. /opt/iba/ib_tools/ff_funcs
	ff_available=y
else
	ff_available=n
fi

detail=1

while getopts d: param
do
	case $param in
        d)
                detail="$OPTARG";;
	?)
		Usage;;
	esac
done
shift $((OPTIND -1))

if [ $# != 1 ]
then
	Usage
fi

if [ `id -u` -ne 0 ]
then
	echo "This must be run as user root" >&2
	Usage
fi

if [ x`expr "$1" : '\(/\).*'` != x'/' ]
then
	# relative path
	tar_file=`pwd`/$1
else
	# absolute path
	tar_file=$1
fi

# append .tgz suffix if not already present
if [ x$(expr "$tar_file" : '.*\(\.tgz\)') != x'.tgz' ]
then
	tar_file="$tar_file.tgz"
fi

dir=tmp/capture$$
rm -rf /$dir
mkdir /$dir

echo "Capture Info: Detail: $detail; Date: $(date)" >> /$dir/capture_info

echo "Getting software and firmware version information ..."
echo "[ICS VERSION STRING: @(#) ./bin/iba_capture 7_4_3_0_2 [01/26/18 09:02]" > /$dir/sw_version
uname -a > /$dir/os_version
# we use query format so we can get ARCH information
rpm --queryformat '[%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n]' -qa > /$dir/rpms.detailed
# get simple version just to be safe
rpm -qa > /$dir/rpms

# Finding the PCI devices
for fw in `/sbin/lspci | grep InfiniBand | grep Mellanox | cut -f 1 -d ' '`
do
	echo "$fw" >> /$dir/fw_info 
	mstflint -d $fw -y q >> /$dir/fw_info
	mstflint -d $fw -y v >> /$dir/fw_info
	mstflint -d $fw -y dc >> /$dir/fw_info
done

# PR 111337 - MHEINZ - 2.5/2.6 Mellanox HCAs can crash 
# when this is called.
#iba_hca_rev -v > /$dir/iba_hca_rev 2>&1

echo "Obtaining OS configuration ..."
# get library config
ldconfig -p > /$dir/ldconfig
# get current runlevel
who -r > /$dir/who_r 2>/dev/null	# not available on all OSs
runlevel > /$dir/runlevel	# not available on all OSs
# get service startup configuration
chkconfig --list > /$dir/chkconfig
ulimit -a > /$dir/ulimit
uptime > /$dir/uptime

echo "Obtaining dmesg logs ..."
dmesg > /$dir/dmesg

echo "Obtaining any Oops information from logs ..."
for f in /var/log/messages /var/log/messages.*
do
	if [ -e "$f" ]
	then
		ksymoops $f >> /$dir/oops 2>&1
	fi
done

echo "Obtaining present process and module list ..."
lsmod > /$dir/lsmod 2>&1
depmod -ae > /$dir/depmod 2>&1
ps -welf > /$dir/ps 2>&1

echo "Obtaining PCI device list ..."
lspci -vv > /$dir/lspci 2>&1
ls -l /dev/ipath* /dev/infiniband > /$dir/lsdev

echo "Obtaining environment variables ..."
env > /$dir/env 2>&1

echo "Obtaining network interfaces ..."
ifconfig -a > /$dir/ifconfig 2>&1

echo "Obtaining DMI information ..."
dmidecode > /$dir/dmidecode 2>&1

echo "Obtaining Shared Memory information ..."
sysctl -a 2>/dev/null | grep kernel.shm > /$dir/shm 2>&1
ls -d /dev/shm >> /$dir/shm 2>&1
ls -lR /dev/shm >> /$dir/shm 2>&1

echo "Obtaining InfiniBand information ..."
# Info on PathScale HCAs
# this script now covers everything which ipathbug-helper does
#if type ipathbug-helper >/dev/null 2>/dev/null
#then
#	echo "Obtaining ipath information ..."
#	ipathbug-helper > /$dir/ipath 2>&1
#fi
if type ipath_control >/dev/null 2>/dev/null
then
	ipath_control -iv > /$dir/ipath_control 2>&1
fi
if type ipath_trace >/dev/null 2>/dev/null
then
	ipath_trace -d > /$dir/ipath_trace 2>&1
fi
if type ipathstats >/dev/null 2>/dev/null
then
	ipathstats > /$dir/ipathstats 2>&1
	ipathstats -e >> /$dir/ipathstats 2>&1
fi

# concise port statistics
p1info -q > /$dir/p1info 2>&1
p1stats > /$dir/p1stats 2>&1
p2info -q > /$dir/p2info 2>&1
p2stats > /$dir/p2stats 2>&1
ibstatus > /$dir/ibstatus 2>&1
ibv_devinfo > /$dir/ibv_devinfo 2>&1

echo "Obtaining MPI configuration ..."
if type mpi-selector >/dev/null 2>/dev/null
then
	mpi-selector --list > /$dir/mpi-selector-list
	mpi-selector --system --query > /$dir/mpi-selector-system
	mpi-selector --user --query > /$dir/mpi-selector-user
fi

mkdir /$dir/proc
echo "Copying configuration and statistics for IB drivers from /proc ..."
for proc_file in cmdline cpuinfo ksyms meminfo mtrr modules net/arp net/dev net/dev_mcast net/route net/rt_cache net/vlan pci interrupts devices filesystems iomem ioports slabinfo version uptime scsi iba driver/ics_dsc driver/ics_inic driver/ics_srp driver/ipoib driver/sdp driver/ics_offload driver/ics_sdp driver/rds irq acpi/processor
do
	if [ -e /proc/$proc_file ]
	then
		cp -r /proc/$proc_file /$dir/proc
	fi
done

mkdir -p /$dir/sys/class
if [ -e /sys/class/infiniband ]
then
	echo "Copying configuration and statistics for IB drivers from /sys ..."
	cp -r /sys/class/*infiniband* /$dir/sys/class 2>/dev/null
	mkdir -p /$dir/sys/class/infiniband
	for f in /sys/class/infiniband/*
	do
		if [ -h $f ]
		then
			rm -f /$dir/$f
			cp -r $f/ /$dir/sys/class/infiniband/ 2>/dev/null
			#if [ -h $f/device ]
			#then
			#	dev=`basename $f`
			#	rm -f /$dir/$f/device
			#	mkdir -p /$dir/sys/class/infiniband/$dev/ 2>/dev/null
			#	cp -r $f/device/ /$dir/sys/class/infiniband/$dev/ 2>/dev/null
			#fi
		fi
	done
fi

if [ -e /sys/class/qlogic_infiniband_srp ]
then
	echo "Copying configuration and statistics for Intel drivers from /sys ..."
	cp -r /sys/class/qlogic* /$dir/sys/class 2>/dev/null
fi
if [ -e /sys/class/scsi_host ]
then
	cp -r /sys/class/scsi_host /$dir/sys/class 2>/dev/null
fi
if [ -e /sys/class/scsi_device ]
then
	cp -r /sys/class/scsi_device /$dir/sys/class 2>/dev/null
fi

if [ -e /sys/module ]
then
	echo "Copying configuration and statistics for IB from /sys/module ..."
	cp -r /sys/module /$dir/sys 2>/dev/null 
fi

if [ -e /sbin/qlgc_srp_stats ]
then 
    echo "Gathering SRP statistics ..."
    /sbin/qlgc_srp_stats > /$dir/qlgc_srp_stats 2>&1
fi
if type ib_qlgc_srp_query > /dev/null 2>/dev/null || type ib_qlgc_srp_stats > /dev/null 2>/dev/null
then 
    echo "Gathering SRP Information ..."
    ib_qlgc_srp_query > /$dir/ib_qlgc_srp_query 2>&1
    ib_qlgc_srp_stats > /$dir/ib_qlgc_srp_stats 2>&1
fi
if type ib_qlgc_vnic3_info > /dev/null 2>/dev/null || type ib_qlgc_vnic3_query > /dev/null 2>/dev/null
then 
    echo "Gathering VNIC3 Information ..."
    ib_qlgc_vnic3_query > /$dir/ib_qlgc_vnic3_query 2>&1
    ib_qlgc_vnic3_info -v > /$dir/ib_qlgc_vnic3_info 2>&1
fi

if [ -f /opt/ifs_fm/bin/fm_capture ]
then
    echo "Gathering Host FM Information ..."
    (cd /$dir; /opt/ifs_fm/bin/fm_capture)
elif [ -f /opt/qlogic_fm/bin/fm_capture ]
then
    echo "Gathering Host FM Information ..."
    (cd /$dir; /opt/qlogic_fm/bin/fm_capture)
elif [ -f /usr/local/iview/util/sm_capture ]
then
    echo "Gathering Host SM Information ..."
    (cd /$dir; /usr/local/iview/util/sm_capture)
fi

if [ -f /sbin/iba_opp_dump ]
then
	echo "Gathering Distributed SA data..."
	iba_opp_dump > /$dir/iba_opp_dump 2>&1
fi

if [ $detail -ge 2 ]
then
    mkdir -p /$dir/fabric
    echo "Gathering Fabric-Level Information ..."
    cd /$dir/fabric/
    if [ "$ff_available" = y ]
    then
        check_ports_args iba_capture
    else
        PORTS='0:0'
        if [ $detail -ge 3 ]
        then
            echo "Warning: iba_capture detail=$detail but FastFabric not available"
        fi
    fi

    for hca_port in $PORTS
    do
        hca=$(expr $hca_port : '\([0-9]*\):[0-9]*')
        port=$(expr $hca_port : '[0-9]*:\([0-9]*\)')
        if [ "$hca" = "" -o "$port" = "" ]
        then
            echo "iba_capture: Error: Invalid port specification: $hca_port" >&2
            continue
	fi
		## fixup name so winzip won't complain
		hca_port_dir=${hca}_${port}

        # make hca_port directory
        mkdir $hca_port_dir

        if [ "$ff_available" = n ]
        then
            # fabric_info doesn't require FF available, but then can't have -p
            /sbin/fabric_info > $hca_port_dir/fabric_info 2>&1
        else
            /sbin/fabric_info -p $hca_port > $hca_port_dir/fabric_info 2>&1
            if [ $detail -eq 2 ]
            then
                # iba_report doesn't accept explicit port specification of 0
                if [ $port -eq 0 ]
                then
                    /sbin/iba_report -h $hca -o snapshot -s > $hca_port_dir/snapshot.xml
                else
                    /sbin/iba_report -h $hca -p $port -o snapshot -s > $hca_port_dir/snapshot.xml
                fi
            else
                if [ $port -eq 0 ]
                then
                    /sbin/iba_report -h $hca -o snapshot -s -r > $hca_port_dir/snapshot.xml
                else
                    /sbin/iba_report -h $hca -p $port -o snapshot -s -r > $hca_port_dir/snapshot.xml
                fi
                /sbin/iba_showmc -p $hca:$port > $hca_port_dir/fabric_showmc 2>&1
            fi
            /sbin/iba_report -o links -X $hca_port_dir/snapshot.xml > $hca_port_dir/fabric_links 2>&1
            /sbin/iba_report -o comps -X $hca_port_dir/snapshot.xml > $hca_port_dir/fabric_comps 2>&1
            /sbin/iba_report -o errors -X $hca_port_dir/snapshot.xml > $hca_port_dir/fabric_errors 2>&1
            /sbin/iba_report -o extlinks -X $hca_port_dir/snapshot.xml > $hca_port_dir/fabric_extlinks 2>&1
            /sbin/iba_report -o slowlinks -X $hca_port_dir/snapshot.xml > $hca_port_dir/fabric_slowlinks 2>&1
        fi
    done
fi

cd /
files="$dir"
for f in var/log/iba* var/log/ics_* var/log/messages* var/log/ksyms.* var/log/boot* etc/*release* etc/sysconfig/ics_inic.cfg* etc/sysconfig/ics_srp.cfg* etc/sysconfig/ipoib.cfg* etc/sysconfig/iba etc/modules.conf* etc/modprobe.conf* etc/sysconfig/network-scripts/ifcfg* etc/dapl/ibhosts etc/hosts etc/sysconfig/boot etc/sysconfig/firstboot etc/dat.conf etc/sysconfig/network/ifcfg* etc/infiniband etc/sysconfig/*config etc/security etc/sysconfig/qlogic_fm.xml etc/sysconfig/ifs_fm.xml etc/sysconfig/iview_fm.config var/log/fm* var/log/sm* var/log/bm* var/log/pm* var/log/fe* var/log/opensm* var/log/ipath* etc/rc.d/rc.local etc/modprobe.d boot/grub/menu.lst boot/grub/grub.conf etc/grub.conf etc/udev* etc/opensm etc/sysconfig/opensm
do
	if [ -e "$f" ]
	then
		files="$files $f"
	fi
done
if [ -e /usr/bin/xml_extract ]
then
	for f in $(xml_extract -H -e LogFile < /etc/sysconfig/ifs_fm.xml 2>/dev/null)
	do
		case "$f" in
		/var/log/fm*|/var/log/sm*|/var/log/bm*|/var/log/pm*|/var/log/fe*)
			>/dev/null;;
		*)
			if [ -e "$f" ]
			then
				files="$files $(echo $f|sed -e 's|^/||')"
			fi;;
		esac
	done
fi

for f in usr/local/src/mpi_apps/core* opt/iba/src/mpi_apps/core* opt/iba/src/shmem_apps/core*
do
	if [ -e "$f" ]
	then
		files="$files $f"
	fi
done
if [ "$ff_available" = y -a -d "$FF_MPI_APPS_DIR" ]
then
	apps_dir=$(echo $FF_MPI_APPS_DIR|sed -e 's|^/||')	# drop leading /
	for f in $apps_dir/core*
	do
		if [ -e "$f" ]
		then
			files="$files $f"
		fi
	done
fi

if [ $detail -ge 4 -a "$ff_available" = y -a -d $FF_ANALYSIS_DIR/baseline ]
then
	if [ ! -d $FF_ANALYSIS_DIR/latest ]
	then
		echo "Performing Fabric Analysis ..."
		rm -f $FF_ANALYSIS_DIR/all_analysis
		/sbin/all_analysis > $FF_ANALYSIS_DIR/all_analysis 2>&1
    fi
	files="$files $(echo $FF_ANALYSIS_DIR|sed -e 's|^/||')"
fi

echo "Creating tar file $tar_file ..."
tar --format=gnu -czf $tar_file $files
retval=$?
rm -rf /$dir

if [ $retval -eq 0 ]
then
	echo "Done."
	echo
	echo "Please include $tar_file with any problem reports to Customer Support"
else
	echo "ERROR: Unable to generate $tar_file" >&2
fi
exit $retval
