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.
#!/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.
We can add a bit details to show the exact amount of disk space remaining (in GB) along the notification.
#!/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:
#!/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
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.
BASH Programming/Shell
- Alarming on High Disk Usage using BASH, AWK, Crontab Job
- Compute GCD in Bash with Input Validation
- Why a Leading Space in Linux Shell Commands Can Matter?
- How to Get HTTP Response Code using cURL Command?
- Three Interesting/Fun BASH Commands
- One Interesting Linux Command (Steam Locomotive in BASH)
- Simple Bash Function to Repeat a Command N times with Retries
- How to Extract a Domain Name from a Full URL in BASH?
- BASH Function to Compute the Greatest Common Divisor and Least Common Multiples
- BASH Function to Get Memory Information via AWK
- BASH Function to Escape Parameters Argument
- BASH Function to Check if sudo Available
- Full Permutation Algorithm Implementation in BASH
- BASH Function to Install Docker
- A Simple BASH Function/Script to Run a Command in Background
- BASH Script to Compute the Average Ping to a Domain
- A Bash Process Controller to Start, Stop and Restart an Daemon Program
- Linux BASH shell - Echo This if you get angry
- Bash SHELL, Chess Board Printing
–EOF (The Ultimate Computing & Technology Blog) —
844 wordsLast Post: Teaching Kids Programming - Implement the Accumulate Function in Python
Next Post: Teaching Kids Programming - Hour and Minute Angle on a Clock
