Algorithms, Blockchain and Cloud

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.

In many cases, the “No enough Disk Space” is one of the many reasons that the servers are down or malfunctioning.

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)" 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

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.

We can add a bit details to show the exact amount of disk space remaining (in GB) along the notification.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
 
usage=`df -h / | tail -1 | awk '{print $5}'`
usage=${usage%?}
 
## get the number of remaining hard disk in GB.
remaining=`df -h / | tail -1 | awk '{print $4}'`
 
## change accordingly e.g. 80%
threshold=95
 
if [[ $usage -gt $threshold ]]; then
    echo "Diskspace: $usage% (Remaining $remaining) Exceeding $threshold%"
    php /var/www/mail.php "Your Server $(echo $HOSTNAME) Diskspace: $usage% (Remaining $remaining) Exceeding $threshold% $(date)"
    exit 1
else
    echo "Diskspace: $usage% - Remaining $remaining"
    exit 0
fi
#!/bin/bash

usage=`df -h / | tail -1 | awk '{print $5}'`
usage=${usage%?}

## get the number of remaining hard disk in GB.
remaining=`df -h / | tail -1 | awk '{print $4}'`

## change accordingly e.g. 80%
threshold=95

if [[ $usage -gt $threshold ]]; then
    echo "Diskspace: $usage% (Remaining $remaining) Exceeding $threshold%"
    php /var/www/mail.php "Your Server $(echo $HOSTNAME) Diskspace: $usage% (Remaining $remaining) Exceeding $threshold% $(date)"
    exit 1
else
    echo "Diskspace: $usage% - Remaining $remaining"
    exit 0
fi

Different ways to Alarm on High CPU Usage

To set up an alarm for high CPU usage on Linux, you can use a combination of tools like `top`, `ps`, and `bash` scripting.

Here’s a general outline of how you can achieve this:

Monitor CPU Usage

You can use the `top` command in the terminal to monitor CPU usage in real-time. Run `top` in the terminal and observe the CPU usage of different processes.

Create a Bash Script

You can create a bash script that periodically checks CPU usage and triggers an alarm if it exceeds a certain threshold.

Here’s a simple example script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
THRESHOLD=90
INTERVAL=5
 
while true; do
    CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
    if (( $(echo "$CPU_USAGE > $THRESHOLD" | bc -l) )); then
        echo "High CPU usage detected! Current CPU usage: $CPU_USAGE%"
        # Add your alert mechanism here (e.g., sending an email, triggering a notification)
        sleep $INTERVAL
    else
        echo "Current CPU usage: $CPU_USAGE%"
        sleep $INTERVAL
    fi
done
#!/bin/bash
THRESHOLD=90
INTERVAL=5
 
while true; do
    CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
    if (( $(echo "$CPU_USAGE > $THRESHOLD" | bc -l) )); then
        echo "High CPU usage detected! Current CPU usage: $CPU_USAGE%"
        # Add your alert mechanism here (e.g., sending an email, triggering a notification)
        sleep $INTERVAL
    else
        echo "Current CPU usage: $CPU_USAGE%"
        sleep $INTERVAL
    fi
done

Alert on High CPU when Threshold is set to 10%

Set Up Monitoring

You can run this script in the background using `nohup` or by adding it to your system’s startup scripts to monitor CPU usage continuously.

Alert Mechanism

Within the script, you can customize the alert mechanism based on your preference. This could be sending an email, triggering a notification, or executing a specific command.

Testing

Once you have set up the script, you can test it by creating artificial load on your system to trigger high CPU usage and ensure that the alarm works as expected.

Please note that this is a basic example, and you can customize the script further based on your specific requirements and environment. Additionally, you may need to adjust the threshold value and monitoring interval based on your system’s normal CPU usage patterns.

–EOF (The Ultimate Computing & Technology Blog) —

815 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 (AMP Version)

Exit mobile version