#!/bin/bash
set +x

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="342"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="${G_TMP_INSPECT_PATH}tmpResult${CurInspectFun}"
>${RESULTFILE}

isPass=0
LOGFILE="/var/log/inspect.log"

function LOG()
{
    echo [${time}][$$][$CurInspectFun]$@ >> $LOGFILE
}

function checkNofsSpaceContinuityTxgOnePool()
{
    local poolname="${1}"
    local obsfile=""
    local nofs_api_file="/tmp/nofs_api_file"
    local nofs_api_txg_file="/tmp/nofs_api_txg_file"
    local nofs_api_meta_file="/tmp/nofs_api_meta_file"
    # ʶƬͳnofsռڴ⣬10μ鵽̿ռռڴ1000MѲ콨Ż

    file_size=$(du -sc /var/log/snas_workload* |tail -n1 | awk '{print $1}')
    if [ -z ${file_size} ];then
        LOG "[$LINENO]failed to du snas_workload."
        echo "[ERR]INFO:failed to du snas_workload." >>${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 1)
        return
    fi
    if [ ${file_size} -ge 102400 ];then
        LOG "[$LINENO]failed to open snas_workload:the size of the snas_workload is exceeds 100 MB."
        echo "[ERR]INFO:failed to open snas_workload:the size of the snas_workload is exceeds 100 MB." >>${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 1)
        return
    fi
    ChunkMem=$( cat /var/log/snas_workload* | Workload NOFS_CACHE chunkmem | tail -n10 |awk '{print $2}' | sed 's/:/ /g' | awk '{sum+=$2}END{print sum/NR}' | awk -F. '{print $1}' )
    if [ X"${ChunkMem}" != X"" ] && [ "${ChunkMem}" -ge 1000 ]; then
        echo "[ERR]INFO:The memory occupied by disk space allocation(${ChunkMem}) is greater than or equal to 1000 MB for 10 consecutive times." >> ${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 4)
    fi


    # ʶ̿ռǷ̿ռ䱶ֵ20000/hǷвgang飬ȡ̽һСʱʱӺƽʱӵƽֵ
    obsfile=$( ls /var/log/backup/DFS_OBS/DFS_OBS.log.* | tail -1 )
    if [ -z ${obsfile} ];then
        LOG "[$LINENO]failed to get DFS_OBS.log."
        echo "[ERR]INFO:failed to get DFS_OBS.log." >>${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 1)
        return
    fi
    file_size=$( du -s ${obsfile}  | awk '{print $1}' )
    if [ -z ${file_size} ];then
        LOG "[$LINENO]failed to du DFS_OBS.log."
        echo "[ERR]INFO:failed to du DFS_OBS.log." >>${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 1)
        return
    fi
    if [ ${file_size} -ge 102400 ];then
        LOG "[$LINENO]failed to open ${obsfile}:the size of the ${obsfile} is exceeds 100 MB."
        echo "[ERR]INFO:failed to open ${obsfile}:the size of the ${obsfile} is exceeds 100 MB." >>${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 1)
        return
    fi

    contain_snasmessages=$( tar -tf ${obsfile} |grep snasmessages )
    if [ -z "${contain_snasmessages}" ]; then
        MapActivedA="0"
        MaxTxgSyncTime="0"
        AvgTxgSyncTime="0"
        WritegangCnt="0"
        ReadgangCnt="0"
        isPass=$(check_pass ${isPass} 4)
        LOG "[$LINENO]DFS_OBS.log not contain snasmessages"
        echo "[ERR]INFO:DFS_OBS.log not contain snasmessages." >>${RESULTFILE} 2>&1
    else
        MapActivedA=$( zcat ${obsfile} | grep -a MAP_ActivedA | awk '{if($5>=$7)print}' | wc -l )
        zcat ${obsfile} | grep -a NOFS_PrintApiStatPoolInfo >${nofs_api_file}
        cat ${nofs_api_file} | grep -a txg | grep -aw ${poolname} >${nofs_api_txg_file}
        cat ${nofs_api_file} | grep -a meta >${nofs_api_meta_file}
        MaxTxgSyncTime=$( cat ${nofs_api_txg_file} |sed 's/\// /g' | awk '{sum+=$6+$10}END{if(NR>0)print int(sum/NR)}' )
        AvgTxgSyncTime=$( cat ${nofs_api_txg_file} |sed 's/\// /g' | awk '{sum+=$7+$11}END{if(NR>0)print int(sum/NR)}' )
        WritegangCnt=$( cat ${nofs_api_meta_file} | awk -F 'frag ' '{print $2}' | awk -F ['/',' '] '{sum=sum+$3+$4}END{print sum}' )
        ReadgangCnt=$( cat ${nofs_api_meta_file} | awk -F 'frag ' '{print $2}' | awk -F ['/',' '] '{sum=sum+$7+$8}END{print sum}' )
    fi



    # ̿ռ䱶ֵ20000/hѲ콨Ż
    if [ X"${MapActivedA}" != X"" ] && [ "${MapActivedA}" -ge 20000 ]; then
        echo "[ERR]INFO:The number of records(${MapActivedA}) whose disk space allocation multiple does not meet the threshold exceeds 20000/h.record file(${obsfile})" >> ${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 4)
    fi
    # ʱӴﵽ5sѲ첻ͨ
    if [ X"${MaxTxgSyncTime}" != X"" ] && [ "${MaxTxgSyncTime}" -ge 5000000 ]; then
        echo "[ERR]INFO:The MaxTxgSyncTime of ${poolname}(${MaxTxgSyncTime})exceeds 5s.record file(${obsfile})" >> ${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 1)
    elif [ X"${MaxTxgSyncTime}" == X"" ]; then
        MaxTxgSyncTime="0"
    fi
    # ƽʱӴﵽ2sѲ첻ͨ
    if [ X"${AvgTxgSyncTime}" != X"" ] && [ "${AvgTxgSyncTime}" -ge 2000000 ]; then
        echo "[ERR]INFO:The AvgTxgSyncTime of ${poolname}(${AvgTxgSyncTime})exceeds 2s.record file(${obsfile})" >> ${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 1)
    elif [ X"${AvgTxgSyncTime}" == X"" ]; then
        AvgTxgSyncTime="0"
    fi
    # ڲдgangѲ첻ͨ
    if [ X"${WritegangCnt}" != X"" ] && [ "${WritegangCnt}" -gt 0 ]; then
        echo "[ERR]INFO:The WritegangCnt is not 0.record file(${obsfile})" >> ${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 1)
    elif [ X"${WritegangCnt}" == X"" ]; then
        WritegangCnt="0"
    fi
    # ڲgangѲ첻ͨ
    if [ X"${ReadgangCnt}" != X"" ] && [ "${ReadgangCnt}" -gt 0 ]; then
        echo "[ERR]INFO:The ReadgangCnt is not 0.record file(${obsfile})" >> ${RESULTFILE} 2>&1
        isPass=$(check_pass ${isPass} 1)
    elif [ X"${ReadgangCnt}" == X"" ]; then
        ReadgangCnt="0"
    fi

    local ToolFilePath="/opt/huawei/snas/sbin/NOFS_DiskSpaceAllocAdaptor.sh"
    local ToolCfgFile="/var/log/NOFS_SPACEALLOC_ADAPTOR/.nofs_spacealloc_adaptor.cfg"
    local ToolStat=-1
    if [ -f ${ToolFilePath} ]; then
        ToolStat=0
        if [ -f ${ToolCfgFile} ]; then
            ToolStat=$(cat ${ToolCfgFile} | grep -a SWITCH_STATE | awk -F'=' '{print $2}')
        fi    
    fi

    local BigBlockStat=0
    local NofsCfgFile="/opt/huawei/snas/etc/nofs.ini"
    grep -qaw BigBlock ${NofsCfgFile}
    if [ $? -eq 0 ]; then
        BigBlockStat=1
    fi

    echo "ChunkMem:${ChunkMem}||MapActivedA:${MapActivedA}||WritegangCnt:${WritegangCnt}||ReadgangCnt:${ReadgangCnt}||PoolName:${poolname}||TxgMax:${MaxTxgSyncTime}||TxgAvg:${AvgTxgSyncTime}||ToolStat:${ToolStat}||BigBlockStat:${BigBlockStat}" >> ${RESULTFILE} 2>&1

    [ -f "${nofs_api_file}" ] && rm -f ${nofs_api_file}
    [ -f "${nofs_api_txg_file}" ] && rm -f ${nofs_api_txg_file}
    [ -f "${nofs_api_meta_file}" ] && rm -f ${nofs_api_meta_file}
    return
}

function checkDiskNofsSpaceContinuity()
{
    local listpool=${G_TMP_INSPECT_PATH}tmplistpool_CheckDiskNofsSpaceContinuity
    local getpoolinfo=${G_TMP_INSPECT_PATH}tmpgetpoolinfo_CheckDiskNofsSpaceContinuity
    local pname=""
    local total_cap=""
    local left_cap=""
    local used_cap=""
    local cap_percent
    local other_info=""
    local state=""
    local iret=""

    #  ÿڵѡȡߵ̣з
    /opt/huawei/snas/sbin/nofs-snas listpool 2>/dev/null |grep ^P | sort -r -nk 5 >${listpool}
    if [ ! -s "${listpool}" ];then
        LOG "listpool result is empty."
        echo "[ERR]INFO:/opt/huawei/snas/sbin/nofs-snas listpool result is empty." >>${RESULTFILE} 2>&1
        isPass=1

        [ -f "${listpool}" ] && rm -f ${listpool}
        return
    fi

    /opt/huawei/snas/sbin/nofs-snas getpoolinfo >${getpoolinfo} 2>/dev/null
    iret=$?
    if [ ${iret} -ne 0 -o ! -s "${getpoolinfo}" ];then
        LOG "getpoolinfo result is empty,iret is:${iret}"
        echo "[ERR]INFO:/opt/huawei/snas/sbin/nofs-snas getpoolinfo result is empty." >>${RESULTFILE} 2>&1
        isPass=1

        [ -f "${listpool}" ] && rm -f ${listpool}
        [ -f "${getpoolinfo}" ] && rm -f ${getpoolinfo}
        return
    fi

    while read pname total_cap left_cap used_cap cap_percent other_info
    do
        if [ X"${pname}" == X"" ]
        then
            continue
        fi

        # ѡзĴ״̬ҪΪ0
        grep "${pname} *State:0 " ${getpoolinfo} >/dev/null 2>&1
        if [ $? -eq 0 ]
        then
            POOLNAME="${pname}"
            CAP_PERCENT="${cap_percent}"
            break
        fi
    done <${listpool}

    if [ X"${POOLNAME}" == X"" -o X"${CAP_PERCENT}" == X"" ]
    then
        LOG "there is no normal(state:0) pool in getpoolinfo result."
        echo "[ERR]INFO:there is no normal(state:0) pool in getpoolinfo result." >>${RESULTFILE} 2>&1
        isPass=1

        [ -f "${listpool}" ] && rm -f ${listpool}
        [ -f "${getpoolinfo}" ] && rm -f ${getpoolinfo}
        return
    fi

    checkNofsSpaceContinuityTxgOnePool ${POOLNAME}

    [ -f "${listpool}" ] && rm -f ${listpool}
    [ -f "${getpoolinfo}" ] && rm -f ${getpoolinfo}
    return
}

checkDiskNofsSpaceContinuity
echo "${CurInspectFun}_Pass ${isPass}" >> ${RESULTFILE} 2>&1

exit 0