HANDS-ON TASK · 1 OF 1

Concurrent health monitor

Project: Concurrent health monitor

You are on call at esc bash. A handful of services each drop a small status file on disk while they are healthy, and you need one command that checks them all and tells you what is up and what is down. Checking them one after another is slow, so run the checks in parallel with goroutines and collect the results into a single JSON report.

Set up the project

Run this first. It seeds the services list and their status files:

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

This creates /root/mission/services.json and the /root/answers folder. The services file is a JSON array, each entry naming a service and the check file that exists only while it is healthy:

[
  {"name": "nginx", "check_file": "/root/mission/health/nginx.pid"},
  {"name": "postgres", "check_file": "/root/mission/health/postgres.pid"}
]

Some of those check files are present and some are missing.

What to build

Create a Go module at /root/healthmon with a main.go. The program reads /root/mission/services.json, then checks every service at the same time, one goroutine per service - a service counts as up when its check file exists and down when it does not. Wait for all the checks to finish, then write /root/answers/health.json with this exact shape:

{
  "up": <number of services that are up>,
  "down": <number of services that are down>,
  "services": {
    "nginx": "up",
    "redis": "down"
  }
}

The services object maps every service name to up or down. Launch the checks with goroutines and a sync.WaitGroup, guard the shared results with a mutex, and build the JSON with encoding/json. Run your program so it writes /root/answers/health.json, for example:

cd /root/healthmon && go run .

Press Submit when the report is written and the counts are correct.

Start the lab on the right to run checks.

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