# Monitoring indicators
<subtitle>View sandbox CPU, memory, and disk usage to troubleshoot performance issues and observe resource trends. </subtitle>

UCloud Sandbox collects sandbox resource usage metrics, including CPU, memory, and disk. Through these indicators, you can observe task load, locate performance bottlenecks, or determine whether the sandbox resource configuration is appropriate.

## Indicator content

The system collects indicator data periodically. The obtained indicators usually include the following dimensions:

- **CPU**: Number of CPU cores and current usage.
- **Memory**: Total memory and currently used memory.
- **Disk**: Total disk space and currently used disk space.
- **Timestamp**: The collection time corresponding to each indicator point.

> Immediately after the sandbox is created, it may take a few seconds for the monitoring component to produce the first batch of metrics. Before this, the SDK fetch results may be empty.

## Use SDK to get metrics

Call `get_metrics()` to obtain the historical indicator data of the sandbox. Metrics are returned as a timestamped array.

```python
from time import sleep
from ucloud_sandbox import Sandbox

sbx = Sandbox.create()
print("Sandbox has been created", sbx.sandbox_id)

# Wait for a while for the system to collect initial indicators
sleep(10)

metrics = sbx.get_metrics()

# It can also be obtained through the sandbox ID:
# metrics = Sandbox.get_metrics(sbx.sandbox_id)

for m in metrics:
    mem_used_mib = m.mem_used / 1024**2
    mem_total_mib = m.mem_total / 1024**2
    disk_used_mib = m.disk_used / 1024**2
    disk_total_mib = m.disk_total / 1024**2

    print(
        f"[{m.timestamp}] "
        f"CPU: {m.cpu_used_pct:.2f}% / {m.cpu_count} Cores | "
        f"Memory: {mem_used_mib:.0f} / {mem_total_mib:.0f} MiB | "
        f"Disk: {disk_used_mib:.0f} / {disk_total_mib:.0f} MiB"
    )
```

## Use CLI to view metrics

Sandbox metrics can also be viewed via the UCloud Sandbox CLI:

```bash
ucloud-sandbox-cli sandbox metrics <sandbox-id>
```

Output example:

```text
Sandbox: iaba1wkrrkyjswxoo4q6e  (21 samples)

── CPU Usage (%)
 100.00 ┤
  87.50 ┤
  75.00 ┤
  62.50 ┤
  50.00 ┤
  37.50 ┤
  25.00 ┤
  12.50 ┼╮
   0.00 ┤╰───────────────────
        └┬────┬────┬────┬────┬
     15:00:30  15:01:20  15:02:10

── Memory Usage (%)
 100.00 ┤
  87.50 ┤
  75.00 ┤
  62.50 ┤
  50.00 ┤
  37.50 ┤
  25.00 ┤
  12.50 ┼────────────────────
   0.00 ┤
        └┬────┬────┬────┬────┬
     15:00:30  15:01:20  15:02:10

── Disk Usage (%)
 100.00 ┤
  87.50 ┤
  75.00 ┤
  62.50 ┤
  50.00 ┤
  37.50 ┤
  25.00 ┤
  12.50 ┼────────────────────
   0.00 ┤
        └┬────┬────┬────┬────┬
     15:00:30  15:01:20  15:02:10
```

The CLI output will draw the collected samples into CPU, memory and disk usage curves, making it easy to observe resource change trends directly on the terminal.

## Notes

- Indicators are mainly used to observe operating trends and troubleshoot problems, and are not recommended as a basis for precise billing.
- If the sandbox has just been created, you may need to wait a few seconds before obtaining the metrics.
- If the sandbox has been destroyed, you cannot continue to obtain the running indicators of the sandbox.
