20181219-PostgreSQL 流復制監控腳本
阿新 • • 發佈:2018-12-19
pos postgresq play and nod user location ash pre
PostgreSQL 流復制監控腳本
https://github.com/AndyYHM/Writing/blob/PostgreSQL/20181219-PostgreSQL%20Stream%20MON.md
整理之前的腳本文檔,搬家至博客園,梳理一下之前寫的shell腳本
適用於PostgreSQL版本10、版本9替換函數名稱即可,系統centos 7驗證
_xlog_location<=> _wal_lsn
_location <=> _lsn
1. 腳本輸出效果
1.1 主節點顯示效果
STREAM_ROLE : Master ------------------------------ Replication Client Info: ------------------------------ PID : 25424 CLIENT_ADDR : 20.9.6.213 SYNC_STATE : async STATE : streaming WRITE_DIFF : 0bytes FLUSH_DIFF : 0bytes REPLAY_DIFF : 0bytes Replication Client Info: ------------------------------ PID : 24586 CLIENT_ADDR : 20.9.6.212 SYNC_STATE : sync STATE : streaming WRITE_DIFF : 0bytes FLUSH_DIFF : 0bytes REPLAY_DIFF : 0bytes
1.2 從節點效果
STREAM_ROLE : Slave ------------------------------ Replication Info ------------------------------ CLUSTER_STATE : in_archive_recovery SERVER_ADDR : 20.9.6.219 USER : replicator APP_NAME : node12 TARGET_TIMELINE: READ_ONLY : on
2. 功能說明
- 判定主機角色
- 如果為Master節點,則顯示復制節點信息,及與主節點差值
- 如果為Replication節點,則顯示為本節點相關信息
3. 使用說明
- 根據實際環境修改腳本pgdata=""
- postgres系統用戶執行,保證有執行權限
- 本地執行腳本
sh pg10_stream.sh
4. pg10_stream.sh腳本內容
#!/bin/bash ################################ ## Author: [email protected] ## Version: 1.0 ## Date: 20181219 ################################ clear host_var="/tmp" pgdata="/pgdb/pgdata" work_dir=$(cd `dirname $0`;pwd) cd $work_dir is_master=$(psql -h $host_var -c ‘select pg_is_in_recovery();‘| sed -n 3p) if [ "$is_master" == " f" ];then rep=$(psql -h $host_var -c ‘select pid, client_addr, sync_state, state,pg_size_pretty( pg_wal_lsn_diff(pg_current_wal_lsn(),write_lsn)) as write_diff, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(),flush_lsn)) as flush_diff, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(),replay_lsn)) as replay_diff from pg_stat_replication;‘| sed -n ‘3,$‘p |tac | sed -n ‘3,$‘p | sed s/" *"//g ) if [ ! "$rep" ];then echo "Waring! No Replication Client Found" else printf "\n\t%-15s: %-15s\n" "STREAM_ROLE" "Master" echo -e "\t------------------------------" echo "$rep" | while read i do echo -e "\tReplication Client Info:\n\t------------------------------" a=(null PID CLIENT_ADDR SYNC_STATE STATE WRITE_DIFF FLUSH_DIFF REPLAY_DIFF) for j in `seq 7` do k=${a[j]} echo -e "$i" | awk -v k=$k -v j=$j -F"|" ‘{printf "\t%-15s: %-15s\n",k,$j}‘ done echo "" done client_check_info=(`psql -h $host_var -c ‘select client_addr from pg_stat_replication;‘ | sed -n ‘3,$‘p |tac | sed -n ‘3,$‘p | sed s/" *"//g`) if [ ! -f pg_stream_client ]; then echo "$client_check_info" > pg_stream_client else client_before=(`cat pg_stream_client`) if [ ${#client_before[*]} -gt 0 ]; then for i in `seq ${#client_before[*]}` do index=$(expr $i - 1) if [ "${client_check_info[$index]}"x != "${client_before[$index]}"x ];then printf "\tWarning! Replication Client Lost.\n" else echo "$client_check_info" > pg_stream_client fi done else printf "\tAttention! No Client Info found in pg_stream_client.\n" fi fi fi elif [ "$is_master" == " t" ];then declare -A connect_info_dict conninfo=$(cat $pgdata/recovery.conf| grep -i "^primary_conninfo" | awk -F"‘" ‘{print $2}‘ |sed s/" *"/" "/g |sed s/"="/"\"]=\""/g | sed s/" "/"\" [\""/g | sed s/^/"[\""/g |sed s/$/"\""/g|sed s/" "/"\n"/g) eval "connect_info_dict=($conninfo)" cluster_state=$(pg_controldata | grep cluster | sed s/" *"/" "/g | cut -d ":" -f2| sed s/"^ "//g| sed s/" "/"_"/g) server_addr=${connect_info_dict["host"]} user=${connect_info_dict["user"]} application_name=${connect_info_dict["application_name"]} recovery_target_timeline=${connect_info_dict["recovery_target_timeline"]} read_only=$(psql -h $host_var -c ‘show transaction_read_only;‘ | sed -n ‘3p‘) output(){ printf "\t%-15s: %-15s\n" $1 $2 } printf "\n\t%-15s: %-15s\n" "STREAM_ROLE" "Slave" echo -e "\t------------------------------" echo -e "\tReplication Info\n\t------------------------------" output "CLUSTER_STATE" $cluster_state output "SERVER_ADDR" $server_addr output "USER" $user output "APP_NAME" $application_name output "TARGET_TIMELINE" $recovery_target_timeline output "READ_ONLY" $read_only echo "" else echo "Warning! Can‘t Connect to Database. Please Check Database Status " fi exit 0 psql -c ‘select application_name,client_addr ,sync_state from pg_stat_replication;‘ | sed -n ‘3,$‘p |tac | sed -n ‘3,$‘p | sed s/" *"//g
20181219-PostgreSQL 流復制監控腳本