Skip to content

Instantly share code, notes, and snippets.

@kkyr
Last active January 14, 2025 14:59
Show Gist options
  • Save kkyr/4e94fee3895000614e36d69394bfbbd7 to your computer and use it in GitHub Desktop.
Save kkyr/4e94fee3895000614e36d69394bfbbd7 to your computer and use it in GitHub Desktop.
Monitor disk space
#!/usr/bin/env bash
set -e
HEALTHCHECKS_URL="https://hc-ping.com/<id>"
BOT_TOKEN="<telegram-bot-token>"
CHAT_ID="<telegram-chat-id>"
send_telegram_message() {
local message="$1"
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" \
-d chat_id="$CHAT_ID" \
-d text="$message" \
-d parse_mode="HTML"
}
DISK_THRESHOLD=10
IGNORE_FS="tmpfs|devtmpfs|efivarfs" # Filesystems to ignore
check_disk_utilization() {
df -h | grep -vE "^Filesystem|${IGNORE_FS}" | while read -r line; do
filesystem=$(echo "$line" | awk '{print $1}')
mount_point=$(echo "$line" | awk '{print $6}')
usage_percent=$(echo "$line" | awk '{print $5}' | sed 's/%//')
if (( usage_percent > DISK_THRESHOLD )); then
local message="🚨 <b>High Disk Usage Alert</b> 🚨%0A"
message+="<b>Filesystem:</b> ${filesystem}%0A"
message+="<b>Mount Point:</b> ${mount_point}%0A"
message+="<b>Usage:</b> ${usage_percent}%%0A"
send_telegram_message "$message"
fi
done
}
check_disk_utilization
curl -fsS -m 10 --retry 5 -o /dev/null "$HEALTHCHECKS_URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment