Skip to content

Instantly share code, notes, and snippets.

@gansbrest
Forked from akinnard/unusedrds.sh
Last active August 22, 2018 21:50
Show Gist options
  • Save gansbrest/6f25301b62ff0a81e8a5308867642888 to your computer and use it in GitHub Desktop.
Save gansbrest/6f25301b62ff0a81e8a5308867642888 to your computer and use it in GitHub Desktop.
Find unused rds instances
#!/bin/bash
osname=$( uname )
if [[ $osname == "Darwin" ]]; then
date="gdate"
else
date="date"
fi
DAYSBACK=14
REGION=us-west-2
PERIOD=3600
SHOWDELETECMD=FALSE
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-d|--daysback)
DAYSBACK="$2"
shift # past argument
shift # past value
;;
-r|--region)
REGION="$2"
shift # past argument
shift # past value
;;
-p|--period)
PERIOD="$2"
shift # past argument
shift # past value
;;
--deletecmd)
SHOWDELETECMD=TRUE
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
UTCDATE=$($date -u +%Y-%m-%dT%H:%M:%S)
STARTDATE=$($date -u -d "$DAYSBACK days ago" "+%Y-%m-%dT%H:%M:%S")
echo "Interval = $DAYSBACK days"
echo "period = $PERIOD"
echo "region = $REGION"
servers=$(aws rds describe-db-instances --output text --query 'DBInstances[*].{DBInstanceIdentifier:DBInstanceIdentifier,InstanceCreateTime:InstanceCreateTime,DBInstanceStatus:DBInstanceStatus}' | grep available)
if [ "$?" != "0" ]
then
# STOP Process
# Future, send email/slack alert
echo "Cannot connect to AWS. Verify you have the aws cli installed and configured" 1>&2
exit 1
fi
echo ""
echo "There have been 0 maximun connections in the last $DAYSBACK days for the following servers:"
echo ""
echo "---------------------------------------------------------------------------------"
#echo "$servers"
IFS=$'\n' # make newlines the only separator
for j in $servers
do
server=$(echo "$j" | cut -f1 -d$'\t')
update=$(echo "$j" | cut -f3 -d$'\t')
#echo "aws cloudwatch get-metric-statistics --metric-name DatabaseConnections --start-time $STARTDATE --end-time $UTCDATE --period $PERIOD --namespace AWS/RDS --statistics Maximum --dimensions Name=DBInstanceIdentifier,Value=$server --output text --query 'Datapoints[0].{Maximum:Maximum}'"
connections=$(aws cloudwatch get-metric-statistics --metric-name DatabaseConnections --region $REGION --start-time $STARTDATE --end-time $UTCDATE --period $PERIOD --namespace AWS/RDS --statistics Maximum --dimensions Name=DBInstanceIdentifier,Value=$server --output text --query 'avg(Datapoints[*].Maximum)')
if [ "$connections" == "0.0" ]
then
echo "$server ( has been up since $update )"
if $SHOWDELETECMD; then
echo "To terminate this instance run one of the following commands:"
echo "aws rds delete-db-instance --db-instance-identifier $server --final-db-snapshot-identifier ${server}-final-${MYDATE}"
echo "aws rds delete-db-instance --db-instance-identifier $server --skip-final-snapshot"
fi
echo "---------------------------------------------------------------------------------"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment