>_ IzzyLab

Hands-on task

This task is step 12 of the course “curl and HTTP APIs”

A URL probe with the code in a log

medium~10 min

Write a script /usr/local/bin/probe that takes a URL as its first argument. If the server answered with code 200, the script writes nothing; otherwise it appends the line FAIL <code> <url> to /var/log/probe.log, e.g. FAIL 404 http://127.0.0.1:18090/missing. When there's no connection at all, the code is whatever curl returned (000). A local site to try the script on is already running at http://127.0.0.1:18090/.

It's handy to write the script in the Editor tab: create the file /usr/local/bin/probe there, and run and test it in the terminal.

What you'll practise:
  • curl -w
  • http codes
  • bash scripts
  • script arguments
  • appending to a log

Topic breakdown

An availability probe relies on curl being able to output just the HTTP status: the body is sent to /dev/null with -o, and the code is printed with the -w '%{http_code}' format. Codes vary: 404 and 500 are the server's answer, while if the port is closed there's no answer at all, and curl puts in 000. Logging the code itself is useful: 000 shows right away that the service is down, 5xx that it's alive but failing. The line is appended with >> so the previous entries aren't wiped.

What's checked

  1. the probe script is executable
  2. a closed port is logged as FAIL 000
  3. a 404 answer is logged with its code
  4. the script stays silent on a 200 answer

What it looks like