#!/bin/bash

#检查所有节点
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
. $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="317"
CurInspectFun=`GetInspectType $CurInspectNum`
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>$RESULTFILE

isPass=0

function main()
{
    local ca_d_confFile="/opt/huawei/snas/etc/ca_d.ini"
    local mds_c_confFile="/opt/huawei/snas/etc/mds_c.ini"
    local snas_confFile="/opt/huawei/snas/etc/snas.ini"
    source $G_INSPECT_MMLPATH/inspect_comm.sh
    LOCAL_BACK_IP=$(GetLocalIp)
    if [ ! -f "${mds_c_confFile}" ];then
        echo "[ERR]INFO:The file ${mds_c_confFile} does not exist." >>${RESULTFILE} 2>&1
        isPass=1
        quota_empty_status="no"
    else
        #quota_empty 为1表示状态为开启非空目录配额
        quota_empty_num=$( grep qm_not_empty_dir_cfg_quota_switch ${mds_c_confFile} | awk -F"=" '{print $2}' | sed 's/ //g' )
        if [ "X1" == "X${quota_empty_num}" ];then
            quota_empty_status="yes"
        else
            quota_empty_status="no"
        fi 
    fi

    if [ ! -f "${ca_d_confFile}" ];then
        echo "[ERR]INFO:The file ${ca_d_confFile} does not exist." >>${RESULTFILE} 2>&1
        isPass=1
        container_status="no"
    else
        #ctnr_policy 为0就是没打开container2.0,为1或2为打开
        container_num=$( grep ctnr_policy ${ca_d_confFile} | awk -F"=" '{print $2}' | sed 's/ //g' )
        if [ "X1" == "X${container_num}" -o "X2" == "X${container_num}" ];then
            container_status="yes"
        else
            container_status="no"
        fi
    fi
    if [ ! -f "${snas_confFile}" ];then
        echo "[ERR]INFO:The file(snas.ini) does not exist." >>${RESULTFILE} 2>&1
        isPass=1
        shutdownEarTemp="no"
        shutdownCpuTemp="no"
        shutdownNvdimmTemp="no"
    else
        #检查本节点是否配置shutdownEarTemp shutdownCpuTemp shutdownNvdimmTemp
        shutdownEarTempInfo=$( grep -w shutdownEarTemp ${snas_confFile} | sed 's/ //g' )
        shutdownCpuTempInfo=$( grep -w shutdownCpuTemp ${snas_confFile} | sed 's/ //g' )
        shutdownNvdimmTempInfo=$( grep -w shutdownNvdimmTemp ${snas_confFile} | sed 's/ //g' )
        if [ "X" != "X${shutdownEarTempInfo}" ];then
            shutdownEarTemp="yes"
        else
            shutdownEarTemp="no"
        fi
        if [ "X" != "X${shutdownCpuTempInfo}" ];then
            shutdownCpuTemp="yes"
        else
            shutdownCpuTemp="no"
        fi
        if [ "X" != "X${shutdownNvdimmTempInfo}" ];then
            shutdownNvdimmTemp="yes"
        else
            shutdownNvdimmTemp="no"
        fi
    fi
    local GangHeaderSwitch="no"
    local HotPatchVersion=$(ls /var/huawei/patch/cur/sph/)
    HotPatchVersion=${HotPatchVersion:-null}
    if [[ "X$HotPatchVersion" == "Xnull" ]] ;then
        isPass=$(check_pass ${isPass} 1)
        echo "[ERR]INFO:Please install hot patch OceanStor_9000_V300R006C00SPH207 or later. And then set the switch GangHeaderSgl207=1 in nofs.ini" >>${RESULTFILE} 2>&1
    else
        local ver1=$(echo "${HotPatchVersion}" | grep -Po '(?<=SPH)[0-9]+')
        local checkVer="207"
        if [ $ver1 -lt $checkVer ];then
            isPass=$(check_pass ${isPass} 1)
            echo "[ERR]INFO:Please install hot patch OceanStor_9000_V300R006C00SPH207 or later. And then set the switch GangHeaderSgl207=1 in nofs.ini" >>${RESULTFILE} 2>&1
        else
            cat /opt/huawei/snas/etc/nofs.ini | grep -wq "GangHeaderSgl207=1"
            if [ $? -eq 0 ];then
                GangHeaderSwitch="yes"
            else
                isPass=$(check_pass ${isPass} 1)
                echo "[ERR]INFO:HotPatch version $HotPatchVersion. Please set the switch GangHeaderSgl207=1 in nofs.ini" >>${RESULTFILE} 2>&1
            fi
        fi
    fi
    echo "Node:${LOCAL_BACK_IP}||quota_empty:${quota_empty_status}||container2.0:${container_status}||shutdownEarTemp:${shutdownEarTemp}||shutdownCpuTemp:${shutdownCpuTemp}||shutdownNvdimmTemp:${shutdownNvdimmTemp}||GangHeaderSgl207:${GangHeaderSwitch}" >>${RESULTFILE} 2>&1

}

main
echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1
exit 0
