Step 5 of 6 · lab · 30 min
Availability and p95 from the log
A script that computes the site availability and the 95th percentile of the response time from the nginx log for any time window — one command for an SLI instead of a pipeline typed from memory.
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
LOGis an nginx log in this format, plain or gzip-compressed (.gz);FROMandTOare times in any formdate -dunderstands:'2026-10-22 13:00','2026-10-22T13:00:00';- the window takes the requests from
FROMinclusive up toTOexclusive — 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.
Lab · on your own
No commands are given in this lab: type them in the terminal yourself. Goals are checked on the machine after every command.
Goals
0 of 4-
~/sli.sh is an executable script that prints one line total=… availability=… p95=… for the machine's log — not done yet
-
On the check's logs total and availability match the reference — not done yet
-
On the check's logs p95 matches the reference — not done yet
-
The script reads a compressed log (.gz) itself and prints total=0 availability=- p95=- for a window without requests — not done yet
The machine is starting
Hints
0 of 3Lab goals
0 of 4-
~/sli.sh is an executable script that prints one line total=… availability=… p95=… for the machine's log — not done yet
-
On the check's logs total and availability match the reference — not done yet
-
On the check's logs p95 matches the reference — not done yet
-
The script reads a compressed log (.gz) itself and prints total=0 availability=- p95=- for a window without requests — not done yet
Goals are checked on the machine after every command.