Wednesday, March 28, 2018

ubuntu - Notification for Disk-Space Shortage Issue in Server




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?


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.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...