#!/bin/bash
set +x
source /opt/huawei/snas/script/inspect_mml/CheckItems
source /opt/huawei/snas/script/inspect_mml/CommonFunc.sh
source /opt/huawei/snas/script/inspect_mml/CommonNodeTypeRecognize.sh

CurInspectNum="130"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
PHYRESULTFILE="/tmp/tmpfilecheckNodeDiskPhy"

isPass=0
>${RESULTFILE}

function checkDiskPhy()
{
    local nodetype=0
    local hssd=0
    local old=0
    declare -a arrphy
    local resulttmp="HSSD-D322XAM:--||chip_181101:--||phy28_status:--||phy29_status:--||phy30_status:--||phy31_status:--||phy32_status:--||phy33_status:--||phy34_status:--||phy35_status:--"
    #C72节点
    nodetype=$(cat  /opt/huawei/deploy/etc/deploy.ini | grep devicetype |awk -F= '{print $2}')
    if [ "${nodetype}" -eq 4 ]; then
        echo "NodeType:C72||${resulttmp}" >>  ${RESULTFILE}  2>&1
        echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1
        return
    fi

    IsRHNode
    if [ $? -eq 0 ];then
        echo "NodeType:RH||${resulttmp}" >>  ${RESULTFILE}  2>&1
        echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1
        return
    fi
    IsTSNode
    if [ $? -eq 0 ];then
        echo "NodeType:TaiShan||${resulttmp}" >>  ${RESULTFILE}  2>&1
        echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1
        return
    fi

    local Phyinfo=$(/opt/huawei/snas/script/inspect_mml/CheckPhyInfo)
    iRet=$?
    if [ $iRet -ne 0 ]; then
        LOG "[$FUNCNAME]||Error: ${iRet}"
        isPass=1
        ErrInfoCommon "INFO:Failed to execute command.(/opt/product/snas/script/inspect_mml/CheckPhyInfo)" "${RESULTFILE}"
        echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1
        return
    fi

    #在此sleep是为了保证所有节点上面命令都执行成功，规避一个奇怪的问题（有些高密框环境会造成该脚本执行中断，导致巡检失败），此处sleep不能删除
    sleep 30

    dos2unix $PHYRESULTFILE

    cat $PHYRESULTFILE >> $LOG_FILE 2>&1

    sgnum=$(cat $PHYRESULTFILE |grep "sginfo" |awk -F= '{print $2}')
    if [ "$sgnum" -eq 1 ];then
        isPass=1
        LOG "[$FUNCNAME]: can't get phy info in this node."
        ErrInfoCommon "INFO:Can't get phy information from command(lsscsi -g |grep enclosu) in this node." "${RESULTFILE}"
        echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1
        return
    fi

    local errorInfo=$(cat $PHYRESULTFILE |grep "[ERR]")
    if [ "X${errorInfo}" != "X" ];then
        isPass=1
        LOG "[$FUNCNAME]disktool:${errorInfo}"
        echo "${errorInfo}" >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1
        return
    fi

    arrphy[28]=$(cat $PHYRESULTFILE |grep phy28 |awk -F= '{print $2}')
    arrphy[29]=$(cat $PHYRESULTFILE |grep phy29 |awk -F= '{print $2}')
    arrphy[30]=$(cat $PHYRESULTFILE |grep phy30 |awk -F= '{print $2}')
    arrphy[31]=$(cat $PHYRESULTFILE |grep phy31 |awk -F= '{print $2}')
    arrphy[32]=$(cat $PHYRESULTFILE |grep phy32 |awk -F= '{print $2}')
    arrphy[33]=$(cat $PHYRESULTFILE |grep phy33 |awk -F= '{print $2}')
    arrphy[34]=$(cat $PHYRESULTFILE |grep phy34 |awk -F= '{print $2}')
    arrphy[35]=$(cat $PHYRESULTFILE |grep phy35 |awk -F= '{print $2}')

    local chip_181101=""
    local HSSD_D322XAM=""
    local hssd=$(cat $PHYRESULTFILE |grep "havehssd" |awk -F= '{print $2}')
    if [ "$hssd" -eq 1 ];then
        local old=$(cat $PHYRESULTFILE |grep "haveold" |awk -F= '{print $2}')
        if [ "$old" -eq 1 ];then
            chip_181101="No"
        else
            chip_181101="Yes"
        fi
        HSSD_D322XAM="Yes"
    else
        HSSD_D322XAM="NO"
        chip_181101="--"
    fi

    Result="NodeType:T3000||HSSD-D322XAM:${HSSD_D322XAM}||chip_181101:${chip_181101}||phy28_status:${arrphy[28]}||phy29_status:${arrphy[29]}||phy30_status:${arrphy[30]}||phy31_status:${arrphy[31]}||phy32_status:${arrphy[32]}||phy33_status:${arrphy[33]}||phy34_status:${arrphy[34]}||phy35_status:${arrphy[35]}"
    echo "<![CDATA[$Result]]>" >>${RESULTFILE} 2>&1

    local inspectres=$(cat $PHYRESULTFILE |grep "inspecttype" |awk -F= '{print $2}')
    if [ "$inspectres" -eq 1 ]; then
        isPass=4
        ErrInfoCommon "INFO:Node PHY status is abnormal." "${RESULTFILE}"
    fi
    echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1

}

checkDiskPhy

