#!/bin/bash
set +x
G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml"
source $G_MML_FILE_PATH/CheckItems
CurInspectNum="148"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
LOG_FILE="/var/log/inspect.log"
>${RESULTFILE}
isPass=0

tmpfiledumpzoneleft="/tmp/tmpdumpzoneleft"
tmpfilepoolinfo="/tmp/tmppoolinfo"
tmpfileClusterInfo="/tmp/tmpClusterInfo"

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

function main()
{
    local LeftCapacity=""
    local Cluster_Capacity=""
    local Cluster_Used_Capacity=""
    local nodeCnt=0
    local obsLogFile=""
    local limit=""
    local checktime="--"
    /opt/huawei/snas/sbin/nofs-snas dumpzoneleft |grep -v "NAME" |grep -w "^P.*" > $tmpfiledumpzoneleft 2>&1
    if [ $? -ne 0 ];then
        echo "ID:There is no SMR disk in the node.||Slot:--||ZoneLeft:--||consume:--||checktime:--||start:--||end:--||RandomZoneTotalAlloc(MB):--"  >>${RESULTFILE}
        LOG "[$LINENO]excute command fail"
        return
    fi

    /opt/huawei/snas/sbin/nofs-snas getpoolinfo > $tmpfilepoolinfo 2>&1
    if [ $? -ne 0 ];then
       sleep 1
       /opt/huawei/snas/sbin/nofs-snas getpoolinfo > $tmpfilepoolinfo 2>&1
       iret=$?
       if [ ${iret} -ne 0 ];then
            echo "[ERR]INFO:Failed to get pool information from command(/opt/product/snas/sbin/nofs-snas getpoolinfo)"  >>${RESULTFILE}
            isPass=1
            LOG "[$LINENO]iret=$iret,get poolinfo fail"
            return
       fi
    fi
    local smrCnt=$( cat $tmpfiledumpzoneleft | wc -l )
    nodeCnt=$( cat /proc/monc_dsmap | grep -w "IpAddr" |wc -l )
    /usr/local/bin/MmlBatch 4016 "cm system" >${tmpfileClusterInfo} 2>&1
    if [ $? -ne 0 ];then
        isPass=1
        Info=$( cat ${tmpfileClusterInfo} )
        LOG "[FUNCNAME] mml_result : $Info"
        echo "[ERR]INFO:Failed to excute MML command(cm system),port(4016).return(${iRet})."
    else
        dos2unix ${tmpfileClusterInfo} >/dev/null 2>&1
        content=$( cat ${tmpfileClusterInfo} )
        if [ "$content" == "" ];then
            isPass=1
            echo "[ERR]INFO:Failed to get the infomation form mml commmand(cm system)."
        else
            Cluster_Capacity=$( cat ${tmpfileClusterInfo} | grep "Cluster_Capacity"|awk -F: '{print $2}' | sed 's/[^0-9]//g' )
            Cluster_Used_Capacity=$( cat ${tmpfileClusterInfo} | grep "Cluster_Used_Capacity"|awk -F: '{print $2}' | sed 's/[^0-9]//g' )
            LeftCapacity=$((Cluster_Capacity-Cluster_Used_Capacity))
        fi
    fi
    if [ $nodeCnt -ne 0 ] && [ "X${LeftCapacity}" != "X" ];then
        #整集群剩余容量MB * 0.99 / 256MB / 节点个数 / 本节点SMR盘个数 = 平均值 ----参考值比平均值低20%（即平均值80%）
        limit=$( echo ""|awk -v x=${LeftCapacity} -v y=${nodeCnt} -v z=${smrCnt} '{printf("%0.0f",x*0.99*0.8/256/y/z)}' )
    fi

    obsLogFile=$(ls -lrt /var/log/backup/DFS_OBS/DFS_OBS.log.* | tail -1 |awk '{print $NF}')  #取backup下最新的转储日志
    if [ -f "${obsLogFile}" ];then
        zgrep -an __GC_FreeZoneResetLocked ${obsLogFile} |grep -av snasmessages > /var/log/__GC_FreeZoneResetLocked.log
        for file in $(/opt/huawei/snas/sbin/nofs-snas listpool|grep -v NAME | grep -w "^P.*" | awk '{print $1}');do 
            grep -anw $file /var/log/__GC_FreeZoneResetLocked.log |grep -a left > /var/log/${file}.smr.zone
        done
        checktime=$(echo "${obsLogFile}" |awk '{print $NF}' | awk -F"." '{print $3}')
    else
        echo "[ERR]INFO:There is no DFS_OBS.log file in /var/log/backup/.Please do inspect later." >>${RESULTFILE}
    fi
    while read line; do
        local startzone="--"
        local endzone="--"
        local rst="--"
        poolId=$( echo $line |awk '{print $1}' )
        slot=$( cat $tmpfilepoolinfo |grep -w "$poolId"|awk '{print $4}'|awk -F":" '{print $2}' )
        zonecount=$( echo $line |awk '{printf "%d",$2}' )
        if [ $zonecount -eq 0 ];then
            LOG "[$LINENO]line =[$line]"
        fi

        #leftzone低于8000才进行后续检查，否则
        if [ ${zonecount} -ge 8000 ]; then
            echo "ID:$poolId||Slot:$slot||ZoneLeft:$zonecount||consume:--||checktime:$checktime||start:--||end:--||RandomZoneTotalAlloc(MB):--" >>${RESULTFILE}
            continue
        fi

        if [ "X$limit" != "X" ] && [[ $zonecount -lt $limit ]]; then
            #zoneleft低于平均值80%
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:ID($poolId),Slot:($slot),ZoneLeft($zonecount).The remaining capacity may be uneven.(less than $limit)" >>${RESULTFILE}
        fi
        if [ $zonecount -lt 3000 ]; then
            isPass=$(check_pass ${isPass} 1)
            echo "[ERR]INFO:ID($poolId),Slot:($slot),ZoneLeft($zonecount).less than 3000." >>${RESULTFILE}
        elif [ $zonecount -lt 6000 ]; then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:ID($poolId),Slot:($slot),ZoneLeft($zonecount).less than 6000." >>${RESULTFILE}
        fi
        if [ "X${obsLogFile}" != "X" ] && [ -f "/var/log/${poolId}.smr.zone" ] && [ "X$(cat /var/log/${poolId}.smr.zone)" != "X" ];then
            startzone=$( cat /var/log/${poolId}.smr.zone |head -n1 |awk -F "left:" '{print $2}' |awk -F "]" '{print $1}' )
            endzone=$( cat /var/log/${poolId}.smr.zone |tail -n1 |awk -F "left:" '{print $2}' |awk -F "]" '{print $1}' )
            LOG "[$LINENO] ${poolId}.smr.zone start $startzone end $endzone!(${obsLogFile})"
            rst=$[startzone - endzone];
            if [[ $rst -ge 100 ]];then
                isPass=$(check_pass ${isPass} 4)
                echo "[ERR]INFO:${poolId}.smr.zone consume $rst in one hour(leftZone:$zonecount) start $startzone end $endzone(checktime:$checktime)! May consume too quickly." >>${RESULTFILE}
            else
                if [[ $zonecount -lt 5000 ]] && [[ $rst -ge 20 ]];then
                    isPass=$(check_pass ${isPass} 4)
                    echo "[ERR]INFO:${poolId}.smr.zone consume $rst in one hour(leftZone:$zonecount) start $startzone end $endzone(checktime:$checktime)!" >>${RESULTFILE}
                else
                    LOG "[OK]INFO:${poolId}.smr.zone consume $rst in one hour(leftZone:$zonecount checktime:$checktime)."
                fi
            fi
            rm /var/log/${poolId}.smr.zone
        fi
        #检查SMR盘随机区占用率 大于70%不通过，[60%-70)建议优化，小于60%则通过       
        tmpfile1=`/opt/huawei/snas/sbin/nofs-snas listdiskmemberinfo $poolId`
        tmpfile2=`echo $tmpfile1 | awk -F 'file' '{print $2}'`
        cat $tmpfile2 > /tmp/tmpAlloc$poolId.listmember
        # C20/7.0.1/7.0.2smr随机区使用率算法 grep出meta为1的内容，size累加为smr盘随机取总大小，totalsize累加算的是smr随机取剩余的大小  (size - totalsize) /size 
        # /tmp/tmpAlloc$poolId.listmember格式大致为
        # pool  meta  idx  start  size  metacnt  datacnt  nodenum  maxsize  totalsize  weight  loadtxg  unloadtxg  synctxg  pass  valid
        # P1    0     0    8888   8888  0        0         0       0        778       
        # P1    0     0    8888   9999  0        0         0       0        0
        # P1    1     0    8888   9999  0        0         0       0        330
        poolTotalSize=$(cat /tmp/tmpAlloc${poolId}.listmember | grep -aw $poolId | grep -v pool | awk '{if ($2 == 1) print $0}' | awk '{sum+=$5}END{print sum}')
        poolLeftSize=$(cat /tmp/tmpAlloc${poolId}.listmember | grep -aw $poolId | grep -v pool | awk '{if ($2 == 1) print $0}' | awk '{sum+=$10}END{print sum}')
        poolUsedSize=$[${poolTotalSize} - ${poolLeftSize}]
        Rate=$(echo ""|awk -v x=${poolUsedSize} -v y=${poolTotalSize} '{printf("%0.2f",x*100/y)}')
        if [ $(echo "$Rate >= 70" | bc) -eq 1 ];then
            isPass=$(check_pass ${isPass} 1)
            echo "[ERR]INFO:${poolId} random zone usage more than 70% " >>${RESULTFILE}
        elif [ $(echo "$Rate >= 60" | bc) -eq 1 ];then
            isPass=$(check_pass ${isPass} 4)
            echo "[ERR]INFO:${poolId} random zone usage between 60%-70%  " >>${RESULTFILE}
        fi
        poolUsedSize=$( echo "scale=2;${poolUsedSize}/1024/1024"|bc )
        LOG "ID:$poolId||Slot:$slot||RandomZoneTotalAlloc(MB):$poolUsedSize."
        echo "ID:$poolId||Slot:$slot||ZoneLeft:$zonecount||consume:$rst||checktime:$checktime||start:$startzone||end:$endzone||RandomZoneTotalAlloc(MB):$poolUsedSize" >>${RESULTFILE}
    done < $tmpfiledumpzoneleft

    rm -f /tmp/tmpAlloc*
    rm -f /var/tmp/nofs_pool_listmemberinfo*
}

main
echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE}

if [ -f $tmpfilepoolinfo ];then
    rm $tmpfilepoolinfo
    LOG "[$LINENO] rm $tmpfilepoolinfo"
fi

if [ -f $tmpfiledumpzoneleft ];then
    rm $tmpfiledumpzoneleft
    LOG "[$LINENO] rm $tmpfiledumpzoneleft"
fi

if [ -f $tmpfileClusterInfo ];then
    rm $tmpfileClusterInfo
    LOG "[$LINENO] rm $tmpfileClusterInfo"
fi

if [ -f /var/log/__GC_FreeZoneResetLocked.log ];then
    rm /var/log/__GC_FreeZoneResetLocked.log
    LOG "[$LINENO] rm /var/log/__GC_FreeZoneResetLocked.log"
fi

exit 0