Your MQTT broker already knows when the internet is down
Utilities publish outage maps. Most ISPs don't — or they hide them behind a customer login and a bot challenge. So when a carrier has a bad night, the first signal you get is a phone call.
The obvious fix is a heartbeat: have something at each site ping you every N minutes, or ping it yourself. Both are wrong if you already run a few thousand agents that hold a persistent MQTT session to AWS IoT Core. That session is the heartbeat. You just aren't listening to it.
The broker tells you, for free
IoT Core publishes a lifecycle event on $aws/events/presence/connected/<clientId> and .../disconnected/<clientId> for every session. The connected event carries the client's public IP. The disconnected event carries a reason, and the reason is the whole trick:
CLIENT_INITIATED_DISCONNECT— someone shut the machine down. Ignore it.MQTT_KEEP_ALIVE_TIMEOUT/CONNECTION_LOST— the line went quiet. With a 60 s keepalive you hear about it ~90 s after the WAN dies.DUPLICATE_CLIENTID— a reconnect kicked the old session, often after the new session's connected event arrives. Ignore it, or you'll mark live machines dead.
One topic rule ships them to SQS:
SELECT eventType, clientId, timestamp, ipAddress, sessionIdentifier, disconnectReason
FROM '$aws/events/presence/+/+'
WHERE startswith(clientId, 'agent-')
A worker keeps one row per client, guarded by the event's own timestamp because SQS and IoT both deliver out of order:
insert into agent_presence (...) values (...)
on conflict (client_id) do update set state = 'down', ...
where agent_presence.event_ms <= excluded.event_ms
and (agent_presence.session_id is null or agent_presence.session_id = excluded.session_id)
One machine going dark is noise
Across ~3,000 agents there are roughly 8,000 keepalive timeouts a day. Laptops sleep, Wi-Fi blips, people yank cables. What you want is correlation:
- Place every agent at a site by its public IP: documented WAN addresses first, then any address a server sits on, then any address two machines share. A laptop at home has its own address, so it never keeps the office looking "up". IPv6 gets cut to its /64, since every device has its own address but an office LAN shares the prefix.
- A site is dark when none of its office machines is connected and the biggest cluster of timeouts (within 20 minutes of each other) is at least two machines or one server, and is older than a 4-minute grace period.
- Group dark sites by carrier and county. The carrier is the documented ISP for that WAN address, or the address's AS name from Team Cymru's DNS service (
4.3.2.1.origin.asn.cymru.com TXT) — no API key needed.
Then the verdict writes itself:
- 2+ sites on one carrier dark in one county, other carriers there fine → carrier outage.
- Everything in the county dark, across carriers → power or something regional.
- One site dark → that site's problem.
- A site's machines all reconnect together from a new address → it failed over to its backup line; the primary is down.
That last one is my favourite. A site on a backup LTE link never drops off at all; it just comes back from somewhere else.
Asking the witness afterwards
An agent can't report while it's offline. It can once it's back. I had an agent-side recorder written (ping the gateway and the internet during the gap, persist across reboots), but the code-signing pipeline was down, so I shipped the version that needs no new binary: when a site's outage clears, the backend sends one recovered machine a read-only command over the existing command topic:
$boot = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
Get-WinEvent -FilterHashtable @{ LogName='Microsoft-Windows-NetworkProfile/Operational'; Id=10000,10001; StartTime=$s; EndTime=$e }
Get-WinEvent -FilterHashtable @{ LogName='System'; Id=41,6008; StartTime=$s; EndTime=$e }
- Booted during the gap (or Kernel-Power 41) → power.
- NetworkProfile 10001 ("disconnected") → the PC lost its LAN: switch, firewall or rack.
- Neither → the LAN stayed up and the line didn't → carrier.
Run it as powershell -EncodedCommand with $ProgressPreference='SilentlyContinue', or the CLIXML progress records that land on stderr will mangle your JSON.
How it went
The first poll after the backfill flagged two sites on the same cable carrier in the same county, dark since 8:00 PM the night before. That matched what the field team was already grumbling about, except this time the map said it before anyone called.
What I'd do differently: dedupe by machine, not client ID, from day one. One laptop with two client IDs produced a lone 10 PM "drop" that briefly masked the real 8 PM outage, because I'd taken the latest burst instead of the biggest.