#!/bin/bash
# Export system logs and check whether it with any error message to those with BMC platforms via IOL.
# conversation dialog for deleting logs or not

# History:
# v3.2 2019/10/22 C.H. Huang	: use array instead of string command

echo -e "Path of previosu log files are under '/home/export_sys_log_with_bmc_via_IOL_*.log'\n"
read -p "Do you want to delete all logs before you run this script? Press 'y' or 'n' then press 'Enter': " choice

if [ "${choice}" == "y" ] || [ "${choice}" == "Y" ]; then
#delete all logs
		echo -e "Deleting log file(s)...\n"
        rm -rf /home/export_sys_log_with_bmc_via_IOL_*.log
elif [ "${choice}" == "n" ] || [ "${choice}" == "N" ]; then
#do nothing
        sleep 1
fi

#get time in integer
time=`date +%s`
date=`date`
uptime=`uptime`

#display log file name
logFile="/home/export_sys_log_with_bmc_via_IOL_${time}.log"
echo -e "This new log file name would be '$logFile'\n"

#user enter target BMC IP address
read -p "Please enter the BMC IP address, ex 192.168.1.1: " bmc_ipaddr
echo -e "The bmc ip addr you input is $bmc_ipaddr\n"

#user enter target BMC username and password
read -p "Enter your BMC username, or press 'Enter' as default=administrator: " bmc_usr

if [ "${bmc_usr}" == "" ]; then
        bmc_usr=administrator
fi

read -p "Enter your BMC password, or press 'Enter' as default=advantech: " bmc_pw
if [ "${bmc_pw}" == "" ]; then
        bmc_pw=advantech
fi

echo -e "The bmc username and password are $bmc_usr and $bmc_pw\n"
echo "Generating log file $logFile"

END=12
#export logs by following command lines
c[1]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw sel"
c[2]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw sel elist"
c[3]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw sel -v elist"
c[4]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw sel list"
c[5]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw sdr elist"
c[6]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw sdr -v"
c[7]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw sensor"
c[8]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw lan print"
c[9]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw fru print"
c[10]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw fru print -v"
c[11]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw mc info"
c[12]="ipmitool -I lanplus -H $bmc_ipaddr -U $bmc_usr -P $bmc_pw hpm check"

##log info in log file
#log Current Date and Time
echo -e "--- Begin of log ---\n" >> $logFile 2>&1
echo -e "Current Date and Time." >> $logFile 2>&1
echo -e ${date}"\n" >> $logFile 2>&1

#log System operation time since last boot
echo -e "System operation time since last boot" >> $logFile 2>&1
echo -e ${uptime}"\n" >> $logFile 2>&1

for ((i=1; i<=$END; i=i+1))
do
	cmd1=${c[$i]}" >> $logFile 2>&1"
	cmd2=${c[$i]}
	echo $i":# "$cmd2
	echo "# "$cmd2 >> $logFile 2>&1
	bash -c "$cmd1"
	if [ "$i" != "$END" ]; then
		echo -e "\n\n" >> $logFile 2>&1
	else
		echo "--- End of log ---" >> $logFile 2>&1
	fi
done

echo -e "log file $logFile has been generated\n"