Showing posts with label disk. Show all posts
Showing posts with label disk. Show all posts

Sunday, January 13, 2013

Linux Server Disk Space Email Notification

I ran into a situation the other day where a virtual server I have hosted with limited disk space just running a couple of MySQL databases and a web server crashed (so to speak) because it ran out of disk space. It's not a very important machine that gets little use so it wasn't a big issue but it did prompt me to write a very basic script to simply email me the report on available disk space. This way if the disk space gets low then I can go to the server and clear some of the old data out to free up space before it causes the machine to stop functioning.

I'm using python because it's quick and easy, but similar scripts could be done in just about any language from bash scripts to PHP or c++. Although it seems pointless to use c++ for something so simple.
(If anyone would like to see this in some other language then let me know in the comments and I'll see what I can do)

Here is a screenshot of the python script. Click here to access the code and copy & paste. (Please excuse the advert, it will continue to the site after a few seconds.)
(I am aware that this is simple and lacking things such as error checking. I am also aware that this might not be the best way to achieve this task, but it's similar to other scripts I've done in the past so it was easy just to stick with it.)


You can run this script on the server in a terminal window to confirm it works and then it's a simple case of configure a cron job to send it to you on a specified timeframe such as daily or weekly. The actual setup of the cron job is up to you as it mostly involves you choosing when to run the script.

Also, don't forget to change the to and from email addresses in the script before you run it. It's up to you to add error checking and anything else you might require.

An example of functionality you might like to add to it is the parsing of the disk space report and having the script only email you if the available space reaches a specified low value.


I hope someone else finds this simple script of use. Please feel free to contribute any additions, fixes or comments in the comments section below.