#!/bin/bash

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source ${G_INSPECT_MMLPATH}/CheckItems
CurInspectNum="298"
CurInspectFun="$( GetInspectType ${CurInspectNum} )"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
LOG_FILE="/var/log/inspect.log"
>${RESULTFILE}

#执行命令: MmlBatch 988 "fc show cfg"|grep fc_switch_disk_disp |awk -F"|" '{print $6}'|tr -d " "
#结果为0,则巡检通过---直接结束
#结果不为0 ,巡检不通过
#当结果不为0 ,并且节点版本是V300R006C00SPC100 时,进行下述规避手段
 #      a) MmlBatch 988 "fc set cfg 4 0"   #关闭流控
  #      b)MmlBatch988 "fc show cfg" | grep fc_switch_disk_disp|awk -F"|" '{print $6}'|tr -d " "
#查看是否更改成功..如果不为0 ,上报一个错误提示
   #     c)sed -i 's/fc_switch_disk_disp=.*/fc_switch_disk_disp=0/g' /opt/huawei/snas/static/base.ini  #持久化
    #          cat /opt/huawei/snas/static/base.ini |grep fc_switch_disk_disp #查看是否持久化成功,如果不为0,上报一个错误提示

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

function main
{
    local ProductVersion="$( grep ProductVersion /opt/huawei/deploy/package/version|awk -F= '{print $2}'|tr -d " " )"   
    local NeedControlVersion="V300R006C00SPC100"
    
    if [ "${ProductVersion}" != "${NeedControlVersion}" -a "X${ProductVersion}" != "X" ];then
        echo "INFO:THE Version ${ProductVersion} does not needed to be checked." >>${RESULTFILE}  2>&1
        echo "${CurInspectFun}_Pass 0" >>${RESULTFILE} 2>&1
        exit 0
    fi
    for i in $(seq 1 3);do
        FluidControlFlag="$( /usr/local/bin/MmlBatch 988 "fc show cfg"|grep fc_switch_disk_disp |awk -F"|" '{print $6}'|tr -d " " )"
        if [ "${FluidControlFlag}""X" != "X" ];then
            break
        fi
        if [ "3" == "$i" ];then
            LOG "[$FUNCTION][$LINENO]the command get information failed."
            echo "the command get the FluidControl Flag information failed." >>${RESULTFILE} 2>&1 
	    echo "${CurInspectFun}_Pass 1" >>${RESULTFILE} 2>&1
            exit 0
        fi
    done
    
    if [ ${FluidControlFlag} -eq 0 ];then
        echo "INFO:THE FluidControl Flag is 0." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass 0" >>${RESULTFILE} 2>&1
        exit 0
    else
        echo "INFO:THE FluidControl Flag is 1." >>${RESULTFILE} 2>&1
        #V300R006C00SPC100版本强制关流控
        #关闭流控
        /usr/local/bin/MmlBatch 988 "fc set cfg 4 0" >/dev/null 2>&1
        if [ $? -ne 0 ];then
            LOG "[$FUNCTION][$LINENO]THE FluidControl close is failure" 
        fi
        local Flag="$( /usr/local/bin/MmlBatch 988 "fc show cfg"|grep fc_switch_disk_disp |awk -F"|" '{print $6}'|tr -d " " )"
        if [ ${Flag} -ne 0 ];then
            echo "INFO:THE FluidControl close is failure" >>${RESULTFILE} 2>&1
        fi
        #持久化
        sed -i 's/fc_switch_disk_disp=.*/fc_switch_disk_disp=0/g' /opt/huawei/snas/static/base.ini >/dev/null 2>&1
        if [ $? -ne 0 ];then
            LOG "[$FUNCTION][$LINENO]THE FluidControl close is failure" 
        fi
        local fileFlag="$( grep -w "fc_switch_disk_disp" /opt/huawei/snas/static/base.ini|awk -F= '{print $2}'|tr -d " " )"
        if [ ${Flag} -ne 0 ];then
            echo "INFO:THE FluidControl close is failure" >>${RESULTFILE} 2>&1
        fi 
        echo "${CurInspectFun}_Pass 1" >>${RESULTFILE} 2>&1
    fi
}

main


