#!/bin/bash # Configuration DISK_PARTITION="/" # The disk partition to monitor (e.g., / or /var) THRESHOLD=90 # Percentage threshold (e.g., 90 means 90% full) ALERT_EMAIL="admin@example.com" HOSTNAME=$(hostname) # Get current disk usage percentage USAGE=$(df -h "$DISK_PARTITION" | grep "$DISK_PARTITION" | awk '{print $5}' | sed 's/%//g') if [[ -z "$USAGE" ]]; then echo "Error: Could not determine disk usage for $DISK_PARTITION. Check partition name." exit 1 fi echo "Current disk usage for $DISK_PARTITION on $HOSTNAME: $USAGE%" if (( USAGE > THRESHOLD )); then SUBJECT="CRITICAL ALERT: High Disk Usage on $HOSTNAME for $DISK_PARTITION" BODY="Disk usage for $DISK_PARTITION is at $USAGE%, which exceeds the threshold of $THRESHOLD%. Please take action immediately. Full disk usage report: $(df -h "$DISK_PARTITION")" echo "Sending email alert to $ALERT_EMAIL..." echo -e "$BODY" | mail -s "$SUBJECT" "$ALERT_EMAIL" if [ $? -eq 0 ]; then echo "Alert email sent successfully." else echo "Error: Failed to send alert email." fi else echo "Disk usage is within acceptable limits ($USAGE% < $THRESHOLD%). No alert sent." fi