Send Prometheus alerts to WhatsApp and restart failed services automatically
At Hashmicro I monitored 100+ servers and web endpoints, and the alert channel that worked was the one the team already had open: WhatsApp. The pattern has four parts. Prometheus detects the failure, Alertmanager groups and routes it, a small relay posts it to WhatsApp, and a restart policy handles the failures that do not need a person.
1. Alert on symptoms, with a for window
groups:
- name: availability
rules:
- alert: EndpointDown
expr: probe_success == 0
for: 1m
labels:
severity: critical
annotations:
summary: "{{ $labels.instance }} is not responding"
- alert: DiskAlmostFull
expr: (node_filesystem_avail_bytes{fstype!~"tmpfs|overlay"} / node_filesystem_size_bytes) < 0.10
for: 10m
labels:
severity: warning
annotations:
summary: "{{ $labels.instance }} has under 10% disk left on {{ $labels.mountpoint }}"probe_success comes from blackbox_exporter, which checks the endpoint from outside, the way a user would. The for: 1m window filters short blips. With a 15 to 30 second scrape interval and a 30 second group_wait, the message arrives in under two minutes after the failure starts.
2. Group and route in Alertmanager
route:
receiver: whatsapp
group_by: [alertname, instance]
group_wait: 30s
group_interval: 5m
repeat_interval: 4h
routes:
- matchers: [severity="warning"]
receiver: whatsapp
repeat_interval: 12h
receivers:
- name: whatsapp
webhook_configs:
- url: http://127.0.0.1:9095/alert
send_resolved: trueGrouping is what keeps the channel readable. One dead host can fire ten alerts; grouped, the team gets one message. send_resolved: true tells them when it is over, so nobody has to ask.
3. The relay: webhook in, WhatsApp out
Alertmanager has no WhatsApp receiver, so a small relay translates the webhook. This example uses the WhatsApp Cloud API. Any gateway that accepts an HTTP POST works the same way.
import { createServer } from 'node:http'
const { WA_TOKEN, WA_PHONE_ID, WA_TO } = process.env
createServer(async (req, res) => {
if (req.method !== 'POST' || req.url !== '/alert') return res.writeHead(404).end()
let raw = ''
for await (const chunk of req) raw += chunk
const { status, alerts } = JSON.parse(raw)
const lines = alerts.map((alert) => '- ' + alert.annotations.summary)
const body = (status === 'resolved' ? 'RESOLVED' : 'FIRING') + '\n' + lines.join('\n')
const sent = await fetch('https://graph.facebook.com/v21.0/' + WA_PHONE_ID + '/messages', {
method: 'POST',
headers: { Authorization: 'Bearer ' + WA_TOKEN, 'Content-Type': 'application/json' },
body: JSON.stringify({ messaging_product: 'whatsapp', to: WA_TO, type: 'text', text: { body } }),
})
res.writeHead(sent.ok ? 200 : 502).end()
}).listen(9095, '127.0.0.1')- Return a non-2xx status when sending fails. Alertmanager then retries instead of dropping the alert.
- Bind to
127.0.0.1. The relay holds a token and must not be reachable from outside. - The Cloud API sends free-form text only inside a 24-hour conversation window. For unattended alerts, register a message template and send that.
4. Let safe failures heal themselves
Where a restart is the known fix, do not wake anyone. systemd already does this well.
# /etc/systemd/system/myapp.service.d/restart.conf
[Unit]
StartLimitIntervalSec=300
StartLimitBurst=5
[Service]
Restart=on-failure
RestartSec=5The start limit is the important part. After five failed restarts in five minutes systemd stops trying, the service stays down, and the alert reaches a person. Self-healing without a limit hides real outages.
Check it before you trust it
- Stop a service on purpose and time how long the message takes.
- Kill the relay and confirm Alertmanager retries. Then add an alert for the relay itself.
- Add an always-firing watchdog alert to an external heartbeat service, so silence from a dead Prometheus is also noticed.
- Keep the token in an environment file that only the service user can read.