#!/bin/bash

#检查集群节点个数
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"

function IsRHNode() 
{
    local name=""
    name=$(dmidecode -t 1 | grep "Product Name:" | awk '{print $3}')
    if [ "$name" = "RH2288" -o "$name" = "5288" -o "$name" = "RH2288H" ]; then
        return 0
    else
        return 1
    fi
}

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"  )
    if [ "X" == "X${ALL_BMCIP}" ];then
        echo "INFO:There are no RH 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 "INFO: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 RH_CheckPort="NIC1"
    local Des_port=""
    local CM_FILE="/opt/huawei/snas/etc/cm.ini"
   
    if [ ! -f "${CM_FILE}" ];then
        isPass=1
        echo "INFO:The Config File ${CM_FILE} 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
    IsRHNode
    if [ $? -eq 0 ];then
        Des_port="${RH_CheckPort}"	
    else
        Des_port="${T3000_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-0 subdomains do not need to be checked." >>${RESULTFILE} 2>&1
	echo "Port:--||Link_detected:--" >>${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 "INFO:the ${Des_port} net port link detected ${LINK} is not match the standard link detected ${Des_Link}." >>${RESULTFILE} 2>&1
    fi
    
    echo "Port:${Des_port}||Link_detected:${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


