KKAIROS
Practical guideIllustrative3 min read

Answer questions about a log file with a pipeline

A pipeline turns raw log lines into counts by extracting fields, sorting and counting. The access log is synthetic; its numbers are not traffic data.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-48-A: a log pipeline over a synthetic file returns the counts that the synthetic data implies

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'GET /a 200\nGET /b 200\nPOST /a 500\nGET /a 200\n' > access.log

Command:

awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -2; echo codes=$(awk '{print $3}' access.log | sort -u | tr '\n' ' ')

Recorded output:

3 GET
      1 POST
codes=200 500

TE-48-B: awk with a field test and grep -c count the lines with status 500 and with status 200

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'GET /a 200\nGET /b 200\nPOST /a 500\nGET /a 200\n' > access.log

Command:

awk '$3==500' access.log | wc -l; grep -c ' 200$' access.log

Recorded output:

1
3

Exit status: 0. This status and the output match what was written before the run.

TE-48-C: expected non-zero exit: grep -c on a log file that does not exist exits with status 2

Setup for this run, inside a throwaway directory (mktemp -d):

printf 'GET /a 200\nGET /b 200\nPOST /a 500\nGET /a 200\n' > access.log

Command:

grep -c 200 missing.log

Recorded output: none, standard output was empty.

Standard error from the same run:

grep: missing.log: No such file or directory

Exit status: 2. This status and the output match what was written before the run.

TE-48-B: by the setup, one line has status 500 and three lines end in 200. awk printed a count of 1 for the 500 line, and grep -c printed 3. TE-48-C: grep -c 200 missing.log exited with status 2, nothing was printed to standard output, and standard error reads grep: missing.log: No such file or directory.

Limits

The access log is synthetic; its numbers are not traffic data.

Ad

Source

Source: https://www.gnu.org/software/gawk/manual/

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-48-A | a log pipeline over a synthetic file returns the counts that the synthetic data implies | 0 | PASS | f5bf7ca04ee9 | | TE-48-B | awk with a field test and grep -c count the lines with status 500 and with status 200 | 0 | PASS | af6dd763e790 | | TE-48-C | expected non-zero exit: grep -c on a log file that does not exist exits with status 2 | 2 | PASS | 1bc665b56f1c |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides