Pensare? Perché pensare! Abbiamo i computer che lo fanno per noi.

— Jean Rostand

Space disk monitor

http://www.stenoweb.it/files/blog/diskusage.png If the servers to be monitored are many, can be difficult sometimes to control disk usage of all. Here we show a handy script found in the Ubuntu forums that help us by sending a simple e-mail if disk usage exceeds, for example, 90%.

Create script

We create the script:

sudo nano /usr/bin/diskusage

and write this:
#!/bin/sh
df -H | grep -vE '^Filesystem|none|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge 90 ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
mail -s "Alert: Almost out of disk space $usep%" youremailaddress@gmail.com
fi
done

replace youremailaddress@gmail.com with our email address, and remember also to make the script executable:
sudo chmod +x /usr/bin/diskusage

Using the script

Then, make add an entry to our root crontab:

sudo crontab -e

to run this daily:
10 0 * * * /usr/bin/diskusage

Well. If the usage of any of the mounted partitions exceed 90%, we receive an email like this:
Subject: Alert: Almost out of disk space 90%

Running out of space "/dev/sda1 (90%)" on myserver as on mar dic 14 16:58:23 CET 2010