Skip to content

Instantly share code, notes, and snippets.

@eduherraiz
Created October 29, 2014 13:43
Show Gist options
  • Save eduherraiz/1fc1469fbcd850f2b8e5 to your computer and use it in GitHub Desktop.
Save eduherraiz/1fc1469fbcd850f2b8e5 to your computer and use it in GitHub Desktop.
#!/bin/bash
# $Id: check_slave_replication.sh 3421 2013-08-09 07:52:44Z jmorano $
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
## Master (p_) and Slave (s_) DB Server Information
export s_host="{{vip_slave}}"
export s_port="5432"
export p_db=$1
export p_host="{{vip_master}}"
export p_port="5432"
export psql=/opt/postgresql/bin/psql
export bc=/usr/bin/bc
## Limits
export critical_limit=83886080 # 5 * 16MB, size of 5 WAL files
export warning_limit=16777216 # 16 MB, size of 1 WAL file
pg_password="{{postgres-replicator-password}}"
pg_user="{{repuser}}"
master_lag=`PGPASSWORD=$pg_password psql -U $pg_user -h$p_host -p$p_port -A -t -c "SELECT pg_xlog_location_diff(pg_current_xlog_location(), '0/0') AS offset" $p_db`
slave_lag=`PGPASSWORD=$pg_password psql -U $pg_user -h$s_host -p$s_port -A -t -c "SELECT pg_xlog_location_diff(pg_last_xlog_receive_location(), '0/0') AS receive" $p_db`
replay_lag=`PGPASSWORD=$pg_password psql -U $pg_user -h$s_host -p$s_port -A -t -c "SELECT pg_xlog_location_diff(pg_last_xlog_replay_location(), '0/0') AS replay" $p_db`
replay_timediff=`PGPASSWORD=$pg_password psql -U $pg_user -h$s_host -p$s_port -A -t -c "SELECT NOW() - pg_last_xact_replay_timestamp() AS replication_delay" $p_db`
bytes_diff=`bc <<< $master_lag-$replay_lag`
if [[ $master_lag -eq '' || $slave_lag -eq '' || $replay_lag -eq '' ]]; then
echo "CRITICAL: Stream has no value to compare (is replication configured or connectivity problem?)"
exit $STATE_CRITICAL
else
if [[ $master_lag -eq $slave_lag && $master_lag -eq $replay_lag && $slave_lag -eq $replay_lag ]] ; then
echo "OK: Stream: MASTER:$master_lag Slave:$slave_lag Replay:$replay_lag | bytes_delay=$bytes_diff "
exit $STATE_OK
else
if [[ $master_lag -eq $slave_lag ]] ; then
if [[ $master_lag -ne $replay_lag ]] ; then
if [ $(bc <<< $master_lag-$replay_lag) -lt $warning_limit ]; then
echo "OK: Stream: MASTER:$master_lag Replay:$replay_lag :: REPLAY BEHIND | bytes_delay=$bytes_diff "
exit $STATE_OK
else
echo "WARNING: Stream: MASTER:$master_lag Replay:$replay_lag :: REPLAY $(bc <<< $master_lag-$replay_lag)bytes BEHIND (${replay_timediff}seconds) | bytes_delay=$bytes_diff "
exit $STATE_WARNING
fi
fi
else
if [ $(bc <<< $master_lag-$slave_lag) -gt $critical_limit ]; then
echo "CRITICAL: Stream: MASTER:$master_lag Slave:$slave_lag :: STREAM BEYOND CRITICAL LIMIT ($(bc <<< $master_lag-$slave_lag)bytes) | bytes_delay=$bytes_diff "
exit $STATE_CRITICAL
else
if [ $(bc <<< $master_lag-$slave_lag) -lt $warning_limit ]; then
echo "OK: Stream: MASTER:$master_lag Slave:$slave_lag Replay:$replay_lag :: STREAM BEHIND | bytes_delay=$bytes_diff "
exit $STATE_OK
else
echo "WARNING: Stream: MASTER:$master_lag Slave:$slave_lag :: STREAM BEYOND WARNING LIMIT ($(bc <<< $master_lag-$replay_lag)bytes) | bytes_delay=$bytes_diff "
exit $STATE_WARNING
fi
fi
fi
echo "UNKNOWN: Stream: MASTER: $master_lag Slave: $slave_lag Replay: $replay_lag"
exit $STATE_UNKNOWN
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment