Linux Basics Lesson 13 of 38
Who is hammering the API?
9 minutes
Learning goals
- Get a first impression of the size of a log before you evaluate it.
- Find the top five callers with one pipe and save the result.
- Follow up with what the top caller is actually requesting.
Everything you need comes from this module. The log is in the format combined: the address is the first field and the requested path is
field 7, counting at single spaces. So cut -d' ' -f7 gets you the path.
On a real server, /var/log/nginx belongs to root:adm. You read it with sudo or as a member of the group adm, and lerner is one here.
Steps
Step 1/3: How big is /var/log/nginx/access.log? Find out the number of lines first. Step 2/3: Which five IP addresses call most often? Save them with their counts in ~/top5.txt, the biggest first. Step 3/3: What does the top caller, 203.0.113.7, request? Save the paths of its calls with their counts in ~/scraper.txt, the biggest first. Scenario solved
The API has been sluggish since this morning, and the team suspects a scraper. Nobody has numbers, only a feeling. In front of you is the nginx log. Find out who is calling most often and what they are after, and save both, so that the ticket says more than "maybe a bot".
-
Step 1 done solved before
How big is /var/log/nginx/access.log? Find out the number of lines first.
Hint 1: Which command counts lines? You met it in the lesson on grep.Hint 2: Type wc -l /var/log/nginx/access.logSolution
wc -l /var/log/nginx/access.logWhy: 300 lines is little, this is a small excerpt. A busy server writes millions. Asking first tells you whether cat is out of the question. And you can read the file at all only because lerner is in the group adm: the log belongs to root:adm with mode 640, as on Debian.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 2 done solved before
Which five IP addresses call most often? Save them with their counts in ~/top5.txt, the biggest first.
Hint 1: The address is the first column, cut takes it out. Then the chain from the lesson, and head keeps the top. awk is not loaded here, cut does the same with one space as separator.Hint 2: Type cut -d' ' -f1 /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -n 5 > ~/top5.txtSolution
cut -d' ' -f1 /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -n 5 > ~/top5.txtWhy: Read the pipe from left to right like a sentence: cut takes the address column, sort puts equal addresses side by side, uniq -c counts each group, sort -rn puts the biggest counts on top, head keeps five. 203.0.113.7 has 120 of 300 calls, the next one 40. There is your scraper.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
-
Step 3 done solved before
What does the top caller, 203.0.113.7, request? Save the paths of its calls with their counts in ~/scraper.txt, the biggest first.
Hint 1: First filter the lines of this address, then the same chain with another column. The path is field 7. In a pattern, a dot stands for any character, [.] for a real dot, and ^ for the start of the line.Hint 2: Type grep '^203[.]0[.]113[.]7 ' /var/log/nginx/access.log | cut -d' ' -f7 | sort | uniq -c | sort -rn > ~/scraper.txtSolution
grep '^203[.]0[.]113[.]7 ' /var/log/nginx/access.log | cut -d' ' -f7 | sort | uniq -c | sort -rn > ~/scraper.txtWhy: Page after page of /api/products, all from python-requests. That is a scraper and not a popular customer. The space after the address in the pattern matters, otherwise 203.0.113.71 would count too. Now you can go to the team with numbers instead of a suspicion.
See the solution? You get fewer XP for this scenario.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Scenario solved
Why each step mattered
- Step 1: 300 lines is little, this is a small excerpt. A busy server writes millions. Asking first tells you whether cat is out of the question. And you can read the file at all only because lerner is in the group adm: the log belongs to root:adm with mode 640, as on Debian.
- Step 2: Read the pipe from left to right like a sentence: cut takes the address column, sort puts equal addresses side by side, uniq -c counts each group, sort -rn puts the biggest counts on top, head keeps five. 203.0.113.7 has 120 of 300 calls, the next one 40. There is your scraper.
- Step 3: Page after page of /api/products, all from python-requests. That is a scraper and not a popular customer. The space after the address in the pattern matters, otherwise 203.0.113.71 would count too. Now you can go to the team with numbers instead of a suspicion.
Start over? The whole scenario goes back to step 1. Your hints stay, XP only comes once.
Loading terminal …
Esc, then Tab leaves the terminal
Step 1 of 3
Cards
-
Card 1 of 2
Tap the card or press Space to turn it over.
-
Card 2 of 2
Tap the card or press Space to turn it over.
Cheat sheet
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 20 KB)
- View as image (PNG, 73 KB)