Keeping an Eye on Disk Space: Monitoring Tools Explained
published: , by nixtnt
Effective disk space monitoring is crucial for maintaining system health and preventing unexpected outages in Unix-like systems. Whether you're managing a single server or a complex homelab setup, understanding how to monitor disk usage can help you avoid the dreaded "disk full" errors that can bring your systems to a grinding halt.
Understanding Disk Monitoring Fundamentals
Before diving into specific commands, it's essential to understand what we're monitoring. The df
command, short for "disk free," provides information about mounted filesystems and their available space. Meanwhile, du
(disk usage) shows how much space files and directories are consuming. Together, these tools give you a complete picture of your storage situation.
The df Command: Your Filesystem Overview
The df
command is your go-to tool for getting a quick overview of disk space across all mounted filesystems. By default, df
displays information in 512-byte blocks, but this format isn't particularly user-friendly for most administrators.
Basic Usage and Human-Readable Output
The most practical way to use df
is with the -h
(human-readable) option, which displays sizes in familiar units like KB, MB, and GB:
df -h
This command produces output similar to:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 15G 4.2G 78% /
/dev/sda2 100G 45G 50G 48% /home
tmpfs 2.0G 0 2.0G 0% /dev/shm
Each line shows the filesystem name, total size, used space, available space, usage percentage, and mount point.
Key df Options for Monitoring
Several df
options are beneficial for monitoring purposes:
-h
: Human-readable format (KB, MB, GB)-T
: Show filesystem type-i
: Display inode information instead of block usage-P
: Use POSIX output format for scripting-x
: Exclude specific filesystem types
For monitoring scripts, the -P
option is especially valuable as it ensures consistent output formatting across different systems.
The du Command: Detailed Usage Analysis
While df
shows overall filesystem usage, du
helps you identify which files and directories are consuming the most space. This makes it invaluable for troubleshooting disk space issues.
Essential du Usage Patterns
The most common du
usage combines the -h
(human-readable) and -s
(summarise) options:
du -sh /var/log
This shows the total size of the /var/log
directory in a readable format.
To see the sizes of all subdirectories within a directory:
du -h /var/log/
Finding Space Hogs
One of the most practical uses of du
is identifying large directories that might need attention:
du -h /home | sort -h | tail -10
This command shows the ten largest directories under /home
, sorted by size. The sort -h
option properly handles human-readable sizes.
Controlling Directory Depth
The --max-depth
option limits how deep du
traverses the directory tree:
du -h --max-depth=1 /var/
This shows only the first level of subdirectories under /var/
, making it easier to identify problematic areas without overwhelming detail.
Monitoring Best Practices
Regular Monitoring Schedule
Effective disk monitoring requires regular checks rather than reactive responses. Consider setting up automated monitoring that checks disk space every 10-15 minutes for critical systems.
Setting Appropriate Thresholds
Different systems require different monitoring thresholds:
- System partitions: Alert at 80-85% usage
- Data partitions: Alert at 90-95% usage
- Log directories: Monitor more frequently due to rapid growth
Combining Commands for Comprehensive Monitoring
A practical monitoring approach combines both commands:
- Use
df -h
to check overall filesystem health - Use
du -sh /*
to identify large directories when issues arise - Use
du -h | sort -h
to find specific files consuming excessive space
Homelab-Specific Considerations
For homelab environments, disk monitoring becomes even more critical due to limited resources. Consider these additional factors:
- Container storage: Monitor Docker volumes and container logs
- Virtual machine disks: Watch for VM disk file growth
- Backup storage: Ensure backup destinations don't fill up
- Media storage: Monitor download directories and media libraries
Creating Simple Monitoring Scripts
A basic monitoring script can automate disk space checks and send alerts when thresholds are exceeded:
#!/bin/bash
THRESHOLD=90
ALERT_EMAIL="admin@example.com"
df -h | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print $5 " " $1}' | while read output; do
usage=$(echo $output | awk '{print $1}' | sed 's/%//g')
partition=$(echo $output | awk '{print $2}')
if [ $usage -ge $THRESHOLD ]; then
echo "Warning: $partition is $usage% full" | mail -s "Disk Space Alert" $ALERT_EMAIL
fi
done
Advanced Monitoring Considerations
Inode Monitoring
Sometimes systems can run out of inodes (file system objects) before running out of space. Monitor inode usage with:
df -i
Monitoring Network Filesystems
Network-mounted filesystems require special attention, as they may not always report accurate space information. Use filesystem-specific tools when available.
Integration with Modern Monitoring
While df
and du
are fundamental tools, consider integrating them with modern monitoring solutions like Prometheus, Grafana, or specialised homelab monitoring tools. These can provide historical data, trends, and more sophisticated alerting.
Conclusion
Mastering disk space monitoring with df
and du
is essential for any Unix administrator. These tools provide the foundation for understanding storage usage patterns, identifying problems before they become critical, and maintaining healthy systems. Regular monitoring, appropriate thresholds, and automated alerting help ensure your systems remain responsive and reliable.
Remember that effective monitoring is not just about running commands—it's about understanding your system's normal patterns and having procedures in place to address issues when they arise. Start with basic monitoring practices and gradually build more sophisticated solutions as your needs grow.