>_ IzzyLab

Hands-on task

This task is step 17 of the course “Monitoring and metrics”

Healthcheck with a Failure Log

medium~10 min

Write /usr/local/bin/healthlog that takes a URL: if the URL responds 200 — stay silent, otherwise (any other code, or the server is unreachable) append DOWN <url> to /var/log/health.log. Test it against the unreachable http://localhost:59999/.

The script is handy to write in the Editor tab: the /usr/local/bin directory shows up there in the file tree; run and test it in the terminal.

What you'll practise:
  • healthcheck
  • curl
  • http codes
  • bash scripts
  • script arguments

Topic breakdown

A healthcheck is one check: get the HTTP status for a URL and compare it against the expected one (200 = alive). curl can return only the response code if you send the body to /dev/null and request the right placeholder via the output format -w. Gotcha: when the port isn't listening at all, that's not an HTTP error but a connection refusal — there's no real code, and curl substitutes 000, which still isn't equal to 200. On a mismatch the event is appended (>>) to the log, where the URL is taken from the positional argument $1.

What's checked

  1. Script is executable
  2. Failure recorded in the log
  3. A 404 response counts as a failure too
  4. Script stays silent on a live URL (200)

What it looks like