#!/bin/bash
# ********************************************** ***************************** #
# File Name: bdm_interlog_collect.sh
# Description: collect internal log from hard disk
# Input: none        
# Output: none
# Created: 2017-10-11
# Function List：
# History
# **************************************************************************** #

source /OSM/script/os_load_base.sh

disktool=/OSM/bin/disktool

ISHSSD=$1
DISKSN=$2
VENDOR=$3
DISKTYPE=$4
LOGNAME=$5
EXPORT_MAX_FILE_SIZE=100
EXPORT_UNCOMPORESSED_FILE_SIZE=150
FREE_MEM=102400
LOGFILENAME=HSSD_inter.log
DHA_INTER_LOG_TEMP_PATH=/OSM/coffer_data/dha/internal_log
CURRENT_PROCESS_FILE=interlog.ini
SCRIPTNAME=bdm_inter
DHA_INTER_LOG_EXPORT_PATH=/OSM/coffer_data/omm/export_import
TEMP_FILE_DIAGNOSE=tmp_diagnose.tmp
TEMP_FILE=tmp.tmp
EXCUTE_TMP_DIR=/tmp
TIME=`date +%Y%m%d%H%M%S`



#Return value: Unknown TB-Toshiba ST-Seagate WD-WD HW-Huasy HG-Hitachi SD-SanDisk
get_disk_vendor()
{
    #get vendor name
    [ -n "$(echo "$VENDOR" | grep -i "Toshiba")" ] && echo "TB" && return
    [ -n "$(echo "$VENDOR" | grep -i "Seagate")" ] && echo "ST" && return
    [ -n "$(echo "$VENDOR" | grep -i -w "WD")" ] && echo "WD" && return
    [ -n "$(echo "$VENDOR" | grep -i -w "WDC")" ] && echo "HG" && return
    [ -n "$(echo "$VENDOR" | grep -i "HUASY")" -o -n "$(echo "$VENDOR" | grep -i "HUASAI")" ] && echo "HW" && return
	[ -n "$(echo "$VENDOR" | grep -i "HUAWEI")" -o -n "$(echo "$VENDOR" | grep -i "HUAWEI")" ] && echo "HW" && return
    [ -n "$(echo "$VENDOR" | grep -i "Hitachi")" -o -n "$(echo "$VENDOR" | grep -i "HGST")" ] && echo "HG" && return
    [ -n "$(echo "$VENDOR" | grep -i "SanDisk")" ] && echo "SD" && return
    [ -n "$(echo "$VENDOR" | grep -i "Samsung")" ] && echo "SM" && return
    echo "UNKNOWN" && return
}

timeout_handle()
{
    max_time=$1
    devname=$2
	filename=$3
    
    time_passed=0
    while [ true ]
    do
        sleep 1
        #add 'head -n 1' to avoid query multiple record result in kill process failed
        pid="$(ps -ef | grep "$EXCUTE_TMP_DIR/$filename" | grep -v grep | awk '{print $2}'|head -n 1)"
		hssd_pid="$(ps -ef | grep "disktool -A o -z us" | grep -v grep | awk '{print $2}'|head -n 1)"
        [ -z "$pid" ] && [ -z "$hssd_pid" ] && rm -f ${EXCUTE_TMP_DIR}/${filename} && return 0
      
        #timeout will kill progress
        time_passed=$(($time_passed+1))
        if [ $time_passed -ge $max_time ]; then
            OS_echo "[bdm_interlog_collect.sh]Failed, internal log collect timeout."
            [ ! -z "$pid" ] && kill -9 $pid > /dev/null 2>&1
			[ ! -z "$hssd_pid" ] && kill -9 $hssd_pid > /dev/null 2>&1
			rm -f ${EXCUTE_TMP_DIR}/${filename}
            return 1
        fi
    done
    
    return 0
}

clean_env()
{
	cd ${DHA_INTER_LOG_TEMP_PATH}
	rm -f ${DHA_INTER_LOG_TEMP_PATH}/${CURRENT_PROCESS_FILE}
	rm -rf ${DHA_INTER_LOG_TEMP_PATH}/${DISKSN}
}

check_process_handling()
{
	pid="$(ps -ef | grep "disktool" | grep -v grep | awk '{print $2}'|head -n 1)"
    [ -z "$pid" ] && return 0
    return 1
}

[ $# -ne 5 ] && OS_echo "[bdm_interlog_collect.sh]Invalid parameter numbers, now is $#, should be 5." && exit 1
[ ! -f "${disktool}" ] && OS_echo "[bdm_interlog_collect.sh]Disk tool is not exist in ${disktool}." && exit 1

#create temp dir to save log 
[ ! -d ${DHA_INTER_LOG_TEMP_PATH} ] && mkdir -p ${DHA_INTER_LOG_TEMP_PATH} > /dev/null 2>&1
chmod 750 ${DHA_INTER_LOG_TEMP_PATH}

cd ${DHA_INTER_LOG_TEMP_PATH}

#创建脚本正在运行的文件，用来判断脚本是否运行,用来保证同一个控制器当前只有一个盘在收集日志
pid="$(ps -ef | grep "${SCRIPTNAME}" | grep -v grep | awk '{print $2}'|head -n 1)"
[ ! -f ${CURRENT_PROCESS_FILE} ] && echo "$pid" > ${CURRENT_PROCESS_FILE}

[ ! -d ${DISKSN}  ] && mkdir -p ${DISKSN} > /dev/null 2>&1
chmod 750 ${DISKSN}
cd ${DISKSN} 

#get dev name by SN
echo -e "sddebug showdisk blkdev\nexit\n" > "${TEMP_FILE_DIAGNOSE}"
diagnose --script="${TEMP_FILE_DIAGNOSE}" > "${TEMP_FILE}"
DEVNAME=`cat ${TEMP_FILE} | grep "${DISKSN}" | awk '{print $8}'`
rm -f ${TEMP_FILE_DIAGNOSE}
rm -f ${TEMP_FILE}

#input wrong SN
[ -z "${DEVNAME}" ] && OS_echo "[bdm_interlog_collect.sh] Failed, cannot get this SN number." && clean_env && exit 1

rm -rf ${DHA_INTER_LOG_TEMP_PATH}/${DISKSN}/*

VENDOR=`get_disk_vendor`

OS_echo "[bdm_interlog_collect.sh]Start collecting internal log of ${DEVNAME}."
#HSSD盘收集额外的明文内部日志
if [ ${ISHSSD} -eq 1 ]; then
    echo "-------------Disk Information-------------" > ${LOGFILENAME}
    echo "Device name: ${DEVNAME}" >> ${LOGFILENAME}
    echo "Vendor: ${VENDOR}" >> ${LOGFILENAME}
    echo "Collect time: ${TIME}" >> ${LOGFILENAME}
    echo "-----------------HSSD internal log------------------" >> ${LOGFILENAME}
    $disktool -A o -z usefullog /dev/$DEVNAME >> ${LOGFILENAME} &
fi

[ "$VENDOR" = "UNKNOWN" ] && OS_echo "[bdm_interlog_collect.sh]Failed to collect internal log of $DEVNAME, vendor($3) is unknown." && clean_env && exit 1

tmp_excute_file=${DISKSN:0:4}
echo "#!/bin/bash" > ${EXCUTE_TMP_DIR}/${tmp_excute_file}
if [ ${DISKTYPE} -eq 1 ]; then
    echo "$disktool -D ${DHA_INTER_LOG_TEMP_PATH}/${DISKSN} -f I -b SAS -z ${VENDOR} -x 100 /dev/${DEVNAME}" >> ${EXCUTE_TMP_DIR}/${tmp_excute_file}
elif [ ${DISKTYPE} -eq 2 ]; then
    echo "$disktool -D ${DHA_INTER_LOG_TEMP_PATH}/${DISKSN} -f I -b SATA -z ${VENDOR} -x 100 /dev/${DEVNAME}" >> ${EXCUTE_TMP_DIR}/${tmp_excute_file}
else
    OS_echo "[bdm_interlog_collect.sh]Failed to collect internal log of $DEVNAME, port type($TYPE) is incorrect."
    clean_env
    exit 1
fi

[ ! -f ${EXCUTE_TMP_DIR}/${tmp_excute_file} ] && OS_echo "[bdm_interlog_collect.sh]Failed to generate template process file." && clean_env && exit 1

chmod 755 ${EXCUTE_TMP_DIR}/${tmp_excute_file} > /dev/null 2>&1
${EXCUTE_TMP_DIR}/${tmp_excute_file} > /dev/null 2>&1 &

timeout_handle 600 ${DEVNAME} ${tmp_excute_file}
[ 0 -ne $? ] && OS_echo "[bdm_interlog_collect.sh]Failed to collect internal log of $DEVNAME." && clean_env && exit 1

#uncompressed internal log cannot be bigger than 100M
list_file_size="$(du -sm|head -n 1|awk '{print $1}')"
[ "$list_file_size" -gt "${EXPORT_UNCOMPORESSED_FILE_SIZE}" ] && OS_echo "[bdm_interlog_collect.sh]Failed, uncompressed files are bigger than ${EXPORT_UNCOMPORESSED_FILE_SIZE}M." && clean_env && exit 1

#get free mem from controller
free_mem=`cat /proc/meminfo|grep "MemFree"|awk '{print $2}'`
[ -z "$free_mem" ] && OS_echo "[bdm_interlog_collect.sh]Failed to get free memory." && clean_env && exit 1
[ "$free_mem" -lt "${FREE_MEM}" ] && OS_echo "[bdm_interlog_collect.sh]Failed, free memory is smaller than ${FREE_MEM}kb now." && clean_env && exit 1

tar -czf ${LOGNAME} * > /dev/null 2>&1
[ ! -f ${LOGNAME} ] && OS_echo "[bdm_interlog_collect.sh]Failed to compress files, return code $?." && clean_env && exit 1

chmod 440 ${LOGNAME} > /dev/null 2>&1

#compressed internal_log cannot be bigger than 60M
log_total_size="$(du -sm ${LOGNAME}|head -n 1|awk '{print $1}')"
[ "$log_total_size" -gt "${EXPORT_MAX_FILE_SIZE}" ] && OS_echo "[bdm_interlog_collect.sh]Failed, compress file is bigger than ${EXPORT_MAX_FILE_SIZE}." && clean_env && exit 1
mv ${LOGNAME} ${DHA_INTER_LOG_EXPORT_PATH} > /dev/null 2>&1
[ 0 -ne $? ] && OS_echo "[bdm_interlog_collect.sh]Failed to copy internal log of $DEVNAME to ${DHA_INTER_LOG_EXPORT_PATH}, return code $?." && clean_env && exit 1

clean_env

exit 0
