# Device health

Calls go to https://supply.coronium.ai, signed in as in [enrolment](/guides/device-enrol.md). The states that decide whether a device may carry buyers at all are in [device states](/guides/device-states.md).

## What it is

Coronium checks that each connected device still carries traffic. A device that is `online` and `admitted` or `established` reads `health` in `GET /app/api/devices`; any other device reads `health` as null.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
curl -sS -b coronium.cookies "$SUPPLY/app/api/devices" | jq -c '.counts.byHealth, (.devices[] | {deviceId, status, health})'
```

Expect: `health.state`, `health.reason` for a failing device, `health.since` for when the state began and `health.firstByteMs` for how quickly the first byte of the last passed check came back. `counts.byHealth` counts those devices by state. Safe to repeat: yes.

<!-- facts:begin health.states -->
| `health` reads | Means | Do |
|---|---|---|
| `unchecked` | Connected. Its first check is on its way. | Nothing. |
| `healthy` | Passed its last check. | Nothing. |
| `slow` | Passes its checks, but slowly. It may be used after faster devices. | Check the line, or move the device closer to its router. |
| `failing`, `silent` | Connected, but it does not answer. | Restart the agent and read its log. |
| `failing`, `no_bytes` | It opens connections and returns no data. | Look for a firewall, a VPN or a sign-in page on this network. |
| `failing`, `tls_failed` | Something on this network alters secure connections. | Remove the inspecting proxy or antivirus, or use another network. |
| `failing`, `no_route` | It cannot reach the internet. | Check the connection and its DNS. |
<!-- facts:end health.states -->

A failing device gets no new buyer traffic, keeps the traffic it carries, and is checked again by itself: nobody has to act for it to come back. A device that connects is checked at once, with two bounds: one that reconnects again and again is not checked for it each time, and one that fails and reconnects sooner than its next check was due waits for that check. A reconnect too quick for the directory to show is not seen as a new connection, so such a device waits for its scheduled check. An account is told once a day of devices that stay failing, by the `device_withheld` [notification](/guides/notifications.md).

## When a device does not connect

Every device also reads `connections` in the same call: how its connections to the relay ended, newest first, each with a count, the first and last time, and the relay's own `code`, `cause` and `fix`. They are the words the agent prints, which [troubleshooting](/guides/device-troubleshoot.md) lists. `stage` says how far a connection got:

<!-- facts:begin connection.stages -->
| `stage` reads | Means | Do |
|---|---|---|
| `admitted` | Let in: the relay answered with its welcome. | Nothing. |
| `tcp` | Connected, then sent nothing in time, or came from an address that already held its share of handshakes. | Dial the relay address enrolment gave and speak first; spread a large fleet behind one address over time. |
| `tls` | The TLS handshake did not finish. | Check the device's clock and certificate store, and that nothing on its network inspects secure connections. |
| `upgrade` | The WebSocket upgrade was refused, or did not arrive whole in time. | Open the WebSocket as the device WebSocket guide says. |
| `hello` | No valid first message arrived in time. | Send the hello first and whole, right after connecting. |
| `admission` | The relay answered the hello with a refusal. | Follow the fix the refusal carries. |
| `keepalive` | Let in, then silent past the keepalive limit. | Answer every ping, and keep the device's network awake. |
| `replaced` | Another connection with the same device id took its place. | Run one agent for each device id. |
| `protocol` | Let in, then a message broke the protocol. | Update the agent, or check a client of your own against the protocol's vectors. |
| `closed_by_peer` | Let in, then the device or its network closed the connection. | Nothing when it is rare. When it repeats, check the device's network and its power saving. |

Connection attempts are counted by the minute, without the address they came from, and the counts are kept 7 days. Each way a device's connections ended is kept 30 days after it was last seen.
<!-- facts:end connection.stages -->

A connection that never names a device id is counted without one and appears on no device: for those, read the agent's own line.

## What the checks cost

<!-- facts:begin health.checks -->
A check is one short secure connection through the device to Coronium's own servers. It uses the device's connection, earns nothing and is in no count of `requests`, `bytesUp` or `bytesDown`. A connected device on a home line that carries no traffic is checked about every 20 minutes. A device on a mobile exit, or one that carries traffic, is checked about every 60 minutes. One check is ended once it has moved 8192 bytes of payload both ways together, as the edge counts them. No check of a device starts once its checks have moved 1048576 bytes of payload in a UTC day, as the edge counts them. The device's line also carries what the edge does not count: the connection's own framing and the name lookup. A device that just connected or failed a check is checked sooner.
<!-- facts:end health.checks -->
