Prepared by: Anwer Sadath Abdhul Muttaliff
This project demonstrates how to create a Bash script to monitor system resources (CPU, memory, disk, and network usage) and send alerts when usage exceeds predefined thresholds. The script also displays alerts in the current terminal session.
system_monitor.sh
#!/bin/bash
# Threshold values for alerts
CPU_THRESHOLD=80 # Percentage
MEMORY_THRESHOLD=80 # Percentage
DISK_THRESHOLD=90 # Percentage
NETWORK_THRESHOLD=100000 # KB/s (Set as needed)
# Email details (Update these with your actual email settings)
EMAIL_TO="your_email@example.com"
EMAIL_SUBJECT="System Alert: Resource Usage Exceeded"
# Function to send an email alert
send_email() {
local message="$1"
echo "$message" | mail -s "$EMAIL_SUBJECT" "$EMAIL_TO"
}
# Function to print alert to the current terminal
alert_terminal() {
local message="$1"
echo "$message" > /dev/pts/$(who am i | awk '{print $2}')
}
# Monitor CPU usage
check_cpu_usage() {
local cpu_usage
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
cpu_usage=${cpu_usage%.*} # Remove decimal places
if (( cpu_usage > CPU_THRESHOLD )); then
local message="High CPU Usage Alert: $cpu_usage% (Threshold: $CPU_THRESHOLD%)"
send_email "$message"
alert_terminal "$message"
fi
}
# Monitor memory usage
check_memory_usage() {
local mem_usage
mem_usage=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100.0}')
if (( mem_usage > MEMORY_THRESHOLD )); then
local message="High Memory Usage Alert: $mem_usage% (Threshold: $MEMORY_THRESHOLD%)"
send_email "$message"
alert_terminal "$message"
fi
}
# Monitor disk usage
check_disk_usage() {
local disk_usage
disk_usage=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if (( disk_usage > DISK_THRESHOLD )); then
local message="High Disk Usage Alert: $disk_usage% (Threshold: $DISK_THRESHOLD%)"
send_email "$message"
alert_terminal "$message"
fi
}
# Monitor network usage
check_network_usage() {
local rx_bytes tx_bytes total_bytes
rx_bytes=$(cat /sys/class/net/eth0/statistics/rx_bytes)
tx_bytes=$(cat /sys/class/net/eth0/statistics/tx_bytes)
total_bytes=$(( (rx_bytes + tx_bytes) / 1024 )) # Convert to KB/s
if (( total_bytes > NETWORK_THRESHOLD )); then
local message="High Network Usage Alert: ${total_bytes}KB/s (Threshold: ${NETWORK_THRESHOLD}KB/s)"
send_email "$message"
alert_terminal "$message"
fi
}
# Main monitoring function
monitor_system() {
while true; do
check_cpu_usage
check_memory_usage
check_disk_usage
check_network_usage
sleep 60 # Check every 60 seconds
done
}
# Start monitoring
monitor_system
top
.free
.df
./sys/class/net/eth0
.CPU_THRESHOLD
, MEMORY_THRESHOLD
, etc.).sleep
value (default: 60 seconds).To enable email notifications, install mailx
or sendmail
:
sudo apt install mailutils -y # For Debian/Ubuntu
sudo yum install mailx -y # For CentOS/RHEL
Update the EMAIL_TO
variable with your email address.
Replace eth0
in the script with your actual network interface. Check your interface using:
ip addr
system_monitor.sh
.chmod +x system_monitor.sh
./system_monitor.sh