>_ IzzyLab

Hands-on task

This task is step 5 of the course “Logs: analysis and housekeeping”

Mail delivery summary by status

medium~7 min

The mail relay writes the log /var/log/mail/delivery.log: time, message id, recipient, sometimes an intermediate hop, and the last word is the status (sent, deferred, bounced). Lines differ in length. Count how many messages are in each status and write lines <status> <count> to /root/delivery_summary.txt — one per status.

What you'll practise:
  • awk
  • $NF
  • sort
  • uniq -c
  • log summary

Topic breakdown

When log lines differ in length, a fixed column number breaks: in some lines the status is the fifth field, in others the seventh. But it is always the last one, and in awk the last field is $NF, where NF is the number of fields in the current line. Then comes the familiar frequency-analysis pattern: extract the value, sort so that equal values end up adjacent, and collapse repeats with a counter. uniq -c prints "count value" with leading spaces, so to get the required "value count" it is rearranged with one more pass.

What's checked

  1. status sent counted
  2. counts per status are correct

What it looks like