Course outline
Course / The terminal and the language

Answer a question from a log file

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each lab: download it, edit the file named in the instructions, then submit.

gonomicon fetch week-1/02-07-answer-a-question-from-a-log
cd 02-07-answer-a-question-from-a-log
gonomicon submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal

Your team asks: "How many requests failed with status 500, and what just happened?" Answer from testdata/access.log: print the number of failed requests, then the last 5 lines of the log.

Example

Each line looks like ip method path status bytes. For this log:

10.0.0.1 GET /home 200 512
10.0.0.2 POST /pay 500 87
10.0.0.3 GET /items/500 200 40

your script prints (the count is 1: only the middle line has status 500; with few lines, "the last 5" is the whole file):

1
10.0.0.1 GET /home 200 512
10.0.0.2 POST /pay 500 87
10.0.0.3 GET /items/500 200 40

Rules

  • Edit answer.sh. Print the count on its own line first, then the last 5 lines.
  • Only the status counts. A 500 inside a path (/items/500) or a byte count is not a failure.
  • If there are no failures, the first line is 0.

What you'll practice

Reading a log to answer a real question with grep -c and tail.

Run it

gonomicon fetch week-1/02-07-answer-a-question-from-a-log
cd 02-07-answer-a-question-from-a-log
gonomicon test
gonomicon submit

Hints

  1. Each line is ip method path status bytes. A failed request has status 500 in the fourth column.
  2. Searching for just 500 also matches paths like /items/500 and byte counts. Add spaces around it.
  3. grep -c " 500 " testdata/access.log then tail -n 5 testdata/access.log.

Something wrong with this lesson? Report a problem