#!/bin/bash

# Version 2.1.6
# 15/08/2014
# Add: judge the run environment of the BMC, if not then exit,else run this shell.
 


# Version 2.1.5
# 17/03/2014
# Add: when collect echo "imtool"   // Run bmc one-key collect

# Version 2.1.4
# 29/01/2014

# This used only for Huawei Tecal Server.
# Test by Myself only Tecal RH2288 V2, and Need more server for test. 


# 20140129
# add collect sel list&history show direct;
# add: echo \n | ipmcget -d sel -v list 
# add: echo \n | ipmcget -d sel -v history	


# 20131220
# add ifconfg -a/netstat -a/ps aux/free/df/
# Not Judge R1 OR V2, then collect all,and echo don't care the Error report.
# Because Some server don't have V2 mark.								

# 20131210
# Add R1 Server BMC Collect.         											

# 20131209
# Have test only in Tecal RH2288 V2  


# Judeg the running environment if BMC ,if not then exit, else run this shell.  20140815

BMC_Env=$(ipmcget 2>/dev/null )
if [ ! -n "$BMC_Env" ];then
	echo
	echo -en '\E[47;31m'"\033[1m This is not a BMC  environment,and exit NOW!       \033[0m"
	echo 
	echo -en '\E[47;31m'"\033[1m Please put this shell into BMC /tmp,and run again! \033[0m"
	echo 
	echo 
	exit
else
	echo
	echo -en '\E[47;34m'"\033[1m Begin to collect BMC information and will need several minutes. \033[0m" 
	echo 
fi

											 

rm -rf /tmp/*.tar.gz

mypath="/tmp/collect"
rm -rf ${mypath}

if [ -e "${mypath}" ]; then
    collect_date=$(ipmcget -d ipmc | awk -F" " '{print $1}')
    collect_time=$(ipmcget -d ipmc | awk -F" " '{print $3}' | sed "s/:/_/g")
    mypath="/tmp/collect_${collect_date}_${collect_time}"
    if [ -e "${mypath}" ]; then
        exit 1
    fi
fi

mkdir ${mypath}

#echo $mypath

Server_V2=$(ipmcget -d fru |grep "Product Name          :" |awk -F ": " '{print $2}' |grep V2)
# echo $Server_V2
# if Server_V2 is NULL that mean is R1 Server.




echo 
echo "################ Collect Board information & sel list/history ################"
echo "ipmcget -d v "
echo 
echo "ipmcget -d fru"
echo
echo "ipmcget -d sel -v list"
echo 
echo "ipmcget -d sel -v history"

echo "root@BMC:/#ipmcget -d v " >>$mypath/ipmcget_v.log
ipmcget -d v >>$mypath/ipmcget_v.log

echo "root@BMC:/#ipmcget -d fru" >>$mypath/ipmcget_fru.log
ipmcget -d fru >>$mypath/ipmcget_fru.log

echo "root@BMC:/#ipmcget -d sel -v list" >>$mypath/ipmcget_sel_list.log
echo \n | ipmcget -d sel -v list >>$mypath/ipmcget_sel_list.log

echo "root@BMC:/#ipmcget -d sel -v history" >>$mypath/ipmcget_sel_history.log
echo \n | ipmcget -d sel -v history >>$mypath/ipmcget_sel_history.log

echo
echo "################ Collect BMC run time ################"

echo "uptime"

echo "root@BMC:/#uptime" >> $mypath/R1_uptime.log
uptime >> $mypath/R1_uptime.log
echo

echo "busybox uptime"

echo "root@BMC:/#busybox uptime" >> $mypath/V2_busybox_uptime.log
busybox uptime >> $mypath/V2_busybox_uptime.log

echo 
echo "################ Collect sensor list ################"
echo "ipmcget -t sensor -d list"
echo "root@BMC:/#ipmcget -t sensor -d list" >>$mypath/ipmcget_sensor_list.log
ipmcget -t sensor -d list >>$mypath/ipmcget_sensor_list.log


echo 
echo "################ Collect Mainboard Information ################"
echo "cp /data/smbios/smbios0 $mypath/"
cp /data/smbios/smbios0 $mypath/


echo
echo "################ Collect /data/var/log file & R1 Server Not Collect this. ################"

echo 
tar cvzf $mypath/data_var_log_file.tar.gz /data/var/log/* 2>/dev/null
echo

echo
echo "################ Collect oplog & R1 Server Not Collect this. ################"
echo
tar cvzf $mypath/oplog.tar.gz /data/share/oplog/* 2>/dev/null
echo

echo
echo "################ Collect ipmicmdlog & R1 Server Not Collect this. ################"
echo
tar cvzf $mypath/ipmicmdlog.tar.gz /data/ipmicmd* 2>/dev/null
echo

echo
echo "################ Collect sel log ################"
echo
tar cvzf $mypath/R1_sel_log.tar.gz /data/sel* 2>/dev/null
echo

tar cvzf $mypath/V2_sel_log.tar.gz /data/share/sel/* 2>/dev/null
echo


echo
echo "################ Collect last screen1/2/3.jpg ################"
echo
tar cvzf $mypath/R1_last_screen_1_2_3.tar.gz /tmp/*.bmp 2>/dev/null
echo
echo
tar cvzf $mypath/V2_last_screen_1_2_3.tar.gz /data/share/screen/* 2>/dev/null
echo

echo
echo "################ Collect sol information,This will be need about 2 Mins & R1 Server Not Collect this. ################"
echo
echo "ipmcset -d download -v 0"
ipmcset -d download -v 0  
echo "And then: cp /dev/shm/share/sol.dat $mypath"
cp /dev/shm/share/sol.dat $mypath
echo
echo "R1 Server Not Collect this. "
echo "If R1 Server don't care the Error report!"


echo 
echo "################ Run bmc one-key collect ,in R1 Server haven't this commomd & R1 Server Not Collect this. ################"
echo
user_name=$(whoami)
if [ "${user_name}" == "root" ]; then
    # only root user can exec imtool
    echo "imtool"
    imtool  2>/dev/null 1>/dev/null
else
    echo "only root user can exec imtool command."
fi
mv /tmp/*.tar.gz $mypath/
rm -rf /tmp/*.tar.gz
echo
echo "R1 Server Not Collect this. "
echo "If R1 Server don't care the Error report!"


echo 
echo "################ Collect: ifconfg -a/netstat -a/ps aux/free/df information. ################"
echo
echo "ifconfig -a"
echo "root@BMC:/#ifconfig -a" >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
ifconfig -a >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
echo
echo "netstat -a"
echo "root@BMC:/#netstat -a" >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
netstat -a >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
echo
echo "ps aux"
echo "root@BMC:/#ps aux" >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
ps aux >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
echo
echo "free"
echo "root@BMC:/#free" >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
free >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
echo
echo "df"
echo "root@BMC:/#df" >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
df >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
echo
echo "df -h"
echo "root@BMC:/#df -h" >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log
df -h >> $mypath/ifconfig_a-netstat_a-ps_aux-free-df.log

# collect cat error log
mkdir -p $mypath/cat_error_log
cp /data/share/oplog/imana_fdmlog $mypath/cat_error_log > /dev/null 2>&1
cp /data/share/oplog/imana_fdmlog.1.gz $mypath/cat_error_log > /dev/null 2>&1
cp /data/share/oplog/imana_fdmlog.2.gz $mypath/cat_error_log > /dev/null 2>&1
cp /data/share/oplog/imana_fdmlog.3.gz $mypath/cat_error_log > /dev/null 2>&1
cp /data/etc/fdminfo $mypath/cat_error_log > /dev/null 2>&1
cp /data/conf/fdm.conf $mypath/cat_error_log > /dev/null 2>&1
mkdir -p $mypath/BIOS_config
cp /data/smbios/display0.ini $mypath/BIOS_config > /dev/null 2>&1
cp /data/smbios/options0.ini $mypath/BIOS_config > /dev/null 2>&1
cp /data/smbios/changed0.ini $mypath/BIOS_config > /dev/null 2>&1

echo
echo "################ Compress the Collect BMC Information. ################" 

Server_Name=$(ipmcget -d fru |grep "Product Name          :" |awk -F ": " '{print $2}')

Board_SN=$(ipmcget -d fru |grep "Board Serial Number   :" |awk '{print $5}')

collect_date=$(ipmcget -d ipmc |awk  '{print $1}')

collect_H=$(ipmcget -d ipmc |awk  '{print $3}' |awk -F ":" '{print $1}')

collect_M=$(ipmcget -d ipmc |awk  '{print $3}' |awk -F ":" '{print $2}')

collect_S=$(ipmcget -d ipmc |awk  '{print $3}' |awk -F ":" '{print $3}')

tar cvzf /tmp/"BMC_$Server_Name"_"$collect_date"_"$collect_H"_"$collect_M"_"$collect_S".tar.gz $mypath/ 2>/dev/null

rm -rf $mypath

echo	
echo "################  BMC Collect End!  ################"


echo
echo "################ ll /tmp/ file ################" 
ls -l /tmp/
echo

echo "################ Please get out the /tmp/"BMC_$Server_Name"_"$collect_date"_"$collect_H"_"$collect_M"_"$collect_S".tar.gz file and then Delete it in BMC /tmp. ################"
echo
echo 