Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psychosquirrel85/bd261a53a4b1de27e363bb443a822d95 to your computer and use it in GitHub Desktop.
Save psychosquirrel85/bd261a53a4b1de27e363bb443a822d95 to your computer and use it in GitHub Desktop.
Monitors local disk space on EC2 instance and resizes volume when usage is above certain percentage. Filesystem and partition is available, too.
#!/bin/bash
#This script needs a bit of work. I would like to have the option to use xvd* or sd*.
#As of right now you can not use this script if you have sd* attachments or are using
#LVM.
#Set Alert percent and volume expansion increment in GiB
pcent=85
incr=500
#End Variables
instance=$(curl -s http://169.254.169.254/latest/meta-data/instance-id/)
df -TH | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $6 " " $1 " " $2 }' | while read output;
do
echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }')
raw=$(printf '%s\n' "${partition//[[:digit:]]/}")
fs=$(echo $output | awk '{print $3}')
ispart=$(parted $raw print | awk '/Partition\ Table/ {print $3}')
if [ $usep -ge $pcent ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)..."
echo "Attempting to resize the $partition volume"
att=$(aws ec2 describe-volumes --query Volumes[*].[VolumeId,Attachments[0].Device,Attachments[0].InstanceId,Size] --output text | grep $instance | grep $raw)
volume=$(echo $att | awk '{print $1}')
origsize=$(echo $att | awk '{print $4}')
newsize=$(( $origsize + $incr ))
aws ec2 modify-volume --volume-id $volume --size $newsize
sleep 60
function growfs {
case $fs in
ext[2-4])
resize2fs $partition
;;
xfs)
xfs_growfs $partition ;;
esac
}
if [ $ispart -eq "loop" ]
then
echo "Resizing Raw disk"
growfs
else
echo "Resizing Partition"
growpart $partition
growfs
fi
echo "The $partition has been resized. You now are using $usep%."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment