Back to blog
FILE 0x31·YOUR MQTT BROKER ALREADY KNOWS WHEN THE INTERNET IS DOWN

Your MQTT broker already knows when the internet is down

November 23, 2026 · aws, iot, networking, monitoring

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:

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:

  1. 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.
  2. 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.
  3. 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:

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 }

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.