Linux Labs

SRE practices: SLOs, incidents, redundancy / Stage 2 of 8 · step 5 of 6

Availability and p95 from the log

medium ~30 min
Not running Starting usually ~20 s
Report a problem

Situation

The on-call engineers of the shop.lab.test store still compute its SLIs — availability and p95 response time — with a pipeline each of them types from memory, and the numbers for the same hour come out different for everyone. One script that everybody trusts is needed.

The site log is /var/log/nginx/shop.access.log (yesterday's is shop.access.log.1.gz); the line format is set in /etc/nginx/conf.d/sre-log.conf: each line ends with the response time $request_time and the request's unix time $msec. The nginx logs are read with sudo.

Task

Write the script ~/sli.sh that runs like this:

~/sli.sh LOG FROM TO
  • LOG is an nginx log in this format, plain or gzip-compressed (.gz);
  • FROM and TO are times in any form date -d understands: '2026-10-22 13:00', '2026-10-22T13:00:00';
  • the window takes the requests from FROM inclusive up to TO exclusive — by the request's unix time.

The script prints one line:

total=<requests in the window> availability=<availability> p95=<p95>
  • availability is the percentage of answers with a code below 500 among all requests of the window, two digits after the point (93.04);
  • p95 is the response time by the nearest rank: the times in ascending order, line number ⌈0.95 · total⌉, the value as written in the log;
  • for a window without requests — total=0 availability=- p95=-.

The check runs the script as you on its own logs that are not on the machine, so the script needs no sudo inside. You can check the script on the machine's log with ~/sli-check.txt: it holds a command and the line it must print.

The tools you need are already installed. There is no internet on the machine.

Visible only to you. Saved as you type.

All notes

Lab goals

0 of 4
  • ~/sli.sh is an executable script that prints one line total=… availability=… p95=… for the machine's log — not checked yet
  • On the check's logs total and availability match the reference — not checked yet
  • On the check's logs p95 matches the reference — not checked yet
  • The script reads a compressed log (.gz) itself and prints total=0 availability=- p95=- for a window without requests — not checked yet

Hints

0 of 3 open · they open one at a time
Hint 1
Show the solution after 3 failed checks

Leave the lab?

The machine keeps running for 10 more minutes, then it is removed. Files are lost; completed goals stay.

Loading…