#!/bin/bash
set +x

#检查BBU状态
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
#source ${G_INSPECT_MMLPATH}/CheckItems
source /opt/inspect/inspect_map.sh
source ${G_INSPECT_MMLPATH}/CommonFunc.sh
CurInspectNum="153"
CurInspectFun="$( GetInspectType $CurInspectNum )"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

LOG_FILE="/var/log/inspect.log"
source /opt/node_type_recognize.sh

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

function CheckBBUStatus
{
    local isPass=0
    IsTSNode
    if [ $? -ne 0 ];then
        echo "BBUI2CStatus:Do not involve to inspect(NOT TSNode).||BBUBatLifEnd:--||BBUBATPreLifEnd:--||HealthStatus:--||LinkSpaceMisMatch:--||BandWithNoEnough:--" >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        exit 0
    fi

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

    /opt/huawei/snas/script/inspect_mml/ShowInspectMml $LOCAL_BACK_IP 4016 "cm bbubattery show" > /tmp/tmpfilebbuinfo 2>&1
    BBUI2CStatus=$(cat /tmp/tmpfilebbuinfo | grep uiBBUI2CStatus | awk -F'\\|\\|' '{print $1}' | awk '{print $2}')
    BBUBatLifEnd=$(cat /tmp/tmpfilebbuinfo | grep ucBBUBatLifEnd | awk -F'\\|\\|' '{print $3}' | awk '{print $2}')
    BBUBATPreLifEnd=$(cat /tmp/tmpfilebbuinfo | grep ucBBUBATPreLifEnd | awk -F'\\|\\|' '{print $4}' | awk '{print $2}')
    if [ "${BBUI2CStatus}X" != "1X" ] && [ "$(ipmitool raw 0x30 0x93 0xdb 0x07 0x00 0x36 0x8c 0x00 0x01 0x06 0x00 0x00 0xff 0xff 0x75 0x1 0x00 0x03 0x00|awk '{print $9}')X" = "00X" ];then
        BBUI2CStatus=1
    fi
    if [ "${BBUBatLifEnd}X" != "1X" ] && [ "$(ipmitool raw 0x30 0x93 0xdb 0x07 0x00 0x55 0x00 0x01 0xC6 0x00 0x04|awk '{print $6}')X" = "00X" ];then
        BBUBatLifEnd=1
    fi
    if [ "${BBUBATPreLifEnd}X" != "1X" ] && [ "$(ipmitool raw 0x30 0x93 0xdb 0x07 0x00 0x55 0x00 0x01 0xC6 0x00 0x04|awk '{print $7}')X" = "00X" ];then
        BBUBATPreLifEnd=1
    fi
    HealthStatus=$(cat /tmp/tmpfilebbuinfo | grep uiHealthStatus | awk -F'\\|\\|' '{print $1}' | awk '{print $2}')
    LinkSpaceMisMatch=$(cat /tmp/tmpfilebbuinfo | grep LinkSpaceMisMatch | awk -F'\\|\\|' '{print $3}' | awk '{print $2}')
    BandWithNoEnough=$(cat /tmp/tmpfilebbuinfo | grep BandWithNoEnough | awk -F'\\|\\|' '{print $4}' | awk '{print $2}')
    #MML命令获取的值1代表true，0代表false
    if [ "${BBUBATPreLifEnd}X" != "1X" ];then
        isPass=4
        echo "[ERR]INFO:BBU status is abnormal;BBUBATPreLifEnd:$BBUBATPreLifEnd." >>${RESULTFILE} 2>&1
    elif [ "${BBUI2CStatus}X" != "1X" -o "${BBUBatLifEnd}X" != "1X" -o "${HealthStatus}X" != "1X" -o "${LinkSpaceMisMatch}X" != "0X" -o "${BandWithNoEnough}X" != "0X" ]; then
        isPass=1
        echo "[ERR]INFO:BBU status is abnormal;BBUI2CStatus:$BBUI2CStatus,BBUBatLifEnd:$BBUBatLifEnd,BBUBATPreLifEnd:$BBUBATPreLifEnd,HealthStatus:$HealthStatus,LinkSpaceMisMatch:$LinkSpaceMisMatch,BandWithNoEnough:$BandWithNoEnough." >>${RESULTFILE} 2>&1
        LOG "BBU status is abnormal;BBUI2CStatus:$BBUI2CStatus,BBUBatLifEnd:$BBUBatLifEnd,BBUBATPreLifEnd:$BBUBATPreLifEnd,HealthStatus:$HealthStatus,LinkSpaceMisMatch:$LinkSpaceMisMatch,BandWithNoEnough:$BandWithNoEnough."
    fi
    echo "BBUI2CStatus:$BBUI2CStatus||BBUBatLifEnd:$BBUBatLifEnd||BBUBATPreLifEnd:$BBUBATPreLifEnd||HealthStatus:$HealthStatus||LinkSpaceMisMatch:$LinkSpaceMisMatch||BandWithNoEnough:$BandWithNoEnough" >>${RESULTFILE} 2>&1
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
}

CheckBBUStatus

if [ -f /tmp/tmpfilebbuinfo ];then
    rm -f /tmp/tmpfilebbuinfo
fi
exit 0