#!/bin/bash

G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="299"
CurInspectFun="$( GetInspectType ${CurInspectNum} )"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

############################################## check whether mon and dlm node number is the same
function CheckNodeNum()
{
    local var_dlm_node_num="$( cat /proc/dlm_info | grep "UUID:" | wc -l )"
    let "var_dlm_vnode_num=${var_dlm_node_num}%2"
    let "var_dlm_node_num=${var_dlm_node_num}/2"
    local notDfsNum=0
    local var_mon_node_num="$( cat /proc/monc_nodemap | grep -E "NodeID.*NodeType\(1\)" | grep "DelTime(0)" | wc -l )"
    local allMonNodeIp="$(cat /proc/monc_nodemap | grep -E "NodeID.*NodeType\(1\)" | grep "DelTime(0)" | grep -Poa '(?<=DevName\().*' | awk -F")" '{print $1}' |tr -d " ")"
    for tmpIp in ${allMonNodeIp}
    do
        tmp_node_type="$(/opt/huawei/deploy/script/localexec.py cmd ${tmpIp} 30 false "cat /opt/huawei/snas/etc/snas.ini" | grep -w "node_service_type" | awk -F= '{print $2}' |tr -d " ")"
        if [ "X1" != "X${tmp_node_type}" ];then
            let "notDfsNum=${notDfsNum}+1"
        fi
    done
    if [ "X${var_dlm_vnode_num}" != "X0" ]
    then
        echo "[ERR]node num illegal.ERRCODE(4)" >>${RESULTFILE} 2>&1
        #标志位异常设置
        isPass=1
    fi
    let "var_mon_node_num=${var_mon_node_num}-${notDfsNum}"
    if [ "${var_dlm_node_num}" -lt "${var_mon_node_num}" ];then
        echo "[ERR]the number of dlm node (${var_dlm_node_num}) is unequally with that of dfs node (${var_mon_node_num}-${notDfsNum}),num check failed.ERRCODE(5) " >>${RESULTFILE} 2>&1
        isPass=1
    fi
}
############################################## check whether there is unstable dir nodes
function CheckNodeStatus()
{
    nodestatus=$(cat /proc/dlm_info |grep "UUID" | sed 's/:/=/g' |
        awk '{ \
            if ($0!~/^UUID.*status=NORMAL.*/){ \
                printf("[ERR]%s.ERRCODE(6)\n",$0); \
                isPass=1; \
            } \
        }' \
    )

    if [ "X" != "X${nodestatus}" ];then
        isPass=1
        echo "$nodestatus" >>${RESULTFILE} 2>&1
    fi
}

############################################## check whether dlm dir nodes exist in mon nodes
function node_exist_in_mon()
{
    local dlm_node_id="$1"
    var_mon_nodes="$( cat /proc/monc_nodemap | grep -E "Node:.*NodeType\(1\).*RegStatus\(1" | awk '{gsub(/[{NodeID}\(\),]/, "", $2); print $2}' )"
    for i in $var_mon_nodes
    {
        if [ "$i" == "${dlm_node_id}" ]; then
            return
        fi
    }
    echo "[ERR]check dlm node id ${dlm_node_id} not found in mon.ERRCODE(7)" >>${RESULTFILE} 2>&1
    isPass=1
    return
}

function CheckNodeId()
{
    node_id="$( cat /proc/dlm_info | grep "UUID:" |awk -F":|," '{print $3}' )"
    for id in ${node_id};do
        node_exist_in_mon "${id}"
    done
}
function set_ifs
{
    IFS_OLD=$IFS
    IFS=$'\n'
}

function restore_ifs
{
    IFS=$IFS_OLD
}

function main()
{
    local var_mon_node_num=0
    local isPass=0

    if [ ! -f /opt/huawei/snas/etc/snas.ini ];then
        echo "INFO:missing /opt/huawei/snas/etc/snas.ini ,only normal node need to be checked ." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass 1" >>${RESULTFILE} 2>&1
        exit 0
    fi
    nodetype="$(cat /opt/huawei/snas/etc/snas.ini | grep -w "node_service_type" | awk -F= '{print $2}' |tr -d " ")"
    if [ "X1" != "X${nodetype}" ];then
        echo "INFO:only DFS node need to be checked ." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass 0" >>${RESULTFILE} 2>&1
        exit 0
    fi
    if [ ! -f /proc/monc_nodemap ]
    then
        echo "INFO:missing /proc/monc_nodemap ,only normal node be checked ." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass 1" >>${RESULTFILE} 2>&1
        exit 0
    fi
    set_ifs
    node_status=$( cat /proc/monc_nodemap | grep "^Node:" |awk '{if($0!~/^Node:.*RegStatus\(1\).*/){print $0}}' )

    if [ "X" != "X${node_status}" ]
    then
        for line in ${node_status}
        do
            node_ip="$( echo "${line}" | awk -F"," '{print $4}'|awk -F"(" '{print $2}'|sed 's/)//g' )"
            find_ip="$( grep -w "${node_ip}" /opt/huawei/snas/etc/snas.ini )"
            if [ "X" != "X${find_ip}" ]
            then
                echo "INFO:The node is unnormal ,only normal node need to be checked ." >>${RESULTFILE} 2>&1
                echo "${CurInspectFun}_Pass 0" >>${RESULTFILE} 2>&1
                exit 0
            fi
        done
    fi
    restore_ifs
    #获取dlm节点信息,该命令由dlm模块提供
    echo -n "dir showall" > /proc/dlm_info

    #检查dlm节点个数与mon节点个数是否相等
    CheckNodeNum
    #检查dlm 节点状态是否为NORMAL
    CheckNodeStatus
    #检查dlm节点id是否存在于monc_nodemap
    CheckNodeId

    if [ "${isPass}" == "0" ]
    then
        echo "INFO:The number of dlm node is the same as that mon node,All dlm node exits in moncmap,All dlm node status are NORMAL." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass 0" >>${RESULTFILE} 2>&1
        exit 0
    else
        echo "${CurInspectFun}_Pass 1" >>${RESULTFILE} 2>&1
        exit 0
    fi
}

main


