#!/bin/sh
G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml"
source $G_MML_FILE_PATH/CheckItems
CurInspectNum="333"
CurInspectFun="CheckXinfoXattr"
LOG_FILE="/var/log/inspect.log"

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

function CheckXinfoXattr()
{
    local isPass=0
    local allPath=""
    local result="Pass"
    local Xattr=""
    local Xinfo=""
    local checkDir="/mnt/fs/share/"
    local tmpdir="/tmp/CheckXinfoPos"
    local tmpfile="/tmp/CheckXinfoPos/fidInfo"
    #获取节点存储类型
    local node_service_type=$(cat /opt/huawei/snas/etc/snas.ini | grep "^node_service_type=" | awk -F'=' '{print $2}')
    if [ "${node_service_type}" != "1" ];then
        result="The node is not DFS node. Does not involve."
        LOG "INFO: Do not involve to inspect.node_service_type=${node_service_type}."
    else
        [ -d ${tmpdir} ] && rm -rf ${tmpdir}
        mkdir -p ${tmpdir}
        nas="${tmpdir}/nas.sh"
        echo "#!/bin/bash" > $nas
        echo "\$*" >> $nas
        echo " " >> $nas
        chmod +x $nas
        >${tmpfile}
        local startTime=$(date +%s)
        local timeLimit=$((startTime + 60*12))  #一级目录如果过多巡检超时，这里限定12分钟，为查完的就不查了
        local unCheckPath=""
        $nas ls ${checkDir} -lhi | grep -v "fs_sysoplog" |grep -v "^total" > ${tmpfile}
        while read line; do
            path=$(echo $line | awk '{print $NF}')
            $nas [ -f ${checkDir}$path ] && continue
            endTime=$(date +%s)
            if [ ${endTime} -gt ${timeLimit} ];then
                unCheckPath="/${path} ${unCheckPath}"
                continue
            fi
            Inode=$(echo $line | awk '{print $1}')
            [ "X${Inode}" = "X" ] && continue
            /usr/local/bin/MmlBatch 988 "mds inode show  ${Inode}" > ${tmpdir}/tmpinfo
            XinfoPos=$(cat ${tmpdir}/tmpinfo | grep -Poa '(?<=XinfoPos\()[0-9]+')   # 结果为3表示外置
            XattrPos=$(cat ${tmpdir}/tmpinfo | grep -Poa '(?<=HaveFragOrChunk\()[0-9]+')  # 结果为2表示外置
            if [ X"${XinfoPos}" = "X3" ];then
                isPass=$(check_pass ${isPass} 4)
                Xinfo="/${path} ${Xinfo}"
            fi
            if [ X"${XattrPos}" = "X2" ];then
                isPass=$(check_pass ${isPass} 4)
                Xattr="/${path} ${Xattr}"
            fi
            allPath="/${path} ${allPath}"
            LOG "[$FUNCNAME] /${path} XinfoPos(${XinfoPos}) HaveFragOrChunk(${XattrPos})"
        done <${tmpfile}
        LOG "[$FUNCNAME] allPath= ${allPath}"
        if [ ${isPass} -ne 0 ];then
            result="Fail"
            echo "[ERR]Node:$back_ip||INFO: The directory is external. Xattr(${Xattr}) Xinfo(${Xinfo})"
        fi
        if [ "x${unCheckPath}" != "x" ];then
            isPass=4
            result="Fail"
            echo "[ERR]Node:$back_ip||INFO: The directory check is not complete.(${unCheckPath})"
        fi
        rm -rf ${tmpdir}
    fi
    echo "Result:${result}"
}

CheckXinfoXattr
exit 0
