๐ Configuring Disk Usage Monitoring in Alpine Linux: Simple Guide
Watching your disk space helps prevent system crashes! ๐ป This tutorial shows you how to set up disk monitoring easily. Donโt worry - itโs simpler than you think! ๐
๐ค What is Disk Usage Monitoring?
Disk usage monitoring watches how much space your files use. Itโs like checking if your closet is getting too full!
Disk monitoring helps you:
- ๐ See how much space is used
- โ ๏ธ Get warnings when space is low
- ๐พ Find files taking too much space
- ๐ Keep your system running smoothly
๐ฏ What You Need
Before we start, you need:
- โ Alpine Linux system running
- โ Root or sudo access
- โ Basic knowledge of terminal commands
- โ Some files on your system to monitor
๐ Step 1: Understanding Disk Usage Commands
๐ Basic Disk Space Checking
Letโs start by learning how to check disk space manually. This is the foundation! ๐
What weโre doing: Looking at how much space your disks are using right now.
# Show disk space for all mounted filesystems
df -h
# Show disk usage of current directory
du -h
# Show disk usage with total
du -sh *
# Show only filesystem usage summary
df -h --total
What this does: ๐ Shows you how much space is used and available on your disks.
Expected Output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 8.5G 10G 46% /
tmpfs 1.0G 0 1.0G 0% /dev/shm
What this means: Your main disk is using 46% of available space! โ
๐ก Important Tips
Tip: The โUse%โ column is most important - watch when it gets above 80%! ๐ก
Warning: When disk space reaches 100%, your system can crash! โ ๏ธ
๐ ๏ธ Step 2: Setting Up Basic Monitoring
๐ Creating a Simple Monitoring Script
Now letโs create a script that checks disk space automatically. This is really helpful! ๐
What weโre doing: Making a script that watches disk space and tells you when itโs getting full.
# Create monitoring script directory
mkdir -p /opt/disk-monitor
# Create the monitoring script
cat > /opt/disk-monitor/check-disk.sh << 'EOF'
#!/bin/sh
# Simple disk usage monitoring script
# Set warning threshold (80%)
THRESHOLD=80
# Get disk usage percentage (remove % sign)
USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
# Check if usage is above threshold
if [ "$USAGE" -gt "$THRESHOLD" ]; then
echo "โ ๏ธ WARNING: Disk usage is ${USAGE}% (threshold: ${THRESHOLD}%)"
echo "๐
Date: $(date)"
echo "๐ Disk details:"
df -h /
else
echo "โ
Disk usage OK: ${USAGE}% (threshold: ${THRESHOLD}%)"
fi
EOF
# Make script executable
chmod +x /opt/disk-monitor/check-disk.sh
# Test the script
/opt/disk-monitor/check-disk.sh
Code explanation:
THRESHOLD=80
: Sets warning level at 80% fulldf / | tail -1
: Gets disk info for root filesystemawk '{print $5}'
: Extracts the usage percentagesed 's/%//'
: Removes the % symbol for comparison
Expected Output:
โ
Disk usage OK: 46% (threshold: 80%)
What this means: Great! Your disk is not full yet! ๐
๐ฎ Letโs Try It!
Time for hands-on practice! This is the fun part! ๐ฏ
What weโre doing: Creating an advanced monitoring script with email alerts and detailed logging.
# Create advanced monitoring script
cat > /opt/disk-monitor/advanced-monitor.sh << 'EOF'
#!/bin/sh
echo "๐ฅ๏ธ Advanced Disk Usage Monitor"
echo "================================"
echo ""
# Set thresholds
WARNING_THRESHOLD=80
CRITICAL_THRESHOLD=90
# Create log directory
mkdir -p /var/log/disk-monitor
# Log file
LOG_FILE="/var/log/disk-monitor/disk-usage.log"
# Get current date
DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo "๐
Scan Time: $DATE"
echo ""
# Check all mounted filesystems
df -h | grep -E '^/dev/' | while read line; do
FILESYSTEM=$(echo $line | awk '{print $1}')
SIZE=$(echo $line | awk '{print $2}')
USED=$(echo $line | awk '{print $3}')
AVAIL=$(echo $line | awk '{print $4}')
USAGE_PCT=$(echo $line | awk '{print $5}' | sed 's/%//')
MOUNT=$(echo $line | awk '{print $6}')
echo "๐ Filesystem: $FILESYSTEM"
echo " Mounted on: $MOUNT"
echo " Size: $SIZE | Used: $USED | Available: $AVAIL"
if [ "$USAGE_PCT" -gt "$CRITICAL_THRESHOLD" ]; then
echo " ๐จ CRITICAL: ${USAGE_PCT}% used!"
echo "$DATE - CRITICAL: $FILESYSTEM at $MOUNT is ${USAGE_PCT}% full" >> $LOG_FILE
elif [ "$USAGE_PCT" -gt "$WARNING_THRESHOLD" ]; then
echo " โ ๏ธ WARNING: ${USAGE_PCT}% used"
echo "$DATE - WARNING: $FILESYSTEM at $MOUNT is ${USAGE_PCT}% full" >> $LOG_FILE
else
echo " โ
OK: ${USAGE_PCT}% used"
echo "$DATE - OK: $FILESYSTEM at $MOUNT is ${USAGE_PCT}% full" >> $LOG_FILE
fi
echo ""
done
echo "๐ Monitoring complete! Check log: $LOG_FILE"
EOF
# Make it executable
chmod +x /opt/disk-monitor/advanced-monitor.sh
# Run the advanced monitor
/opt/disk-monitor/advanced-monitor.sh
You should see:
๐ฅ๏ธ Advanced Disk Usage Monitor
================================
๐
Scan Time: 2025-06-07 13:30:00
๐ Filesystem: /dev/sda1
Mounted on: /
Size: 20G | Used: 8.5G | Available: 10G
โ
OK: 46% used
Awesome work! ๐
๐ Quick Summary Table
Command | Purpose | Shows |
---|---|---|
๐ df -h | ๐ง Disk space overview | โ All filesystem usage |
๐ ๏ธ du -h | โ Directory sizes | โ Space used by folders |
๐ du -sh * | โ Current dir breakdown | โ Size of each item |
๐ฏ Custom script | โ Automated monitoring | โ Alerts and logging |
โฐ Step 3: Automated Monitoring with Cron
๐ Setting Up Regular Checks
Letโs make your monitoring run automatically every hour! ๐
What weโre doing: Using cron to run disk checks automatically so you donโt have to remember.
# Open cron editor
crontab -e
# Add this line to run monitoring every hour
# 0 * * * * /opt/disk-monitor/check-disk.sh >> /var/log/disk-monitor/hourly.log 2>&1
# Or add this line to run every 30 minutes
# */30 * * * * /opt/disk-monitor/advanced-monitor.sh >> /var/log/disk-monitor/detailed.log 2>&1
# Save and exit the editor
# Alternative: Create cron file directly
echo "0 * * * * /opt/disk-monitor/check-disk.sh >> /var/log/disk-monitor/hourly.log 2>&1" | crontab -
# Verify cron job was added
crontab -l
Code explanation:
0 * * * *
: Run at minute 0 of every hour*/30 * * * *
: Run every 30 minutes>> /var/log/...
: Append output to log file2>&1
: Include error messages in log
Expected Output:
0 * * * * /opt/disk-monitor/check-disk.sh >> /var/log/disk-monitor/hourly.log 2>&1
What this does: Your system now checks disk space automatically every hour! ๐
๐ฎ Practice Time!
Letโs practice what you learned! Try these simple examples:
Example 1: Find Large Files ๐ข
What weโre doing: Finding files that are taking up too much space.
# Find files larger than 100MB
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | head -10
# Find largest directories in current location
du -h --max-depth=1 | sort -hr | head -10
# Find largest files in /var directory
find /var -type f -exec ls -s {} \; 2>/dev/null | sort -n -r | head -10
What this does: Shows you which files are eating up your disk space! ๐
Example 2: Create Disk Usage Report ๐ก
What weโre doing: Making a nice report about disk usage.
# Create reporting script
cat > /opt/disk-monitor/daily-report.sh << 'EOF'
#!/bin/sh
echo "๐ Daily Disk Usage Report - $(date +%Y-%m-%d)"
echo "=============================================="
echo ""
echo "๐พ Filesystem Overview:"
df -h
echo ""
echo "๐ Largest Directories in /:"
du -h --max-depth=1 / 2>/dev/null | sort -hr | head -5
echo ""
echo "๐๏ธ /var Directory Usage:"
du -sh /var/* 2>/dev/null | sort -hr | head -5
echo ""
echo "๐ Disk Usage Trend (last 5 entries):"
tail -5 /var/log/disk-monitor/disk-usage.log 2>/dev/null || echo "No previous logs found"
EOF
chmod +x /opt/disk-monitor/daily-report.sh
# Run the daily report
/opt/disk-monitor/daily-report.sh
What this does: Creates a complete daily report of your disk usage! ๐
๐จ Fix Common Problems
Problem 1: Monitoring script not running โ
What happened: Cron job doesnโt seem to work. How to fix it: Check cron service and permissions!
# Check if cron service is running
rc-service crond status
# Start cron if not running
rc-service crond start
# Enable cron to start at boot
rc-update add crond default
# Check cron logs
tail -f /var/log/cron
Problem 2: Canโt write to log directory โ
What happened: Permission denied when creating log files. How to fix it: Fix permissions and create directories!
# Create log directory with correct permissions
mkdir -p /var/log/disk-monitor
chmod 755 /var/log/disk-monitor
# Make sure script has execute permissions
chmod +x /opt/disk-monitor/*.sh
# Test writing to log directory
echo "Test" > /var/log/disk-monitor/test.log && echo "โ
Permissions OK" || echo "โ Permission problem"
Donโt worry! These problems are easy to fix. Youโre doing great! ๐ช
๐ก Simple Tips
- Check regularly ๐ - Look at disk usage reports daily
- Set low thresholds ๐ฑ - Start warnings at 80% full, not 95%
- Clean up often ๐ค - Remove old files and logs regularly
- Plan ahead ๐ช - Add more storage before you need it
โ Check Everything Works
Letโs make sure all monitoring is working perfectly:
# Test basic monitoring
/opt/disk-monitor/check-disk.sh
# Test advanced monitoring
/opt/disk-monitor/advanced-monitor.sh
# Check if cron job exists
crontab -l
# Check recent logs
ls -la /var/log/disk-monitor/
echo "๐ Disk monitoring is ready! โ
"
Good output:
โ
Disk usage OK: 46% (threshold: 80%)
๐ Disk monitoring is ready! โ
๐ What You Learned
Great job! Now you can:
- โ Check disk usage with df and du commands
- โ Create custom monitoring scripts
- โ Set up automated monitoring with cron
- โ Generate detailed disk usage reports
๐ฏ Whatโs Next?
Now you can try:
- ๐ Setting up email alerts for critical disk usage
- ๐ ๏ธ Creating graphical monitoring dashboards
- ๐ค Monitoring multiple servers from one location
- ๐ Learning about log rotation and cleanup!
Remember: Monitoring disk usage prevents system crashes! Youโre keeping your server healthy! ๐
Keep practicing and youโll become a system monitoring expert too! ๐ซ