[Greenplum]检查greenplum是否同步的一个脚本

此脚本可以与一些监控系统配置,执行脚本后如果返回非零,则说明有节点不同步了。 使用之间请把脚本中GPDB的名称改成你实际的数据库名称。 脚本内容如下:

#!/bin/bash

#请把GPDB变量改成你实际的数据库名称
GPDB=mydb

LOGPATH=/export/home/gpadmin/log
LOGFILE=${LOGPATH}/gp_check_sync.log


#GPHOME=`echo \$GPHOME |tail -1`
EXECPATH=/export/home/gpadmin/monitor


source ~/.bash_profile

GPVERSION=`$GPHOME/bin/gpstart --version |awk '{print \$3}'|awk -F'.' '{print \$1}'`

IS_OK=1

if [ "$GPVERSION" -eq 4 ];then
    sqlcmd="select dbid,content,hostname,port,status,mode from gp_segment_configuration"          
elif [ "$GPVERSION" -eq 3 ];then
    sqlcmd="select dbid,content,hostname,port,valid from gp_configuration" 
else
    echo `date "+%Y-%m-%d %H:%M:%S"`" : ERROR: unknown greenplum version : $GPVERSION" \
         |tee -a ${LOGPATH}/gp_check_sync.log
    exit 1
fi



$GPHOME/bin/psql -d $GPDB -A -P fieldsep=' ' -P tuples_only=1 \
-c "$sqlcmd" >${EXECPATH}/tmp_$$.res 2>${EXECPATH}/tmp_$$.err

retcode=$?
if [ "$retcode" -ne 0 ];then
    errmsg="run sql [ ${sqlcmd} ] error : `cat ${EXECPATH}/tmp_$$.err`"
    echo `date "+%Y-%m-%d %H:%M:%S"`" : ERROR: $0 : $errmsg" |tee -a ${LOGFILE}
    rm ${EXECPATH}/tmp_$$.res
    rm ${EXECPATH}/tmp_$$.err
    exit -1
fi
if [ "$GPVERSION" -eq 4 ];then
    while read dbid content hostname port status mode
    do
        if [ "$status" != "u" ];then
            echo `date "+%Y-%m-%d %H:%M:%S"`" : ERROR: segment $hostname status became '$status' !!!" |tee -a ${LOGFILE}
            IS_OK=0
        fi
    done <${EXECPATH}/tmp_$$.res
else
    while read dbid content hostname port valid
    do
        if [ "$valid" != "t" ];then
            echo `date "+%Y-%m-%d %H:%M:%S"`" : ERROR: segment $hostname status became '$status' !!!" |tee -a ${LOGFILE}
            IS_OK=0
        fi
    done <${EXECPATH}/tmp_$$.res
fi

rm ${EXECPATH}/tmp_$$.res
rm ${EXECPATH}/tmp_$$.err


$GPHOME/bin/psql -d $GPDB -A -P fieldsep=' ' -P tuples_only=1 \
-c "select summary_state from gp_master_mirroring" >${EXECPATH}/tmp_$$.res 2>${EXECPATH}/tmp_$$.err

retcode=$?
if [ "$retcode" -ne 0 ];then
    errmsg="run sql [ ${sqlcmd} ] error : `cat ${EXECPATH}/tmp_$$.err`"
    echo `date "+%Y-%m-%d %H:%M:%S"`" : ERROR: $errmsg" |tee -a ${LOGFILE}
    rm ${EXECPATH}/tmp_$$.res
    rm ${EXECPATH}/tmp_$$.err
    exit -1
fi

while read summary_state
do
    if [ "$summary_state" != "Synchronized" ];then
        echo `date "+%Y-%m-%d %H:%M:%S"`" : ERROR: master sync status is '$summary_state' !!!" |tee -a ${LOGFILE}
        IS_OK=0
    fi
done <${EXECPATH}/tmp_$$.res

rm ${EXECPATH}/tmp_$$.res
rm ${EXECPATH}/tmp_$$.err


if [ "$IS_OK" -eq 1 ];then
    echo "sync is ok."
else
    exit -1
fi