Skip to content

Instantly share code, notes, and snippets.

@logarytm
Created July 22, 2017 11:35
Show Gist options
  • Save logarytm/00c89fad535437b3fa8cd9ac9cd374d3 to your computer and use it in GitHub Desktop.
Save logarytm/00c89fad535437b3fa8cd9ac9cd374d3 to your computer and use it in GitHub Desktop.
disk usage check
#!/bin/bash
email=overlord@localhost
free_threshold=20
next_reminder=$((60*60*24*3))
disks=(/dev/sda1 ...)
for disk in ${disks[*]}; do
total=$(df $disk -P | tail -n 1 | awk '{print $2}')
free=$(df $disk -P | tail -n 1 | awk '{print $4}')
free_percentage=$((free*100/total))
cache_file=~/.du-$(basename $disk)
if [ $free_percentage -le $free_threshold ]; then
last_notified=$(stat -c %Y $cache_file 2>/dev/null || echo 0)
now=$(date +%s)
time_elapsed=$((now-last_notified))
if [ $time_elapsed -ge $next_reminder ]; then
(
echo "There is low (less than $free_threshold%) free disk space left on"
echo -n "the device $disk"
if grep $disk /etc/mtab >>/dev/null 2>&1; then
echo -n ", mounted at $(grep $disk /etc/mtab | awk '{print $2}')"
fi
echo "."
echo
echo "Kindest regards,"
echo "$(uname -s) at $(uname -n)"
) | mail -s "Alert - Device $(basename $disk) is low on disk space" $email &&
touch $cache_file
fi
else
rm -f $cache_file
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment