How to Monitor VPS Disk Usage and Get Alerts via Webhook

Running out of disk space is one of the most common causes of server downtime. Whether it’s uncapped log files, Docker containers running wild, or just natural data growth, a full disk can crash your database and stop your applications.

In this tutorial, we will write a simple bash script to check your disk usage and trigger an AlertHub notification if it crosses a safety threshold (80%).

Prerequisites

  • A Linux VPS (Ubuntu, Debian, CentOS, etc.)
  • curl installed (sudo apt install curl or sudo yum install curl)
  • An AlertHub Project Key (Get it from your dashboard)

The Monitoring Script

We will create a script named check_disk.sh. This script uses the df command to inspect filesystem usage.

  1. Create the file:
nano ~/check_disk.sh
  1. Paste the following content:
#!/bin/bash

# Configuration
THRESHOLD=80
ALERTHUB_KEY="your_sk_key_here"
PROJECT_URL="https://api.thealerthub.com/sendAlert"

# Check disk usage of the root filesystem "/"
# awk prints the 5th column (percentage) and sed removes the '%' sign
CURRENT_USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')

if [ "$CURRENT_USAGE" -gt "$THRESHOLD" ] ; then
    echo "Disk usage is critical: $CURRENT_USAGE%"

    # Send Alert to AlertHub
    curl -s -X POST "$PROJECT_URL" \
      -H "Content-Type: application/json" \
      -d "{
        \"ah_key\": \"$ALERTHUB_KEY\",
        \"ah_title\": \"Low Disk Space Alert\",
        \"ah_description\": \"Server storage is at ${CURRENT_USAGE}%. Please investigate immediately.\",
        \"ah_level\": \"warning\",
        \"ah_source\": \"VPSMonitor\"
      }"
fi

Alternatively, using alerthub_alert

If you followed our previous guide and installed the alerthub_alert function, you can simplify the alert sending part:

# Load alerthub_alert function (Adjust path if needed)
if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

if [ "$CURRENT_USAGE" -gt "$THRESHOLD" ] ; then
    echo "Disk usage is critical: $CURRENT_USAGE%"

    # Send Alert using our custom function
    # Ensure ALERTHUB_KEY is exported in your environment or .bashrc
    alerthub_alert -t "Low Disk Space Alert" \
                   -d "Server storage is at ${CURRENT_USAGE}%. Please investigate immediately." \
                   -l "warning" \
                   -s "VPSMonitor"
fi

Note: Replace your_sk_key_here with your actual project secret key.

How it Works

  • df /: Lists disk space for the root partition.
  • awk '{ print $5 }': Extracts the percentage column (e.g., 85%).
  • sed 's/%//g': Removes the % symbol so we can compare numbers (e.g., 85).
  • The if statement checks if the current usage is greater than our THRESHOLD (80).
  • If true, it fires a curl request to AlertHub with a warning level alert.

Automating with Cron

A script is only useful if it runs automatically. We’ll use cron to run this check every hour.

  1. Benchmark the script first to make sure it’s executable:
chmod +x ~/check_disk.sh
./check_disk.sh

(If your disk usage is below 80%, nothing will happen. You can temporarily lower THRESHOLD to 10 in the script to test the alert.)

  1. Open your crontab:
crontab -e
  1. Add the following line to run the check at the top of every hour:
0 * * * * /home/your_user/check_disk.sh

Make sure to replace /home/your_user/ with the actual path to your script.

Conclusion

With just a few lines of bash, you now have a proactive monitoring system. AlertHub will route this notification to your Slack, Telegram, or Email, ensuring you can react before your server runs out of space completely.