Skip to content

Instantly share code, notes, and snippets.

@M1ke
Last active June 11, 2024 09:18
Show Gist options
  • Save M1ke/5300297 to your computer and use it in GitHub Desktop.
Save M1ke/5300297 to your computer and use it in GitHub Desktop.
A shell script to check your disk usage and alert you if it hits a limit. Change the value (percentage) in the "if" statement on line 7 to alter the threshold. Change the email address to your email address. See readme for mail.cf instructions and crontab.

Disk Usage Alerter

Install mailutils with:

sudo apt-get install mailutils

Set up the cronjob with:

crontab -e

To send at 10 mins past the hour add:

10 * * * * sh /path/to/script

If you'll be emailing the same domain that your mailname is set to:

Navigate to /etc/postfix/

sudo vi main.cf

Find line beginning mydestination, it should say something like:

mydestination =  ubuntu, localhost.localdomain, localhost

Crucially it shouldn't contain your mailname or references to it, or your emails will be sent internally on the server.

#!/bin/sh
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
echo $output
used=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge 80 ]; then
echo "The partition \"$partition\" on $(hostname) has used $used% at $(date)" | mail -s "Disk space alert: $used% used" [email protected]
fi
done
@HareshChhelana
Copy link

@seenusankara replace $usep with $used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment