HANDS-ON TASK · 1 OF 1

Health check monitor

Project: Health check monitor

You're an SRE at esc bash. A handful of services are supposed to be running on every box, and right now the only way anyone knows if one fell over is when a customer notices. Build a small monitor that reads a list of services from a config file, checks each one, writes down what it found, and prints a status report an on call engineer can scan in two seconds.

Set up the project

Run this first. It seeds the config and the state the checker reads:

bash
curl -fsSL https://raw.githubusercontent.com/Esc-Bash/project-init-scripts/main/python-for-devops/project-2/init.sh | bash

This creates /root/mission/services.yaml, a config that lists four services. Each entry has a name and a check_file, the path of a file that exists only while that service is healthy (think of a pid file):

services:
  - name: nginx
    check_file: /root/mission/health/nginx.pid
  - ...

The seed also creates the health files for the services that are up and leaves them missing for the services that are down.

What to build

Write /root/scripts/healthcheck.py. When run with no arguments it reads /root/mission/services.yaml. It should also accept an alternate config path as its first argument, so it can be pointed at a different file.

The script must:

  1. Exit with code 1, after logging the problem and printing an error to stderr, if the config file does not exist.
  2. Otherwise load the YAML config and, for each service, treat it as UP when its check_file exists and DOWN when it does not.
  3. Log one line per service to /root/answers/healthcheck.log using the logging module.
  4. Write a status report to /root/answers/status.json with this exact shape:
{
  "total": <number of services>,
  "up": <how many are UP>,
  "down": <how many are DOWN>,
  "services": {
    "<name>": "UP" or "DOWN",
    ...
  }
}

Run the monitor against the seeded config so it produces /root/answers/status.json and /root/answers/healthcheck.log.

Press Submit when done.

Start the lab on the right to run checks.

Pass the checks to continue
Spin up a fresh environment and practice live.
python-devops · fresh machine · ready in under a minute