#!/bin/sh
#
# Copyright 2013-2023. Quantum Corporation. All Rights Reserved.
# StorNext is either a trademark or registered trademark of
# Quantum Corporation in the US and/or other countries.
#
prog=$0

if [ -f /usr/cvfs/bin/sn_functions ]
then
    # SN Library functions
    . /usr/cvfs/bin/sn_functions
fi

ECHO=echo

os=`uname`
host=`uname -n`

#
# On Mac OS X, make sure the user is running as the superuser (usually
# with sudo(1)).
#
if [ x${os} = xDarwin ]; then
	euid=`id -u`
	if [ "${euid}" != "0" ]; then
		echo "Insufficient administrative privileges."
		exit 1
	fi
fi

if [ -z "${TMPDIR}" ]; then
	echo TMPDIR not set, using /tmp.
	TMPDIR=/tmp
fi

if [ ! -d "${TMPDIR}" ]; then
	if [ -e "${TMPDIR}" ]; then
		echo TMPDIR \"${TMPDIR}\" is not a directory.
		echo Set TMPDIR to a place with enough space to hold dumps and log files.
		exit 1
	fi

	# Use a reasonable default.
	TMPDIR=/tmp
fi

GATHER_TEMP=/${TMPDIR}/gather_$$

rm -rf ${GATHER_TEMP}

if [ -r ${GATHER_TEMP} ]
then
    echo "Cannot clear ${GATHER_TEMP}"
    exit 1
fi

mkdir ${GATHER_TEMP}

signal_cleanup()
{
    echo "Signal caught... cleaning up temp files before error exit."
    rm -rf "${GATHER_TEMP}"
    exit 1
}

# Catch SIGHUP, SIGINT (Ctrl-C), SIGQUIT (Ctrl-D), SIGTERM (kill)
trap signal_cleanup 1 2 3 15

if [ x${os} = xDarwin ]
then
    ALTPATHALLOWED=0
    cvdirpath=/Library/Logs/Xsan
    cvbinpath=/System/Library/Filesystems/acfs.fs/Contents/bin
    configpath=/Library/Preferences/Xsan
    OPTS=":f:ho:"
else
    ALTPATHALLOWED=1
    cvdirpath=/usr/cvfs
    cvbinpath=$cvdirpath/bin
    configpath=/usr/cvfs/config
    OPTS=":f:hkK:n:o:p:TuU:xr"
fi

if [ $os = "Linux" ]    ; then
    VENDOR="Linux"
    if [ -f /var/log/messages ]; then
        SYSLOG=/var/log/messages
    else
        SYSLOG=/var/log/syslog
    fi
elif [ $os = "Darwin" ]	; then
    VENDOR="Xsan"
    SYSLOG=/var/log/system.log
else
    echo "Insufficient system information (from uname) to do a gather."
    exit 2
fi

COMPRESS_PROGRAM="gzip"
EXTENSION=tgz
if [ $os = "Linux" ]; then
    which pigz >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        COMPRESS_PROGRAM=/usr/cvfs/lib/do_compress
        export COMPRESS_BIN=pigz
        export COMPRESS_THREADS_ARGUMENT="-p 6"
    fi
fi

oshost=${VENDOR}_${host}
# maximum number of qustat days to gather
MaxQstatDays=120

oshostfsname() {

    if [ $# -ne 1 ]; then
    	echo "usage: oshostfsname fsname" 1>&2
	exit 1
    fi

    echo ${oshost}_${1}
}

#
# Usage macro
#
# pse_snapshot calls cvgather in a loop, calling it once for each active
# file system. pse_snapshot calls cvgather with the -x option to prevent
# cvgather from collecting files that are already collected by pse_snapshot.
#
usage() {

    echo "${prog} [-f fsname [...]] [-o outputfile]"

    if [ ${ALTPATHALLOWED} -ne 0 ]
    then
	echo "[-n number of FSS logs]"
	echo "[-u] [-U [user core]] [-k] [-K [kernel core]]"
	echo "[-s] : Gather symbol information without core files"
	echo "[-p path to program directory]"
	echo "[-x] : Exclude files collected by pse_snapshot"
	echo "[-T] : Do not append timestamp to output filename"
    fi
    exit 2
}

#
# Gather macro
#
gather_internal() {

    if [ x"${3}" = "x" ]; then
        targname=`basename "${2}"`
    else
        targname=${3}
    fi

    # if the source is a directory copy the entire
    # directory. Need to also handle symlink
    if [ -d "${2}/." ]
    then
	${ECHO} "  ${prog}: directory ${2}"
	cp -r "${2}/." "${GATHER_TEMP}/${1}.${targname}"
	return
    fi

    #
    # If the source exists, copy it into gather temp dir.
    #
    if [ -f "${2}" ]
    then
	${ECHO} "  ${prog}: file ${2}"
	cp "${2}" "${GATHER_TEMP}/${1}.${targname}"
        if [ x${os} = xDarwin ]; then
		mtime=`stat -f "%Sm" -t "%G%m%d%H%M.%S" "${2}"`
		touch -m -t ${mtime} \
		    "${GATHER_TEMP}/${1}.${targname}"
	fi
	return
    fi
}

gather()
{

    if [ $# -lt 1 -o $# -gt 2 ]
    then
        echo "usage: gather source_file [dest_file]" 1>&2
        exit 2
    fi

    gather_internal ${oshost} "${1}" "${2}"
}

gather_fs()
{

    if [ $# -lt 2 -o $# -gt 3 ]
    then
        echo "usage: gather_fs fsname source_file [dest_file]" 1>&2
        exit 2
    fi

    gather_internal `oshostfsname ${1}` "${2}" "${3}"
}

rolldown() {

    if [ $# -ne 1 ]
    then
	echo "usage: rolldown pathname" 1>&2
	exit 1
    fi

    if [ -f "$1-" ]
    then
	rolldown "$1-"

	if [ $? -ne 0 ]
	then
	    exit 1
	fi
    fi

    mv "$1" "$1-"

    if [ $? -ne 0 ]
    then
	echo "rolldown: Error moving $1 to $1- (Exiting)."
	exit 1
    fi
}

gather_unit()
{
    unit_file=$1

    gather $unit_file
    bn=`basename $i`

    if [ -d /etc/systemd/system/$bn.d ]; then
        # get all the drop-in files here
        gather /etc/systemd/system/$bn.d
    fi
}

gather_systemctl()
{
    if [ ! -d "$LIBSYSTEMD" ]; then
        # LIBSYSTEMD is not set or empty, bail out
        echo LIBSYSTEMD is not set correctly, not collecting systemd units
        return
    fi

    # collect some systemctl files
    for i in $LIBSYSTEMD/cvfs*
    do
        gather_unit $i
    done
    for i in $LIBSYSTEMD/stornext*
    do
        gather_unit $i
    done
}

echo ""

getkcore=0
getucore=0
usercore="$cvdirpath/debug/core"
symbols=0
exclude_snapshot_files=0
dns_resolution_off=0
exclude_timestamp=0

MYCD=`pwd`

SNUPDATE_DIR="/opt/quantum/snupdate"
SNUPDATE="$SNUPDATE_DIR/bin/snupdate"

#
# Command line processing
#
while getopts "${OPTS}" opt; do
    case $opt in
        f ) fsnames="$fsnames $OPTARG"
	    ;;
	K ) kernelcore=$OPTARG
	    getkcore="2"
	    ;;
	k ) if [ "getkcore = 0" ] ; then
		getkcore="1"
	    fi 
	    ;;
	n ) cvlogs=$OPTARG
	    ;;
	o ) case $OPTARG in
	        /*) outputfile=$OPTARG ;;
		*)  outputfile=${MYCD}/$OPTARG ;;
	    esac
	    ;;
	p ) cvdirpath=$OPTARG
	    cvbinpath=$OPTARG/bin
	    configpath=$OPTARG/config
	    ;;
        s ) symbols="1"
	    ;;
	T ) exclude_timestamp="1"
	    ;;
	U ) usercore=$OPTARG
	    getucore="2"
	    ;;
	u ) if [ "getucore = 0" ] ; then
	    getucore="1"
	    fi 
	    ;;
	x ) exclude_snapshot_files="1"
	    ;;
	r ) dns_resolution_off="1"
        export DNS_OFF=1
	    ;;
	* ) usage ;;
    esac
done

#
# Re-initialize usercore in case cvdirpath changed
#
if [ ${os} = "Linux" ] ; then
    if  [ $getucore -eq 0 ] || [ $getucore -eq 1 ] ; then
        grep -q "systemd-coredump" /proc/sys/kernel/core_pattern
        if [ $? -eq 0 ] ; then
            usercore="/var/lib/systemd/coredump/core"
            systemd_coredump=1
        else
            usercore="$cvdirpath/debug/core"
        fi
    fi
fi

#
# Some default behavior handling
#
if [ -z "$fsnames" ] ; then
    # Gather every file system if none were specified.
    for file in `find $configpath -name "*.cfg" -o -name "*.cfgx" -o -name "*.cfgp"` ; do
	if  [ -f $file ]; then 
	    fsname=`basename $file | cut -f 1 -d .`
	    fsnames="$fsnames $fsname"
	fi
    done

    if [ -z "$outputfile" ] ; then
        outputfile=${MYCD}/${oshost}_ALL_FILESYSTEMS
    fi
else
    for fsname in ${fsnames}; do
        check1=`ls $configpath | grep "$fsname"`;
        check2=`$cvbinpath/cvadmin -e "select" | grep "$fsname"`;
        check3=`cat /proc/mounts | grep "$fsname"`;
        check4=`cat /etc/fstab | grep "$fsname"`;
        if [ -n "$check1" -o -n "$check2" -o -n "$check3" -o -n "$check4" ]
        then
            continue
        else
            echo "Specified file system name '$fsname' does not exist."
            exit 1
        fi
    done
    if [ -z "$outputfile" ] ; then
    	outputfile=${MYCD}/${oshost}
    	for fsname in ${fsnames}; do
	    outputfile=${outputfile}_${fsname}
	done
    fi
fi

# append timestamp unless explicitly excluded by caller
if [ $exclude_timestamp -eq 0 ] ; then 
    outputfile=${outputfile}_`/bin/date +%Y%m%d%H%M%S%z`
fi

if [ -r ${outputfile}.${EXTENSION} ]
then
    rolldown ${outputfile}.${EXTENSION}
fi

if [ -r ${outputfile}.${EXTENSION} ]
then
    echo "Cannot move ${outputfile}.${EXTNSION}"
    exit 1
fi

if [ -z "$cvlogs" ] ; then
    cvlogs="4"
fi

#
# <fsname outputfile cvdirpath cvlogs kernelcore>
#
# Get the system type
#
${ECHO} "  ${prog}: uname -a"
uname -a > ${GATHER_TEMP}/${oshost}.uname

if [ ${os} = "Linux" ] 
then
   if [ -f /etc/redhat-release ] ; then
	cat /etc/redhat-release >> ${GATHER_TEMP}/${oshost}.uname
   fi
   if [ -f /etc/SuSE-release ] ; then
	cat /etc/SuSE-release >> ${GATHER_TEMP}/${oshost}.uname
   fi
fi

if [ x${os} = xDarwin ]
then
	${ECHO} "  ${prog}: ps auxwww"
	ps auxwww > ${GATHER_TEMP}/${oshost}.ps-auxwww

	${ECHO} "  ${prog}: system_profiler"
	system_profiler -xml > ${GATHER_TEMP}/${oshost}.system_profile.spx 2>/dev/null

	${ECHO} "  ${prog}: ioreg"
	ioreg -l -w 0 > ${GATHER_TEMP}/${oshost}.ioreg-dump.txt

	if [ -f /Library/Preferences/Xsan/.auth_secret ]; then
	    shasum -a 512 /Library/Preferences/Xsan/.auth_secret > ${GATHER_TEMP}/${oshost}.auth_secret.shasum_512
	fi

	# Gather Crash Reporter logs before running any Xsan utilities,
	# in case they crash again.
	for file in /Library/Logs/DiagnosticReports/cv*{,/*} \
	    /Library/Logs/DiagnosticReports/fsm*{,/*} \
	    /Library/Logs/DiagnosticReports/servermgr*{,/*} \
	    /Library/Logs/DiagnosticReports/Kernel_*{,/*} \
	    /Library/Logs/DiagnosticReports/com.apple.xsan*{,/*} \
	    /Library/Logs/DiagnosticReports/xsan*{,/*}; do
		gather "${file}"
	done

	gather /Library/Preferences/Xsan/uuid
	gather /Library/Preferences/com.apple.xsan.plist

	# OD Server logs
	gather /var/log/slapd.log
	gather /Library/Logs/slapconfig.log
	if [ -d /Library/Logs/PasswordService ]; then
		tar czf "${GATHER_TEMP}/${oshost}.PasswordServiceLogs.tgz" -C /Library/Logs/ PasswordService
	fi
	for file in /Library/Logs/DiagnosticReports/opendirectoryd*{,/*} \
	    /Library/Logs/DiagnosticReports/slapd*{,/*} \
	    /Library/Logs/DiagnosticReports/slapconfig*{,/*} \
	    /Library/Logs/DiagnosticReports/PasswordService*{,/*}; do
		gather "${file}"
	done

	# Server.app log bundle.
	if [ -x /Applications/Server.app/Contents/ServerRoot/usr/sbin/serverdiagnose ]; then
		${ECHO} "  ${prog}: serverdiagnose"
		/Applications/Server.app/Contents/ServerRoot/usr/sbin/serverdiagnose -nA ${GATHER_TEMP}/serverdiagnose.tgz
	fi

	# Gather up OD binding info
	tar czf "${GATHER_TEMP}/${oshost}.OpenDirectoryConfig.tgz" -C /Library/Preferences/ OpenDirectory

	# Gather up OD current state
	odutil show all > "${GATHER_TEMP}/${oshost}.ODUtil_show_all.txt"
	killall -INFO opendirectoryd
	for file in /var/log/opendirectoryd*; do
		gather "${file}"
	done

	if [ -r /var/run/ldapi ]; then
	    ${ECHO} "  ${prog}: xsanLdapConfig.plist"
	    summary_file=${GATHER_TEMP}/${oshost}.xsanLdapConfig.plist
	    xsanctl dumpLdapConfig > ${summary_file}
	fi

	# Gather up system/networking config info
	for file in /Library/Preferences/SystemConfiguration/*; do
		gather "${file}" "SystemConfiguration-`basename ${file}`" 2>/dev/null
	done
fi

#
# create the summary file
#
if [ $exclude_snapshot_files -eq 0 -a -f $cvbinpath/cvgather_sum ]; then
    ${ECHO} "  ${prog}: $cvbinpath/cvgather_sum"
    summary_file=${GATHER_TEMP}/${oshost}.cvsummary
    $cvbinpath/cvgather_sum > ${summary_file}
fi

${ECHO} "  ${prog}: $cvbinpath/cvversions"
$cvbinpath/cvversions > ${GATHER_TEMP}/${oshost}.cvversions

${ECHO} "  ${prog}: $cvbinpath/cvlabel -c"
$cvbinpath/cvlabel -c > ${GATHER_TEMP}/${oshost}.cvlabel-c

${ECHO} "  ${prog}: $cvbinpath/cvlabel -l"
$cvbinpath/cvlabel -l > ${GATHER_TEMP}/${oshost}.cvlabel-l

# cvfsid in cvgather_sum
#${ECHO} "  ${prog}: $cvbinpath/cvfsid"
#$cvbinpath/cvfsid > ${GATHER_TEMP}/${oshost}.cvfsid

if [ $os = "Darwin" ]; then
	for file in ${SYSLOG}*; do
		gather "${file}"
	done
        # sysdiagnose complains when no tty is present for stdout but skips
        # that check when user interaction is skipped using the -u flag
        ${ECHO} "Gathering sysdiagnose"
        /usr/bin/sysdiagnose -f ${GATHER_TEMP} -A ${oshost}.sysdiagnose -b -u
else
	for file in ${SYSLOG}*; do
		gather "${file}"
	done
fi

if [ $os = "Darwin" ]; then
	XsanAdminDir="Library/Application Support/Xsan Admin"
	UserCrashLogs="Library/Logs/CrashReporter"
	UserDiagnosticLogs="Library/Logs/DiagnosticReports"
	DSCL="dscl /Search"

	if [ x"${SUDO_USER}" != "x" ]; then
		adminUser=${SUDO_USER}
	else
		adminUser=${USER}
	fi

	Users="root ${adminUser}"

	for user in ${Users}; do
		output=`${DSCL} -read /Users/${user} NFSHomeDirectory 2>&1`
		dsKey=`echo ${output} | cut -d ' ' -f 1`

		if [ "${dsKey}" != "NFSHomeDirectory:" ]; then
			continue;
		fi

		homeDir=`echo ${output} | cut -d ' ' -f 2`

		lookdir="${homeDir}/${UserDiagnosticLogs}"

		if [ -d "${lookdir}" ]; then
		    for i in cvupdatefs cvfsck snfsdefrag; do
		        for file in "${lookdir}/${i}"*{,/*}; do
			    base=`basename "${file}"`
			    gather "${file}" "${user}-${base}"
			done
		    done
		fi
	done

	gather "$configpath/automount.plist"

	gather "$configpath/config.plist"

	${ECHO} "  ${prog}: ${oshost}.cvadmin_showrevisions"
	$cvbinpath/cvadmin -e showrevisions > ${GATHER_TEMP}/${oshost}.cvadmin_showrevisions
fi

if [ -f $cvbinpath/cvadmin ]
then
    ${ECHO} "  ${prog}: ${oshost}.cvadmin_select"
    $cvbinpath/cvadmin -e select > ${GATHER_TEMP}/${oshost}.cvadmin_select
fi

gather "$configpath/fsnameservers"

gather "$configpath/fsforeignservers"

if [ -f "$configpath/fsmlist" ]
then
    gather "$configpath/fsmlist"
fi

if [ -d "${cvdirpath}/qustats" ]; then
    echo "Packaging up to ${MaxQstatDays} days of qustats in: ${oshost}.qustats.${EXTENSION}"
    curdir=$PWD
    cd ${cvdirpath}
    find qustats -type f -mtime -${MaxQstatDays} -print | \
	tar --use-compress-program "${COMPRESS_PROGRAM}" \
          -cf ${GATHER_TEMP}/${oshost}.qustats.${EXTENSION} -T - > /dev/null 2>&1 \;
    cd $curdir
fi

# Gather the /usr/cvfs/config directory.
gather "$configpath" cvfs_config

gather "$cvdirpath/debug/nssdbg.out"
x=0
while [ $x -le $cvlogs ] ; do 

    gather "$cvdirpath/debug/nssdbg.out_$x"

    x=`expr $x + 1`

done

gather "$cvdirpath/debug/fsmpm.out"
x=0
while [ $x -le $cvlogs ] ; do

    gather "$cvdirpath/debug/fsmpm.out_$x"

    x=`expr $x + 1`

done

# Gather sntier logs and database
gather "$cvdirpath/debug/sntierd.out"
sntierlogs=16  # default
x=0
while [ $x -le $sntierlogs ] ; do

    gather "$cvdirpath/debug/sntierd.out_$x"

    x=`expr $x + 1`

done
for i in /usr/cvfs/data/tier_db.dat*
do
    gather $i
done

gather "$cvdirpath/debug/cvfsd.errs"

gather "$cvdirpath/debug/cvfsd.out"

gather "$cvdirpath/debug/quantum_disk_license_report.xml"

gather "$cvdirpath/debug/cvadmin.log"

gather "$cvdirpath/debug/qustat_cmd.log"

gather "$cvdirpath/debug/qustat_lib.log"

gather "$cvdirpath/debug/snadmin.log"

gather "$cvdirpath/debug/snstatd.log"

gather "$cvdirpath/debug/sn_web_gui.log"

gather "$cvdirpath/debug/snlicense.log"

if [ -x "$SNUPDATE" ] ; then
    gather "$SNUPDATE_DIR/log/snupdate.log"
    gather "$SNUPDATE_DIR/etc/snupdate.conf"
    gather "$SNUPDATE_DIR/etc/pkg.list" "snupdate.pkg.list"

    ${ECHO} "  ${prog}: $SNUPDATE show info"
    $SNUPDATE show info > ${GATHER_TEMP}/${oshost}.snupdate.show_info

    ${ECHO} "  ${prog}: $SNUPDATE show versions"
    $SNUPDATE show versions > ${GATHER_TEMP}/${oshost}.snupdate.show_versions
fi

if [ $os = "Linux" ] ; then
    gather "$cvdirpath/debug/smithlog"

    gather "$cvdirpath/debug/snldapd.out"

    gather "$cvdirpath/debug/ha_mgr.out"

    gather "$cvdirpath/debug/.5_4_0_3_upgraded"

    gather "/usr/adic/HAM/shared/.5_4_0_3_upgraded"

    gather /sys/devices/system/cpu/cpuidle/current_driver

    gather /sys/module/intel_idle/parameters/max_cstate

    gather /proc/acpi/processor/CPU0/power

    gather /proc/acpi/processor/CPU0/info

    gather /etc/sysconfig/cpuspeed

    for i in "$cvdirpath"/debug/snactivated.*.log
    do
        gather "$i"
    done

    # Gather the label_history data
    if [ $exclude_snapshot_files -eq 0 -a -d $cvdirpath/label_history ]
    then
	for i in "$cvdirpath"/label_history/*.log
	do
	    gather "$i"
	done
    fi

    # gather sar binary and text files if sysstat has been installed
    if [ $exclude_snapshot_files -eq 0 -a -d /var/log/sa ]; then
	gather /var/log/sa
    fi

    for i in /var/log/boot.*
    do
        gather $i
    done

    ${ECHO} "  ${prog}: $cvbinpath/snprobe"
    if [ $dns_resolution_off -eq 1 ]; then
       $cvbinpath/snprobe -r > ${GATHER_TEMP}/${oshost}.snprobe
    else
       $cvbinpath/snprobe > ${GATHER_TEMP}/${oshost}.snprobe
    fi

    ${ECHO} "  ${prog}: $cvbinpath/snprobe.license_report"
    if [ $dns_resolution_off -eq 1 ]; then
       $cvbinpath/snprobe -R -r > ${GATHER_TEMP}/${oshost}.snprobe.license_report
    else
       $cvbinpath/snprobe -R > ${GATHER_TEMP}/${oshost}.snprobe.license_report
    fi

    gather $cvdirpath/config/snfs_rest_config.json

    ${ECHO} "  ${prog}: $cvbinpath/snrest gateway show"
    $cvbinpath/snrest gateway show > ${GATHER_TEMP}/${oshost}.snrest_gateway_show

    ${ECHO} "  ${prog}: $cvbinpath/snrest gateway status -d"
    $cvbinpath/snrest gateway status -d > ${GATHER_TEMP}/${oshost}.snrest_gateway_status

    if [ -x $SYSTEMCTL ]; then
        gather_systemctl
    fi
fi

#
# File-system specific files.
#
for fsname in ${fsnames}; do
	gather_fs "${fsname}" "$configpath/${fsname}.cfgp"
	gather_fs "${fsname}" "$configpath/${fsname}.cfgx"
	gather_fs "${fsname}" "$configpath/${fsname}.cfg"
	gather_fs "${fsname}" "$configpath/${fsname}.cfg-old"
	gather_fs "${fsname}" "$configpath/${fsname}-auxdata.plist"	# Xsan

	gather_fs "${fsname}" "$cvdirpath/data/${fsname}/log/cvlog"

	x=0
	while [ $x -le $cvlogs ]; do 
		gather_fs "${fsname}" "$cvdirpath/data/${fsname}/log/cvlog_${x}"
		x=`expr ${x} + 1`
	done

	if [ -d "$cvdirpath/data/${fsname}/trace" ]; then
		for x in `ls -tr "$cvdirpath/data/${fsname}/trace/"`; do
			gather_fs "${fsname}" "$cvdirpath/data/${fsname}/trace/${x}"
		done
	fi

        if [ -d "$cvdirpath/data/${fsname}/${fsname}-mdarchive" ]; then
            ls -al $cvdirpath/data/${fsname}/${fsname}-mdarchive > "${GATHER_TEMP}/${oshost}_${fsname}.mdarchive_listing"
        fi

        if [ -d "/usr/adic/database/mdarchives/${fsname}-mdarchive" ]; then
            ls -al /usr/adic/database/mdarchives/${fsname}-mdarchive > "${GATHER_TEMP}/${oshost}_${fsname}.mdarchive_listing"
        fi

	if [ -f $cvbinpath/cvadmin ]; then
	    $cvbinpath/cvadmin -F "${fsname}" -e stat > "${GATHER_TEMP}/${oshost}_${fsname}.stat"
	    $cvbinpath/cvadmin -F "${fsname}" -e 'show long' > "${GATHER_TEMP}/${oshost}_${fsname}.showLong"
	    $cvbinpath/cvadmin -F "${fsname}" -e 'who long' > "${GATHER_TEMP}/${oshost}_${fsname}.whoLong"
	    $cvbinpath/cvadmin -F "${fsname}" -e 'mdarchive status' > "${GATHER_TEMP}/${oshost}_${fsname}.mdarchiveStatus"
	fi

	if [ -f $cvdirpath/bin/snquota ]
	then
	    ${ECHO} "  ${prog}: snquota  $fsname"
	    $cvdirpath/bin/snquota -F $fsname -X &> /dev/null
	elif [ -f $cvbinpath/cvadmin ]
	then
	    ${ECHO} "  ${prog}: repquota $fsname"
	    $cvbinpath/cvadmin -F $fsname -e repquota &> /dev/null
	fi

	for i in $cvdirpath/data/$fsname/quota_*
	do
	    gather_fs "${fsname}" $i
	done

done

gather "$cvdirpath/ras/raslog"

gather "$cvdirpath/ras/OLDraslog"

for i in /etc/fstab /etc/vfstab /etc/exports /etc/dfs/dfstab /etc/hosts /etc/*release /proc/cpuinfo /proc/meminfo /proc/slabinfo /proc/modules
do
    if [ -f $i ]; then
	gather $i
    fi
done

for i in "hostname" "ypcat hosts" "ifconfig -a" "netstat -rn" "netstat -s" "vmstat 2 5" "coredumpctl list" "coredumpctl info"
do
    nm=`echo $i | sed 's/ /_/g'`
    $ECHO running $i ...
    $i > ${GATHER_TEMP}/${oshost}.$nm 2>&1
done

#
# Kernel core
#
if [ $getkcore -eq 2 ] ; then

    gather "$kernelcore" 
fi 
	

if [ $getkcore -eq 1 ] ; then 

    host=`hostname`

    if [ $os = "Linux" ]    ; then
    	echo "    No system cores on Linux systems."
    fi 

    if [ $os = "SunOS" ]  ; then 
    	x="0"

        DumpPath=`/usr/sbin/dumpadm | awk '/Savecore directory:/ {print $3}'`

    	for file in `ls "${DumpPath}/" |grep vmcore.` ; do

    	    if [ "$file > $x" ]; then
	    	x=$file
	    fi
    	done

    	if [ "$x != 0" ] ; then

    	    gather "${DumpPath}/$x"

    	else
    	    echo "No ${DumpPath}/vmcore available."
    	fi

    	x="0"

    	for file in `ls "${DumpPath}/" |grep unix.` ; do

            if [ "$file > $x" ]; then
	    	x=$file
	    fi

    	done

    	if [ "$x != 0" ] ; then

    	    gather "${DumpPath}/$x"

    	else
	    echo "No ${DumpPath}/unix file available."
	    
    	fi 
    fi
fi


#
# symbols will gather all the symbol information, but not the actual corefiles
# This is to enable gathering of reasonable sized files without an 8GB corefile.
#
if [ $symbols -eq 1 ] ; then 

    host=`hostname`

    if [ $os = "Linux" ]    ; then
	suffix=`uname -r`
	gather /boot/System.map-${suffix}
	gather /usr/lib/debug/lib/modules/${suffix}/vmlinux
	gather /usr/bin/crash
    fi 

fi

#
# User core files
#
/bin/rm -f ${GATHER_TEMP}/${oshost}.corelist
if [ x${os} = xDarwin ] ; then

    # Always try to gather fsm and fsmpm core files on Mac OS X.
    # Remove the file after gathering it, else they pile up.

    for file in /cores/core.fsm.* /cores/core.fsmpm.* /cores/core.xsan*; do
    	gather "$file"
	rm -f "$file"
    done

else
    got1core=0
    if [ $systemd_coredump ] ; then
        for core in `/bin/ls -1 -t ${usercore}*.lz4 2>/dev/null`
        do
            corestring=`dd if=$core bs=1048576 count=8 status=none | lz4 -d 2>/dev/null | file -`
            echo "$corestring" | grep -q -E "/usr/cvfs|/usr/adic/DSM"
            if [ $? == 0 ] ; then
                ls -l $core >> ${GATHER_TEMP}/${oshost}.corelist 2>&1
                echo $corestring >> ${GATHER_TEMP}/${oshost}.corelist 2>&1

                # To follow the original idea of collecting a single core
                # (according to the man page)
                if [ $getucore -eq 1 ] && [ $got1core -eq 0 ] ; then
                    gather "$core"
                    got1core=1
                fi
            fi
        done
    else
        # Collect all core info from the specified directory
        for i in ${usercore}*
        do
            if [ ! -f $i ]; then
                continue
            fi
            ls -l $i >> ${GATHER_TEMP}/${oshost}.corelist 2>&1
            file $i >> ${GATHER_TEMP}/${oshost}.corelist 2>&1
        done

        # grab the specified user core

        if [ -f "${usercore}" ]; then
            gather "$usercore"
        fi

        if [ $getucore -eq 1 ]; then
            # grab most recently touched core.PID file
            for i in `/bin/ls -1 -t ${usercore}.* 2>/dev/null | head -1`
            do
                gather "$i"
            done
        fi
    fi

    if [ ! -f ${GATHER_TEMP}/${oshost}.corelist ]; then
        echo "No core files found in ${usercore}." > ${GATHER_TEMP}/${oshost}.corelist
    fi
fi

#
# Collect FSM and the libraries it uses to make it much easier to deal with
# any core files. This adds some extra size to the cvgather, but can save
# a lot of time.
#
if [ $os = "Linux" ]; then
    if [ -f /usr/cvfs/bin/fsm ]; then
        liblist=`ldd /usr/cvfs/bin/fsm | \
            sed -e "s/.*=>//"            \
                -e "s/ (.*//"            \
                -e 's/^[ \t]*//'         \
                -e '/^$/d'               \
                -e 's;^/;;'`

        cat > ${GATHER_TEMP}/README.fsm_debugging <<EOF
Here is how to run GDB on a core file so that it uses the system libraries
and FSM binary collected from the customer site:

gdb -ex "set solib-absolute-prefix /path/of/library/root" \\
    -ex "core /path/of/core_file" \\
    /path/to/fsm

For example, if you unpacked the fsm.${EXTENSION} file into the directory
/tmp/unpacked/fsm_dir, and the core file you want to debug is in the
current working directory and is named fsm-1486187967-164834-reported,
you would run:

gdb -ex "set solib-absolute-prefix /tmp/unpacked_gather/fsm_dir" \\
    -ex "core fsm-1486187967-164834-reported" \\
    /tmp/unpacked_gather/fsm_dir/usr/cvfs/bin/fsm
EOF

        echo "Packaging FSM and associated libraries in: ${oshost}.fsm.${EXTENSION}"
        tar --use-compress-program "${COMPRESS_PROGRAM}"  \
	    -h -cf ${GATHER_TEMP}/${oshost}.fsm.${EXTENSION} \
                            -C "${GATHER_TEMP}" README.fsm_debugging \
                            -C "/" usr/cvfs/bin/fsm $liblist > /dev/null 2>&1
        /bin/rm -f ${GATHER_TEMP}/README.fsm_debugging
    fi
fi

#
# Tar it up
#
${ECHO}
${ECHO} "Creating tar ball."

CFILE=$outputfile.${EXTENSION}

tar --use-compress-program "${COMPRESS_PROGRAM}" -cf "${CFILE}" -C "${GATHER_TEMP}" .

SUCCESS=$?

#
# Get rid of the temp files.
#
rm -rf "${GATHER_TEMP}"

${ECHO}
if [ "$SUCCESS" -ne 0 ]; then
    ${ECHO} "Unable to create tar ball."
else
    ${ECHO} "Tar ball created at ${CFILE}"
fi
${ECHO}

${ECHO} "${prog}: Complete."
