#!/bin/bash
set +x
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
[ -f ${G_INSPECT_MMLPATH}/CheckItems ] && source ${G_INSPECT_MMLPATH}/CheckItems

function CheckRouteConfigured()
{
    local iRet=0
    local isPass=0
    local dfscloud="no"
    local routecfg="no"
    local dfs_conf="/mnt/fs/system/cm/aumtenent.ini"
    local rule_cfg="/opt/huawei/snas/script/snas_routeCfg.sh"
    local tmpdir="/tmp/CheckMgtRouteConfigured"
    local curInspectNum="326"
    local curInspectFun="$(GetInspectType $curInspectNum)"
    local resultFile="/tmp/tmpResult${curInspectFun}"
    [ -f ${resultFile} ] && rm ${resultFile}
    >${resultFile}

    #否配置了走管理的路由 #/opt/huawei/snas/etc/cm_conf.db, CM_IPRULE_CONFIG_T
    # C00 C20没有CM_IPRULE_CONFIG_T表 在/opt/huawei/snas/script/snas_routeCfg.sh中
    # cat /opt/huawei/snas/script/snas_routeCfg.sh |grep "om1" |grep "ip rule add to" | grep "/32""
    #检查已配置的路由是否存在
    local ip_rule_db=$(cat ${rule_cfg} | grep "rule add to" | grep -vE "net1|net4|net5" | sort -u)
    local ip_rule_cmd=$(ip rule show; ip -6 rule show)
    local routecfgtmp=""
    set_ifs
    for rule in $ip_rule_db;do
        routecfg="yes"
        local ip_tmp=""
        local subnet_tmp=""

        #区别IPv6
        echo $rule | grep ":" >/dev/null 2>&1
        if [ $? -eq 0 ];then
            ip_tmp=$(echo $rule | awk '{print $6}')
            subnet_tmp=$(echo $rule | awk '{print $10}' | tr -d ';')
        else
            ip_tmp=$(echo $rule | awk '{print $5}')
            subnet_tmp=$(echo $rule | awk '{print $9}' | tr -d ';')
        fi

        #特殊处理32或128位的ip
        echo $ip_tmp | grep -E "/32|/128" >/dev/null 2>&1
        iRet=$?
        if [ $iRet -eq 0 ];then
            ip_tmp1=$(echo $ip_tmp | awk -F'/' '{print  $1}')
        else
            ip_tmp1="${ip_tmp}"
        fi

        #32位掩码,管理网段,排除IPV6 32位的地址.
        echo $ip_tmp | grep -v : | grep "/32" >/dev/null 2>&1
        if [ $? -eq 0 ] && [ "${subnet_tmp}" == "om1" ];then
            routecfgtmp="yes" 
        fi
        #
        echo "$ip_rule_cmd" | grep -w $subnet_tmp | grep -w $ip_tmp1 >/dev/null 2>&1
        if [ $? -eq 1 ];then
            isPass=$(check_pass $isPass 1)
            echo "[ERR]INFO:Route $ip_tmp does not exist in the ip rule show command." >> ${resultFile} 2>&1
        fi
    done
    restore_ifs

    mkdir -p ${tmpdir}
    local nas="${tmpdir}/nas.sh"
    echo "#!/bin/bash" > $nas
    echo "\$*" >> $nas
    echo " " >> $nas
    chmod +x $nas

    ##define CM_DFSCLOUD_CONFIG_FILE  "/mnt/fs/system/cm/aumtenent.ini"
    #检查dfscloud
    iam_cfg=$($nas "cat ${dfs_conf}" 2>&1 | grep -a "IAM_" )
    [ -d ${tmpdir} ] && rm -rf ${tmpdir}
    if [ "X${iam_cfg}" != "X" ] && [ "${routecfgtmp}" == "yes" ];then
        dfscloud="yes"
        isPass=$(check_pass $isPass 4)
        echo "[ERR]INFO:The host route configuration of the management network segment of the node is risky. Please handle it." >> ${resultFile} 2>&1
    fi

    echo "dfscloud config:${dfscloud}||route config:${routecfg}" >> ${resultFile}  2>&1
    echo "${curInspectFun}_Pass ${isPass}" >>${resultFile} 2>&1

}

CheckRouteConfigured
#总是返回成功，以[ERR]标识是否有误
exit 0

