Heartbeat monitoring

What heartbeat monitoring is

A heartbeat monitor watches the jobs that run by themselves: by a schedule or in the background. The job sends a signal, a heartbeat, after every run. While the signals come, all is well. When the signals stop, or the job reports a failure, mon.red opens an incident and sends you an alert. Such a monitor is also called a dead man’s switch or cron monitoring.

Where it is used

  • Cron jobs and the tasks of the Windows Task Scheduler
  • Backups and dumps of databases
  • Imports, exports and the synchronisation of data
  • Reports and mailings sent by a schedule
  • The renewal of certificates (certbot) and other maintenance scripts
  • Queue workers and services in the background: they send a signal every few minutes while they run
  • Scheduled runs of GitHub Actions and other CI
  • Servers and devices behind NAT or a firewall that cannot be checked from outside: a home server, a router, an IoT device

How it works

A website can be checked from outside. A cron job cannot: it runs inside your server, and when it stops, nobody sees it. A heartbeat monitor turns the check around: your job calls the address of the monitor each time it has done its work.

You set how often a signal is expected (the period) and how late it may come (the grace time). When the period and the grace time pass without a signal, the monitor goes down and mon.red sends an alert to your channels. The next signal brings it back up.

Nothing is checked from our side, so it works for machines that cannot be reached from the internet. The job only has to make one outgoing HTTPS request.

Create a heartbeat monitor

  1. In the board press Add a monitor and open the Heartbeat tab.
  2. Give the monitor a name, choose Expect a signal every (how often the job runs) and Grace time (how late a signal may come).
  3. Press Create. The card of the monitor shows its address: https://ping.mon.red/<token>

Until the first signal no alert is raised. Keep the address secret: anyone who has it can send signals to the monitor.

Signals

Every signal is one HTTP request to the address of the monitor, GET or POST. The end of the address says what happened.

Success

The job has done its work. The monitor is up, an open incident closes, and the next signal is due within the period and the grace time. Any of these addresses means a success:

  • /ok
  • https://ping.mon.red/<token> the address without an ending, the short form for crontab and simple scripts
  • /0 the exit code 0

Start

/start the job has begun its work. The state of the monitor does not change. If a success does not come within the grace time, the monitor goes down: the job hangs. Between the start and the success mon.red counts how long the job ran. This signal is optional.

Failure

The job reports an error. The monitor goes down at once and an incident opens. Any of these addresses means a failure:

  • /fail
  • /<code> any exit code other than 0, the code is shown in the alert. In a shell script the code of the last command is in $?.

The text of a signal

A short text may come with any signal: ?msg=… in the address or the body of a POST request. It is shown in the last signals of the monitor, in the alert and on the incident page.

mon.red keeps the text as one line of up to 200 bytes: line breaks become spaces, control and invisible characters are removed, the rest is cut off. If a signal has both, the text of ?msg= is taken.

What mon.red answers

  • OK the signal is accepted. A paused monitor answers the same, but the signal changes nothing.
  • 404 Not found mon.red does not know the address: the token is wrong, or the monitor was deleted.
  • 429 Too many signals more than 60 signals a minute for one monitor.

The address without a token answers pong: a quick way to see that the signals get through.

$ curl https://ping.mon.red/
pong

Examples

In crontab

After the job, only when it has finished well:

0 3 * * * /path/to/backup.sh && curl -fsS -m 10 --retry 3 https://ping.mon.red/<token> > /dev/null

Or with the exit code, so that a failure is reported at once:

0 3 * * * /path/to/backup.sh; curl -fsS -m 10 --retry 3 https://ping.mon.red/<token>/$? > /dev/null

A script with its start and end

#!/bin/sh
URL=https://ping.mon.red/<token>

curl -fsS -m 10 --retry 3 "$URL/start" > /dev/null
/path/to/backup.sh
curl -fsS -m 10 --retry 3 "$URL/$?" > /dev/null

mon.red then shows how long the backup ran and raises an alert when it hangs.

A text with the signal

curl -fsS -m 10 --retry 3 -G --data-urlencode "msg=db1: 4.2 GB in 3 min" https://ping.mon.red/<token> > /dev/null

Windows: PowerShell and Task Scheduler

After a program its exit code is in $LASTEXITCODE:

C:\scripts\backup.exe
Invoke-RestMethod -Uri "https://ping.mon.red/<token>/$LASTEXITCODE" -TimeoutSec 10 | Out-Null

GitHub Actions

Our action mon.red Heartbeat Monitor sends the signal from a workflow, with the status of the job and a link to the run:

- uses: mon-red/heartbeat@v1
  if: always()
  with:
    url: ${{ secrets.MONRED_HEARTBEAT_URL }}
    status: ${{ job.status }}

Put the address of the monitor into the secret MONRED_HEARTBEAT_URL of the repository. More examples are in the README of the action.

Period and grace time

The period is how often the job runs: a day for a nightly backup, an hour for an hourly one. The grace time is how late a signal may come. A nightly backup with a period of a day and a grace time of an hour raises an alert when there is no signal for 25 hours.

Give a job that runs for long or starts late enough grace time. Scheduled runs of GitHub Actions often start late, sometimes by tens of minutes: an hour or more suits them.

When you change the period or the grace time, the time of the next signal is counted again from the last signal.

Alerts and incidents

An incident opens when the time of a signal has passed (no signal came, or the job started and did not report its end) or when a failure comes. It closes with the next success. The alerts go to the channels of the monitor, as its notifications are set.

A paused monitor ignores its signals. After the pause the next signal is due within the period and the grace time, counted from the end of the pause: if it does not come, the monitor goes down and you get an alert. A monitor that has not had a single signal yet waits for the first one without an alert.

Good to know

  • A job that sometimes runs longer: send /start before it and give it a grace time longer than its longest run.
  • The same job on several servers: give each server its own monitor. With one monitor the signals of one server hide the silence of another.
  • The address got into the wrong hands: create a new monitor and delete the old one. The old address stops working at once.
  • The time zone of the server does not matter: mon.red counts only the time between the signals.