Sitemap

Disk Usage Check with Bash

May 27, 2025

--

#!/bin/bash

# Get disk usage, excluding the header
DISK_USAGE=$(df -hT | grep -v Filesystem)

# Disk usage threshold
DISK_THRESHOLD=1 # Set to 75% in production

# Initialize the HTML-formatted message
MSG=""

# Get private IP of EC2 instance (works only in AWS)
IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)

# Loop through each disk entry
while IFS= read -r line; do
# Extract usage percentage and partition name
USAGE=$(echo $line | awk '{print $6}' | cut -d "%" -f1)
PARTITION=$(echo $line | awk '{print $7}')

# Check against threshold
if [ "$USAGE" -ge "$DISK_THRESHOLD" ]; then
MSG+="High Disk Usage on $PARTITION: $USAGE % <br>"
fi
done <<< "$DISK_USAGE"

# Optional: echo the message
# echo -e "$MSG"

--

--