Course / The terminal and the language
Answer a question from a log file
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach 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 submitGoal
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
500inside 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
- Each line is
ip method path status bytes. A failed request has status 500 in the fourth column. - Searching for just
500also matches paths like/items/500and byte counts. Add spaces around it. grep -c " 500 " testdata/access.logthentail -n 5 testdata/access.log.