Rightsizing with monitoring data: look at what's used, not what's allocated
Downsizing, shutting down and honestly tracking VMs that were sized at creation and never revisited, using 30 days of CPU and memory data.
The cloud bill grew a little every month, but traffic stayed the same. Once we looked, the reason was simple: VMs had been sized "just in case" on the day they were created, and nobody had looked at them again. And the bill is based not on the resources used, but on the resources allocated.
Symptoms
- Most VMs run at low CPU all day.
- The memory graph has sat in the same place for years, below the halfway mark.
- In Kubernetes, pod request values are several times the actual usage.
- Nobody knows what some VMs do, or who they belong to.
Why does it happen?
Sizing is done once, usually at install time and by guesswork. Then the system runs, nobody complains, and nobody thinks about downsizing. Excess capacity never raises an alert. Its only symptom is the bill.
Method
1. Collect the data. Pull 30 days of CPU and memory usage per VM from the monitoring system (something like Zabbix). The history API gives raw values; the trends API gives hourly averages and maximums. If history retention is shorter than 30 days, continue from trends: use hourly averages for p95 and hourly maximums for the peak.
For memory, pick an "available"-based metric rather than "used". The Linux page cache makes memory look full, which blocks downsizing for no reason.
2. Add the other sources. Take DB metrics from the database monitoring tool, and requests versus actual usage from Kubernetes. In Kubernetes the real waste is usually not in the nodes but in bloated request values.
3. Produce a candidate list. An example rule: if p95 CPU is below 20% and peak memory usage is below 50%, the VM is a downsize candidate. Keep 30% headroom when computing the new size.
import csv, math, statistics
from collections import defaultdict
def load(path):
# history export: host,clock,value (value in percent)
vals = defaultdict(list)
with open(path) as f:
for row in csv.DictReader(f):
vals[row["host"]].append(float(row["value"]))
return vals
def p95(xs):
return statistics.quantiles(xs, n=100)[94]
cpu = load("cpu_util_30d.csv")
mem = load("mem_util_30d.csv")
with open("inventory.csv") as f:
inv = {r["host"]: r for r in csv.DictReader(f)} # host,vcpu,mem_gb,owner,exclude
HEADROOM = 0.7 # 30% headroom
missing = []
for host in sorted(cpu):
item = inv.get(host)
if item is None:
missing.append(host) # not in inventory: find the owner first
continue
if item["exclude"] == "1" or host not in mem:
continue
c95, mmax = p95(cpu[host]), max(mem[host])
if c95 < 20 and mmax < 50:
vcpu = max(1, math.ceil(int(item["vcpu"]) * c95 / 100 / HEADROOM))
gb = max(1, math.ceil(float(item["mem_gb"]) * mmax / 100 / HEADROOM))
print(f"{host}\t{item['owner']}\tcpu_p95={c95:.0f}%\tmem_max={mmax:.0f}%\t"
f"{item['vcpu']}->{vcpu} vCPU\t{item['mem_gb']}->{gb} GB")
print("not in inventory:", ", ".join(missing) or "-")
4. Set aside the exceptions. These come off the list, whatever the numbers say:
- Database primaries. Memory that looks idle may actually be doing work as cache and buffers.
- Clustered nodes. Shrink one, and load distribution and failover capacity change.
- Anything with seasonal peaks. The last 30 days may have been quiet; at certain times of the year the same machine hits the ceiling.
Decommissioning what isn't used
Some VMs are candidates not for downsizing but for shutting down. Order matters here:
- Ask the owner. If no answer comes, that is a finding too.
- Shut it down first, don't delete it. Wait a week. If something breaks, someone will definitely call.
- Then delete it. Remove it from the monitoring system and the inventory as well. Otherwise an "unreachable" alert stays lit on the monitoring screen forever.
Track savings honestly
Write a price into the savings sheet only when there is a real quote or invoice line. Otherwise, write down resources: how many vCPUs, how many GB of memory, how many GB of disk were given back. Don't write estimates. Add up estimates and you get a number nobody believes, and the work itself loses its value.
Watch out
- Watch closely for at least a week after downsizing. p95 is a good starting point, not a guarantee.
- Downsize in a maintenance window. On most platforms, changing vCPU and memory requires a restart.
- When lowering requests in Kubernetes, review the limits too. A memory limit that's too tight gets the pod OOM-killed.
Lesson
The prerequisite for rightsizing isn't a good script; it's an inventory with an owner for every VM. A machine without an owner can be neither downsized nor shut down; you just keep paying for it.
Checklist
- [ ] Does every VM have an owner in the inventory?
- [ ] Have 30 days of CPU p95 and peak memory been pulled?
- [ ] Have DB primaries, clustered nodes and seasonal systems been taken off the list?
- [ ] Was 30% headroom kept in the new size?
- [ ] Were VMs to be removed shut down first, with a week's wait?
- [ ] Were deleted VMs removed from monitoring and inventory?
- [ ] Is the savings sheet free of estimated figures?