1

I am building a new CentOS 6.4 server.

I was wondering if there is a way I can receive a warning email when the use of any partition exceeds 80% in the server.

EDIT:

As Aaron Digulla pointed out, this question is better suited for Server Fault.

Please view or answer this question in the following post in Server Fault. https://serverfault.com/questions/570647/linux-how-to-receive-warning-email-from-a-server-when-not-much-hard-drive-space

EDIT:

Server Fault put my post on hold. I guess I have no choice but continue this post here.

As Sayajin suggested, the following script can do the trick.

usage=$(df | awk '{print $1,$5}' | tail -n +2 | tr -d '%');
echo "$usage" | while read FS PERCENT; do [ "$PERCENT" -ge "80" ] && echo "$FS has used ${PERCENT}% Disk Space"; done

This is exactly what I want to do. However for my case, the df output looks something like this:

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup-LogVol01
                     197836036   5765212 182021288   4% /

As you see, filesystem and Use% are not in the same line. This causes $1 and $5 are not the info I want to get. Any idea to fix this? Thanks.

EDIT: The trick is

df -P

I also found shell script example in the following link doing exactly the same thing: http://bash.cyberciti.biz/monitoring/shell-script-monitor-unix-linux-diskspace/

Community
  • 1
  • 1
Xin
  • 1,169
  • 1
  • 10
  • 20

3 Answers3

0

Install a monitoring service like Nagios.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

You could always create a bash script & then have it email you:

usage=$(df | awk '{print $1,$5}' | tail -n +2 | tr -d '%');
echo "$usage" | while read FS PERCENT; do [ "$PERCENT" -ge "80" ] && echo "$FS has used ${PERCENT}% Disk Space"; done

Obviously instead of the && echo "$FS has used ${PERCENT}% Disk Space" you would send the warning email.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
Sayajin
  • 33
  • 2
  • 9
0

For people who do not have a monitoring system like Nagios (as suggested by @Aaron Digulla), this simple script can do the job :

#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    mail -s 'Disk Space Alert' mailid@domainname.com << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi

Then just add a cron job.

fred727
  • 2,644
  • 1
  • 20
  • 16