-
-
Save spham/62fe0f2fa6d882627f27a9034c7cc5aa to your computer and use it in GitHub Desktop.
Linux disk space email alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # www.fduran.com | |
| # script that will send an email to EMAIL when disk use in partition PART is bigger than %MAX | |
| # adapt these 3 parameters to your case | |
| MAX=95 | |
| [email protected] | |
| PART=sda1 | |
| USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1` | |
| if [ $USE -gt $MAX ]; then | |
| echo "Percent used: $USE" | mail -s "Running out of disk space" $EMAIL | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you don't want to step up to a full monitoring solution such as Nagios you can create your own scripts for monitoring the things that you want to monitor, such as disk space. The following script alerts you when your root partition is almost full:
`
!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' [email protected] << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi
`
The script sends an email when the disk usage rises above the percentage specified by the THRESHOLD varialbe (90% here).
To run it daily, for example, save the script to the file sample.sh in your home directory, change the email to your email, and add the following line at the end of /etc/crontab file:
@daily ~/sample.sh