Last active
August 15, 2017 18:12
-
-
Save dexterous/4d60ff213cc4450c4c83cbae5fcc02c7 to your computer and use it in GitHub Desktop.
Quick n' dirty script to monitor PostgreSQL connectivity and connect latency and post metrics to AWS Cloudwatch
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
#!/bin/bash | |
export PGPASSWORD='password' | |
IFS=':' | |
echo '' | |
date '+%F %T' | |
for HOST in www.{foo,bar}.com; do | |
for PORT in 543{2,5}; do | |
PUT_DATA=0 | |
NODE="${HOST}:${PORT}" | |
echo '' | |
echo "Checking $HOST:$PORT" | |
/usr/bin/time --format 'code:%x\ntime:%e' \ | |
psql --host ${HOST} --port ${PORT} --username diagnostic --no-password \ | |
--tuples-only --command 'select version();' \ | |
postgres 2>&1 | while read key value; do | |
case $key in | |
code) | |
if [[ $value -eq 0 ]]; then | |
PUT_DATA=1 | |
else | |
echo "psql exited with code ${value}" | |
fi | |
;; | |
time) | |
if [[ $PUT_DATA -eq 1 ]]; then | |
echo "psql connected to $NODE with latency $value" | |
aws cloudwatch put-metric-data \ | |
--namespace="Org/Connection" \ | |
--metric-name="DBConnLatency" \ | |
--unit="Seconds" \ | |
--value="$value" \ | |
--dimensions="Location=Office,Host=$(hostname),Target=$NODE" | |
echo "posted metric to cloudwatch" | |
fi | |
;; | |
*) | |
if [[ -n $DEBUG ]]; then | |
echo "$key:$value" | |
fi | |
;; | |
esac | |
done | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment