#!/bin/bash
LOG_FILE="/var/log/inspect.log"
G_REMOTE_TMP_FILE="/tmp/tmpfileProStat"
G_REMOTE_TMP_FILE1="/tmp/tmpfileProStat1"
G_REMOTE_TMP_FILE2="/tmp/tmpfileProStat2"
>$G_REMOTE_TMP_FILE
>$G_REMOTE_TMP_FILE1
>$G_REMOTE_TMP_FILE2

Queue=""
osc=""
s3fs=""
s3fs_bill=""
memcache=""

POE=""
visitor=""

dns=""
gaussdb_mdc=""
gaussdb_upf=""

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

function checkS3Servicemanager()
{
	#所有的节点上都有
	Queue=$(sh /opt/obs/service/queue/bin/queue_monitor.sh status|awk -F: '{print $2}')
	LOG "Queue = $Queue" 
	osc=$(sh /opt/obs/service/osc/bin/osc.sh status)
	LOG "osc = $osc" 
	s3fs=$(sh /opt/obs/scripts/s3fs/uds_s3fs.sh status|awk -F: '{print $2}')
	LOG "s3fs = $s3fs" 
	s3fs_bill=$(sh /opt/obs/scripts/s3fs/uds_s3fs_bill.sh status|awk -F: '{print $2}')
	LOG "s3fs_bill = $s3fs_bill"
	memcache=$(sh /opt/obs/service/memcache/bin/memcache.sh status|awk -F: '{print $2}')
	LOG "memcache = $memcache"

	#仅OM 主节点有
	sh /opt/obs/service/poe/bin/poe.sh status > $G_REMOTE_TMP_FILE1 2>&1
	POE=$(cat $G_REMOTE_TMP_FILE1|grep running)
	LOG "POE = $POE"
	sh /opt/obs/service/visitor/bin/visitor.sh status > $G_REMOTE_TMP_FILE2 2>&1
	visitor=$(cat $G_REMOTE_TMP_FILE2|grep running)
	LOG "visitor = $visitor"

	#OM主备节点都有
	dns=$(sh /opt/obs/scripts/dns/nsd_service.sh status|awk -F: '{print $2}')
	gaussdb_mdc=$(su - gaussdba -c "/opt/huawei/gaussdb/app/bin/gs_ctl query -D /var/uds/user/data_mdc"| grep LOCAL_ROLE | awk -F ':' '{print $2}' | head -1)
	LOG "gaussdb_mdc = $gaussdb_mdc"
	gaussdb_upf=$(su - gaussdba -c "/opt/huawei/gaussdb/app/bin/gs_ctl query -D /var/uds/user/data_upf"| grep LOCAL_ROLE | awk -F ':' '{print $2}' | head -1)
	LOG "gaussdb_upf = $gaussdb_upf"
}


function checkS3ServiceBussniss()
{
	#所有的节点上都有
	Queue=$(sh /opt/obs/service/queue/bin/queue_monitor.sh status|awk -F: '{print $2}')
	LOG "Queue = $Queue" 
	osc=$(sh /opt/obs/service/osc/bin/osc.sh status)
	LOG "osc = $osc" 
	s3fs=$(sh /opt/obs/scripts/s3fs/uds_s3fs.sh status|awk -F: '{print $2}')
	LOG "s3fs = $s3fs" 
	s3fs_bill=$(sh /opt/obs/scripts/s3fs/uds_s3fs_bill.sh status|awk -F: '{print $2}')
	LOG "s3fs_bill = $s3fs_bill"
	memcache=$(sh /opt/obs/service/memcache/bin/memcache.sh status|awk -F: '{print $2}')
	LOG "memcache = $memcache"
}

function checkResS3Manager()
{
    local role=$1
    LOG "role = $role"
    result="NodeType:$NodeType||NodeObjssType:$NodeObjssType||Queue:$Queue||osc:$osc||s3fs:${s3fs}||s3fs_bill:$s3fs_bill||memcache:$memcache||dns:$dns||gaussdb_mdc:$gaussdb_mdc||gaussdb_upf:$gaussdb_upf||POE:$POE||visitor:"$visitor""
    LOG "result : $result"
    if [[ "$Queue" == "normal" && "$osc" == "running" && "$s3fs" == "normal" && "$s3fs_bill" == "normal" && "$memcache" == "normal" && "$dns" == "normal" && "$gaussdb_mdc" == " ${role}" && "$gaussdb_upf" == " ${role}" && "$POE" == "running" && "$visitor" == "running" ]];then
        Status="normal"  
    else
        Status="abnormal"
    fi
    result="Status:$Status||$result"
    echo "$result"
    if [ "X$Queue" != "Xnormal" ];then
        echo "[ERR]Queue status($Queue) is not normal.ERRCODE(4)"
    fi
    if [ "X$osc" != "Xrunning" ];then
        echo "[ERR]osc status($osc) is not running.ERRCODE(5)"
    fi
    if [ "X$s3fs" != "Xnormal" ];then
        echo "[ERR]s3fs status($s3fs) is not normal.ERRCODE(6)"
    fi
    if [ "X${s3fs_bill}" != "Xnormal" ];then
        echo "[ERR]s3fs_bill status(${s3fs_bill}) is not normal.ERRCODE(7)"
    fi
    if [ "X$memcache" != "Xnormal" ];then
        echo "[ERR]memcache status($memcache) is not normal.ERRCODE(8)"
    fi
    if [ "X$dns" != "Xnormal" ];then
        echo "[ERR]dns status($dns) is not normal.ERRCODE(9)"
    fi
    if [ "X$gaussdb_mdc" != "X ${role}" ];then
        echo "[ERR]Primary node gaussdb_mdc status($gaussdb_mdc) is not ${role}.ERRCODE(10)"
    fi
    if [ "X$gaussdb_upf" != "X ${role}" ];then
        echo "[ERR]Primary node gaussdb_upf status($gaussdb_upf) is not  ${role}.ERRCODE(14)"
    fi
    if [ "X$POE" != "Xrunning" ];then
        echo "[ERR]Primary node POE status($POE) is not running.ERRCODE(15)"
    fi
    if [ "X$visitor" != "Xrunning" ];then
        echo "[ERR]Primary node visitor status($visitor) is not running.ERRCODE(16)"
    fi
    
}

function checkResS3Standby()
{
    local role=$1
    LOG "role = $role"
    result="NodeType:$NodeType||NodeObjssType:$NodeObjssType||Queue:$Queue||osc:$osc||s3fs:${s3fs}||s3fs_bill:$s3fs_bill||memcache:$memcache||dns:$dns||gaussdb_mdc:$gaussdb_mdc||gaussdb_upf:$gaussdb_upf||POE:$POE||visitor:"$visitor""
    LOG "result : $result"
    if [[ "$Queue" == "normal" && "$osc" == "running" && "$s3fs" == "normal" && "$s3fs_bill" == "normal" && "$memcache" == "normal" && "$dns" == "normal" && "$POE" != "running" && "$visitor" != "running" ]];then
        Status="normal"
    else
        Status="abnormal"
    fi
    result="Status:$Status||$result"
    echo "$result"
    if [ "X$Queue" != "Xnormal" ];then
        echo "[ERR]Queue status($Queue) is not normal.ERRCODE(4)"
    fi
    if [ "X$osc" != "Xrunning" ];then
        echo "[ERR]osc status($osc) is not running.ERRCODE(5)"
    fi
    if [ "X$s3fs" != "Xnormal" ];then
        echo "[ERR]s3fs status($s3fs) is not normal.ERRCODE(6)"
    fi
    if [ "X${s3fs_bill}" != "Xnormal" ];then
        echo "[ERR]s3fs_bill status(${s3fs_bill}) is not normal.ERRCODE(7)"
    fi
    if [ "X$memcache" != "Xnormal" ];then
        echo "[ERR]memcache status($memcache) is not normal.ERRCODE(8)"
    fi
    if [ "X$dns" != "Xnormal" ];then
        echo "[ERR]dns status($dns) is not normal.ERRCODE(9)"
    fi
    if [ "X$POE" == "Xrunning" ];then
        echo "[ERR]Standby node POE status($POE) is running.ERRCODE(17)"
    fi
    if [ "X$visitor" == "Xrunning" ];then
        echo "[ERR]Standby node visitor status($visitor) is running.ERRCODE(18)"
    fi
    
}
function checkResS3Buss()
{
    result="NodeType:$NodeType||NodeObjssType:$NodeObjssType||Queue:$Queue||osc:$osc||s3fs:$s3fs||s3fs_bill:$s3fs_bill||memcache:$memcache||dns:||gaussdb_mdc:||gaussdb_upf:||POE:||visitor:"
    LOG "result : $result"
    if [[ "$Queue" == "normal" && "$osc" == "running" && "$s3fs" == "normal" && "$s3fs_bill" == "normal" && "$memcache" == "normal" ]];then
        Status="normal"  
    else
        Status="abnormal"   
    fi
    result="Status:$Status||$result"
    echo "$result"
    
    if [ "X$Queue" != "Xnormal" ];then
        echo "[ERR]Queue status($Queue) is not normal.ERRCODE(4)"
    fi
    if [ "X$osc" != "Xrunning" ];then
        echo "[ERR]osc status($osc) is not running.ERRCODE(5)"
    fi
    if [ "X$s3fs" != "Xnormal" ];then
        echo "[ERR]s3fs status($s3fs) is not normal.ERRCODE(6)"
    fi
    if [ "X${s3fs_bill}" != "Xnormal" ];then
        echo "[ERR]s3fs_bill status(${s3fs_bill}) is not normal.ERRCODE(7)"
    fi
    if [ "X$memcache" != "Xnormal" ];then
        echo "[ERR]memcache status($memcache) is not normal.ERRCODE(8)"
    fi
    
}

#获取节点业务类型（主/备节点/业务节点）
function getNodeType()
{
	localIp=$(cat /opt/huawei/snas/etc/snas.ini|grep ipaddr_1|awk -F= '{print $2}')
	
	LOG "localIp:$localIp"
	primaryIp=$(cat /proc/monc_hamap|grep role\(1\))	
	pIp=$(echo $primaryIp | grep $localIp)
	LOG "pIp:$pIp"
	
	standbyIp=$(cat /proc/monc_hamap|grep role\(2\))
	sIp=$(echo $standbyIp | grep $localIp)	
	LOG "sIp:$sIp"

	if [[ "$pIp" != "" ]];then
		return 1 #主节点返回“1”
	elif [[ "$pIp" == "" && "$sIp" != "" ]];then
		return 2 #备节点返回“2”
	else	
		return 3	#非主备业务节点返回“3”
	fi
}
#节点存储类型
MapObjsType()
{
	case $1 in
		1)
			echo "DFS"
			;;
		2)
			echo "S3"
			;;
		3)
			echo "Swift"
			;;
		*)
			echo "UNKNOWN"
			;;
	esac
}
#单节点类型
MapNodeType()
{
	case $1 in
		1)
			echo "Primary"
			;;
		2)
			echo "Standby"
			;;
		3)
			echo "Business"
			;;
		*)
			echo "UNKNOWN"
			;;
	esac
}

#获取节点存储类型
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 [ "${node_service_type}" == "1" ];then
	echo "Does not involve"
	LOG "node_service_type :${node_service_type}.Does not involve"
	exit 0
fi

if [ "${node_service_type}" == "2" -o "${node_service_type}" == "3" ];then
	LOG "node_service_type :${node_service_type}"
	getNodeType
	NodeTypeNum=$?
	NodeType=$(MapNodeType "$NodeTypeNum")
	NodeObjssType=$(MapObjsType "$node_service_type")
	LOG "NodeTypeNum:${NodeTypeNum},NodeType:${NodeType},NodeObjssType:${NodeObjssType}"
	
	case ${NodeTypeNum} in
		1)
			checkS3Servicemanager
			checkResS3Manager "Primary"
		;;
		2)
			checkS3Servicemanager
			checkResS3Standby "Standby"
		;;
		3)
			checkS3ServiceBussniss
			checkResS3Buss
		;;
		*)
			echo "type is error!"
		;;
	esac
fi
exit 0

