#!/bin/bash E_OK=0;E_WARN=1;E_CRIT=2;E_UNKNOWN=3 #HOST="localhost" HOST="/var/run/postgresql" while IFS='|' read -r node_id hname port status lb_weight role select_cnt load_balance_node replication_delay replication_state replication_sync_state last_status_change do if [[ "$role" == "primary" ]]; then master=$hname; break; fi done < <(psql -tA -h localhost -c "SHOW pool_nodes;" postgres postgres) res=$E_OK; res_txt="OK"; res_txt2="" IFS='|' read -r client_addr state sent_lsn write_lsn flush_lsn replay_lsn \ <<< $(echo $(psql -tA -h $master -c 'select client_addr, state, sent_lsn, write_lsn,flush_lsn, replay_lsn from pg_stat_replication;' postgres postgres)) lsn_equal=1 for i in "$write_lsn" "$flush_lsn" "$replay_lsn" do if [[ "$sent_lsn" != "$i" ]]; then lsn_equal=0;break; fi done if [[ $lsn_equal -ne 1 ]]; then res=$E_WARN; res_txt="WARN"; res_txt2="LSNs is not equal"; fi if [[ "$state" != "streaming" ]]; then res=$E_CRIT; res_txt="CRIT"; res_txt2="state != streaming"; fi echo -n "$res_txt: Master is $master. $res_txt2 " echo "client_addr:$client_addr state:$state sent_lsn:$sent_lsn write_lsn:$write_lsn flush_lsn:$flush_lsn replay_lsn:$replay_lsn|result=$res" exit $res