Created
July 8, 2013 20:24
-
-
Save jaytaylor/5952175 to your computer and use it in GitHub Desktop.
Nagios Postgres replication delay check.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| ## | |
| # Postgres replication delay check. | |
| # | |
| # @author Jay Taylor [@jtaylor] | |
| # | |
| # @date 2013-07-08 | |
| # | |
| while getopts “u:p:d:w:c:” OPTION; do | |
| case $OPTION in | |
| u) | |
| user=$OPTARG | |
| ;; | |
| p) | |
| password=$OPTARG | |
| ;; | |
| d) | |
| dbname=$OPTARG | |
| ;; | |
| w) | |
| warning=$OPTARG | |
| ;; | |
| c) | |
| critical=$OPTARG | |
| ;; | |
| esac | |
| done | |
| if [ -z "${user}" ]; then | |
| echo 'error: missing required parameter: -u [user]' 1>&2 | |
| exit 1 | |
| fi | |
| if [ -z "${password}" ]; then | |
| echo 'error: missing required parameter: -p [password]' 1>&2 | |
| exit 1 | |
| fi | |
| if [ -z "${dbname}" ]; then | |
| echo 'error: missing required parameter: -d [dbname]' 1>&2 | |
| exit 1 | |
| fi | |
| if [ -z "${warning}" ]; then | |
| echo 'error: missing required parameter: -w [warningThresholdSeconds]' 1>&2 | |
| exit 1 | |
| fi | |
| if [ -z "${critical}" ]; then | |
| echo 'error: missing required parameter: -c [criticalThresholdSeconds]' 1>&2 | |
| exit 1 | |
| fi | |
| psqlResult=$(psql "host=localhost user=${user} password=${password} dbname=${dbname}" -c 'SELECT now() - pg_last_xact_replay_timestamp()' 2>&1) | |
| exitCode=$? | |
| if [ $exitCode -ne 0 ]; then | |
| echo "psql exited with non-zero code: $exitCode" | |
| exit 3 | |
| fi | |
| value=$(echo $psqlResult | grep -o '[0-9:\.]\+' | head -n1 | sed 's/00\([:\.]\)/0\1/g' | \ | |
| python -c "import sys; sys.stdout.write('{0}'.format(round(sum((float(x) if '.' in x else int(x)) * 60 ** i for i,x in enumerate(reversed(sys.stdin.read().strip().split(':')))), 2)))") | |
| echo "replication delay=${value}s" | |
| status=$(python -c "import sys; value=float(${value}); sys.stdout.write('OK' if value < float(${warning}) else 'WARNING' if value < float(${critical}) else 'CRITICAL')") | |
| if [[ $status = 'OK' ]]; then | |
| exit 0 | |
| elif [[ $status = 'WARNING' ]]; then | |
| exit 1 | |
| elif [[ $status = 'CRITICAL' ]]; then | |
| exit 2 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment