#!/bin/bash
set +x

#ipǷǰ,,ipmiͬһ
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source ${G_INSPECT_MMLPATH}/CheckItems
CurInspectNum="354"
CurInspectFun="$( GetInspectType $CurInspectNum )"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

LOG_FILE="/var/log/inspect.log"
cm_conf_file="/opt/huawei/snas/etc/cm.ini"
cm_db_file="/opt/huawei/snas/etc/cm_conf.db"
NetConfig="/etc/sysconfig/network-scripts/ifcfg-"
my_nid=$( cat "${cm_conf_file}" | grep "^NID=" | awk -F= '{print $2}' )
source /opt/node_type_recognize.sh
source ${G_INSPECT_MMLPATH}/CommonFunc.sh


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

function IsMasterNode()
{
    /usr/local/bin/MmlBatch 4016 "cm master" >/tmp/backnet_cm_master_info 2>/dev/null
    dos2unix /tmp/backnet_cm_master_info >/dev/null 2>&1
    local master_nodename=$( grep "Node Name" /tmp/backnet_cm_master_info |awk -F ':' '{print $2}' |tr -d ' '| dos2unix |dos2unix )
    local local_nodename=$(hostname)
    rm -rf /tmp/backnet_cm_master_info
    if [ "${master_nodename}" == "${local_nodename}" ];then
        return 0
    else
        return 1
    fi
}

#0ipv4,1Ǵipv6,2
function checkiptype()
{
    local ip=$1
    local pattern_ipv4="^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]?[0-9])"
    local pattern_ipv6="^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^:((:[0-9a-fA-F]{1,4}){1,6}|:)$|^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,5}|:)$|^([0-9a-fA-F]{1,4}:){2}((:[0-9a-fA-F]{1,4}){1,4}|:)$|^([0-9a-fA-F]{1,4}:){3}((:[0-9a-fA-F]{1,4}){1,3}|:)$|^([0-9a-fA-F]{1,4}:){4}((:[0-9a-fA-F]{1,4}){1,2}|:)$|^([0-9a-fA-F]{1,4}:){5}:([0-9a-fA-F]{1,4})?$|^([0-9a-fA-F]{1,4}:){6}:$"
    if [[ "${ip}" =~ ${pattern_ipv4} ]];then
        echo "0"
    elif [[ "${ip}" =~ ${pattern_ipv6} ]];then
        echo "1"
    else
        echo "2"

    fi
}
function get_ipv6_subnet()
{
    local ipv6=$1
    local PREFIX=$2
    local ipv6_mask="16"
    local ipv6_mask_binary="1111111111111111"
    local subnet=""
    local ipv6_mask_quotient
    ipv6_mask_quotient=$(( PREFIX / ipv6_mask ))
    ret0=$?
    local ipv6_mask_remainder
    ipv6_mask_remainder=$(( PREFIX % ipv6_mask ))
    ret1=$?
    LOG "[$FUNCNAME][${LINENO}]ipv6=$1, PREFIX=$2, ret0=${ret0}, ret1=${ret1}, ipv6_mask_quotient=${ipv6_mask_quotient}, ipv6_mask_remainder=${ipv6_mask_remainder}."
    if [ "${ret0}" -ne "0" ] || [ "${ret1}" -ne "0" ] || [ ${ipv6_mask_quotient} == "0" -a "${ipv6_mask_remainder}" == "0" ];then
        echo "[ERR]INFO:Get IPv6(${ipv6}) subnet failed. Mask ${PREFIX} is invalid." >> "${RESULTFILE}" 2>&1
        echo "${subnet}"
        return
    fi
    ipv6_mask_full=$((num=2#${ipv6_mask_binary}))
    for i in $(seq 1 ${ipv6_mask_quotient})
    do
        ipv6_tmp=$((num=16#$( echo "${ipv6}" | awk -F":" -v num="${i}" '{print $num}' )))
        subnet_tmp=$(( ${ipv6_tmp} & ${ipv6_mask_full} ))
        subnet=${subnet}${subnet_tmp}":"
    done
    if [ "$ipv6_mask_remainder" -gt 0 ];then
        ipv6_mask_binary_tmp=$( str=$(printf "%-${ipv6_mask_remainder}s" "1");echo "${str// /1}" )
        ipv6_mask_tmp=$((num=2#${ipv6_mask_binary_tmp}))
        ipv6_mask_len_tmp=$(( ${ipv6_mask_quotient} + 1))
        ipv6_tmp=$((num=16#$( echo "${ipv6}" | awk -F":"  -v num="${ipv6_mask_len_tmp}" '{print $num}' )))
        subnet_tmp=$(( ${ipv6_tmp} & ${ipv6_mask_tmp} ))
        subnet=${subnet}${subnet_tmp}":"
    fi
    # ȥĩβķָ:
    subnet=${subnet%:}
    echo "${subnet}"
    LOG "[$FUNCNAME][${LINENO}]subnet=${subnet}."
}
function get_all_ipv6()
{
    local ip=$1
    local ipv6_iplen="8"
    local ipv6_all=""
    patter_len=$( echo "${ip}" | awk '{n=split($0,ip,":");print n}' )
    if [ "${patter_len}" -lt "${ipv6_iplen}" ];then
        ipv6_zero_len=$(( ${ipv6_iplen} - ${patter_len} ))
        for i in $(seq 1 ${patter_len})
        do
            ipv6_tmp=$(echo "${ip}" | awk -F":" -v num=$i '{print $num}' )
            if [ "X${ipv6_tmp}" == "X" ];then
                ipv6_tag=${i}
                ipv6_all=${ipv6_all}"0:"
                break
            else
	        ipv6_all=${ipv6_all}${ipv6_tmp}":"
            fi
        done
        for i in $(seq 1 ${ipv6_zero_len})
        do
            ipv6_all=${ipv6_all}"0:"
        done
        if [ ${ipv6_tag} -lt $(( ${patter_len} - 1 )) ];then
            for i in $(seq $((${ipv6_tag} + 1)) ${patter_len})
            do
                ipv6_tmp=$( echo "${ip}" | awk -F":" -v num=$i '{print $num}' )
                ipv6_all=${ipv6_all}${ipv6_tmp}":"
            done
        fi
        #ȥĩβķָ:
        ipv6_all=${ipv6_all%:}
    else
        ipv6_all="$ip"
    fi
    echo "${ipv6_all}"
}
function get_subnet_fromcfg()
{
    local subnet=""
    local portname=$1
    local net_config="${NetConfig}"

    ip=$( grep -w "^IPADDR" "${net_config}${portname}" |awk -F"=" '{print $2}' | sed "s/'//g" )
    netmask=$( grep -w "^NETMASK" "${net_config}${portname}" |awk -F"=" '{print $2}' | sed "s/'//g" )
    PREFIX=$( grep -w "^PREFIX" "${net_config}${portname}" |awk -F"=" '{print $2}' | sed "s/'//g" )
    iptype=$( checkiptype "$ip" )
    LOG "[$FUNCNAME][${LINENO}]portname=${portname}, ip=${ip}, netmask=${netmask}, PREFIX=${PREFIX}, iptype=${iptype}."
    if [ "X${iptype}" == "X0" ];then
        subnet_1=$(( $( echo "$ip" |awk -F. '{print $1}' ) & $( echo "$netmask" | awk -F. '{print $1}' ) ))
        subnet_2=$(( $( echo "$ip" |awk -F. '{print $2}' ) & $( echo "$netmask" |awk -F. '{print $2}' ) ))
        subnet_3=$(( $( echo "$ip" |awk -F. '{print $3}' ) & $( echo "$netmask" |awk -F. '{print $3}' ) ))
        subnet_4=$(( $( echo "$ip" |awk -F. '{print $4}' ) & $( echo "$netmask" |awk -F. '{print $4}' ) ))
        subnet=${subnet_1}"."${subnet_2}"."${subnet_3}"."${subnet_4}
        echo "${subnet}"
    elif [ "X${iptype}" == "X1" ];then

        #ȡipv6ȫip
        ipv6=$( get_all_ipv6 "${ip}")
        subnet=$( get_ipv6_subnet "${ipv6}" "${PREFIX}" )
        echo "${subnet}"
    else
        LOG "[$FUNCNAME][${LINENO}]port ${portname} ip ${ip} get subnet failed,ip is invalid."
        echo "${subnet}"
    fi
}
function getfrontip()
{
    local net_config="${NetConfig}"
    local result=""
    front_card_name=$( /usr/bin/sqlite3 "${cm_db_file}" "select CARD_NAME from CM_NODE_NETCARD_T where SUBNET_ID=2 and NID=${my_nid}" )
    for eth_front in $(sort -V <<< "${front_card_name}" || echo ${front_card_name})
    do
        if [ -f "${net_config}${eth_front}" ];then
            subnet=$( get_subnet_fromcfg "${eth_front}" )
        fi

        result="${subnet} ${result}"
    done
    echo "${result}"
}
function getbackip()
{
    local result=""
    local net_config="${NetConfig}"
    local RoceCard="mlx5_core"
    back_card_name=$( /usr/bin/sqlite3 "${cm_db_file}"  "select CARD_NAME from CM_NODE_NETCARD_T where SUBNET_ID=1 and NID=${my_nid}" )
    for eth_back in $(sort -V <<< "$back_card_name" || echo $back_card_name)
    do
        IsRHNode
        if [ 0 -eq $? ];then
            cardtype=$( ethtool -i ${eth_back} | grep driver | awk -F":" '{print $2}' | sed 's/ //g;s/\t//g' )
            if [ "X${RoceCard}" == "X${cardtype}" ];then
                eth_back=${eth_back}".2"
            fi
        fi
        if [ -f "${net_config}${eth_back}" ];then
            subnet=$( get_subnet_fromcfg "${eth_back}" )
        fi

        result="${subnet} ${result}"
    done
    echo "${result}"
}

function getManagerIp()
{
    local result=""
    local omip_v4="$( grep "^omip=" ${cm_conf_file} | awk -F"=" '{print $2}' | sed 's/"//g' )"
    local omip_v6="$( grep "^omip_v6=" ${cm_conf_file} | awk -F"=" '{print $2}' | sed 's/"//g' )"
    local omNetMask="$( grep "^omNetMask=" ${cm_conf_file} | awk -F"=" '{print $2}' | sed 's/"//g' )"
    local omNetMask_v6="$( grep "^omNetMask_v6=" ${cm_conf_file} | awk -F"=" '{print $2}' | sed 's/"//g' )"
    if [ "X${omip_v4}" != "X" ];then
        subnet_1=$(( $( echo "$omip_v4" |awk -F. '{print $1}' ) & $( echo "$omNetMask" |awk -F. '{print $1}' ) ))
        subnet_2=$(( $( echo "$omip_v4" |awk -F. '{print $2}' ) & $( echo "$omNetMask" |awk -F. '{print $2}' ) ))
        subnet_3=$(( $( echo "$omip_v4" |awk -F. '{print $3}' ) & $( echo "$omNetMask" |awk -F. '{print $3}' ) ))
        subnet_4=$(( $( echo "$omip_v4" |awk -F. '{print $4}' ) & $( echo "$omNetMask" |awk -F. '{print $4}' ) ))
        subnet_ipv4="${subnet_1}.${subnet_2}.${subnet_3}.${subnet_4}"
        result="${result}${subnet_ipv4} "
    fi
    if [ "X${omip_v6}" != "X" ];then
        #ȡipv6ȫip
        ipv6=$( get_all_ipv6 "${omip_v6}")
        subnet_ipv6=$( get_ipv6_subnet "${ipv6}" "${omNetMask_v6}" )
        result="${result}${subnet_ipv6} "
    fi
    echo "${result}"
}

function getIPMI()
{
    local result=""
    local ipmi_v4="$( grep "^ipmi=" ${cm_conf_file} | awk -F"=" '{print $2}' | sed 's/"//g' )"
    local ipmi_v6="$( grep "^ipmi_v6=" ${cm_conf_file} | awk -F"=" '{print $2}' | sed 's/"//g' )"
    local ipmiNetMask="$( grep "^ipmiNetMask=" ${cm_conf_file} | awk -F"=" '{print $2}' | sed 's/"//g' )"
    if [ "X${ipmi_v4}" != "X" ];then
        subnet_1=$(( $( echo "${ipmi_v4}" |awk -F. '{print $1}' ) & $( echo "${ipmiNetMask}" |awk -F. '{print $1}' ) ))
        subnet_2=$(( $( echo "${ipmi_v4}" |awk -F. '{print $2}' ) & $( echo "${ipmiNetMask}" |awk -F. '{print $2}' ) ))
        subnet_3=$(( $( echo "${ipmi_v4}" |awk -F. '{print $3}' ) & $( echo "${ipmiNetMask}" |awk -F. '{print $3}' ) ))
        subnet_4=$(( $( echo "${ipmi_v4}" |awk -F. '{print $4}' ) & $( echo "${ipmiNetMask}" |awk -F. '{print $4}' ) ))
        subnet_ipv4="${subnet_1}.${subnet_2}.${subnet_3}.${subnet_4}"
        result="${subnet_ipv4} ${result}"
    elif [ "X${ipmi_v6}" != "X" ];then
        #ȡipv6ȫip
        ipv6=$( get_all_ipv6 "${ipmi_v6}")
        ipv6_subnet=$( get_ipv6_subnet "${ipv6}" "${ipmiNetMask}" )
        result="${ipv6_subnet} ${result}"
    fi
    echo "${result}"

}

function checkipsubnet()
{
    local isPass=0
    Back_subnet=$( getbackip )
    Front_subnet=$( getfrontip )
    Ipmi_subnet=$( getIPMI )
    LOG "[$FUNCNAME]Back_subnet=${Back_subnet}, Front_subnet${Front_subnet}"
    IsMasterNode
    iRet=$?
    if [ "${iRet}" -eq 0 ];then
        Manager_subnet=$( getManagerIp )
    fi

    for item in ${Back_subnet}
    do
        Front_same= $( echo "${Front_subnet}" |grep -w "${item}" )
        if [ "X${Front_same}" != "X" ];then
            echo "[ERR]INFO:Subnet(${item}) of backend network is same with frontend subnet(${Front_subnet})." >>${RESULTFILE} 2>&1
            isPass=1

        fi
        Ipmi_same= $( echo "${Ipmi_subnet}" |grep -w "${item}" )
        if [ "X${Ipmi_same}" != "X" ];then
            echo "[ERR]INFO:Subnet(${item}) of backend network is same with IPMI subnet(${Ipmi_subnet})." >>${RESULTFILE} 2>&1
            isPass=1

        fi
        Manager_same= $( echo "${Manager_subnet}" |grep -w "${item}" )
        if [ "X${Manager_same}" != "X" ];then
            echo "[ERR]INFO:Subnet(${item}) of backend network is same with management subnet(${Manager_subnet})." >>${RESULTFILE} 2>&1
            isPass=1

        fi
    done
    if [ "${isPass}" == "0" ];then
        echo "INFO:The inspection result is passed." >>${RESULTFILE} 2>&1
    fi
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
    LOG "[$FUNCNAME]${CurInspectFun}_Pass ${isPass}"
    return

}

checkipsubnet


