#!/bin/bash
set +x

#系统盘RAID状态，必须是双系统盘，单系统盘判定不通过

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

CHECK_PASSED=0
CHECK_FAILED=1
isPass=0

LOGFILE="/var/log/inspect.log"
. /opt/node_type_recognize.sh
. /opt/install_os_common_func.sh

function set_ifs
{
    IFS_OLD=$IFS
    IFS=$'\n'
}

function restore_ifs
{
    IFS=$IFS_OLD
}

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
}

#处理3416 和3048 SAS卡
function Check3416SASCard()
{
    local sysdisknum=0
    local sysdiskinfo=""
    local OS_STAT=""
    local MODEL=""
    local slot=0
    #获取磁盘个数
    sysdiskinfo="$(/usr/bin/storcli64 /c0/v0 show all |grep -A 4 'EID:Slt' | grep -E "^[0-9]+:[0-9]+")"
    sysdisknum=$(echo "${sysdiskinfo}" | wc -l)
    if [ "${sysdisknum}" != "2" ];then
        echo "[ERR]INFO:the number of SystemDisk is not 2" >> ${RESULTFILE} 2>&1
        isPass=1
        LOG "[$LINENO]count=$count"
    fi
    set_ifs
    for item in ${sysdiskinfo}; do
        #获取槽位号
        slot=$(echo "${item}" | awk '{print $1}' | awk -F':' '{print "SLOT" $2}')
        #获取磁盘OS_STAT状态
        OS_STAT=$(echo "${item}" | awk '{print $3}' |tr "[:lower:]" "[:upper:]")
        if [ ${OS_STAT} != "ONLN" ];then
            echo "[ERR]INFO:the disk is not running!" >> ${RESULTFILE} 2>&1
            isPass=1
            LOG "[$LINENO]OS_STAT=${OS_STAT}"
        else
            #为了与RH V3保持一致，需要把ONLN转换为RUNNING
            OS_STAT="RUNNING"
        fi
        #获取磁盘厂商VENDOR
        VENDOR="--"
        #获取磁盘MODEL
        MODEL=$(echo "${item}" | awk '{print $12}')

        INOUT="--"
        PHY="--"
        POWER="--"
        LED="--"
        OS_IN="--"
        SN="--"
        FW_VER="--"
        LINK="--"
        DISK_SIZE="--"
        SPEED="--"
        TEMP="--"
        TYPE="--"
        echo "SLOT:$slot||INOUT:$INOUT||PHY:$PHY||POWER:$POWER||LED:$LED||OS_IN:$OS_IN||OS_STAT:$OS_STAT||SN:$SN||VENDOR:$VENDOR||FW_VER:$FW_VER||MODEL:$MODEL||LINK:$LINK||DISK_SIZE:$DISK_SIZE||SPEED:$SPEED||TEMP(degrees):$TEMP||TYPE:$TYPE" >> ${RESULTFILE} 2>&1
        LOG "[$LINENO]OS_STAT=$OS_STAT,slot=$slot"
    done
    restore_ifs
}

function CheckOtherSASCard()
{
    SNAS_CONFIG_PATH=/opt/huawei/snas/etc/snas.ini
    G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml/"
    source $G_MML_FILE_PATH/CommonFunc.sh
    local tmpSlotFile="/tmp/tmpfilelocaldisk$$"
    LOCAL_BACK_IP=`GetLocalIp`

    if [ -z "${LOCAL_BACK_IP}" ]; then
        echo "[ERR]INFO:Can't get backend IP." >> ${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${CHECK_FAILED}" >>${RESULTFILE} 2>&1
        exit 1
    fi

    /opt/huawei/snas/script/inspect_mml/ShowInspectMml $LOCAL_BACK_IP 4016 "cm RHsysdiskslot show" > /tmp/tmpfilesysdiskslot 2>&1
    #smr "[10:0:3:0]   (0x14)  ATA      HGST HSH721414AL TT03  /dev/sde   /dev/sg4"
    #HGST HSH721414AL 中间存在空格,导致$6并不是取的盘符
    javaId=$(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, javaId列表包含没有盘符的业务
    count=$(lsscsi -g | awk '{if ($1!="[0:0:0:0]" && $2=="disk" && $(NF-1)=="-") print $1}'| wc -l)

    if [ "$count" != "2" ];then
        echo "[ERR]INFO:The number of SystemDisk is not 2." >> ${RESULTFILE} 2>&1
        isPass=1
        LOG "[$LINENO]count=$count"
    fi
    set_ifs
    for line in $javaId; do
        OS_STAT=`cat /sys/class/scsi_device/$line/device/state 2>&1`
        VENDOR=`cat /sys/class/scsi_device/$line/device/vendor 2>&1`
        MODEL=`cat /sys/class/scsi_device/$line/device/model 2>&1`
        innername=$(lsscsi -g | grep -w "$line"|awk '{print $NF}')

        if  [ "$OS_STAT" != "running" ];then
            echo "[ERR]INFO:The disk is not running!" >> ${RESULTFILE} 2>&1
            isPass=1
            LOG "[$LINENO]OS_STAT=$OS_STAT"
        fi
        slot=$(cat /tmp/tmpfilesysdiskslot |grep -w "$innername" |awk '{print $2}'|tr -d ' ')
        #对没slot内容的盘,进行业务盘位置查询.如果有数据显示槽位号,并提示业务盘带了raid信息.
        if [ "X$slot" == "X" ];then
            #2块盘的时候,存在坏了一块盘,一块带raid的情况,但如果有一块盘故障,DM上应该有告警.
            #此处补处理这种情况.
            getlodaldiskinfo "${tmpSlotFile}"
            local snline=$(smartctl -i $innername | grep -i "Serial Number:" | awk '{print $3}')
            if [ "X${snline}" != "X" ] && [ -f ${tmpSlotFile} ];then
                slot=$(cat ${tmpSlotFile} | grep "${snline}" | grep -Po '(?<=location: )[0-9]+')
                if [ "X$slot" != "X" ];then
                    isPass=1
                    echo "[ERR]INFO:The service disk in slot $slot contains RAID information." >> ${RESULTFILE} 2>&1
                fi
            fi
        fi
        #状态小写转换成大写
        OS_STAT=`echo $OS_STAT|tr "[:lower:]" "[:upper:]"`

        INOUT="--"
        PHY="--"
        POWER="--"
        LED="--"
        OS_IN="--"
        SN="--"
        FW_VER="--"
        LINK="--"
        DISK_SIZE="--"
        SPEED="--"
        TEMP="--"
        TYPE="--"
        echo "SLOT:$slot||INOUT:$INOUT||PHY:$PHY||POWER:$POWER||LED:$LED||OS_IN:$OS_IN||OS_STAT:$OS_STAT||SN:$SN||VENDOR:$VENDOR||FW_VER:$FW_VER||MODEL:$MODEL||LINK:$LINK||DISK_SIZE:$DISK_SIZE||SPEED:$SPEED||TEMP(degrees):$TEMP||TYPE:$TYPE" >> ${RESULTFILE} 2>&1
        slotcount=$((slotcount+1))
        LOG "[$LINENO]OS_STAT=$OS_STAT,slot=$slot,count=$count"
    done
    restore_ifs
    [ -f $tmpSlotFile ] && rm -f $tmpSlotFile
}

function CheckRHdiskinfo()
{
    is3008SasCard
    if [ $? -eq 1 ];then
        #处理3416和3408SAS卡
        Check3416SASCard
    else
        #处理其他的SAS卡
        CheckOtherSASCard
    fi
}
function Checksysdiskinfo()
{
    info=`ipmitool fru | grep 'Product Name' 2>&1`
    iRet=$?
    if [ $iRet -ne 0 ]; then
        echo "[ERR]INFO:Failed to execute command(ipmitool fru)." >> ${RESULTFILE} 2>&1
        isPass=1
        return 0
    fi

    #为高密框时
    IsC72Node
    if [ $? -eq 0 ]; then
        RAID=$(cat /proc/mdstat | grep -A1 "active raid1")
        if [ "$RAID" != "" ];then
            find_info=$(echo "${RAID}" |grep "\[2/2\] \[UU\]")
            if [ -z "${find_info}" ]; then
                echo "[ERR]INFO:Software RAID is abnormal." >> ${RESULTFILE} 2>&1
                isPass=1
            else
                echo "SLOT:Software RAID is normal||INOUT:--||PHY:--||POWER:--||LED:--||OS_IN:--||OS_STAT:--||SN:--||VENDOR:--||FW_VER:--||MODEL:--||LINK:--||DISK_SIZE:--||SPEED:--||TEMP(degrees):--||TYPE:--" >> ${RESULTFILE} 2>&1
            fi
        else
            echo "[ERR]INFO:Software RAID hadn't been created." >> ${RESULTFILE} 2>&1
            isPass=1
        fi
        return 0
    fi

    info=`grep SYSTEMDISK /proc/mpt2sas/mpt2sas0-drives 2>&1`
    iRet=$?
    if [ $iRet -ne 0 ]; then
        echo "[ERR]INFO:Failed to get sysdisk info from /proc/mpt2sas/mpt2sas0-drives." >> ${RESULTFILE} 2>&1
        isPass=1
        return 0
    fi
    javaId=`echo "$info"|sed 's/^ *//g'|sed "/^$/d"`
    count=`echo "$info"|wc -l`

    if [ "$count" != "2" ];then
        echo "[ERR]INFO:the number of SystemDisk is not 2." >> ${RESULTFILE} 2>&1
        isPass=1
        LOG "[$LINENO]count=$count"
    fi

    set_ifs
    for line in $javaId; do
        slot=`echo $line|awk '{print $1}'`
        INOUT=`echo $line|awk '{print $2}'`
        PHY=`echo $line|awk '{print $3}'`
        POWER=`echo $line|awk '{print $4}'`
        LED=`echo $line|awk '{print $5}'`
        OS_IN=`echo $line|awk '{print $6}'`
        OS_STAT=`echo $line|awk '{print $7}'`
        SN=`echo $line|awk '{print $8}'`
        VENDOR=`echo $line|awk '{print $9}'`
        FW_VER=`echo $line|awk '{print $10}'`
        MODEL=`echo $line|awk '{print $11}'`
        LINK=`echo $line|awk '{print $12}'`
        DISK_SIZE=`echo $line|awk '{print $13}'`
        SPEED=`echo $line|awk '{print $14}'`
        TEMP=`echo $line|awk '{print $15}'`
        TYPE=`echo $line|awk '{print $16}'`

        #判断状态， LED    OS_IN    OS_STAT
        if  [ "$LED" != "0" ] || [ "$OS_IN" != "1" ] || [ "$OS_STAT" != "running" ];then
            echo "[ERR]INFO:the sysdisk is abnormal in the SLOT($slot)" >> ${RESULTFILE} 2>&1
            isPass=1
        fi

        #状态小写转换成大写
        OS_STAT=`echo $OS_STAT|tr "[:lower:]" "[:upper:]"`
        echo "SLOT:$slot||INOUT:$INOUT||PHY:$PHY||POWER:$POWER||LED:$LED||OS_IN:$OS_IN||OS_STAT:$OS_STAT||SN:$SN||VENDOR:$VENDOR||FW_VER:$FW_VER||MODEL:$MODEL||LINK:$LINK||DISK_SIZE:$DISK_SIZE||SPEED:$SPEED||TEMP(degrees):$TEMP||TYPE:$TYPE" >> ${RESULTFILE} 2>&1
    done
    restore_ifs
}


function checksystemdisk()
{
    LOG "[$LINENO]ProductName=$PRODUCT_NAME"
    IsStandardNode
    if [ $? -eq 0 ];then
        echo "SLOT:not involve(VM)||INOUT:--||PHY:--||POWER:--||LED:--||OS_IN:--||OS_STAT:--||SN:--||VENDOR:--||FW_VER:--||MODEL:--||LINK:--||DISK_SIZE:--||SPEED:--||TEMP(degrees):--||TYPE:--" >> ${RESULTFILE} 2>&1
        LOG "[$LINENO]UVP case, Does not involve"
        return 0
    fi
    IsRHNode
    if [ $? -eq 0 ]; then
        CheckRHdiskinfo
    else
        Checksysdiskinfo
    fi

}

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