r/devops7 • u/Alternative_Pin9598 • Nov 03 '26
Automate it #4: "disk full in N days" from one SQL query, plus the check that catches a box that started filling 16x faster today (real output)
A 90% disk alert fires too late to do anything calm about it. A trend line ("full in 27 days") is better, but it is slow: the morning someone leaves DEBUG logging on, a 14-day trend still says you have two months.
So this daily job does two things with one table of df samples:
- Trend: least-squares slope over the last 14 days, so you get "full in N days" per server.
- Odd day: scores today's growth against what each server usually does, and flags the box that is suddenly filling much faster than normal, before the trend notices.
(Disclosure: this sub is run by the SynapCores team, and step 2 uses SynapCores. Run this morning on 2.3.2, real output.)
Collect: one line per server per day
for h in web-01 db-01 app-02 cache-01 logs-01; do
ssh $h "df -P /" | awk -v h=$h -v d=$(date +%F) 'NR==2{print d, h, $6, $5+0}'
done | python3 disk_trend.py ingestThat prints 2026-11-03 web-01 / 62 lines. The script stores each one with today's growth, the server's usual growth (its 14-day average) and the difference:
CREATE TABLE disk_samples (day TEXT, host TEXT, mount TEXT, used_pct REAL, growth REAL, usual REAL, excess REAL);For this post: 30 days of samples for 5 test servers (seeded generator in the first comment, so you can reproduce every number). On the last day, logs-01 gets a DEBUG-logging spike.
Step 1, the trend: plain SQL, nothing special
SELECT s.host, s.mount, MAX(t.used_pct) AS used,
(COUNT(*)*SUM(s.n*s.used_pct) - SUM(s.n)*SUM(s.used_pct)) / (COUNT(*)*SUM(s.n*s.n) - SUM(s.n)*SUM(s.n)) AS slope
FROM (SELECT host, mount, used_pct, DATEDIFF(day, '2026-11-03') AS n FROM disk_samples
WHERE DATEDIFF(day, '2026-11-03') > -14) s
JOIN disk_samples t ON t.host = s.host AND t.mount = s.mount AND t.day = '2026-11-03'
GROUP BY s.host, s.mount;Then (100 - used) / slope in Python is the days left. Here is every server:
TREND cache-01 / 33.3% used, +0.10/day -> full in 643 days
TREND db-01 / 78.1% used, +0.80/day -> full in 27 days
TREND logs-01 / 64.6% used, +0.54/day -> full in 66 days
TREND web-01 / 62.2% used, +0.24/day -> full in 159 daysdb-01 is the one to plan for. logs-01 looks fine: 66 days.
Step 2, the odd day: an anomaly model in the database, scored inside the SELECT
Every morning, retrain on the last 30 days (the whole report runs in under 0.1 s for 150 rows), then score today:
CREATE EXPERIMENT disk_growth_exp AS
SELECT excess FROM disk_samples WHERE DATEDIFF('2026-11-03', day) <= 30
WITH (task_type = 'anomaly_detection', algorithms = ['isolation_forest'], max_trials = 5, random_seed = 42);
DEPLOY MODEL disk_growth_model FROM EXPERIMENT disk_growth_exp;
SELECT host, mount, used_pct, growth, usual, ANOMALY_SCORE('disk_growth_model', excess) AS score
FROM disk_samples WHERE day = '2026-11-03' AND ANOMALY_SCORE('disk_growth_model', excess) > 0.8;No labels and no per-server thresholds. "Weird" is learned from 30 days of what your fleet normally does, and excess (today minus usual) keeps a fast-growing DB from looking weird just for being busy.
Run it
Yesterday's run, before the spike:
$ python3 disk_trend.py report 2026-11-02
TREND db-01 / 77.2% used, +0.79/day -> full in 29 daysThis morning:
$ python3 disk_trend.py report 2026-11-03
TREND db-01 / 78.1% used, +0.80/day -> full in 27 days
ODD logs-01 / +6.1 pts today, usually +0.37 (anomaly 0.91) -> at today's rate full in 6 daysThe trend still says logs-01 has 66 days. The anomaly score says something changed today, and at today's rate it has 6. The top 5 scores over all 150 server-days:
logs-01 2026-11-03 +6.1 usual 0.37 0.91
db-01 2026-10-06 +1.0 usual 0.00 0.79
cache-01 2026-10-09 -0.3 usual 0.23 0.75
cache-01 2026-10-12 -0.3 usual 0.11 0.67
logs-01 2026-10-06 +0.5 usual 0.00 0.66The spike is alone above 0.8. db-01 grew +0.9 today, like most days, and scores 0.37: busy, not odd.
Wire it up: 30 7 * * * collect.sh && python3 disk_trend.py report $(date +%F) | grep -E 'TREND|ODD', then send that to Slack. TREND lines are for the weekly capacity chat, ODD lines are for today. Full disk_trend.py (about 50 lines, stdlib only) and the sample generator are in the first comment.
Update: inodes. A reader pointed out that inodes can run out long before the disk percentage looks scary. No second table needed: collect df -Pi as its own "mount", and both checks run unchanged, because everything groups by host + mount:
ssh $h "df -Pi /" | awk -v h=$h -v d=$(date +%F) 'NR==2{print d, h, $6":inodes", $5+0}'Same seeded fleet on 2.3.2, plus an app-02 that sits at ~41% disk but uses ~1.1% of its inodes a day (lots of tiny cache files). Generator for that, so you can reproduce it:
import random, datetime
random.seed(11)
start, u = datetime.date(2026, 10, 5), 52.0
for d in range(30):
if d: u += max(0.0, random.gauss(1.1, 0.15))
print((start + datetime.timedelta(days=d)).isoformat(), "app-02", "/:inodes", round(u, 1))Output:
$ WARN_DAYS=60 python3 disk_trend.py report 2026-11-03
TREND app-02 /:inodes 84.1% used, +1.10/day -> full in 14 days
TREND db-01 / 78.1% used, +0.80/day -> full in 27 days
ODD logs-01 / +6.1 pts today, usually +0.37 (anomaly 0.91) -> at today's rate full in 6 daysapp-02's disk line never shows up (it has years left), but its inodes run out in two weeks.
Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=devops7-automate-it-4
What filled up a disk on you the fastest: logs, core dumps, Docker images, or something stranger?