5

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?

4 Answers 4

4

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.

10

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.

2
  • 3
    +1 for not only answering the nominal question, but also employing logrotate to avoid the need to manually intervene in the first place. Apr 18, 2016 at 14:17
  • 2
    What Monty Harder said. Apr 18, 2016 at 20:12
3

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.

0

Can some one help me out with my below concern? Issue : need to setup one automatic job to perform disc space clear on Linux machine but the job which we create should be outside the Linux machine , so once we trigger that job the space in that Linux machine needs to be get cleared Any suggestions?

2
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – Dave M
    Dec 11, 2021 at 14:42
  • This is another question, not an answer.
    – рüффп
    Dec 20, 2021 at 22:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .