amuck-landowner

Bash scripting help... Check disk space, send email when disk is XX% full.

MannDude

Just a dude
vpsBoard Founder
Moderator
Coding isn't a strong point of mine but I've got a basic backup script that I use and while it gets the job done it still requires me to manually audit and check the server disk space and manually create additional storage by removing archived data as needed. In an effort to streamline things a bit more, I'd like to simply add a little bit of code that will check the disk space of the backup server, and say, at 80% (or 90%) send an email to my address that pipes to my phone for urgent action stuff.

Thing is, I'm not quite sure where to begin. The only similar thing I have in operation at this time is a script that emails me to inform me when someone logs into the server via SSH but I'm not sure where I'd begin with slicing that up to piece together what I want for the disk space check.

Anyone care to give this a shot?
 

KuJoe

Well-Known Member
Verified Provider
Here you go, just tested it and it works but I'm sure somebody here will have a more efficient method of doing it. :)

Just change the device on the second line and change the servername and your e-mail on line 4.


#!/bin/sh
percentused=`df -h | grep "/dev/vda1" | awk '{print $5}' | rev | cut -c 2- | rev`
if [ $percentused -ge 80 ] ; then
echo "Your disk space is getting mighty low right about now." | mail -s "Disk space used is at $percentused percent for servername" [email protected]
fi

EDIT: Somewhat related, if you're having issues getting e-mails delivered from your server I would recommend Mandrill's free service for mail delivery and you can use this script I wrote to replace sendmail so e-mail from your server doesn't get sent to spam or undelivered for some free e-mail services: https://github.com/KuJoe/Sendmail-to-SSMTP
 
Last edited by a moderator:

MannDude

Just a dude
vpsBoard Founder
Moderator
Here you go, just tested it and it works but I'm sure somebody here will have a more efficient method of doing it. :)

Just change the device on the second line and change the servername and your e-mail on line 4.


#!/bin/sh
percentused=`df -h | grep "/dev/vda1" | awk '{print $5}' | rev | cut -c 2- | rev`
if [ $percentused -ge 80 ] ; then
echo "Your disk space is getting mighty low right about now." | mail -s "Disk space used is at $percentused percent for servername" [email protected]
fi

EDIT: Somewhat related, if you're having issues getting e-mails delivered from your server I would recommend Mandrill's free service for mail delivery and you can use this script I wrote to replace sendmail so e-mail from your server doesn't get sent to spam or undelivered for some free e-mail services: https://github.com/KuJoe/Sendmail-to-SSMTP
Awesome!

I'm messing with backups tonight so am going to give that a whirl! Mind if I use it with my 'simple backup script' that is more or less a copy of a tutorial somewhere (Actually, may have been from your KB) that I tinkered with a bit? Actually I need to update that anyhow as the script I'm actually running for backups is different a bit, but that's more or less the jist of it.
 

Jonathan

Woohoo
Administrator
Verified Provider
Hmm I just posted a script in response to this exact question on StackOverflow (or exchange whichever) a few days ago.  Wasn't you by chance was it @MannDude?
 
Top
amuck-landowner