#!/bin/bash
set +x
LOG_FILE="/var/log/inspect.log"
G_MML_FILE_PATH="/opt/huawei/snas/script/inspect_mml"
source $G_MML_FILE_PATH/CheckItems
CurInspectNum="305"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="${G_TMP_INSPECT_PATH}tmpResult${CurInspectFun}"
>${RESULTFILE}
nofsspace_monitor_tool_version=""
function LOG
{
    time=$(date)
    echo [${time}][$$][$CurInspectFun]$@ >> $LOG_FILE
}

function checkNodeSMRConfig()
{
    local timer
    
    activeness_switch=$( cat /opt/huawei/snas/etc/ca_d.ini 2>/dev/null | grep activeness_switch | awk -F=  '{print $2}' | sed -e "s/^ *//g" -e "s/ $//g"  )
    
    timer=$( cat /sys/module/snas_dfs/parameters/g_uiFetchSmrActivenessTimer )
    
    SMR_THRESHOLD_=$(cat /opt/huawei/snas/etc/ca_d.ini | grep SMR_THRESHOLD_  | awk -F=  '{print $2}' | sed -e "s/^ *//g" -e "s/ $//g" )

    # һڵǷsmrڵ㣬ڵڵ
    /opt/huawei/snas/sbin/nofs-snas dumpzoneleft |grep -v "NAME" |grep -w "^P.*"  >/dev/null
    isSmrNode=$?
    if [ "X${isSmrNode}" == X"0" ]; then
        isSmrNode=Yes
    else
        isSmrNode=No
    fi

    SmrFcSwitch=$( cat /opt/huawei/snas/etc/nofs.ini | grep SmrFcSwitch | awk -F= '{print $2}' )
    if [ "X${SmrFcSwitch}" == "X" ]; then
        SmrFcSwitch="-"
    fi
    # ǷcrontabԴȷǷװSMR
    cat /etc/crontab | grep nas_check_nofs_space_alarm.sh >/dev/null
    ret=$?
    nofsspace_monitor_tool_version=""
    # δöʱ񣬱ʾδװ
    if [ ${ret} -ne 0 ]; then 
        nofsspace_monitor_tool_version="Not_Installed"
    else
        GetSmrToolVersion
    fi
    nodepoolName=$( grep ^nodepool  /opt/huawei/snas/etc/snas.ini 2>/dev/null | awk -F=  '{print $2}' | sed -e "s/^ *//g" -e "s/ $//g" )
    # ȫֱ
    g_ullClearZoneDst="--"
    mml_result=$( MmlBatch 988 "trns show globvalue g_ullClearZoneDst 0 U64" | grep Value )
    if [ "X${mml_result}" != "X" ]; then
        g_ullClearZoneDst=$( echo ${mml_result} | awk -F'Value' '{print $2}'|awk -F'(' '{print $2}' |awk -F')' '{print $1}' )
    fi

    # Ƭ
    DefragEnable=1
    defrag_result=$( /opt/huawei/snas/sbin/nofs-snas printnofsconfigure | grep DefragEnable )
    if [ "X${defrag_result}" != "X" ]; then
        DefragEnable=$( echo ${defrag_result} | awk -F'= ' '{print $2}'|awk -F' ' '{print $1}' )
    fi
    confstr="activeness_switch=${activeness_switch} g_uiFetchSmrActivenessTimer=${timer} SMR_THRESHOLD_=${SMR_THRESHOLD_} isSmrNode=${isSmrNode} nodepoolName=${nodepoolName} ${nofsspace_monitor_tool_version} ${SmrFcSwitch} ${g_ullClearZoneDst} ${DefragEnable}"
    echo "${confstr}" >>${RESULTFILE} 2>&1

    LOG "[$LINENO]${confstr}"
}

function GetSmrToolVersion()
{
    recoverUsage=$( cat /opt/huawei/snas/bin/nas_check_nofs_space_alarm.sh | grep recoverUsage )
    if [ "${recoverUsage}X" != "X" ]; then
        # 鵽recoverUsagerecoverUsage5.0еֶ
        nofsspace_monitor_tool_version="5.0"
    else
        # ݰװĹߵmd5УֵжϰװSMR߰汾
        md5res=$( md5sum /opt/huawei/snas/bin/nas_check_nofs_space_alarm.sh | awk '{print $1}' )
        if [ "${md5res}" == "e844f3448476f3f517547dd17b5946d2" ]; then # 4.0
            nofsspace_monitor_tool_version="4.0"
        elif [ "${md5res}" == "e7455b1191dd874a9a838e010555a401" ];then # 3.0
            nofsspace_monitor_tool_version="3.0"
        elif [ "${md5res}" == "b023fc11d8140c8a5249196bc5c9e2db" ];then # 2.0
            nofsspace_monitor_tool_version="2.0"
        else
            nofsspace_monitor_tool_version="Version_Unknown"
        fi
    fi
}

checkNodeSMRConfig
