Notification for Disk-Space Shortage Issue in Server
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
.
You can use Nagios for monitoring and sending email. But, before setting up monitoring, I would start by setting up logrotate to periodically rotate log files.
The appropriate system depends on where you host your server. For example, if it is hosted on AWS, you can configure Amazon's built-in CloudWatch to monitor its health rather than deploy a new monitoring solution.
You can adopt two different strategies:
- use a custom script which will run every minute/hour/day to detect and notify you about the space shortage
- install and deploy a true monitoring solution as Zabbix or the likes.
For a single server, solution n.1 is surely the faster path. However, for multiple servers and/or to check multiple data point (eg: ram usage, cpu load, etc) the second one is preferable, by far.