Sub topic 14
Tracking growth and planning capacity
This chapter assumes you can read df and du output, find large files, and write and schedule a small shell script. Earlier chapters covered those skills. A single df reading says how full a filesystem is right now. It doesn't say how fast it is filling or when it will run out. Here you will record usage over time, turn it into a growth rate, and estimate when you need to act.
Why a single reading is not enough
Two servers can both show 70% used on /var. One has been at 70% for a year. The other was at 40% a month ago. Only the second needs attention this week.
Percent thresholds have a second weakness: they ignore the size of the disk. 85% of a 20 GB volume leaves about 3 GB, which a busy log can fill in an hour. 85% of a 10 TB volume leaves about 1.5 TB, which may last months. Time remaining is the number you need.
Planning capacity comes down to three questions:
- How much is used now, for both blocks and inodes?
- How fast is it growing?
- How long does it take you to add space, and does that fit inside the time remaining?
The third question sets your alert point. If a resize takes a day to approve and perform, an alert at "five days until full" is useful and one at "six hours until full" is not.
Recording usage over time
You need a history. The simplest history is a CSV file with one line per filesystem per sample. Record raw byte counts instead of percentages, because percentages lose precision and change meaning if the filesystem is resized.
This script writes one line per real filesystem. The columns are epoch seconds, mount point, used bytes, available bytes, used inodes, and free inodes.
#!/usr/bin/env bash
# Usage: record-disk.sh
# Appends one CSV line per filesystem:
# epoch,mount,used_bytes,avail_bytes,inodes_used,inodes_free
set -euo pipefail
log=/var/log/disk-usage.csv
now="$(date +%s)"
df -B1 --output=target,used,avail,iused,iavail \
-x tmpfs -x devtmpfs -x squashfs |
tail -n +2 |
while read -r mount used avail iused iavail; do
printf '%s,%s,%s,%s,%s,%s\n' "$now" "$mount" "$used" "$avail" "$iused" "$iavail"
done >> "$log"Some details matter here:
-B1reports sizes in bytes, so no unit conversion is needed later.- Epoch seconds are easy to subtract. You don't have to parse dates.
readsplits on whitespace, so a mount point containing a space breaks the parsing. Server mount points rarely contain spaces. If yours do, use a different tool or quote the fields.- Some filesystems, such as Btrfs, report
-for inode columns. The line is still recorded. The forecast script skips non-numeric values. - The script appends to a file under
/var/log, so run it as root.
Sample hourly from root's crontab:
0 * * * * /usr/local/bin/record-disk.shHourly sampling costs almost nothing. With four mounts, the file grows by about 100 lines a day, well under 10 KB. Add the file to your logrotate configuration, or trim it yearly, so the history file doesn't become the thing that grows without bound.
Turning samples into a forecast
The simplest forecast is a straight line. Take the first and last samples for a mount, divide the change in used bytes by the elapsed time, then divide the current available space by that rate.
#!/usr/bin/env bash
# Usage: forecast-disk.sh MOUNT [CSV]
# Estimates growth per day and days until full for one mount point.
set -euo pipefail
mount="${1:?usage: forecast-disk.sh MOUNT [CSV]}"
csv="${2:-/var/log/disk-usage.csv}"
awk -F, -v m="$mount" '
$2 == m && $3 ~ /^[0-9]+$/ {
if (!n) { t0 = $1; u0 = $3 }
t1 = $1; u1 = $3; a1 = $4; n++
}
END {
if (n < 2 || t1 == t0) { print "not enough data"; exit 1 }
rate = (u1 - u0) / (t1 - t0) * 86400
printf "samples=%d span_days=%.1f growth=%.1f MiB/day\n", n, (t1 - t0) / 86400, rate / 1048576
if (rate <= 0) { print "no growth; no projected full date"; exit 0 }
printf "days until full: %.0f\n", a1 / rate
}' "$csv"Run it with forecast-disk.sh /var. Example output after two weeks of samples:
samples=336 span_days=14.0 growth=412.5 MiB/day
days until full: 38The available figure comes from df, so it already excludes blocks reserved for root on ext4. The forecast measures the room your services can actually use.
To forecast inodes, apply the same arithmetic to columns 5 and 6. Inode growth is often much steadier than space growth, so it is worth watching on mail servers, build hosts, and anything that writes many small files.
Why straight lines mislead
The two-point estimate is a starting point. Real usage rarely grows in a straight line, and several patterns skew the result.
| Pattern | Cause | Effect on the forecast |
|---|---|---|
| Sawtooth | Logrotate and cleanup jobs free space on a schedule | The first and last samples may fall at different points in the cycle, so growth looks higher or lower than it is |
| Steps | A large import, a new application, or a database restore | One jump dominates the average and hides the normal rate |
| Seasonal peaks | Month-end reports, holiday traffic, nightly backups staging data locally | A quiet-period forecast underestimates the busy period |
| Plateau after cleanup | Someone deleted old data | The rate goes negative and the tool reports no growth |
You can reduce these distortions with a few habits:
- Compare samples taken at the same time of day. Filtering to one hour of the day removes most of the sawtooth.
- Use a window of at least one full cycle of your slowest recurring job, such as a week or a month.
- Forecast from the recent window and not the whole history, so an old step doesn't skew today's rate.
- Look at the raw numbers when a forecast surprises you. Sort the CSV by time for one mount and read it.
Choosing alert points from lead time
Add time remaining to your checks. The threshold script from earlier chapters warns on a percentage. A forecast lets you add a second rule: warn when days until full drops below your lead time.
Pick a lead time from how long it takes you to get more space:
- A cloud volume that can be resized online: a few hours to a day.
- A volume that needs approval and a maintenance window: a week or more.
- A physical server that needs new disks ordered: weeks.
Set the warning at about twice the lead time. This leaves room for a missed alert or a weekend. Keep a percentage threshold as well. It catches sudden jumps that a forecast built from an average will not see for several hours.
Planning the headroom
Capacity planning is arithmetic on three numbers: current use, growth rate, and the target ceiling. Treat a filesystem as full for planning purposes at 80 to 85% of its size. Above that, ext4 allocation gets slower on heavily fragmented disks, Btrfs and other copy-on-write filesystems need free space to rewrite data, and the margin for a surprise gets thin.
Worked example for /var on a 200 GB volume:
- Used now: 120 GB. Growth: 2 GB per day.
- Ceiling at 85%: 170 GB. Room before the ceiling: 50 GB.
- Time to the ceiling: 50 / 2 = 25 days.
- If a resize needs 7 days of lead time, you must start by day 18.
If you resize to 400 GB, the ceiling moves to 340 GB. Time to the ceiling becomes (340 − 120) / 2 = 110 days. Doubling the disk is not the only answer. If most of the growth is logs, shortening retention changes the rate itself and costs nothing.
Before you buy space, ask what is growing. The du drill-down from earlier chapters tells you whether the growth belongs to logs, a database, caches, or user uploads. Each has a different fix: retention for logs, archiving or partitioning for databases, expiry for caches, and quotas or separate storage for uploads.
Using a monitoring system instead
A shell script and a CSV are enough for a few servers. For a fleet, use a monitoring system that already stores time series. With Prometheus and node_exporter, one expression predicts whether a filesystem fills within four days, based on the last six hours:
predict_linear(node_filesystem_avail_bytes{mountpoint="/var"}[6h], 4 * 24 * 3600) < 0It has the same limitation as your script. It fits a straight line to a window, so a short window reacts quickly to spikes and a long window smooths them out. Choose the window to match the pattern you saw in your own history.
Common mistakes
- Recording percentages only. You lose the ability to compute growth in bytes and to compare across resizes.
- Sampling too rarely. Daily samples can miss a fast fill that peaks and recovers between readings. Hourly is a reasonable default.
- Forgetting inodes. A filesystem can be nowhere near full in bytes and still fail on inodes.
- Forecasting from too little data. A few hours of samples captures one phase of the daily cycle. Wait at least a week.
- Ignoring the history file itself. Make sure it is rotated and that its own directory has room.
Exercise
A volume mounted at /data has 300 GB of space in total. Your CSV shows 150 GB used on day 0 and 192 GB used on day 14. Your ceiling for planning is 85% of the volume, and a resize takes 10 days to arrange. What is the growth rate per day, how many days until the ceiling is reached from day 14, and by which day (counting from day 0) must you start the resize?
Show answer
Growth is (192 − 150) / 14 = 3 GB per day. The ceiling is 0.85 × 300 = 255 GB. Room left at day 14 is 255 − 192 = 63 GB, so the ceiling is reached in 63 / 3 = 21 days, which is day 35. Starting a 10-day resize must happen by day 25 at the latest. Starting earlier, for example at day 21 with a 2x lead time, leaves a safety margin.
Exercise
Your forecast script reports "no growth; no projected full date" for /var, but df shows /var has risen from 60% to 82% over the last month. Give two reasons the script's output could be misleading, and one change that would make the forecast more reliable.
Show answer
The first and last samples may fall at different points in a cleanup or logrotate cycle, so the last sample is lower than an earlier peak and the net change looks flat or negative. A recent large deletion can also make the rate negative even though the longer trend is upward. To make the forecast more reliable, use only samples taken at the same time of day, and compute the rate over a window covering several complete cleanup cycles, such as the most recent two to four weeks.