>_ IzzyLab

Hands-on task

This task is step 9 of the course “Monitoring and metrics”

Count errors in a log

easy~3 min

In /var/log/app/app.log there are lines with levels: the level is the first word of the line. Count the number of lines at level ERROR and write only the number into /root/error_count.txt. Careful: the word ERROR also appears in the message text of other levels.

What you'll practise:
  • grep
  • line counting
  • logs
  • redirection
  • patterns

Topic breakdown

Counting events by a pattern is the foundation of log monitoring: before building an alert, you need to reliably count an event within a window (here the window is the whole file). For this grep has a counting mode that prints not the lines themselves but their number. The gotcha — a crude pattern matches a substring anywhere, so on other logs a level like ERROR could get counted inside SUPERERROR; the habit of checking for a word boundary saves you from over-counting. Redirect the result into the file so that only the number itself remains, without extra spaces and newlines.

What's checked

  1. ERROR lines counted correctly

What it looks like