Ubuntu – Notification for Disk-Space Shortage Issue in Server

disk-space-utilizationmonitoringUbuntu

I have a ubuntu server, and facing frequent space issue, i.e. logs are eating up lot of disk space. So, I want a check to be applied, so that whenever there is less than 5 GB free disk space, I should get an e-mail notification, so that I can delete the logs. How can I configure this. Do I need any other application?

Best Answer

On my Ubuntu server, I have the following script in /etc/cron.daily that alerts me by email whenever /dev/sdc (my /srv partition) has less than 200MB of free space.

ALERT=200
UNIT=M
PARTITION=/dev/sdc

df -B$UNIT | grep "^$PARTITION" |
while read partition size used free perc mnt ;
do
        free_space=$(echo $free | tr -d $UNIT )
        if [ $free_space -le $ALERT ]; then
                echo "Partition $partition ($mnt) running out of space ($free) on $(hostname) as on $(date)" |
                mail -s "Alert: $mnt almost out of disk space on $(hostname) - $free" root
        fi
done

It was initially taken and adapted from this blog post on nixCraft. Save this into a file in /etc/cron.hourly as root, modify the first 3 lines to suit your server and needs, and make the file executable. If you want to have it executed more often, save it as a script and create a regular cron job.

Note that you will need something providing the mail command, typically from the packages qmail-run or courier-mta.