---
name: capacity-planner
description: Predict resource exhaustion, plan capacity needs, and alert before problems occur. Proactive infrastructure planning.
---

# Capacity Planner

Predict the future. Know when you'll run out of resources BEFORE it happens.

## Metrics Tracked

### Disk Space
```bash
# Current usage
df -h /

# Growth rate (daily)
# Compare snapshots over time

# Prediction formula:
# Days until full = (Total - Used) / Daily Growth Rate
```

### Memory
```bash
# Current usage
free -h

# Track RSS growth over time
# Detect memory leaks
```

### Database
```bash
# Table sizes
SELECT pg_size_pretty(pg_total_relation_size('table_name'));

# Row count growth
SELECT reltuples FROM pg_class WHERE relname = 'table_name';

# Connection pool usage
SELECT count(*) FROM pg_stat_activity;
```

### Docker
```bash
# Image/volume growth
docker system df

# Container resource usage
docker stats --no-stream
```

## Prediction Model

```
Current: 60% disk used (120GB / 200GB)
Growth: 2GB per day
Remaining: 80GB

⏰ PREDICTION: Disk full in 40 days
📅 Critical date: March 10, 2026
⚠️ Warning threshold (80%): March 1, 2026
```

## Alert Thresholds

| Resource | Warning | Critical | Action |
|----------|---------|----------|--------|
| Disk | 80% or <14 days | 90% or <7 days | Cleanup/Expand |
| Memory | 85% | 95% | Optimize/Scale |
| CPU | 80% sustained | 95% sustained | Scale |
| DB Connections | 80% pool | 95% pool | Increase pool |
| SSL Cert | 30 days | 7 days | Renew |

## Capacity Report Template

```
📊 CAPACITY FORECAST: VPS 60
Generated: January 29, 2026

┌─────────────────────────────────────────────┐
│ Disk Space                                  │
├─────────────────────────────────────────────┤
│ Current: 59GB / 193GB (31%)                │
│ Growth Rate: 1.2GB/day                      │
│ Forecast:                                   │
│ ├─ 50%: Feb 15 (~17 days)                  │
│ ├─ 80%: Mar 20 (~50 days) ⚠️               │
│ └─ 100%: Apr 10 (~71 days)                 │
│                                             │
│ Status: ✅ Healthy                          │
│ Action: None needed                         │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Memory                                      │
├─────────────────────────────────────────────┤
│ Current: 650MB / 1.5GB (43%)               │
│ Peak (24h): 890MB (59%)                    │
│ Trend: Stable (no leak detected)           │
│                                             │
│ Status: ✅ Healthy                          │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ Database (PostgreSQL)                       │
├─────────────────────────────────────────────┤
│ Size: 156MB                                 │
│ Growth: 2MB/day                             │
│ Connections: 8/100 (8%)                     │
│ Largest tables:                             │
│ ├─ generations: 89MB (56 rows)             │
│ └─ users: 12KB (2 rows)                    │
│                                             │
│ Status: ✅ Healthy                          │
└─────────────────────────────────────────────┘

┌─────────────────────────────────────────────┐
│ SSL Certificates                            │
├─────────────────────────────────────────────┤
│ rima.zesbe.my.id: 89 days remaining ✅     │
│ rima-api.zesbe.my.id: 89 days remaining ✅ │
└─────────────────────────────────────────────┘

📋 SUMMARY:
- No immediate capacity concerns
- Next review: February 15, 2026
- Projected needs: None for 60 days
```

## Proactive Actions

### When Approaching Limits:
```
Disk > 70%:
1. Auto-cleanup Docker (images, cache)
2. Rotate/compress logs
3. Archive old data
4. Alert if still > 70%

Memory > 80%:
1. Identify memory hogs
2. Restart leaky services
3. Increase swap if needed
4. Alert for manual review
```

## Scaling Recommendations

```
When to scale UP:
- Sustained CPU > 80% for 1 hour
- Memory > 85% consistently
- Response time degrading
- Disk growth > capacity growth

When to scale OUT:
- Single instance at limits
- Need high availability
- Geographic distribution needed

Recommendation format:
"Based on growth trends, upgrade to X within Y days"
```

## Integration

### Daily:
- Collect resource metrics
- Update growth projections
- Check against thresholds

### Weekly:
- Generate capacity report
- Update forecasts
- Alert on upcoming limits

### Monthly:
- Review scaling needs
- Cost optimization analysis
- Infrastructure planning
