#!/bin/bash
set +x

CM_DB="/opt/huawei/snas/etc/cm_conf.db"
LOG_FILE="/var/log/inspect.log"
G_INSPECT_MMLPATH="/opt/huawei/snas/script/inspect_mml"
source $G_INSPECT_MMLPATH/CheckItems
CurInspectNum="310"
CurInspectFun="$(GetInspectType $CurInspectNum)"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

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

#===================================================
# 函数名称：获取cm_conf.db中节点对应的服务类型
# 函数功能：
# 描述：
#===================================================
function get_service_type_from_db()
{
    local temp=1
    local ip=$1
    local nid=$(/usr/bin/sqlite3 ${CM_DB} "select NID from CM_NODE_IP_T where IP_ADDR='${ip}'"| tr -d ' ')
    while [[ "${nid}" == "" ]]
    do
        sleep 2
        LOG "[$LINENO]Failed to get nid from db ,temp=$temp"
        nid=$(/usr/bin/sqlite3 ${CM_DB} "select NID from CM_NODE_IP_T where IP_ADDR='${ip}'"| tr -d ' ')
        temp=$((temp + 1))
        if [ $temp -gt 10 ];then
            temp=1
            break
        fi
    done
    temp=1
    local service_type=$(/usr/bin/sqlite3 ${CM_DB} "select Reserved2 from CM_NODE_T where NID='${nid}'" | tr -d ' ')
    while [[ "${service_type}" == "" ]]
    do
        sleep 2
        LOG "[$LINENO]Failed to get service_type from db ,temp=$temp"
        service_type=$(/usr/bin/sqlite3 ${CM_DB} "select Reserved2 from CM_NODE_T where NID='${nid}'" | tr -d ' ')
        temp=$((temp + 1))
        if [ $temp -gt 10 ];then
            temp=1
            break
        fi
    done

    echo ${service_type}
}

function nascheck_static_mount()
{
    #check节点类型
	local result=""
    local resultTmp=""
	local isPass=0
    source $G_INSPECT_MMLPATH/CommonFunc.sh
    LOCAL_BACK_IP=$(GetLocalIp)
    local service_type=$(get_service_type_from_db "${LOCAL_BACK_IP}")
    if [ "${service_type}" != "1" ];then
        LOG "[$LINENO]service_type = ${service_type}.skip ${GetLocalIp}."
        echo "INFO:It is not DFS node. Do not involve to inspect." >> ${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1	
        return 0
    fi
    
    STATIC_IP=$(/usr/local/bin/nas_proto_cluster -c get_node -n $(hostname) | grep node_static_outter_ip | awk 'BEGIN {FS="[><]"} {print $3}' | awk 'BEGIN {FS=","} {for(i=1;i<=NF;i++) printf($i" ");printf("\n")}')
    #echo "static ip:${STATIC_IP}"
    SERV_IP=$(cat /proc/fs/nfsd/conn_info | awk '{print $2}' | awk 'BEGIN {FS=":"} !a[$0]++ {print $2}')
    #echo "serv ip $SERV_IP"

    if [ "X" ==  "X${STATIC_IP}" -o "X" ==  "X${SERV_IP}" ];then
	    LOG "[$LINENO]STATIC_IP=$STATIC_IP,SERV_IP=$SERV_IP."
        echo "INFO:The NFS client does not exist." >> ${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        return 0
    fi

	for ip1 in ${STATIC_IP}
    do
	    for ip2 in ${SERV_IP}
	    do
		    if [ "$ip1" = "$ip2" ]; then
                resultTmp=$(grep $ip1 /proc/fs/nfsd/conn_info | awk '{print $4}' | awk 'BEGIN {FS=":"} !a[$0]++ {ORS=" "} {print $2}')
                if [ "X" != "X${resultTmp}" ];then
                    result="$resultTmp $result"
                fi  
		    fi
	    done
    done

    if [ "X" != "X${result}" ];then
        isPass=4
        echo "[ERR]INFO:NFS clients are $result." >> ${RESULTFILE} 2>&1
    else
        echo "INFO:The NFS client does not exist." >> ${RESULTFILE} 2>&1
    fi
    LOG "[$LINENO]result=$result,isPass=$isPass."
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1

    return 0
}

nascheck_static_mount

exit 0