#!/bin/bash
set +x

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="123"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

CHECK_PASSED=0
CHECK_FAILED=1
isPass=0
LOGFILE="/var/log/inspect.log"
tmpSmartctl="/tmp/tmpSmartctlFile"
tmpDisktool="/tmp/tmpDisktoolFile"
PRODUCT_NAME=$(dmidecode -t 1 | grep "Product Name:" | awk -F: '{print $2}' | sed 's/^\s*//;s/\s*$//')
. /opt/node_type_recognize.sh
. /opt/install_os_common_func.sh
IS_SAS3008=0

function LOG()
{
    echo [${time}][$$][$CurInspectFun]$@ >> $LOGFILE
}

function getlodaldiskinfo()
{
    local tmplocaldiskfile="/tmp/tmpfilelocaldiskinfo"
    local tmpSlotFile="$1"
    if [ -f $tmpSlotFile ];then
        return
    fi
    #ɲλϢ
    /usr/local/bin/MmlBatch 4016 "cm localdiskinfo show" >/dev/null 2>&1
    for i in $(seq 1 60)
    do
        if [ -f "${tmplocaldiskfile}" ];then
            dos2unix ${tmplocaldiskfile} >/dev/null 2>&1
            cat ${tmplocaldiskfile} > $tmpSlotFile 2>&1
            break
        fi
        sleep 1
    done
}

# ϵͳ60iostatУ>=30ϵͳutil>=90Ϊϵͳ̷æֶȷϣӰϵͳк
function get_60_count_per_util()
{
    local sysdiskOuterName="${1}"
    local each_util=0
    local per_util=0
    local count=0
    local gt90count=0 # util>=90Ĵ
    local sum=0
    local ret=0

    iostat -x 1 ${sysdiskOuterName} -c 60 | grep -w ${sysdiskOuterName} | awk '{print $NF}' | awk -F'.' '{print $1}'>/tmp/iostat_${sysdiskOuterName}
    while read each_util
    do
        if [ "X${each_util}" == "X" ]; then
            continue
        fi

        if [ ${each_util} -ge 90 ]; then
            gt90count=$((gt90count+1))
        fi
        count=$((count+1))
        sum=$((sum+each_util))
    done </tmp/iostat_${sysdiskOuterName}

    if [ ${count} -ne 0 ]; then
        per_util=$((sum/count))
    fi

    if [ ${gt90count} -ge 30 ]; then
        ret=1
        echo "[ERR]INFO:The system disk(/dev/${sysdiskOuterName}) is busy.iostat detection times($count),util >= 90% times(${gt90count})" >>${RESULTFILE} 2>&1
    fi

    rm /tmp/iostat_${sysdiskOuterName}
    echo "${per_util}"

    return ${ret}
}

function checksysdiskinfo()
{
    local sysdisklist="$*"
    local innername=""
    local iret=0
    local SingleHeader=0
    local line=""
    local HealthStatus=""
    local Vendor=""
    local Product=""
    local blockSize=""
    local speed=""
    local SN=""
    local PROTOCOL=""
    local CAPACITY=""
    local PowerOnTime=""
    local ReadErrCnt=""
    local WriteErrCnt=""
    local VerifyErrCnt=""
    local AllHeader=0
    local cnt=0
    local head=""
    local nopass=0
    local tmpSlotFile="/tmp/tmpfilelocaldisk$$"
    LOCAL_BACK_IP=$(GetLocalIp)
    local have_check=0        #SAS3008ֵϵͳ̷sdaһڵִֻһiostat -x 1 sdaɡ
    local util=0

    /opt/huawei/snas/script/inspect_mml/ShowInspectMml ${LOCAL_BACK_IP} 4016 "cm RHsysdiskslot show" > /tmp/tmpfilesysdiskslot 2>&1

    for line in $sysdisklist; do
        nopass=0
        SingleHeader=0
        innername=$(lsscsi -g | grep -w "$line"|awk '{print $NF}')
        >${tmpSmartctl}
        >${tmpDisktool}
        cnt=0

        if [ X"${innername}" == X"" ]; then
            isPass=$( check_pass $isPass 1 )
            echo "[ERR]INFO:failed to get system disk innername about(${line})." >>${RESULTFILE} 2>&1
            continue
        fi

        # T3000ڵ㲻ȥȡutilֱΪutilֵΪ10
        dmidecode -t 1 | grep "Product Name:" | egrep "(T3500 G3|T3200 G3|T3000 G3)"
        if [ $? -eq 0 ]; then
            util=10
        else
            if [ ${IS_SAS3008} -eq 1 ]; then
                # SAS3008ѹĿϵͳУϵͳֵһiostatʹͬһutil
                if [ ${have_check} -eq 0 ]; then
                    util=$( get_60_count_per_util "sda" )
                    ret=$?
                    if [ ${ret} -ne 0 ]; then
                        isPass=$( check_pass $isPass ${ret} )
                    fi
                    have_check=1
                fi
            else
                outername=$(lsscsi -g | grep -w "$line"|awk '{print $(NF-1)}' | awk -F/ '{print $NF}')
                if [ "X${outername}" != "X" ]; then
                    util=$( get_60_count_per_util "${outername}" )
                    ret=$?
                    if [ ${ret} -ne 0 ]; then
                        isPass=$( check_pass $isPass ${ret} )
                    fi
                fi
            fi
        fi

        smartctl -a ${innername} >${tmpSmartctl}
        iret=$?
        if [ ${iret} -ne 0 -o ! -s "${tmpSmartctl}" ]; then
            isPass=$( check_pass $isPass 1 )
            echo "[ERR]INFO:failed to excute command:smartctl -a ${innername}." >>${RESULTFILE} 2>&1
            LOG "[$LINENO]failed to execute smartctl -a ${innername}"
            continue
        fi

        disktool -f a ${innername} >${tmpDisktool}
        iret=$?
        if [ ${iret} -ne  0  -o ! -s "${tmpDisktool}" ]; then
            isPass=$( check_pass $isPass 1 )
            echo "[ERR]INFO:failed to excute command:disktool -f a ${innername}." >>${RESULTFILE} 2>&1
            LOG "[$LINENO]failed to execute smartctl -a ${innername}"
            continue
        fi

        SLOT=$(cat /tmp/tmpfilesysdiskslot |grep -w "${innername}" |awk '{print $2}'|tr -d ' ')
        #ûslotݵ,ҵλòѯ.ʾλ,ʾҵ̴raidϢ.
        if [[ -z "${SLOT}" ]];then
            #2̵ʱ,ڻһ,һraid,һ̹,DMӦи澯.
            #˴.
            getlodaldiskinfo "${tmpSlotFile}"
            local serialNumber=$(smartctl -i ${innername} | grep -i "Serial Number:" | awk '{print $3}')
            if [[ -n "${serialNumber}" ]] && [[ -f ${tmpSlotFile} ]];then
                SLOT=$(cat ${tmpSlotFile} | grep "${serialNumber}" | grep -Po '(?<=location: )[0-9]+')
                if [[ -n "${SLOT}" ]];then
                    isPass=$( check_pass $isPass 1 )
                    nopass=1
                    echo "[ERR]INFO:The service disk in SLOT ${SLOT} contains RAID information." >> ${RESULTFILE} 2>&1
                fi
            fi
        fi
        OS_STAT=`cat /sys/class/scsi_device/$line/device/state 2>&1`
        if  [[ "${OS_STAT}" != "running" ]];then
            echo "[ERR]INFO:The disk is not running!" >> ${RESULTFILE} 2>&1
            isPass=$( check_pass $isPass 1 )
            nopass=1
            LOG "[$LINENO]OS_STAT=$OS_STAT"
        fi
        #״̬Сдתɴд
        OS_STAT=$(echo "${OS_STAT}" | tr "[:lower:]" "[:upper:]")
        HealthStatus=$( grep "SMART Health Status:" ${tmpSmartctl} | awk -F: '{print $2}' | sed -e "s/^[ \t]*//g" -e "s/[ \t]*$//g"  )
        Vendor=$( grep "Vendor:" ${tmpSmartctl} | awk -F: '{print $2}' | sed -e "s/^[ \t]*//g" -e "s/[ \t]*$//g" )
        Product=$( grep "Product:" ${tmpSmartctl} | awk -F: '{print $2}' | sed -e "s/^[ \t]*//g" -e "s/[ \t]*$//g" )
        blockSize=$( grep "Logical block size: " ${tmpSmartctl} | awk -F: '{print $2}' | sed -e "s/^[ \t]*//g" -e "s/[ \t]*$//g" )
        speed=$( grep "Rotation Rate:" ${tmpSmartctl} | awk -F: '{print $2}' | sed -e "s/^[ \t]*//g" -e "s/[ \t]*$//g" )
        SN=$( grep "Serial number:" ${tmpSmartctl} | awk -F: '{print $2}' | sed -e "s/^[ \t]*//g" -e "s/[ \t]*$//g" )
        PROTOCOL=$( grep "Transport protocol: " ${tmpSmartctl} | awk -F: '{print $2}' | sed -e "s/^[ \t]*//g" -e "s/[ \t]*$//g" )
        CAPACITY=$( grep "User Capacity: " ${tmpSmartctl} | awk -F [ '{print $2}' | awk -F ] '{print $1}' )
        PowerOnTime=$( grep "Power on time:" ${tmpDisktool} |  awk -F: '{print $2}' | sed -e "s/^[ \t]*//g" -e "s/[ \t]*$//g" )
        ReadErrCnt=$( sed -n "/Error Counters (read) page/,/-------/p" ${tmpDisktool} | grep "Total uncorrected errors" | awk -F= '{print $2}' | grep -o "[0-9]"* )
        WriteErrCnt=$( sed -n "/Error Counters (write) page/,/-------/p" ${tmpDisktool} | grep "Total uncorrected errors" | awk -F= '{print $2}' | grep -o "[0-9]"*  )
        VerifyErrCnt=$( sed -n "/Error Counters (verify) page/,/-------/p" ${tmpDisktool} | grep "Total uncorrected errors" | awk -F= '{print $2}' | grep -o "[0-9]"*  )
       
        # ȱб
        grep "Grown Defect List is empty" ${tmpDisktool} >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            AllHeader=$( grep "^0x" ${tmpDisktool} | wc -l )
            grep "^0x" ${tmpDisktool} | awk -F "-" '{print $2}' | sort | uniq -c >${tmpDisktool}.count
            while read cnt head
            do
                if [ ${SingleHeader} -lt ${cnt} ]; then
                    SingleHeader=${cnt}
                fi
            done <${tmpDisktool}.count
        else
            SingleHeader=0
            AllHeader=0
        fi

        [[ -f "${tmpDisktool}.count" ]] && rm -f "${tmpDisktool}.count"

        # ͳһȡ쳣
        [[ -z "${HealthStatus}" ]] && HealthStatus="---"
        [[ -z "${Vendor}" ]] && Vendor="---"
        [[ -z "${Product}" ]] && Product="---"
        [[ -z "${blockSize}" ]] && blockSize="---"
        [[ -z "${speed}" ]] && speed="---"
        [[ -z "${SN}" ]] && SN="---"
        [[ -z "${PROTOCOL}" ]] && PROTOCOL="---"
        [[ -z "${CAPACITY}" ]] && CAPACITY="---"
        [[ -z "${PowerOnTime}" ]] && PowerOnTime="---"
        [[ -z "${ReadErrCnt}" ]] && ReadErrCnt="---"
        [[ -z "${WriteErrCnt}" ]] && WriteErrCnt="---"
        [[ -z "${VerifyErrCnt}" ]] && VerifyErrCnt="---"

        if [ X"${ReadErrCnt}" != X"---" -a X"${ReadErrCnt}" != X"0" ];then
            nopass=1
        fi

        if [ X"${WriteErrCnt}" != X"---" -a X"${WriteErrCnt}" != X"0" ];then
            nopass=1
        fi

        if [ X"${AllHeader}" != X"---" -a X"${AllHeader}" != X"0" ];then
            nopass=1
        fi
        result="InnerName:${innername}||Slot:${SLOT}||OS_STAT:${OS_STAT}||HealthStatus:${HealthStatus}||Vendor:${Vendor}||Product:${Product}||blockSize:${blockSize}||speed:${speed}||SN:${SN}||PROTOCOL:${PROTOCOL}||CAPACITY:${CAPACITY}||PowerOnTime:${PowerOnTime}||ReadErrCnt:${ReadErrCnt}||WriteErrCnt:${WriteErrCnt}||VerifyErrCnt:${VerifyErrCnt}||AllHeader:${AllHeader}||SingleHeader:${SingleHeader}||IostatUtil:${util}"
        if [ ${nopass} -ne 0 ]; then
            isPass=$( check_pass $isPass 4 )
        fi
        echo "${result}" >> ${RESULTFILE} 2>&1
        LOG "[$LINENO]${result}"
    done
 
    [[ -f "${tmpSmartctl}" ]] && rm -f "${tmpSmartctl}"
    [[ -f "${tmpDisktool}" ]] && rm -f "${tmpDisktool}"
    [[ -f "${tmpSlotFile}" ]] && rm -f "${tmpSlotFile}"

    return
}

# V3/V5/TSڵϵͳϢĻȡ
function CheckV3V5TSdiskinfo()
{
    local is3416Card=""
    local CpuMode=""
    local SingleHeader=0
    local sysdisklist=""

    lspci |grep SAS3008 >/dev/null 2>&1
    [ $? -eq 0 ] && IS_SAS3008=1
    GetCpuModel
    CpuMode=$?
    if [ ${CpuMode} -eq $HISI1620 -o ${CpuMode} -eq $KUNPENG920 ];then
        sysdisklist=$(lsscsi -g |grep -we $(mdadm -D /dev/md1 |grep "/dev/sd*" |sed -n '1p' |awk '{print $NF}') -we $(mdadm -D /dev/md1 |grep "/dev/sd*" |sed -n '2p' |awk '{print $NF}') | awk '{print $1}' |sed 's/[][]//g')
        count=$(echo "${sysdisklist}"|wc -l)
    else
        #smr "[10:0:3:0]   (0x14)  ATA      HGST HSH721414AL TT03  /dev/sde   /dev/sg4"
        #HGST HSH721414AL мڿո,$6ȡ̷
        sysdisklist=$(lsscsi -g | awk '{if ($1!="[0:0:0:0]" && ($2=="disk" || $2=="(0x14)" || $2=="zbc") && $(NF-1)=="-") print $1}' |tr -d '[] ' |sed 's/^ *//g'|sed "/^$/d")
        #ֱ²ϵͳ,עǷsmr, sysdisklist
        count=$(lsscsi -g | awk '{if ($1!="[0:0:0:0]" && $2=="disk" && $(NF-1)=="-") print $1}'| wc -l)
    fi
    if [ "$count" != "2" ];then
        echo "[ERR]INFO:The number of SystemDisk is not 2." >> ${RESULTFILE} 2>&1
        isPass=$( check_pass $isPass 1 )
        LOG "[$LINENO]count=$count"
    fi
    checksysdiskinfo "${sysdisklist}"
    return
}

# UVP
function IsStandardNode()
{
    local name=''
    local saspci=''
    name=$(dmidecode -t 1 | grep "Product Name:" | awk '{print $3}')
    if [ "$name" = "Standard" -o "$name" = "OpenStack" ]; then
        saspci=$(lspci |grep "SAS")
        if [ "$saspci" != "" ]; then
            return 0
        else
            return 1
        fi
    else
        return 1
    fi
}

function checkSysDiskHealth()
{

    LOG "[$LINENO]ProductName=CheckSysDiskHealth"
    IsStandardNode
    if [ $? -eq 0 ];then
        echo "UVP case, Does not involve." >> ${RESULTFILE} 2>&1
        isPass=3
        LOG "[$LINENO]UVP case, Does not involve"
        return 0
    fi

    CheckV3V5TSdiskinfo


    return
}

checkSysDiskHealth
echo "${CurInspectFun}_Pass ${isPass}" >> ${RESULTFILE} 2>&1
[ -f "${tmpSmartctl}" ] && rm -f "${tmpSmartctl}"
[ -f "${tmpDisktool}" ] && rm -f "${tmpDisktool}"
[ -f "/tmp/tmpfilesysdiskslot" ] && rm -f "/tmp/tmpfilesysdiskslot"
exit 0