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.
Run this first. It seeds the config and the state the checker reads:
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.
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, after logging the problem and printing an error to
stderr, if the config file does not exist.UP
when its check_file exists and DOWN when it does not./root/answers/healthcheck.log using the
logging module./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.