#!/bin/sh
set +x

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

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

# 7.x 海量小文件32K，默认和媒资是1024K或者512K，其他都是512K
function CheckStripeCfg()
{
    local isPass=0
    local productVersion=$(grep ProductVersion /opt/huawei/deploy/package/version |awk -F= '{print $2}')
    local stripeSize="---"
    local share_path=""
    #获取节点业务类型、存储类型
    local serviceType=$(cat /opt/huawei/snas/etc/snas.ini  | grep "^serviceType=" | awk -F'=' '{print $2}')
    if [ "${serviceType}" = "other" ];then
        serviceType="default"
    fi
    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
        share_path="node_service_type=${node_service_type}. Does not involve"
        LOG "servicetype : ${serviceType}.node_service_type=${node_service_type}.Does not involve"
    elif [ "${serviceType}" = "surveillance" ] || [ "${serviceType}" = "media" ] || [ "${serviceType}" = "archive" ] || [ "${serviceType}" = "default" ];then
        checkDir="/mnt/fs/share/"
        tmpdir="/tmp/CheckStripeCfg"
        [ -d ${tmpdir} ] && rm -rf ${tmpdir}
        mkdir -p ${tmpdir}
        nas="${tmpdir}/nas.sh"
        echo "#!/bin/bash" > $nas
        echo "\$*" >> $nas
        echo " " >> $nas
        chmod +x $nas
        local my_nid=$(grep "^NID=" "/opt/huawei/snas/etc/cm.ini"| awk -F= '{print $2}')
        local backip=$(get_node_backnet_ip ${my_nid})
        local Version=$(/opt/huawei/snas/upd/patch_script/cluster_patch.sh "$backip" show| grep "Version:" |awk -F, '{print $3}' | awk -F: '{print $2}' 2>/dev/dull)
        Version=${Version:-null}
        local ver2=$(echo "${Version}" | grep -Po '(?<=SPH)[0-9]+')
        local Size=(16KB 128KB 32KB 256KB 512KB 1024KB)
        tmp_path="$($nas find ${checkDir} -maxdepth 1 -type d -print | grep -v ".fs_sysoplog" | awk -F"/" '{print $NF}' | shuf -n50 | sort -r | tr "\n" " " 2>&1)"
        for path in ${tmp_path};do
            local Inode=$($nas stat /mnt/fs/share/${path} | grep -Po '(?<=Inode: )[0-9]+')
            [ "X${Inode}" = "X" ] && continue
            stripeSize=$(/usr/local/bin/MmlBatch 988 "mds xcache show ${Inode}" | grep -w Anchor | awk -F"," '{print $3}' | sed "s/[^0-9]//g")
            [ "X${stripeSize}" = "X" ] && continue
            if [ "${serviceType}" = "media" ] || [ "${serviceType}" = "default" ];then
                #0-16k 1-128k 2-32k 3-256k 4-512k 5-1024k
                if [ "X${stripeSize}" != "X4" ] && [ "X${stripeSize}" != "X5" ];then
                    isPass=1
                    echo "[ERR]sharePath:/${path}||servicetype:${serviceType}||StripSize:${Size[${stripeSize}]}||Default_Value:${Size[4]} ${Size[5]}" >> ${RESULTFILE} 2>&1
                elif [ "X${stripeSize}" = "X5" ] && [[ ! "$productVersion" =~ "V100R006C10SPC" ]];then
                    local ver1="0"
                    if [ "$productVersion" == "V500R007C10SPC400" ];then
                        isArm=$(arch)
                        if [ "${isArm}" = "x86_64" ];then   #ARM 和 X86 解决此问题的补丁不一样
                            ver1="111"
                        else
                            ver1="109"
                        fi
                    elif [ "$productVersion" == "V100R006C30SPC900" ];then
                        ver1="906"
                    fi
                    if [[ ${ver1} -gt ${ver2:-0} ]];then  #没打补丁或者补丁版本低于要求 7.1.1.SPH109 (arm) 7.1.1.SPH111(x86) V100R006C30SPH906
                        isPass=$(check_pass $isPass 4)
                        echo "[ERR]sharePath:/${path}||servicetype:${serviceType}||StripSize:${Size[${stripeSize}]}||Default_Value:The strip size may be risky.Please install the latest hot patch." >> ${RESULTFILE} 2>&1
                    fi
                fi
            elif [ "${serviceType}" = "surveillance" ];then
                if [ "X${stripeSize}" != "X4" ];then
                    isPass=1
                    echo "[ERR]sharePath:/${path}||servicetype:${serviceType}||StripSize:${Size[${stripeSize}]}||Default_Value:${Size[4]}" >> ${RESULTFILE} 2>&1
                fi
            elif [ "${serviceType}" = "archive" ];then
                if [ "X${stripeSize}" != "X2" ];then
                    isPass=1
                    echo "[ERR]sharePath:/${path}||servicetype:${serviceType}||StripSize:${Size[${stripeSize}]}||Default_Value:${Size[2]}" >> ${RESULTFILE} 2>&1
                fi
            fi
            share_path="/${path} ${share_path}"
        done
        rm -rf ${tmpdir}
    else
        share_path="Does not involve"
    fi
    echo "servicetype:${serviceType}||sharePath:${share_path:---}" >>${RESULTFILE} 2>&1
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
}

CheckStripeCfg
exit 0
