+
*
+
+
+
numpy
+
+
+
+
+
+
+
gh
play
+
+
+
+
azure
+
+
+
pinecone
+
+
+
+
pip
+
+
+
+
+
+
lisp
stencil
+
travis
+
webstorm
redhat
+
+
+
cargo
+
fortran
qwik
clion
rb
โˆ‰
+
+
yarn
--
abap
+
=
+
qwik
express
+
+
+
gradle
+
+
+
laravel
+
http
html
dns
+
+
!!
go
+
+
+
[]
perl
lit
+
npm
php
+
+
travis
Back to Blog
๐Ÿ“Š Configuring Disk Usage Monitoring in Alpine Linux: Simple Guide
Alpine Linux System Monitoring Disk Management

๐Ÿ“Š Configuring Disk Usage Monitoring in Alpine Linux: Simple Guide

Published Jun 7, 2025

Easy tutorial on setting up disk usage monitoring in Alpine Linux. Perfect for beginners with step-by-step instructions and practical monitoring examples.

8 min read
0 views
Table of Contents

๐Ÿ“Š 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% full
  • df / | tail -1: Gets disk info for root filesystem
  • awk '{print $5}': Extracts the usage percentage
  • sed '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

CommandPurposeShows
๐Ÿ“Š 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 file
  • 2>&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

  1. Check regularly ๐Ÿ“… - Look at disk usage reports daily
  2. Set low thresholds ๐ŸŒฑ - Start warnings at 80% full, not 95%
  3. Clean up often ๐Ÿค - Remove old files and logs regularly
  4. 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! ๐Ÿ’ซ