#!/bin/bash
set +x

LOG_FILE="/var/log/inspect.log"
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
source ${G_INSPECT_MMLPATH}/CommonFunc.sh

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

function getHotPatchVer()
{
    #ȡback_ip
    local iRet=""
    local ver="0"
    local back_ip=$(GetLocalIp)
    if [ -z "${back_ip}" ]; then 
        LOG "[${FUNCNAME}][$LINENO]ERR: local back ip is empty, abort!"
        echo "-1"
        return
    fi

    local patchInfo=$(/opt/huawei/snas/upd/patch_script/cluster_patch.sh "$back_ip" show)
    iRet=$?
    if [[ ${iRet} -ne 0 ]];then
        LOG "[${FUNCNAME}][$LINENO]ERR: Get hot patch version Failed! $patchInfo"
        echo "-1"
        return
    fi

    #Ҫ״̬Ϊconfirm ok
    local patchVersion=$(echo "${patchInfo}" | grep "confirm ok" | grep "Version:" |awk -F, '{print $3}' | awk -F: '{print $2}' 2>/dev/dull)
    patchVersion=${patchVersion:-null}
    if [[ "${patchVersion}" != "null" ]] ;then
        ver=$(echo "${patchVersion}" | grep -Po '(?<=SPH)[0-9]+')
        if [[ "X$ver" == "X" ]] ;then
            LOG "[${FUNCNAME}][$LINENO]ERR: Mtalformed patch name (${patchVersion}) format errord!"
            echo "-1"
            return
        fi
    fi
    echo "$ver"
}

function checkProtocolLogSize()
{
    #дnas.shļ
    local tmpdir=$(mktemp -d /tmp/XXXXXX)
    local nas="${tmpdir}/nas.sh"
    echo "#!/bin/bash" > $nas
    echo "\$*" >> $nas
    echo " " >> $nas
    chmod +x $nas

    local optLogDir=$(sqlite3 /opt/huawei/etc/ccdb/cmr_db_0_0/cmr_db_0 "select value from cmr_tenant_config_pair where key='opt_log_dir'")
    #ȡǰʱ, ж7־Ŀ¼ĴС,40MB,Ѳ첻ͨ
    local daylist=$($nas "ls ${optLogDir}")
    for ((i=0; i< 7; i++))
    do
        local tday=$(date -d "now-${i}day" '+%Y%m%d')
        #Ŀ¼ڵ
        echo $daylist | grep -qw $tday
        if [ $? -eq 0 ];then
            local optLogSize=$($nas "du -ms ${optLogDir}/${tday}" | awk '{print $1}')
            if [ $optLogSize -ge 40 ];then
                LOG "[${FUNCNAME}][$LINENO]ERR: The log size in the ${optLogDir}/${tday} directory is $optLogSize MB !"
                [ -d $tmpdir ] && rm -rf $tmpdir
                echo "1"
                return
            fi
        fi
    done
    echo "0"
    #nasļ
    [ -d $tmpdir ] && rm -rf $tmpdir
}

function checkProtocolLogInfo()
{
    local isPass=0
    local proLogInfo='Off'
    local curInspectNum="329"
    local curInspectFun="$(GetInspectType $curInspectNum)"
    local resultFile="/tmp/tmpResult${curInspectFun}"

    >${resultFile}

    #ǷTSڵ
    local tslist=$(grep -Po '(?<=TSNodeTypeList=\[)[0-9,]+' /opt/node_devicename.py)
    local tsnode=$(sqlite3 /opt/huawei/snas/etc/cm_conf.db "select nid from cm_node_t where nodetype in ($tslist)")

    #x86, sfs, ںԴز
    local productVersion=$(grep ProductVersion /opt/huawei/deploy/package/version |awk -F= '{print $2}' | tr -d ' ')
    if [ "X$tsnode" == "X" ] || [ "$productVersion" != "V500R007C10SPC400" ];then
        echo "Protocol Log:No need check" >> ${resultFile} 2>&1
        echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1
        return 0
    fi

    #armڵ־
    local optLoglv=$(sqlite3 /opt/huawei/etc/ccdb/cmr_db_0_0/cmr_db_0 "select value from cmr_tenant_config_pair where key='opt_log_level'")
    if [ $optLoglv -gt 0 ];then
        #Ȳ汾ڵsph105Ѳͨ,
        proLogInfo="On"
        local patchVer=$(getHotPatchVer)
        if [ $patchVer -eq -1 ];then
            isPass=$(check_pass $isPass 1)
            echo "[ERR]INFO:Failed to obtain the node patch version." >> ${resultFile} 2>&1
        elif [ $patchVer -lt 105 ];then
            #readwrite򿪣Ѳ첻ͨ 
            let "var_wite=(optLoglv&0x02)>>1"
            let "var_read=(optLoglv&0x04)>>2"
            # 7־140MB
            local flag=$(checkProtocolLogSize)
            if [ $var_read -eq 1 ] || [ $var_wite -eq 1 ] || [ $flag -eq 1 ];then
                isPass=$(check_pass $isPass 1)
                echo "[ERR]INFO:Upgrade to the 7.1.1.SPH105 or later." >> ${resultFile} 2>&1
                LOG "[${FUNCNAME}][$LINENO]ERR: opt_log_level is: $optLoglv; opt_log_dir flag is:$flag !"
            fi 
        fi
    fi

    echo "Protocol Log:$proLogInfo" >> ${resultFile} 2>&1
    echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1

    return 0
}

checkProtocolLogInfo
