#!/bin/bash

#
#巡检S3 DNS服务状态
#只在OMS 主节点上检查
#

#设置sqlite环境
export LD_LIBRARY_PATH=/usr/sqlite3/lib/:/opt/huawei/snas/lib/

#CM_INI_PATH
readonly CM_INI_PATH="/opt/huawei/snas/etc/cm.ini"


#判断该节点是否需要检查,返回：1，需要检查；0，不需要检查
#1.检查本节点是否是S3/Swift节点
#2.检查本节点是否是S3/Swift管理节点
#3.检查本节点是否是S3/Swift管理POE主节点
function isNotNeedCheck()
{
    local node_service_type=$(egrep '[[]|^'node_service_type'=' /opt/huawei/snas/etc/snas.ini | tr -d '\n' | grep -Po '(?<=[[]'NODE'[]]'node_service_type'=)[0-9]+')
    if [ "X$node_service_type" == "X2" -o "X$node_service_type" == "X3" ];then
        source /opt/obs/scripts/common/s3_config_utility.sh >> /dev/null 2>&1
        is_s3_management_node > /dev/null
        if [ $? -eq 1 ];then
            localNID=$(grep -w NID ${CM_INI_PATH}|awk -F"=" '{print $2}')
            
            hainfo=$(grep -w "node id(${localNID})" /proc/monc_hamap)
            if [ "$hainfo" = "" ];then
                return 1
            else
                role=${hainfo##*role(}
                role=${role%%)status*}
                if [ "$role" = "1" ];then
                    return 1;
                elif [ "$role" = "2" ];then
                    return 0;
                else
                    #非管理节点
                    return 0
                fi
            fi    	
        fi
    fi
}

#检查S3 DNS服务
function checkS3DNSService()
{
    #isPass为0才通过检查
    local isPass=0
    
    #obs.conf配置项root_domain项要存在
    local dnsDomain_nsd=$(cat /etc/nsd/obs.conf|grep root_domain |awk -F"=" '{print $2}' | sed 's/\ //g')
    if [ "X$dnsDomain_nsd" == "X" ];then
        isPass=1
        echo "isPass:$isPass"
        echo "[ERR]The root_domain field in /etc/nsd/obs.conf file is empty.ERRCODE(4)"
        return 
    fi
    
    #obs_sod.properties中obs_api_root_domain_name项要存在
    local dnsDomain_obsconf=$(cat /opt/obs/obsconf/obs_sod.properties|grep obs_api_root_domain_name |awk -F"=" '{print $2}' | sed 's/\ //g')
    if [ "X$dnsDomain_obsconf" == "X" ];then
        isPass=1
        echo "isPass:$isPass"
        echo "[ERR]The obs_api_root_domain_name field in /opt/obs/obsconf/obs_sod.properties file is empty.ERRCODE(5)"
        return 
    fi

    #以上两个domain要相等
    if [ "$dnsDomain_nsd" != "$dnsDomain_obsconf" ];then
        isPass=1
        echo "isPass:$isPass"
        echo "[ERR]The value of the root_domain field($dnsDomain_nsd) is not the same as the obs_api_root_domain_name field($dnsDomain_obsconf).ERRCODE(6)"
        return 
    fi
    
    
    #获取前端ip
    source /opt/obs/scripts/mdc/getLocalFrontIp.sh
    localFrontIp=$(getLocalHaFrontIp)
    
    local multiaz=$(cat /opt/huawei/snas/etc/multiaz.ini | grep "Enable" | awk -F"=" '{print $2}')
    
    if [ $multiaz -eq 1 ];then
        #多集群,应该匹配域名,否则不通过
        local count=$(dig @"$localFrontIp" $dnsDomain_nsd 2>/dev/null |grep -A 2 "ANSWER SECTION"| grep $dnsDomain_nsd -c)
        if [ $count -eq 0 ];then
           isPass=1
           echo "[ERR]Multi-cluster domain name mismatch.ERRCODE(7)"
        fi
    else
        #单集群,应该匹配ip地址(这里不用严格检查,只检查了ipv4),否则不通过
        dig @"$localFrontIp" $dnsDomain_obsconf 2>/dev/null |grep -A 2 "ANSWER SECTION"| egrep -q '([0-9]+.){3}[0-9]+'
        if [ $? -eq 1 ];then
           isPass=1
           echo "[ERR]Single-Cluster IP mismatch.ERRCODE(8)"
        fi
    fi
    echo "isPass:$isPass"
}

#如果不需要检查，则直接退出脚本执行,输出isCheck:0
if isNotNeedCheck;then
    echo "isCheck:0"
    exit 0;
fi

#开始检查
echo "isCheck:1"
checkS3DNSService
