📊 Monitoring File System Usage: Simple Guide
Let’s learn to monitor disk space on Alpine Linux! This is super useful! 🎉 We’ll check how much storage you’re using. Keep your system healthy! 😊
🤔 What is File System Monitoring?
File system monitoring watches your disk space and storage. Think of it like checking how full your closet is!
Monitoring helps with:
- 📈 Tracking disk space usage
- 🚨 Warning when space gets low
- 🔧 Finding what uses most space
🎯 What You Need
Before we start, you need:
- ✅ Alpine Linux system running
- ✅ Basic terminal knowledge
- ✅ Root access for some commands
- ✅ Understanding of file paths
📋 Step 1: Basic Disk Space Commands
Using df Command
Let’s start with the most basic command! It’s really easy! 😊
What we’re doing: Checking overall disk space usage.
# Check disk space in human-readable format
df -h
# Check disk space for specific filesystem
df -h /
# Show all filesystem types
df -T
# Check inodes usage
df -i
What this does: 📖 Shows how much space is used and available on each disk.
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 3.2G 15G 18% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/sda2 100G 45G 50G 48% /home
What this means: Your root filesystem has 18% usage - that’s healthy! ✅
💡 Important Tips
Tip: Always monitor disk usage regularly! 💡
Warning: When usage reaches 90%, take action quickly! ⚠️
🛠️ Step 2: Detailed File Usage Analysis
Using du Command
Now let’s find what’s using the most space! This is like detective work! 🎯
What we’re doing: Finding which files and folders use most disk space.
# Check current directory usage
du -h
# Check usage summary for current directory
du -sh
# Check top 10 largest directories
du -h / | sort -hr | head -10
# Check specific directory usage
du -sh /var/log
# Show usage for each subdirectory
du -h --max-depth=1 /home
Code explanation:
du -h
: Shows sizes in human-readable format (KB, MB, GB)du -sh
: Shows total summary onlysort -hr
: Sorts by size, largest first--max-depth=1
: Shows only one level deep
Expected Output:
1.2G /var/lib
800M /usr/lib
512M /var/log
256M /home/user
128M /tmp
What this means: Now you know where your space is going! 🌟
📊 Quick Summary Table
Command | Purpose | Result |
---|---|---|
📊 df -h | Overall disk usage | ✅ Shows free space |
🔍 du -sh | Directory size | ✅ Shows folder usage |
📈 ncdu | Interactive browser | ✅ Visual disk explorer |
🚨 watch df -h | Real-time monitoring | ✅ Live updates |
🎮 Step 3: Advanced Monitoring Tools
Installing ncdu (Interactive Disk Usage)
Let’s install a super cool visual tool! You’ll love this! 🌟
What we’re doing: Installing an interactive disk usage analyzer.
# Install ncdu (NCurses Disk Usage)
apk add ncdu
# Run ncdu on root filesystem
ncdu /
# Run ncdu on specific directory
ncdu /home
# Run ncdu with real-time scan
ncdu -x /var
What this does: Gives you a visual, interactive way to explore disk usage! 📚
Using watch for Real-time Monitoring
What we’re doing: Setting up continuous monitoring that updates automatically.
# Watch disk usage every 2 seconds
watch -n 2 df -h
# Watch specific directory size
watch -n 5 "du -sh /var/log"
# Watch multiple locations
watch -n 10 "df -h; echo; du -sh /tmp /var/log /home"
# Save watch output to file
watch -n 30 "df -h >> /var/log/disk-usage.log"
Expected Output:
Every 2.0s: df -h Mon Jun 3 16:30:15 2025
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 3.3G 15G 19% /
tmpfs 2.0G 12M 2.0G 1% /dev/shm
What this means: Your disk usage updates live! Perfect for monitoring! 🎉
🎮 Practice Time!
Let’s practice what you learned! Try these simple examples:
Example 1: Find Large Files 🟢
What we’re doing: Finding the biggest files on your system.
# Find files larger than 100MB
find / -size +100M -type f -exec ls -lh {} \; 2>/dev/null | head -10
# Find largest files in /var
find /var -type f -exec ls -s {} \; | sort -nr | head -10
# Find large files in current directory
ls -lSh | head -10
# Create a report of large files
find / -size +50M -type f -exec ls -lh {} \; 2>/dev/null > /tmp/large-files.txt
echo "Large files report saved to /tmp/large-files.txt ✅"
What this does: Helps you find space hogs quickly! 🌟
Example 2: Set Up Automated Monitoring 🟡
What we’re doing: Creating scripts that monitor disk usage automatically.
# Create disk monitoring script
cat > /usr/local/bin/disk-monitor.sh << 'EOF'
#!/bin/sh
# Disk Usage Monitor Script
LOG_FILE="/var/log/disk-monitor.log"
ALERT_THRESHOLD=80
log_message() {
echo "$(date): $1" | tee -a "$LOG_FILE"
}
# Check disk usage for root filesystem
USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
log_message "Disk usage: ${USAGE}%"
if [ "$USAGE" -gt "$ALERT_THRESHOLD" ]; then
log_message "WARNING: Disk usage is ${USAGE}% - exceeds threshold!"
# Show top space users
log_message "Top space users:"
du -sh /* 2>/dev/null | sort -hr | head -5 | tee -a "$LOG_FILE"
# Clean temporary files if needed
if [ "$USAGE" -gt 90 ]; then
log_message "CRITICAL: Cleaning temporary files"
find /tmp -type f -mtime +7 -delete
find /var/log -name "*.log" -mtime +30 -delete
fi
else
log_message "Disk usage normal"
fi
EOF
# Make script executable
chmod +x /usr/local/bin/disk-monitor.sh
# Test the script
/usr/local/bin/disk-monitor.sh
# Set up cron job to run every hour
echo "0 * * * * /usr/local/bin/disk-monitor.sh" | crontab -
echo "Automated disk monitoring configured! 📚"
What this does: Monitors your disk automatically and cleans up when needed! 📚
🚨 Fix Common Problems
Problem 1: Disk suddenly full ❌
What happened: Your disk space disappeared quickly. How to fix it: Find and clean the culprit!
# Find what filled up recently
find / -type f -mtime -1 -size +10M 2>/dev/null
# Check log files
du -sh /var/log/*
# Clean old log files
find /var/log -name "*.log" -mtime +7 -delete
# Clear package cache
apk cache clean
Problem 2: Can’t find space users ❌
What happened: Disk shows full but can’t find large files. How to fix it: Check for hidden files and deleted files!
# Check for hidden files
du -sh .[^.]* * 2>/dev/null
# Check for deleted files still open
lsof +L1
# Check system directories
du -sh /var/* /usr/* /opt/* 2>/dev/null | sort -hr
Problem 3: Monitoring not working ❌
What happened: Scripts don’t run automatically. How to fix it: Check cron and permissions!
# Check if cron is running
rc-service crond status
# Start cron if needed
rc-service crond start
# Check cron logs
tail -f /var/log/cron.log
# Test script manually
bash -x /usr/local/bin/disk-monitor.sh
Don’t worry! Disk monitoring takes practice. You’re doing great! 💪
💡 Simple Tips
- Monitor daily 📅 - Check disk space regularly
- Set alerts 🌱 - Know before you run out
- Clean regularly 🤝 - Remove old files often
- Use tools 💪 - Visual tools make it easier
✅ Check Everything Works
Let’s verify monitoring is working:
# Check current disk usage
df -h
# Test ncdu if installed
which ncdu && echo "ncdu available ✅"
# Check monitoring script
test -x /usr/local/bin/disk-monitor.sh && echo "Monitor script ready ✅"
# Check cron job
crontab -l | grep disk-monitor && echo "Cron job configured ✅"
# View recent monitoring logs
tail -5 /var/log/disk-monitor.log
echo "File system monitoring verification completed! ✅"
Good output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 3.3G 15G 19% /
ncdu available ✅
Monitor script ready ✅
Cron job configured ✅
Mon Jun 3 16:45:01 2025: Disk usage normal
File system monitoring verification completed! ✅
🔧 Advanced Monitoring Features
Setting Up Disk Usage Alerts
Let’s create email alerts when disk space gets low! This is professional! 🎯
What we’re doing: Creating smart alerts that notify you of disk issues.
# Install mail utilities
apk add mailx
# Create advanced monitoring script
cat > /usr/local/bin/advanced-disk-monitor.sh << 'EOF'
#!/bin/sh
# Advanced Disk Usage Monitor
CONFIG_FILE="/etc/disk-monitor.conf"
LOG_FILE="/var/log/disk-monitor.log"
# Default configuration
WARNING_THRESHOLD=75
CRITICAL_THRESHOLD=90
EMAIL_ALERTS=true
ADMIN_EMAIL="[email protected]"
# Load configuration if exists
[ -f "$CONFIG_FILE" ] && . "$CONFIG_FILE"
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S'): $1" | tee -a "$LOG_FILE"
}
send_alert() {
local subject="$1"
local message="$2"
if [ "$EMAIL_ALERTS" = "true" ] && command -v mail >/dev/null; then
echo "$message" | mail -s "$subject" "$ADMIN_EMAIL"
fi
}
check_filesystem() {
local fs="$1"
local usage=$(df "$fs" | tail -1 | awk '{print $5}' | sed 's/%//')
local used=$(df -h "$fs" | tail -1 | awk '{print $3}')
local avail=$(df -h "$fs" | tail -1 | awk '{print $4}')
log_message "Filesystem $fs: ${usage}% used (${used} used, ${avail} available)"
if [ "$usage" -ge "$CRITICAL_THRESHOLD" ]; then
log_message "CRITICAL: Filesystem $fs usage at ${usage}%"
send_alert "CRITICAL: Disk Space Alert" \
"Filesystem $fs usage is critically high at ${usage}%\nUsed: $used\nAvailable: $avail"
# Emergency cleanup
emergency_cleanup "$fs"
elif [ "$usage" -ge "$WARNING_THRESHOLD" ]; then
log_message "WARNING: Filesystem $fs usage at ${usage}%"
send_alert "WARNING: Disk Space Alert" \
"Filesystem $fs usage is approaching limits at ${usage}%\nUsed: $used\nAvailable: $avail"
fi
}
emergency_cleanup() {
local fs="$1"
log_message "Starting emergency cleanup for $fs"
# Clean temporary files
find /tmp -type f -mtime +1 -delete 2>/dev/null
# Compress old logs
find /var/log -name "*.log" -mtime +7 -exec gzip {} \; 2>/dev/null
# Clean package cache
apk cache clean
log_message "Emergency cleanup completed"
}
# Check all mounted filesystems
df | awk 'NR>1 {print $6}' | while read filesystem; do
check_filesystem "$filesystem"
done
# Generate usage report
log_message "=== Disk Usage Summary ==="
df -h | tee -a "$LOG_FILE"
EOF
chmod +x /usr/local/bin/advanced-disk-monitor.sh
# Create configuration file
cat > /etc/disk-monitor.conf << 'EOF'
# Disk Monitor Configuration
WARNING_THRESHOLD=75
CRITICAL_THRESHOLD=90
EMAIL_ALERTS=true
ADMIN_EMAIL="[email protected]"
EOF
# Test advanced monitoring
/usr/local/bin/advanced-disk-monitor.sh
What this does: Creates professional-grade disk monitoring with alerts! 🌟
Creating Disk Usage Dashboard
What we’re doing: Building a simple web dashboard to view disk usage.
# Create web dashboard script
cat > /usr/local/bin/disk-usage-web.sh << 'EOF'
#!/bin/sh
# Disk Usage Web Dashboard Generator
WEB_DIR="/var/www/html"
HTML_FILE="$WEB_DIR/disk-usage.html"
mkdir -p "$WEB_DIR"
cat > "$HTML_FILE" << 'HTML'
<!DOCTYPE html>
<html>
<head>
<title>Alpine Linux Disk Usage Dashboard</title>
<meta http-equiv="refresh" content="60">
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.header { background: #2c3e50; color: white; padding: 20px; border-radius: 5px; }
.metric { background: #ecf0f1; padding: 15px; margin: 10px 0; border-radius: 5px; }
.warning { background: #f39c12; color: white; }
.critical { background: #e74c3c; color: white; }
pre { background: #34495e; color: white; padding: 15px; border-radius: 5px; overflow-x: auto; }
</style>
</head>
<body>
<div class="header">
<h1>🖥️ Alpine Linux Disk Usage Dashboard</h1>
<p>Last updated: $(date)</p>
</div>
HTML
# Add filesystem information
echo '<div class="metric">' >> "$HTML_FILE"
echo '<h2>📊 Filesystem Usage</h2>' >> "$HTML_FILE"
echo '<pre>' >> "$HTML_FILE"
df -h >> "$HTML_FILE"
echo '</pre>' >> "$HTML_FILE"
echo '</div>' >> "$HTML_FILE"
# Add top space users
echo '<div class="metric">' >> "$HTML_FILE"
echo '<h2>🔍 Top Space Users</h2>' >> "$HTML_FILE"
echo '<pre>' >> "$HTML_FILE"
du -sh /* 2>/dev/null | sort -hr | head -10 >> "$HTML_FILE"
echo '</pre>' >> "$HTML_FILE"
echo '</div>' >> "$HTML_FILE"
# Close HTML
cat >> "$HTML_FILE" << 'HTML'
</body>
</html>
HTML
echo "Dashboard updated: $HTML_FILE"
EOF
chmod +x /usr/local/bin/disk-usage-web.sh
# Set up automatic dashboard updates
echo "*/5 * * * * /usr/local/bin/disk-usage-web.sh" | crontab -
# Generate initial dashboard
/usr/local/bin/disk-usage-web.sh
echo "Web dashboard created! View at file://$WEB_DIR/disk-usage.html"
Expected Output:
Dashboard updated: /var/www/html/disk-usage.html
Web dashboard created! View at file:///var/www/html/disk-usage.html
✅ Professional monitoring dashboard ready
What this means: You now have a web dashboard for monitoring! 🎉
🏆 What You Learned
Great job! Now you can:
- ✅ Monitor disk space with df and du commands
- ✅ Use interactive tools like ncdu for visual exploration
- ✅ Set up automated monitoring with alerts
- ✅ Create professional dashboards for disk usage!
🎯 What’s Next?
Now you can try:
- 📚 Learning advanced filesystem tools like iotop
- 🛠️ Setting up centralized monitoring with Prometheus
- 🤝 Creating automated disk cleanup policies
- 🌟 Building custom monitoring solutions!
Remember: Every system administrator was once a beginner. You’re doing amazing! 🎉
Keep practicing and you’ll become an expert too! 💫