#!/bin/bash
set +x

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

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

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

function checkTxgOnePool()
{
    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"

    # 识别磁盘空间是否分配过慢（磁盘空间分配倍数不满足阈值的条数超过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
        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
        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
    # 最大事务时延达到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
    # 平均事务时延达到1s，巡检不通过
    if [ X"${AvgTxgSyncTime}" != X"" ] && [ "${AvgTxgSyncTime}" -ge 1000000 ]; then
        echo "[ERR]INFO:The AvgTxgSyncTime of ${poolname}(${AvgTxgSyncTime})exceeds 1s.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

    echo "WritegangCnt:${WritegangCnt}||ReadgangCnt:${ReadgangCnt}||PoolName:${poolname}||TxgMax:${MaxTxgSyncTime}||TxgAvg:${AvgTxgSyncTime}" >> ${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=$( check_pass $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=$( check_pass $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=$( check_pass $isPass 1 )

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

    checkTxgOnePool ${POOLNAME}

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

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