#!/bin/bash

# 检测集群服务
##########################################################
function check_cluster_service()
{
    flag=0
    echo "[$TIME]集群服务检测"
    cluster_service="pacemaker corosync pcsd"
    for svc in $cluster_service; do
        check_service_available $svc
        if [ $? -eq 0 ];then

            check_service_enabled $svc
            if [ $? -eq 0 ];then
        	echo "$svc已启用开机启动"
        	log "$svc已启用开机启动" >> $ha_check_log
            else
        	echo "$svc已禁用开机启动"
        	log "$svc已禁用开机启动" >> $ha_check_log
            fi

            check_service_active $svc
            if [ $? -eq 0 ];then
        	echo "$svc正在运行"
        	log "$svc正在运行" >> $ha_check_log
            else
		flag=2
        	echo "$svc没有运行"
        	log "$svc没有运行" >> $ha_check_log
            fi

        else
	   flag=1
           echo "ERROR:没有安装$svc服务"
           log "ERROR:没有安装$svc服务" >> $ha_check_log
        fi

    done

    if [ $flag -eq 1 ];then
	return 1
    elif [ $flag -eq 2 ]; then
	return 2
    else
	return 0
    fi		
	
}


# 检测节点信息
##########################################################
function check_nodes()
{
    echo "[$TIME]集群节点检测"
    #检测DC节点
    DC_Node=$(crm_mon -r1 |grep "Current DC" | awk -F'(' '{ print $1 }' | awk -F':' '{ print $2 }')
    if [[ -n "$DC_Node" ]]; then
	echo "DC节点:$DC_Node"
	log "DC节点:$DC_Node" >> $ha_check_log
    fi

    #检测quorum
    QUORUM=$(crm_mon -r1 | grep "partition with quorum" | wc -l)
    if [[ -n "$QUORUM" ]]; then
	echo "集群可以仲裁"
	log "集群可以仲裁" >> $ha_check_log
    else
	echo "集群失去仲裁"
	log "集群失去仲裁" >> $ha_check_log
    fi

    #检测在线节点
    Online_Node=$(crm_mon -r1 | sed -n '/Online: \[.*\]/p'  | cut -d \[ -f2 |cut -d \] -f1)
    if [[ -n "$Online_Node" ]]; then
	echo "在线节点:$Online_Node"
	log "在线节点:$Online_Node" >> $ha_check_log
    fi

    #检测离线节点
    OFFLINE_Node=$(crm_mon -r1 | sed -n '/OFFLINE: \[.*\]/p'  | cut -d \[ -f2 |cut -d \] -f1)
    if [[ -n "$OFFLINE_Node" ]]; then
	echo "离线节点:$OFFLINE_Node"
	log "离线节点:$OFFLINE_Node" >> $ha_check_log
    fi

    #检测UNCLEAN节点
    UNCLEAN_Nodes=$(crm_mon -r1 | sed -n '/Node .*\: UNCLEAN/p' |awk -F':' '{ print $1 }' | awk '{ print $3}')
    if [[ -n "$UNCLEAN_Nodes" ]];then
	for unclean_node in $UNCLEAN_Nodes;do
	    echo "节点: $unclean_node is UNCLEAN"
	    log "节点: $unclean_node is UNCLEAN" >> $ha_check_log
    	done
    fi

    #检测待机节点
    standby_nodes=$(crm_mon -r1 | sed -n '/Node .*\: standby/p' |awk -F':' '{ print $1 }' | awk '{ print $3}')
    if [[ -n "$standby_nodes" ]]; then
	for standby_node in $standby_nodes;do
	    echo "节点$standby_node处于待机状态"
	    log "节点$standby_node处于待机状态" >> $ha_check_log
	done
    fi
    
    #检测维护中的节点
    maintenance_nodes=$(crm_mon -r1 | sed -n '/Node .*\: maintenance/p' |awk -F':' '{ print $1 }' | awk '{ print $3}')
    if [[ -n "$maintenance_nodes" ]]; then
        for maintenance_node in $maintenance_nodes;do
            echo "节点$maintenance_node处于维护状态"
	    log "节点$maintenance_node处于维护状态" >> $ha_check_log
        done
    fi

}

function check_resources()
{
    echo "[$TIME]集群资源检测"
    started_resources=$(pcs resource | sed -n '/.* Started/p' | awk '{ print $2 }')
    stopped_resources=$(pcs resource | sed -n '/.* Stopped/p' | awk '{ print $2 }')
    failed_resources=$(pcs resource | sed -n '/.* FAILED/p' | awk '{ print $2 }')


    if [[ -z $started_resources && -z $stopped_resources && -z $failed_resources ]]; then
	echo "集群中没有资源"
	log "集群中没有资源" >> $ha_check_log
    fi

    #正在运行的资源
    if [[ -n $started_resources ]]; then
        for started_resource in $started_resources;do
            echo "资源: $started_resource已经启动"
	    log "资源: $started_resource已经启动" >> $ha_check_log
        done
    fi

    #停止的资源
    if [[ -n $stopped_resources ]]; then
        for stopped_resource in $stopped_resources;do
            echo "资源: $stopped_resource已经停止"
	    log "资源: $stopped_resource已经停止" >> $ha_check_log
        done
    fi

    #失败的资源
    if [[ -n $failed_resources ]]; then
        for failed_resource in $failed_resources;do
            echo "资源: $failed_resource出现错误，无法启动"
	    log "资源: $failed_resource出现错误，无法启动" >> $ha_check_log
        done
    fi

}

function check_fence_state()
{
    echo "[$TIME]集群fence检测"
    is_fence_enabled 
    if [ $? -eq 0 ];then
        echo "集群已经启用stonith"
	log "集群已经启用stonith" >> $ha_check_log
    else
        echo "集群没有启用stonith"
	log "集群没有启用stonith" >> $ha_check_log
    fi

    use_sbd=0
    fence_names=$(pcs stonith | sed -n '/(stonith:.*):/p' | awk '{ print $2 }')

    if [[ -n $fence_names ]];then
	for fence_name in $fence_names;do
	    fence_agent=$(pcs stonith | grep $fence_name | sed -n '/(stonith:.*):/p' | awk -F":" '{ print $2 }' | awk -F")" '{ print $1 }')
	    fence_state=$(pcs stonith | grep $fence_name | sed -n '/(stonith:.*):/p' | awk '{ print $4 }')
	    echo "stonith资源: $fence_name($fence_agent) is $fence_state"
	    log "stonith资源 : $fence_name($fence_agent) is $fence_state" >> $ha_check_log
	    result=$(echo $fence_agent | grep "sbd" | wc -l)
	    if [ $result -eq 1 ];then
		use_sbd=1
	    fi
	done
    else
	echo "集群中没有stonith资源"
	log "集群中没有stonith资源" >> $ha_check_log
    fi

    if [ $use_sbd -eq 1 ];then
	check_service_active "sbd"
    fi

}

function is_fence_enabled()
{
    fence_enabled=$(pcs property config --all | grep stonith-enabled | awk '{ print $2 }')
    if [ "$fence_enabled" = "true" ];then
        return 0
    else
        return 1
    fi
}

