#!/bin/bash
#
# Copyright (C) 2025 Actiphy, Inc.
#   All rights reserved.
#
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Actiphy, Inc.
# The copyright notice above does not evidence any actual or intended
# publication of such source code.
#
# Created by Yoshinobu Ito
#
# aipsupport: Support Information collecting tool
# 
# File:  aipsupport
#
# Date:  07/24/25
#
# Requirements:
#	- AIP Linux version 2018 or higher.
#

export LANG=C
export PATH=/usr/bin:/sbin:/usr/sbin:/bin:$PATH

fullpath=$(readlink -f $0)
INSTALLER_PATH=${fullpath%/*}

GenDate=$(date '+%Y%m%d_%H%M')
ScVer="1.0.19"

TAR_SUPPORT_INFO=AipSupportInfo_${GenDate}.zip
if [ $# -eq 2 -a "$1" == "-d" -a -d "$2" ] ; then
   TAR_SUPPORT_INFO=$2/${TAR_SUPPORT_INFO}
else
   TAR_SUPPORT_INFO=${INSTALLER_PATH}/${TAR_SUPPORT_INFO}
fi

cd $INSTALLER_PATH

cleanup(){

echo "`date '+%Y-%m-%d %H:%M:%S'` *** Working directory will be cleaned up."
rm -f $SUPPORT_FILE
rm -rf ${INSTALLER_PATH}/system-report
rm -rf ${INSTALLER_PATH}/datto-report
rm -f ${INSTALLER_PATH}/licensecheck
rm -f ${INSTALLER_PATH}/serviceinfo
rm -f ${INSTALLER_PATH}/diskinfo
rm -f ${INSTALLER_PATH}/aipinstall.log
rm -f ${INSTALLER_PATH}/aipuninstall.log
rm -f ${INSTALLER_PATH}/aiptarerr.log
rm -f ${INSTALLER_PATH}/AIP-packages-tool.log 
rm -f ${INSTALLER_PATH}/ptov_log.txt
rm -fr ${TMP}

exit 0

}


print_head()
{
   clear
   echo -e "\e[44;1m**************************************************\e[0m"
   echo -e "\e[44;1m***    ActiveImage Protector Linux Edition     ***\e[0m"
   echo -e "\e[44;1m**************************************************\e[0m"
   echo ""
   echo -e "\e[32;1m$1\e[0m"
   echo ""
}

print_head "Collecting of Support information"
echo "`date '+%Y-%m-%d %H:%M:%S'` *** Ver $ScVer Start"

echo "Collecting of system information has started."

#start of Collect systeminfo
export PATH=/sbin:/usr/sbin:${PATH} 
TMP=`mktemp -d /tmp/system-report-XXXXXX`
mkdir -p ${TMP}/var/log
mkdir -p ${TMP}/lvminfo

chk_env=`cat $INSTALLER_PATH/aip.ini | grep WINBE | wc -l`
efi_detect(){
if which dpkg >/dev/null 2>&1 ; then
	e_rpm=`dpkg-query -l | grep ii | grep efibootmgr | wc -l`
	if [ $e_rpm -eq 1 ]; then
		chk_efiboot=`efibootmgr 2>&1 | grep "EFI variables are not supported" | wc -l`
		if [ $chk_efiboot -eq 1 ]; then
			str_efi=0	#echo "BIOS system."
		else
			str_efi=1	#echo "EFI system."
		fi
	else
		#echo "BIOS system."
		str_efi=0
	fi
else
	e_rpm=`rpm -qa | grep efibootmgr | wc -l`
	if [ $e_rpm -eq 1 ]; then
		chk_efiboot=`efibootmgr 2>&1 | grep "EFI variables are not supported" | wc -l`
		if [ $chk_efiboot -eq 1 ]; then
			str_efi=0
		else
			str_efi=1
		fi
	else
		str_efi=0
	fi
fi
}

grub_cfg(){
	unset cfg_arry
	cfg_arry+=(`find /boot -type f -name grub.cfg`)
	if [ ${#cfg_arry[@]} -ge 2 ]; then
		for var_file in "${cfg_arry[@]}";do
			real_grubcfg=`cat $var_file | grep "set timeout" | wc -l`
			if [ $real_grubcfg -ge 1 ]; then
				grubcfg_file=$var_file
				if [ -n "$grubcfg_file" ]; then
					break
				fi
			fi		
		done
	elif [ ${#cfg_arry[@]} -eq 1 ]; then
		for var_file in "${cfg_arry[@]}";do
			grubcfg_file=`echo $var_file`
		done
	fi
	echo "grubcfg_file "$grubcfg_file
}

efi_detect
grub_cfg
if which grubby >/dev/null 2>&1 ; then	#RHEL
	grby=`rpm -q grubby | grep -oE "el[0-9]*" | sed -e 's/el//'`
	if rpm -q grubby | grep -E "el" ; then
		if [ $grby -eq 6 ]; then
			cat /etc/grub.conf | grep title | sed -e 's/title //' > ${TMP}/grub.menu.lst 2>&1
		elif [ $grby -eq 7 ]; then
			grubby --info ALL | grep title | sed -e 's/title=//' > ${TMP}/grub.menu.lst 2>&1
		elif [ $grby -ge 8 ]; then
			grubPara=`cat /etc/default/grub | grep GRUB_ENABLE_BLSCFG | awk -F = '{print $2}'`
			if [ "$grubPara" = "true" ]; then	#RHEL8 default
				#echo "`date '+%Y-%m-%d %H:%M:%S'` GRUB_ENABLE_BLSCFG=true"
				if [ $str_efi -eq 0 ]; then	#BIOS
					grubby --info ALL | grep title | sed -e 's/title=//' > ${TMP}/grub.menu.lst 2>&1
					cat $grubcfg_file | grep -e "^menuentry" | awk -F "['\"]" '{print $2}' >> ${TMP}/grub.menu.lst 2>&1
				else
					grubby --info ALL | grep title | sed -e 's/title=//' > ${TMP}/grub.menu.lst 2>&1
					cat $grubcfg_file | grep -e "\smenuentry\s" -e "^menuentry\s" | awk -F "['\"]" '{print $2}' >> ${TMP}/grub.menu.lst 2>&1
				fi
			else
				#echo "`date '+%Y-%m-%d %H:%M:%S'` GRUB_ENABLE_BLSCFG=false"
				if [ $str_efi -eq 0 ]; then	#BIOS
					if cat $grubcfg_file | grep "Advanced options" ; then
						cat $grubcfg_file | grep -e "^menuentry" -e "^submenu" | awk -F "['\"]" '{print $2}' >> ${TMP}/grub.menu.lst 2>&1
					else
						cat $grubcfg_file | grep -e "^menuentry" | awk -F "['\"]" '{print $2}' >> ${TMP}/grub.menu.lst 2>&1
					fi
				else				#efi
					cat $grubcfg_file | grep -e "\smenuentry\s" -e "^menuentry\s" | awk -F "['\"]" '{print $2}' >> ${TMP}/grub.menu.lst 2>&1
				fi
			fi
		fi
	fi
	if rpm -q grubby | grep -o "amzn" >/dev/null 2>&1 ; then
		grubby --info ALL | grep title | grep -v recovery | sed -e 's/title=//' > ${TMP}/grub.menu.lst 2>&1
	fi
else	#Debian,Ubuntu,suse
	cat $grubcfg_file | grep -e "^menuentry" -e "^submenu" -e "menuentry 'UEFI Firmware Settings'" | awk -F "['\"]" '{print $2}' > ${TMP}/grub.menu.lst 2>&1
fi

lspci > ${TMP}/lspci 2>&1
if `which dpkg >/dev/null 2>&1` ; then
   dpkg -l > ${TMP}/dpkg 2>&1
else
   rpm -qa | sort > ${TMP}/rpm 2>&1
fi

mount > ${TMP}/mount 2>&1
top -b -d 3 -n 1 > ${TMP}/top 2>&1
dmidecode > ${TMP}/dmidecode 2>&1
cat /proc/cpuinfo > ${TMP}/cpuinfo 2>&1
free -h --si> ${TMP}/free 2>&1
uptime > ${TMP}/uptime 2>&1
# Include only vmcore-dmesg.txt

echo "Collect /var/log."
rsync -varm --include={'*/','*.log','syslog*','messages*','dmesg*'} --exclude="*" /var/log ${TMP}/var/ 2>/dev/null
journalctl -b > ${TMP}/var/log/journalctl.log

echo "Collect crash dump."
if [ -e /var/crash ]; then
	rsync -v -a --exclude={'vmcore','*incomplete','dump.*','.datto*'} /var/crash/ ${TMP}/crash 2>/dev/null
fi

fdisk -l > ${TMP}/fdisk 2>&1
date > ${TMP}/date 2>&1
uname -a > ${TMP}/uname 2>&1
ls -la /dev > ${TMP}/dev 2>&1
lsblk >${TMP}/lsblk 2>&1
lsmod > ${TMP}/lsmod 2>&1
if `which ifconfig >/dev/null 2>&1` ; then
	ifconfig -a > ${TMP}/ifconfig 2>&1
else
	ip a show > ${TMP}/ip_a 2>&1
fi
if `which route >/dev/null 2>&1` ; then
	route -n > ${TMP}/route 2>&1
else
	ip route show table local  > ${TMP}/ip_route 2>&1
fi
if `which netstat >/dev/null 2>&1` ; then
	netstat -anp > ${TMP}/netstat 2>&1
else
	ss -anp > ${TMP}/ss 2>&1
fi
ps auxf > ${TMP}/ps 2>&1
w > ${TMP}/w 2>&1
df -h > ${TMP}/df 2>&1
if `which dmraid >/dev/null 2>&1` ; then
	dmraid -r > ${TMP}/dmraid 2>&1
fi
cat /proc/mdstat > ${TMP}/mdstat 2>&1
blkid > ${TMP}/blkid 2>&1
env > ${TMP}/env 2>&1
if `which chkconfig >/dev/null 2>&1` ; then
	chkconfig > ${TMP}/chkconfig 2>&1
fi
cp /etc/fstab ${TMP} 2>&1 2>/dev/null
if [ -f /etc/redhat-release ]; then
	cat /etc/redhat-release > ${TMP}/redhat-release 2>&1
else
	cat /etc/os-release > ${TMP}/os-release 2>&1
fi
if `which parted >/dev/null 2>&1` ; then
	parted -ls > ${TMP}/parted 2>&1
fi
systemctl status aipservice > ${TMP}/aipservice_status 2>&1

#LVMINFO
if `which pvdisplay >/dev/null 2>&1` ; then
	pvdisplay > ${TMP}/lvminfo/pvdisplay 2>&1
fi
if `which vgdisplay >/dev/null 2>&1` ; then
	vgdisplay > ${TMP}/lvminfo/vgdisplay 2>&1
fi
if `which lvdisplay >/dev/null 2>&1` ; then
	lvdisplay > ${TMP}/lvminfo/lvdisplay 2>&1
fi
sleep 3

mv ${TMP} ${INSTALLER_PATH}/system-report
sleep 3
echo "Collecting of system information has completed."
#end of Collect systeminfo

#Start of aipsnapsupport
echo "Collecting of datto information has started."
export PATH=/sbin:/usr/sbin:${PATH} 
TMPDIR=`mktemp -d /tmp/datto-report-XXXXXX`
modinfo dattobd 2>/dev/null | grep -v ^parm: > ${TMPDIR}/datto.modinfo 
cat /proc/datto-info > ${TMPDIR}/datto-info 2>/dev/null
journalctl -kb | grep -E "datto" > ${TMPDIR}/datto.log

mv ${TMPDIR} ${INSTALLER_PATH}/datto-report
rm -rf ${TMPDIR}
echo "Collecting of datto information has completed."
#end of aipsnapsupport

#Start of aip command
echo "Collecting of aip information has started."
$INSTALLER_PATH/aipcontrol licensecheck > licensecheck
$INSTALLER_PATH/aipcontrol serviceinfo > serviceinfo
$INSTALLER_PATH/aipcontrol diskinfo > diskinfo
echo "Collecting of aip information has completed."
#end of aip command

#Start of aipinstall.log and AIP-packages-tool.log
find /tmp -type f -name "*.log" -or  -name "*.txt" | grep -ie aip -ie nbt -ie detect | xargs -I{} cp -v {} ${INSTALLER_PATH}

#end of aipinstall.log

zip -r "$TAR_SUPPORT_INFO" *.log *.txt *.ini licensecheck serviceinfo diskinfo logs profiles schedules consolidation replication verify LicenseFile Manifest system-report datto-report

echo ""
echo "The support information archive is collected at:"
echo "${TAR_SUPPORT_INFO}" 
echo ""
echo "Finished."

cleanup

exit 0
