Alarming on High Disk Usage using BASH + AWK + Crontab Job


Disk high usage are one of the common cause of server down. Some administrators would create a 8GB empty file, and once the disk usage is exceeding a threshold, they would just delete the empty 8GB file to regain the disk space and gives enough time for proper fixing/investigation.

Here is a nice BASH script that uses the AWK Programming language to find out the disk usage percentage for the mount point “/” aka root. If it exceeds a threshold e.g. 80%, it will send an email to notify you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
# check_hdd.sh
 
usage=`df -h | awk '{if ($6=="/") {print substr($5,0,length($5)-1)}}'`
threshold=80
 
if [[ $usage -gt $threshold ]]; then
    echo "Diskspace: $usage% Exceeding $threshold%"
    # add your command to send an email. For example:
    echo "Your Server Load $(echo $HOSTNAME) Alert Needs Attention! (High Disk Usage)" | mail -s " $usage% Exceeding $threshold% $(date)" support@helloacm.com
    php /var/www/mail.php "Your Server $(echo $HOSTNAME) Diskspace: $usage% Exceeding $threshold% $(date)"
    exit 1
else
    echo "Diskspace: $usage%"
    exit 0
fi
#!/bin/bash
# check_hdd.sh

usage=`df -h | awk '{if ($6=="/") {print substr($5,0,length($5)-1)}}'`
threshold=80

if [[ $usage -gt $threshold ]]; then
    echo "Diskspace: $usage% Exceeding $threshold%"
    # add your command to send an email. For example:
    echo "Your Server Load $(echo $HOSTNAME) Alert Needs Attention! (High Disk Usage)" | mail -s " $usage% Exceeding $threshold% $(date)" [email protected]
    php /var/www/mail.php "Your Server $(echo $HOSTNAME) Diskspace: $usage% Exceeding $threshold% $(date)"
    exit 1
else
    echo "Diskspace: $usage%"
    exit 0
fi

You can save this as check_hdd.sh (and make sure you give it a proper permission e.g. chmod +x) and put this in Crontab Job that schedules running every hour or so.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
279 words
Last Post: Teaching Kids Programming - Implement the Accumulate Function in Python
Next Post: Teaching Kids Programming - Hour and Minute Angle on a Clock

The Permanent URL is: Alarming on High Disk Usage using BASH + AWK + Crontab Job

Leave a Reply