#!/bin/bash
set +x

#Verify
LOG_FILE="/var/log/inspect.log"

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="109"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="${G_TMP_INSPECT_PATH}tmpResult${CurInspectFun}"
>${RESULTFILE}
CHECK_PASSED=0
CHECK_FAILED=1


function LOG
{
   time=$(date)
   echo [${time}][$$][$CurInspectFun]$@ >> $LOG_FILE
}

function main()
{
    local readErr=0
    local writeErr=0
    local verifyErr=0
    local isPass=0
    local disktoolPID=""
    local timeCnt=0
    local unCorrectedErrors=""
    local tmplocaldiskfile="/tmp/tmpfilelocaldiskinfo"
    local tmpFile="${G_TMP_INSPECT_PATH}/tmpfileunCorrectedErrors"
    local tmpSlotFile="${G_TMP_INSPECT_PATH}/tmpfilelocaldiskx"
    local tmpSnFile="${G_TMP_INSPECT_PATH}/tmpfilesnlist"
    #disktool ִ̨
    {
        /usr/local/bin/disktool -f A  > ${tmpFile}
    } &
    disktoolPID=$!
    while [ "X$(pidstat -l -p ${disktoolPID} | grep ${disktoolPID} | awk '{print $4}')" != "X" ];do
        sleep 5
        timeCnt=$((timeCnt+1))
        if [ ${timeCnt} -ge 60 ];then
            $(kill -1 ${disktoolPID} >/dev/null 2>&1)
            LOG "[$FUNCNAME][$LINENO]disktool:exec cmd 'disktool' timeout(${timeCnt}*5 s).The service may be busy, please Try again later!"
            echo "[ERR]INFO:exec cmd 'disktool' timeout(${timeCnt}*5 s).(${disktoolPID})The service may be busy, please try again later!"  >>${RESULTFILE} 2>&1
            echo "${CurInspectFun}_Pass ${CHECK_FAILED}" >>${RESULTFILE} 2>&1
            return 0
        fi
    done

    #ڵûTotal uncorrected errors,ֱӷ
    local fifoContext=$(cat $tmpFile | grep "Total uncorrected errors" )
    if [ "$fifoContext" = "" ];then
        echo "location:No SAS disk||ReadError:--||WriteErr:--||VerifyErrorNumber:--" >> ${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        [ -f ${tmpFile} ] && rm -f ${tmpFile}
        return
    fi

    #mmlѯλ, жϳɹ,Ӱս, ûȡ, ֻʾlocationλû, 
    #ϵͳͨѯλ
    /usr/local/bin/MmlBatch 4016 "cm localdiskinfo show" >/dev/null 2>&1
    for i in $(seq 1 15)
    do
        if [ -f "${tmplocaldiskfile}" ];then
            dos2unix ${tmplocaldiskfile} >/dev/null 2>&1
            cat ${tmplocaldiskfile} > $tmpSlotFile 2>&1
            break
        fi
        sleep 3
    done

    local index=0
    #SNȡϢ
    cat ${tmpFile} | grep -n "Serial Number:" > $tmpSnFile
    local fistsn=""
    local snlist=$(cat $tmpSnFile | wc -l) 
    for((i=1; i<= $snlist + 1; i++)) ; do
        local snline=$(sed -n ${i}p $tmpSnFile | awk -F':' '{print $3}'| tr -d ' ')
        local snstart=$(sed -n ${i}p $tmpSnFile | awk -F':' '{print $1}' | tr -d ' ')
        local nexline=$(expr $i + 1)
        local snsend=$(sed -n ${nexline}p $tmpSnFile | awk -F':' '{print $1}' | tr -d ' ')

        if [[ "X$snstart" == "X" ]];then
            break
        fi

        echo "${fistsn}" | grep -w "${snline}" >/dev/null
        if [ $? -eq 0 ];then
            #˵disktool ѯظ
            continue
        else
            fistsn="$fistsn $snline"
        fi
        local unCorrected=""
        if [[ "X$snsend" != "X" ]];then
            unCorrected=$(sed -n ${snstart},${snsend}p ${tmpFile} )
        else
            unCorrected=$(tail -n +${snstart} ${tmpFile} )
        fi

        #ҵӲ̶Ϊsd-ʽ.ųҵӲ
        local isServerDisk=$(echo "${unCorrected}" | grep "System Device:"| grep "/dev/sd-")
        if [ "X${isServerDisk}" == "X" ];then
            continue
        fi
        unCorrectedErrors=$(echo "${unCorrected}" | grep "Total uncorrected errors" | awk -F'=' '{print $2}'|tr -d ' ')
        if [ "X${unCorrectedErrors}" == "X" ];then
            #ûTotal uncorrected errors,ʾ
            continue
        fi

        local tmpIndex=0
        IFS_OLD=$IFS
        IFS=$'\n'

        for errline in $unCorrectedErrors; do
            if [ $tmpIndex -eq 0 ];then
                readErr=$errline
                tmpIndex=$(expr $tmpIndex + 1)
            elif [ $tmpIndex -eq 1 ]; then
                writeErr=$errline
                tmpIndex=$(expr $tmpIndex + 1)
            else
                verifyErr=$errline
                tmpIndex=0
            fi
        done
        IFS=$IFS_OLD

        location=$(cat ${tmpSlotFile} | grep "${snline}" | grep -Po '(?<=location: )[0-9]+')
        if [ $readErr -gt 600 ] ; then
            isPass=1
            echo "[ERR]INFO:The value of readErr ($readErr) in slot ${location} exceeds the threshold (600)." >> ${RESULTFILE} 2>&1
        elif [ $writeErr -gt 30 ];then
            isPass=1
            echo "[ERR]INFO:The value of writeErr ($writeErr) in slot ${location} exceeds the threshold (30)." >> ${RESULTFILE} 2>&1
        elif [ $verifyErr -gt 600 ];then
            isPass=1
            echo "[ERR]INFO:The value of verifyErr ($verifyErr) in slot ${location} exceeds the threshold (600)." >> ${RESULTFILE} 2>&1
        fi
        index=$((index+1))
        echo "location:$location||ReadError:$readErr||WriteErr:$writeErr||VerifyErrorNumber:$verifyErr" >> ${RESULTFILE} 2>&1
    done

    #ڵû򷵻no sas
    if [ $index -eq 0 ];then
        echo "location:No SAS disk||ReadError:--||WriteErr:--||VerifyErrorNumber:--" >> ${RESULTFILE} 2>&1
    fi

    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1

    [ -f ${tmpFile} ] && rm -f ${tmpFile}
    [ -f ${tmpSlotFile} ] && rm -f ${tmpSlotFile}
    [ -f ${tmpSnFile} ] && rm -f ${tmpSnFile}
    return 0
}

main
