System Monitoring and Alert Script

Prepared by: Anwer Sadath Abdhul Muttaliff

Project Overview

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.

1. Script for System Monitoring

Bash Script: 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

2. What the Script Does

3. Requirements

4. How to Use

  1. Save the script as system_monitor.sh.
  2. Make the script executable:
  3. chmod +x system_monitor.sh
  4. Run the script:
  5. ./system_monitor.sh
  6. Monitor your system and receive alerts when thresholds are exceeded.
Back to Top Back to Home