Skip to content

Instantly share code, notes, and snippets.

@theeye-io
Created February 18, 2018 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save theeye-io/bb606b8afd5b706e32f61b6a3c43eb3c to your computer and use it in GitHub Desktop.
Save theeye-io/bb606b8afd5b706e32f61b6a3c43eb3c to your computer and use it in GitHub Desktop.
This script monitors free storage for a given database and limit.
#!/bin/bash
if [ $# -ne 2 ];then echo "Run $0 db-instance-identifier freeStorageLimit" ; exit ; fi
database=$1
limit=$2
region=$(/usr/bin/curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep region | awk -F\" '{print $4}')
conv=$(echo '1024^3' | bc)
freeStorage=$(aws cloudwatch --region ${region} get-metric-statistics --start-time $(date -u +%FT%T --date '1 minute ago') --end-time $(date -u +%FT%T) --period 60 --namespace AWS/RDS --statistics Maximum --dimensions Name=DBInstanceIdentifier,Value=${database} --metric-name FreeStorageSpace | jq -r '.Datapoints[].Maximum')
freeStorage=$(($freeStorage/$conv)) #bytes to gb
totalStorage=$(aws rds describe-db-instances --db-instance-identifier ${database} --region ${region} | jq -r '.DBInstances[].AllocatedStorage')
if [ "$freeStorage" -gt "$limit" ]
then
echo "FreeStorageSpace is ${freeStorage}gb TotalStorage: ${totalStorage}gb"
echo normal
else
echo "FREE STORAGE WARNING | FreeStorageSpace is less than ${limit}gb FreeStorage: ${freeStorage}gb TotalStorage: ${totalStorage}gb"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment