TT Lab
Blog Get started
Learn Learning paths Courses

Linux Fundamentals

Pulling the Answer Out of a Log

Continue in TT Lab

Goal

You practice turning questions into commands on a single log file. /opt/lab/data/app.log is in the format IP 날짜 메서드 경로 상태코드 [ERROR] (IP, date, method, path, status code, [ERROR]) and has 480 lines. In the last two steps you receive an incident where "the counts differ from the dashboard", and you find and fix the pipelines that produced the wrong numbers.

Why it matters

What you need in an incident is not a log viewer but the ability to turn a question into a pipeline. The question "what is the most frequent?" can almost always be answered with one idiom, sort | uniq -c | sort -rn | head. Two points explain why this idiom is fast: uniq -c counts only adjacent duplicates, so the sort in front is essential, and the commands connected by pipes each run at the same time in separate processes, so even a huge file is processed without being loaded into memory.

A pipeline does not raise an error when it is wrong. Miss a rotated log, shift a column, or forget to sort, and a plausible number comes out silently. So in the incident steps you practice the order of first checking whether the number came out "small" or "large", then pulling the miscounted lines yourself and checking them by eye. The grader runs your fixed script once more on a different day's log, so plugging in the right values does not pass.

Steps

  1. Write only the number of lines that contain ERROR to /root/error_count.txt.
  2. Write the single IP that appears most often to /root/top_ip.txt.
  3. Save only the lines whose status code is 500, unchanged, to /root/five_hundreds.txt.
  4. Save the list of unique paths (4th field) that appear to /root/paths.txt, one per line.
  5. Save the count for each status code, in the format 코드 개수 (code, then count), as three lines in ascending code order, to /root/status_counts.txt.
  6. Save the top three paths by request count, in the format 경로 개수 (path, then count), as three lines in descending count order, to /root/top3_paths.txt.
  7. Create /root/report.sh <로그 파일> (log file). It must read that log and print four lines in this order: total=<전체 줄 수> (total number of lines), errors=<200 이 아닌 줄 수> (number of lines that are not 200), error_rate=<오류 백분율, 소수점 첫째 자리, % 없이> (error percentage, one decimal place, no %), and top_path=<가장 많이 요청된 경로> (most requested path).
  8. Incident lb-count-drift: yesterday's request count comes out much lower than the dashboard. Pick and record the cause, and fix the script so it produces the right number on other days and in other directories too.
  9. Incident lb-report-mismatch: three numbers in the operations report differ from the dashboard. Pick and record the cause for each script, and fix the three scripts.

Notes

Count the ERROR lines

Write only the number of lines that contain ERROR to /root/error_count.txt.

Connect a selecting tool and a counting tool with a pipe. grep also has an option that only counts.

The IP with the most requests

Write the single IP that appears most often to /root/top_ip.txt.

"What is most frequent?" always has the same shape: extract only the column you care about, group identical values and count them, sort by the count, and look at the top. Remember that the counting tool (uniq -c) groups only identical lines that are adjacent. At the end, drop the count and keep only the IP.

Extract only the 500 responses

Save only the lines whose status code is 500, unchanged, to /root/five_hundreds.txt.

The status code is the 5th field. If you grep for the string, it may also catch 500 in other fields.

A list of unique paths

Save the list of unique paths (4th field) that appear to /root/paths.txt, one per line.

The path is the 4th field. Extract only that column and reduce identical values to one; the sorting tool already has an option for "keep only one of each duplicate".

Count by status code

Save the count for each status code, in the format 코드 개수 (code, then count), as three lines in ascending code order, to /root/status_counts.txt.

Collect only the status code column and count per code. There are two ways to count: sort and then group and count adjacent ones, or have awk accumulate a count for each code and output them all at once at the end. The grouping-and-counting tool outputs in "count value" order, so you need to swap the column order.

Top 3 most requested paths

Save the top three paths by request count, in the format 경로 개수 (path, then count), as three lines in descending count order, to /root/top3_paths.txt.

It is the same shape as "the most frequent" in step 2, but the column is the path, and you want three lines rather than just the top one. The result must be in "path count" order, so swap the column order that the counting tool outputs at the end.

Error rate and summary report script

Create /root/report.sh <로그 파일> (log file). It reads a log in the format IP 날짜 메서드 경로 상태코드 (IP, date, method, path, status code) and prints the following four lines in this order: total=<전체 줄 수> (total number of lines), errors=<상태코드가 200 이 아닌 줄 수> (number of lines whose status code is not 200), error_rate=<errors 의 백분율, 소수점 첫째 자리, % 없이> (errors as a percentage, one decimal place, no %), and top_path=<가장 많이 요청된 경로> (most requested path). It must be executable, and the grader runs it once on /opt/lab/data/app.log and once on another log the grader generates on the spot.

The first three values can be counted in a single awk pass: count the total and the errors on each line, then compute the ratio in the END block, so you do not read the file twice. printf's format specifier handles rounding to one decimal place. You may compute the top path separately with the "most frequent" shape from steps 2 and 6 and append it. Take the log file name as the script's first argument.

Incident: yesterday's request count came out far lower than the dashboard

Running incident lb-count-drift raises the incident. /srv/lbreport/daily-count.sh 2026-11-04 outputs 184, but the dashboard's request count for 2026-11-04 is 1905. Find the cause and fix the script so the same command outputs 1905. You must not hard-code the number: the grader runs it once more, passing a log directory with a different rotation layout and date as the second argument (daily-count.sh <날짜> <로그 디렉터리>, date and log directory). You must not delete or modify the logs in /var/log/lbweb/. In /root/incident/lb-count-drift/report.txt, write cause: <코드> and evidence: <관찰한 출력 한 줄> (one line of output you observed). The code is one of the following: rotated-missed (missed rotated logs), timezone-shift (time zone mismatch), substring-match (matched columns other than the date), duplicate-lines (counted the same line twice).

Direction: the number came out "small". If it undercounted, find where it did not read; if it overcounted, find what it read twice. Tools: ls -l, head, tail, zcat, awk, wc. Concretely: the file list of the log directory and the timestamps of each file's first and last lines show which files yesterday is split across. .gz is a compressed rotated file, so plain reading tools do not show its content. Check how many files the script reads right now, and whether it holds up if the rotation layout changes.

Incident: three numbers in the operations report differ from the dashboard

Running incident lb-report-mismatch raises the incident. The values that three scripts in /srv/lbreport/ produce from /var/log/lbweb/today.log differ from the dashboard: errors-500.sh gives 59 (dashboard 77), not-found.sh gives 219 (135), and top-path.sh gives /login (/api/list). Find the cause for each script and fix the three scripts. You must not hard-code the values: the grader runs them once more with a different day's log as the first argument (<스크립트> <로그 파일>, script and log file). You must not modify the log. In /root/incident/lb-report-mismatch/report.txt, write three lines, errors-500: <코드>, not-found: <코드>, and top-path: <코드>, plus evidence: <관찰한 로그 한 줄> (the log line that made a script wrong). Each script's code is one of the following: field-split (split columns wrongly and looked at the wrong column), substring-match (also matched characters in another column), unsorted-uniq (grouped and counted without sorting), rotated-missed (missed rotated logs).

Direction: first see whether the script undercounted or overcounted. Then pull a few lines the script judged "yes" and a few it judged "no" and compare them by eye. Do not assume all three have the same cause. Tools: cat, grep, cut, awk, sort, uniq, diff. Concretely: using the ticket's remark that "one LB node was added this week" as a clue, compare how the lines written by the new node differ in shape from the original lines. For each script, find one line it counted that it should not have, or one line it missed, and see which column of that line changed the judgment.