#!/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
source $G_MML_FILE_PATH/CommonFunc.sh
CurInspectNum="251"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
LOCAL_BACK_IP=""
isPass=0
>${RESULTFILE}

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


function checkTLVBlackList()
{
    local service_type=0
    local Result=""
    local status=""
    local iRet=0
    local info1=""

    LOCAL_BACK_IP=`GetLocalIp`
    if [ "${LOCAL_BACK_IP}X" == "X" ];then
        echo "[ERR]Node:$(hostname) ||Error:Can't get backend IP." >> ${RESULTFILE} 2>&1
        LOG "[ERR]Node:$(hostname) ||Error:Can't get backend IP."
        isPass=1
        return
    fi

    service_type=$(get_service_type_from_db "${LOCAL_BACK_IP}")
    if [ "${service_type}" != "1" ];then
        LOG "service_type = ${service_type}.skip ${LOCAL_BACK_IP}."
        Result="Does not involve to inspect."
        echo "Node:$LOCAL_BACK_IP||INFO:$Result" >> ${RESULTFILE} 2>&1
        return
    fi

    status=$($G_MML_FILE_PATH/ShowInspectMml $LOCAL_BACK_IP 988 'nvshow black' 2>&1)
    iRet=$?
    if [ $iRet -ne 0 ]; then
        LOG "[$FUNCNAME] Node: $LOCAL_BACK_IP||Error: ${iRet}"
        echo "[ERR]Node:$LOCAL_BACK_IP||Error:Failed to execute command(MmlBatch 988 'nvshow black')." >> ${RESULTFILE} 2>&1
        isPass=1
        return
    fi
    if [ "${status}" = "" ]; then
        return
    fi
    LOG "${status}"

    set_ifs
    info1=$(echo "${status}" |grep -A1 g_astNv_MetaBlack)
    if [ -z "${info1}" ]; then
        LOG "[$FUNCNAME] Node: $LOCAL_BACK_IP||Error: can not get key word g_astNv_MetaBlack"
        echo "[ERR]Node:$LOCAL_BACK_IP||Error:Can't get key word g_astNv_MetaBlack." >> ${RESULTFILE} 2>&1
        isPass=1
    fi

    info2=$(echo "${info1}" |grep -v g_astNv_MetaBlack)
    if [ -z "${info2}" ]; then #巡检通过
        echo "Node:$LOCAL_BACK_IP||INFO:There are no metadata black lists in the node." >> ${RESULTFILE} 2>&1
    else                       #巡检不通过
        LOG "[$FUNCNAME] Node: $LOCAL_BACK_IP||Error: There are some metadata black lists in the node."
        echo "[ERR]Node:$LOCAL_BACK_IP||Error:There are some metadata black lists in the node." >> ${RESULTFILE} 2>&1
        isPass=1
    fi
    restore_ifs
}

checkTLVBlackList
echo "${CurInspectFun}_Pass $isPass" >>${RESULTFILE} 2>&1

