# Metrics and usage

Read a miniVM's CPU, memory and disk, and your organization's usage.

## A miniVM's metrics

Each running miniVM is sampled every 10 seconds: CPU, memory and disk. Samples are kept for 7 days.

### ons

```bash
ons metrics <minivm-id>
```

`<minivm-id>` is the id `ons create` printed (`ons ls` lists yours).

- `--since <seconds>` sets how much history to show (default 300).
- `--watch` keeps printing new samples until you press Ctrl-C. With `--json`, it prints one JSON object per line.

### Python

```python
from e2b import Sandbox

sbx = Sandbox.connect("<minivm-id>")
for m in sbx.get_metrics():
    print(m.timestamp, m.cpu_used_pct, m.mem_used, m.mem_total)
```

### JavaScript

```javascript
import { Sandbox } from 'e2b'

const sbx = await Sandbox.connect('<minivm-id>')
for (const m of await sbx.getMetrics()) {
  console.log(m.timestamp, m.cpuUsedPct, m.memUsed, m.memTotal)
}
```

Set `E2B_DOMAIN=onshore.sh` and `E2B_API_KEY` before you run the SDK examples. See [Migrate from E2B](https://onshore.sh/docs/guides/migrate-from-e2b).

### What each field means

| Field                   | Meaning                                           |
| ----------------------- | ------------------------------------------------- |
| `cpuCount`              | vCPUs of the miniVM.                              |
| `cpuUsedPct`            | CPU use, in percent of its vCPUs.                 |
| `memUsed`               | Memory the guest uses: `MemTotal - MemAvailable`. |
| `memTotal`              | The miniVM's memory.                              |
| `diskUsed`, `diskTotal` | Used and total size of the root disk.             |
| `timestamp`             | When the sample was taken.                        |

> **Note:** A miniVM whose template was built before its in-VM daemon could report guest metrics shows `memUsed`, `diskUsed` and `diskTotal` as 0. Rebuild the template, then create a new miniVM.

### From the REST API

```bash
curl -s -H "X-API-Key: $ONS_API_KEY" "https://api.onshore.sh/sandboxes/<minivm-id>/metrics?start=<unix-start>&end=<unix-end>"
```

- `<unix-start>` and `<unix-end>` are Unix times in seconds. Leave both out for all 7 days.
- It returns at most the newest 1000 samples, oldest first.
- `GET /sandboxes/metrics?sandbox_ids=<id-1>,<id-2>` returns the newest sample of each listed running miniVM (up to 100 ids).

You only see the metrics of miniVMs your key created; any other id answers `404`.

## Your organization's usage

In the console, open **Usage**. It shows the last 30 days, per day: vCPU-hours, memory GiB-hours and the number of miniVMs. **Fleet** shows your running and paused miniVMs, their size and how long they ran.

What you pay for this usage is on **Billing**: your balance, the credit ledger and invoices. See [Billing](https://onshore.sh/docs/account/billing) and [Pricing](https://onshore.sh/docs/account/pricing).

Markdown version: https://onshore.sh/docs/guides/metrics.md
