#!/bin/bash
set +x

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="339"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}
isPass=0
LOGFILE="/var/log/inspect.log"
POOLNAME=""
CAP_PERCENT=""
FilepathLog=""
RECYCLE_CNT=""

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

function set_ifs
{
    IFS_OLD=$IFS
    IFS=$'\n'
}

function restore_ifs
{
    IFS=$IFS_OLD
}

function readdirOnePoolInfo()
{
    local poolname="${1}"
    local pool_readdir_file=""
    local pool_recycle_file=""
    local pool_filepathLog_cnt=""
    local result=""
    local ret=""

    # nofsȡ̵ĸĿ¼
    result=$( /opt/huawei/snas/sbin/nofs-snas readdir ${poolname} / 2>/dev/null )
    ret=$?

    pool_readdir_file=$( echo "${result}" | awk -F "[()]" '{print $2}' )
    if [ ${ret} -ne 0 -o X"${pool_readdir_file}" == X"" -o ! -f "${pool_readdir_file}" ]
    then
        LOG "failed to excute /opt/huawei/snas/sbin/nofs-snas readdir ${poolname}.iret:${ret}"
        echo "[ERR]||INFO:failed to excute /opt/huawei/snas/sbin/nofs-snas readdir ${poolname}." >>${RESULTFILE} 2>&1
        isPass=1
        [ -f "${pool_readdir_file}" ] && rm -f ${pool_readdir_file}

        return
    fi

    # ͳƵǰnlun
    relacnt=$( cat "${pool_readdir_file}" | grep -aw relation | awk '{print $3}' )
    if [ "X${relacnt}" == "X" ]; then
        relacnt=0
    fi
    MaxObjectsCount=$( cat "${pool_readdir_file}" | grep "-" | sort -r -nk 3 | head -1 | awk '{print $3}' )
    tmpnlunid=$( cat "${pool_readdir_file}" | grep "-" | sort -r -nk 3 | head -1 | awk '{print $1}' )
    # nlun30WͬʱС800WѲ콨Ż
    if [ X"${MaxObjectsCount}" != X"" -a ${MaxObjectsCount} -le 8000000 -a ${MaxObjectsCount} -gt 300000 ]
    then
        echo "[ERR]||INFO:the number of objects owned by nlun(${tmpnlunid}) on the disk($poolname) reaches ${MaxObjectsCount}.Optimization is recommended." >> ${RESULTFILE} 2>&1
        isPass=4
    # nlun800WѲ첻ͨ
    elif [ X"${MaxObjectsCount}" != X"" -a ${MaxObjectsCount} -gt 8000000 ]; then
        echo "[ERR]||INFO:the number of objects owned by nlun(${tmpnlunid}) on the disk($poolname) reaches ${MaxObjectsCount}.Failed to pass the inspection." >> ${RESULTFILE} 2>&1
        isPass=1
    fi

    # ͳFilepathLogĸ  1-->漰2[0, 50000)-->ͨ;3[50000, 100000)-->Ż;4[100000, ++)-->ͨ
    pool_filepathLog_cnt=$( grep FilepathLog ${pool_readdir_file} 2>/dev/null | awk '{print $3}' | grep -o "[0-9]"* )
    if [ X"${pool_filepathLog_cnt}" == X"" ]; then
        pool_filepathLog_cnt="0"

    elif [ ${pool_filepathLog_cnt} -ge 50000 -a ${pool_filepathLog_cnt} -lt 100000 ]; then
        isPass=$( check_pass $isPass 4 )        
        echo "[ERR]||INFO:pool(${POOLNAME}),FilepathLog count is ${pool_filepathLog_cnt}." >>${RESULTFILE} 2>&1

    elif [ ${pool_filepathLog_cnt} -ge 100000 ]; then
        isPass=$( check_pass $isPass 1 )

        echo "[ERR]||INFO:pool(${POOLNAME}),FilepathLog count is ${pool_filepathLog_cnt}." >>${RESULTFILE} 2>&1

    fi

    FilepathLog=${pool_filepathLog_cnt}
    
    # ͳ/recycleĿ¼µĿ¼ 
    # 170%£Ѳͨ
    # 2[75%, 80%Ϊ0Ż    # todo 70--75գд˰
    # 380%(>=80),С15Ż
    # 4Ѳ첻ͨ

    RECYCLE_CNT=$( cat "${pool_readdir_file}" | grep -w recycle | awk '{print $3}' )
    if [ X"${RECYCLE_CNT}" == X"" ]; then
        RECYCLE_CNT="0"
    fi



    int_cap_percent=$( echo "$( echo ${CAP_PERCENT} | sed 's/\%//g' ) * 100" | bc | awk -F. '{print $1}' )
    if [ ${RECYCLE_CNT} -ne 0 ]
    then
        if [ ${int_cap_percent} -ge 7000 -a ${int_cap_percent} -lt 8000 ]
        then
            isPass=$( check_pass $isPass 4 )
            LOG "pool:${POOLNAME},capacity usage:${CAP_PERCENT},recycle dirs numbers is:${RECYCLE_CNT}."
            echo "[ERR]||INFO:pool(${POOLNAME}),capacity usage(${CAP_PERCENT}),recycle dirs numbers is(${RECYCLE_CNT})." >>${RESULTFILE} 2>&1
        
        elif [ ${int_cap_percent} -ge 8000 -a ${RECYCLE_CNT} -lt 15 ]
        then
            isPass=$( check_pass $isPass 4 )
            LOG "pool:${POOLNAME},capacity usage:${CAP_PERCENT},recycle dirs numbers is:${RECYCLE_CNT}."
            echo "[ERR]||INFO:pool(${POOLNAME}),capacity usage(${CAP_PERCENT}),recycle dirs numbers is(${RECYCLE_CNT})." >>${RESULTFILE} 2>&1
        
        elif [ ${int_cap_percent} -gt 8000 -a ${RECYCLE_CNT} -ge 15 ]
        then
            isPass=$( check_pass $isPass 1 )
            LOG "pool:${POOLNAME},capacity usage:${CAP_PERCENT},recycle dirs numbers is:${RECYCLE_CNT}."
            echo "[ERR]||INFO:pool(${POOLNAME}),capacity usage(${CAP_PERCENT}),recycle dirs numbers is(${RECYCLE_CNT})." >>${RESULTFILE} 2>&1
        fi
    fi

    if [ ${relacnt} -gt 1800000 ]; then
        isPass=$( check_pass $isPass 1 )
        LOG "relationcnt:${relacnt}."
        echo "[ERR]INFO: RelationCount(${relacnt}). is greater than 1800000." >>${RESULTFILE} 2>&1
    fi


    # նӸߵ򣬷ǰ30NLUNµ/logļ
    local head30nlun_file=/tmp/tmphead30nlun
    cat ${pool_readdir_file} |grep -v snaptrack |grep -v removetrack |grep -v recycle_dir |grep -v recycle |grep -v FilepathLog |grep -v space |grep -v quota|grep -v relation|sort -r -nk 3 |head -n 30 >${head30nlun_file}
    if [ ! -f "${head30nlun_file}" -o ! -s "${head30nlun_file}" ]
    then
        isPass=$( check_pass $isPass 1 )
        LOG "failed to get nlunid from readdir ${poolname} /"
        echo "[ERR]||INFO:failed to get nlunid from readdir ${poolname} /" >>${RESULTFILE} 2>&1
        [ -f "${pool_readdir_file}" ] && rm -f ${pool_readdir_file}

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

    local nlunid=""
    local other=""
    local count=""
    local nlun_log_file=""
    local nlun_handoff_file=""
    local tmpnlun_log_handoff_cnt_file="/tmp/tmpnlun_log_handoff_cnt_file"
    local nlun_log_cnt=""
    local nlun_handoff_cnt=""
    >${tmpnlun_log_handoff_cnt_file}
    while read nlunid other count
    do
        nlun_log_cnt=""
        result=$( /opt/huawei/snas/sbin/nofs-snas readdir ${poolname} ${nlunid}/log plus 2>/dev/null )
        ret=$?
        if [ ${ret} -ne 0 ]; then
            nlun_log_cnt="--"
            if [ ${ret} -ne 254 ]; then
                LOG "failed to exucute /opt/huawei/snas/sbin/nofs-snas readdir ${poolname} ${nlunid}/log plus,iret:${ret}."
            fi
        fi

        # ͳnlunid/logļ
        nlun_log_file=$( echo "${result}" | awk -F "[()]" '{print $2}' )
        if [ ${ret} -eq 0 ]; then
            if [ X"${nlun_log_file}" == X"" -o ! -f "${nlun_log_file}" ]; then
                nlun_log_cnt="--"
                LOG "failed to get file by exucute /opt/huawei/snas/sbin/nofs-snas readdir ${poolname} ${nlunid}/log plus."
            fi
        fi
        
        if [ "${nlun_log_cnt}" != "--" ]; then
            nlun_log_cnt=$( wc -l ${nlun_log_file} | awk '{print $1}' )
        fi

        nlun_handoff_cnt=""
        result=$( /opt/huawei/snas/sbin/nofs-snas readdir ${poolname} ${nlunid}/handoff plus 2>/dev/null )
        ret=$?
        if [ ${ret} -ne 0 ]; then
            nlun_handoff_cnt="--"
            if [ ${ret} -ne 254 ]; then
                LOG "failed to exucute /opt/huawei/snas/sbin/nofs-snas readdir ${poolname} ${nlunid}/handoff plus,iret:${ret}."
            fi
        fi

        # ͳnlunid/handoffļ
        nlun_handoff_file=$( echo "${result}" | awk -F "[()]" '{print $2}' )
        if [ ${ret} -eq 0 ]; then
            if [ X"${nlun_handoff_file}" == X"" -o ! -f "${nlun_handoff_file}" ]; then
                nlun_handoff_cnt="--"
                LOG "failed to get file by exucute /opt/huawei/snas/sbin/nofs-snas readdir ${poolname} ${nlunid}/handoff plus."
            fi
        fi

        if [ "${nlun_handoff_cnt}" != "--" ]; then
            nlun_handoff_cnt=$( wc -l ${nlun_handoff_file} | awk '{print $1}' )
        fi

        echo "$nlunid ${nlun_log_cnt} ${nlun_handoff_cnt}" >>${tmpnlun_log_handoff_cnt_file}
        [ -f "${nlun_log_file}" ] && rm -f "${nlun_log_file}"
        [ -f "${nlun_handoff_file}" ] && rm -f "${nlun_handoff_file}"
    done <${head30nlun_file}

    [ -f "${head30nlun_file}" ] && rm -f "${head30nlun_file}"
    # nlunidlog
    sort -rn -k2 ${tmpnlun_log_handoff_cnt_file} -o ${tmpnlun_log_handoff_cnt_file}
    local nlun_num=1
    local log_cnt=""
    local handoff_cnt=""
    local exist_nlun_list=""
    while read nlunid log_cnt handoff_cnt
    do
        if [ ${nlun_num} -gt 3 ]; then
            break
        fi

        echo "POOLNAME:${POOLNAME}||CAP_PERCENT:${CAP_PERCENT}||FilepathLog:${FilepathLog}||RECYCLE_CNT:${RECYCLE_CNT}||NLUN:${nlunid}||NLUN_LOG_CNT:${log_cnt}||NLUN_HANDOFF_CNT:${handoff_cnt}||RELATION_CNT:${relacnt}" >>${RESULTFILE} 2>&1
        nlun_num=$((nlun_num+1))
        exist_nlun_list="${exist_nlun_list} ${nlunid}"
    done <${tmpnlun_log_handoff_cnt_file}

     # nlunidhandoff
    sort -rn -k3 ${tmpnlun_log_handoff_cnt_file} -o ${tmpnlun_log_handoff_cnt_file}
    nlun_num=1
    while read nlunid log_cnt handoff_cnt
    do
        if [ ${nlun_num} -gt 3 ]; then
            break
        fi

        # logʱͬʱhandoffûбҪͬ
        echo ${exist_nlun_list} | grep -w ${nlunid} >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            nlun_num=$((nlun_num+1))
            continue
        fi

        echo "POOLNAME:${POOLNAME}||CAP_PERCENT:${CAP_PERCENT}||FilepathLog:${FilepathLog}||RECYCLE_CNT:${RECYCLE_CNT}||NLUN:${nlunid}||NLUN_LOG_CNT:${log_cnt}||NLUN_HANDOFF_CNT:${handoff_cnt}||RELATION_CNT:${relacnt}" >>${RESULTFILE} 2>&1
        nlun_num=$((nlun_num+1))
    done <${tmpnlun_log_handoff_cnt_file}

    [ -f "${tmpnlun_log_handoff_cnt_file}" ] && rm -f ${tmpnlun_log_handoff_cnt_file}
    return
}

function checkConcomitantLogs()
{
    local listpool=/tmp/tmplistpool
    local getpoolinfo=/tmp/tmpgetpoolinfo
    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

    readdirOnePoolInfo ${POOLNAME}

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

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