This shows you the differences between two versions of the page.
postgresql_replication_monitor [2011/07/12 16:22] k2patel |
postgresql_replication_monitor [2020/08/10 02:35] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== postgresql replication monitor ====== | ||
- | <code bash> | ||
- | #!/bin/bash | ||
- | ############################# | ||
- | # Requirement Specification | ||
- | ############################# | ||
- | # - It require `bc` | ||
- | # - Only monitor streaming replication status | ||
- | |||
- | |||
- | ############################ | ||
- | ## Define Custom Variables | ||
- | ############################ | ||
- | |||
- | ## Define Nagios utils | ||
- | LIBEXEC="/usr/lib64/nagios/plugins" | ||
- | . $LIBEXEC/utils.sh | ||
- | |||
- | ## Exit Codes | ||
- | STATE_OK=0 | ||
- | STATE_WARNING=1 | ||
- | STATE_CRITICAL=2 | ||
- | STATE_UNKNOWN=3 | ||
- | |||
- | ## Primary Server Information | ||
- | export p_host=127.0.0.1 | ||
- | export p_port=5436 | ||
- | |||
- | ## Secondary Server Information (generally not used unless you have diff. port) | ||
- | ## In case of diff. port please change resules in codes section. | ||
- | export s_host=localhost | ||
- | export s_port=5432 | ||
- | |||
- | ## Psql / bc Location | ||
- | export psql=/usr/local/pg-9.0.3/bin/psql | ||
- | export bc=/usr/bin/bc | ||
- | |||
- | ## Delay Limit | ||
- | export dlylmt=100000 | ||
- | |||
- | |||
- | ## Results in codes | ||
- | prime_num=$($psql -t -c "SELECT pg_current_xlog_location()" -h$p_host -p$p_port) | ||
- | sec_num=$($psql -t -c "select pg_last_xlog_receive_location()") | ||
- | thr_num=$($psql -t -c "select pg_last_xlog_replay_location()") | ||
- | |||
- | pr_num=$(echo $prime_num | awk -F / '{print $2}') | ||
- | sr_num=$(echo $sec_num | awk -F / '{print $2}') | ||
- | tr_num=$(echo $thr_num | awk -F / '{print $2}') | ||
- | |||
- | pr_int_num=$(echo "ibase=16; $pr_num" | $bc) | ||
- | sr_int_num=$(echo "ibase=16; $sr_num" | $bc) | ||
- | tr_int_num=$(echo "ibase=16; $tr_num" | $bc) | ||
- | |||
- | echo "Prime $pr_int_num" | ||
- | echo "Secon $sr_int_num" | ||
- | echo "thrir $tr_int_num" | ||
- | |||
- | ########################### | ||
- | ## Customization End here | ||
- | ########################### | ||
- | |||
- | |||
- | if [[ $pr_int_num -eq $sr_int_num && $pr_int_num -eq $tr_int_num && $sr_int_num -eq $tr_int_num ]] ; then | ||
- | echo "Stream: Primary: $pr_int_num Secondary: $sr_int_num Import: $tr_int_num : OK" | ||
- | exit $STATE_OK | ||
- | else | ||
- | if [[ $pr_int_num -eq $sr_int_num ]] ; then | ||
- | if [[ $pr_int_num -ne $tr_int_num ]] ; then | ||
- | echo "Stream: Primary: $pr_int_num Secondary: $sr_int_num Import: $tr_int_num : WARNING" | ||
- | exit $STATE_WARNING | ||
- | fi | ||
- | else | ||
- | if [[ $(bc <<< $pr_int_num-$sr_int_num) -gt $dlylmt ]]; then | ||
- | echo "Stream: Primary: $pr_int_num Secondary: $sr_int_num Import: $tr_int_num : CRITICAL" | ||
- | exit $STATE_CRITICAL | ||
- | else | ||
- | echo "Stream: Primary: $pr_int_num Secondary: $sr_int_num Import: $tr_int_num : WARNING" | ||
- | exit $STATE_WARNING | ||
- | fi | ||
- | fi | ||
- | echo "Stream: Primary: $pr_int_num Secondary: $sr_int_num Import: $tr_int_num : UNKNOWN" | ||
- | exit $STATE_UNKNOWN | ||
- | fi | ||
- | </code> |