#!/bin/bash
set +x

#检查集群节点个数
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source ${G_INSPECT_MMLPATH}/CheckItems
CurInspectNum="358"
CurInspectFun="$( GetInspectType $CurInspectNum )"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

LOG_FILE="/var/log/inspect.log"
source /opt/node_type_recognize.sh

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

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

function checkBMCIP()
{
    local isPass=0
    isMasterNode
    if [ "$?" != "0" ];then
        return
    fi

    ALL_BMCIP=$( sqlite3 /opt/huawei/snas/etc/cm_conf.db "select IPMI from CM_NODE_T where HARD_TYPE=75 or HARD_TYPE=76 or HARD_TYPE=77 or HARD_TYPE=109 or HARD_TYPE=110 or HARD_TYPE=111 or HARD_TYPE=112 or HARD_TYPE=113 or HARD_TYPE=114"  )
    if [ "X" == "X${ALL_BMCIP}" ];then
        echo "INFO:There are no RH node or the node in the cluster and no need to check the connectivity of BMC IP." >>${RESULTFILE} 2>&1
        return
    fi
    for bmcip in ${ALL_BMCIP}
    do
        connectoin=$( ping -q -c 1 ${bmcip}  | grep received | awk '{print $4}' )
        if [ "X1" != "X${connectoin}" ];then
            isPass=1
            echo "[ERR]INFO:Check the BMC IP(${bmcip}) connectivity failure." >>${RESULTFILE} 2>&1
        fi
    done
    if [ "${isPass}" == "0" ];then
        echo "INFO:The BMC ip of all RH nodes is connectable." >>${RESULTFILE} 2>&1
    fi
    return
}

function CheckManagerNet()
{
    local isPass=0
    local Manager_domain="0"
    local Des_Link="yes"
    local T3000_CheckPort="NIC0"
    local RHV3_CheckPort="NIC1"
    local RHV5_CheckPort="NIC3"
    local TS_CheckPort="NIC1-1"
    local Des_port=""
    local CM_FILE="/opt/huawei/snas/etc/cm.ini"

    if [ ! -f "${CM_FILE}" ];then
        isPass=1
        echo "[ERR]INFO:The Config File(cm.ini) does not exist, can not determine whether this node belongs to the 0 subdomain." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]${CurInspectFun}_Pass isPass=${isPass},The Config File ${CM_FILE} does not exist, can not determine whether this node belongs to the 0 subdomain"
        exit 0
    fi
    checkBMCIP

    IsRHV5Node
    if [ $? -eq 0 ];then
        Des_port="${RHV5_CheckPort}"
    fi
    IsRHV3Node
    if [ $? -eq 0 ];then
        Des_port="${RHV3_CheckPort}"
    fi
    IsT3000Node
    if [ $? -eq 0 ];then
        Des_port="${T3000_CheckPort}"
    fi
    IsTSNode
    if [ $? -eq 0 ];then
        Des_port="${TS_CheckPort}"
    fi

    domain=$( grep SubDomain ${CM_FILE} | awk -F"=" '{print $2}' | sed 's/ //g' )
    if [ "X${Manager_domain}" != "X${domain}" ];then
        echo "INFO:Only nodes in the 0 subdomain are checked,nodes in non-zero subdomains do not need to be checked." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        LOG "[$FUNCNAME]${CurInspectFun}_Pass isPass=${isPass},Only nodes in the 0 subdomain are checked,nodes in non-0 subdomains do not need to be checked."
        exit 0

    fi

    LINK=$( ethtool ${Des_port} | grep "Link detected" | awk -F":" '{print $2}' | sed 's/ //g;s/\t//g' )
    if [ "X${Des_Link}" != "X${LINK}" ];then
        isPass=1
        echo "[ERR]INFO:The port(${Des_port}) link detected(${LINK}) is not match the standard link detected(${Des_Link})." >>${RESULTFILE} 2>&1
    fi

    echo "INFO:Port(${Des_port}) status of link detected is ${LINK}." >>${RESULTFILE} 2>&1
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
    LOG "[$FUNCNAME]${CurInspectFun}_Pass isPass=${isPass},the manager net port link detected is ${LINK}."
    exit 0
}

CheckManagerNet


