Day 10 Task: Log Analyzer and Report Generator

Faizan ShaikhFaizan Shaikh
3 min read

Bash script that accomplishes the task of analyzing log files and generating a daily summary report. The script will:

  1. Accept the path to the log file as a command-line argument.

  2. Count the number of error messages containing "ERROR" or "Failed".

  3. Identify and print "CRITICAL" lines with their line numbers.

  4. Display the top 5 most common error messages.

  5. Generate a summary report and save it to a separate file.

  6. Optionally, move the processed log file to an archive directory.

#!/bin/bash

# Check if log file path is provided as an argument
if [ $# -eq 0 ]; then
    echo "Usage: $0 /home/ubuntu/logs/sample_log.logs"
    exit 1
fi

LOG_FILE=$1

# Check if the provided log file exists
if [ ! -f "$LOG_FILE" ]; then
    echo "Error: Log file not found!"
    exit 1
fi

# Variables for report
DATE=$(date)
TOTAL_LINES=$(wc -l < "$LOG_FILE")
ERROR_COUNT=$(grep -Ei "ERROR|Failed" "$LOG_FILE" | wc -l)
REPORT_FILE="log_summary_$(date +%Y%m%d).txt"

# 1. Count total error messages
echo "Analyzing log file: $LOG_FILE"
echo "Total error count: $ERROR_COUNT"

# 2. Identify lines with "CRITICAL" and print line numbers
echo -e "\nCritical Events:"
grep -in "CRITICAL" "$LOG_FILE"

# 3. Identify the top 5 most common error messages
echo -e "\nTop 5 Error Messages:"
grep -Ei "ERROR|Failed" "$LOG_FILE" | awk -F': ' '{print $NF}' | sort | uniq -c | sort -nr | head -n 5

# 4. Generate Summary Report
echo "Generating report in $REPORT_FILE..."
{
    echo "Date of Analysis: $DATE"
    echo "Log file name: $LOG_FILE"
    echo "Total lines processed: $TOTAL_LINES"
    echo "Total error count: $ERROR_COUNT"
    echo ""
    echo "Top 5 Error Messages:"
    grep -Ei "ERROR|Failed" "$LOG_FILE" | awk -F': ' '{print $NF}' | sort | uniq -c | sort -nr | head -n 5
    echo ""
    echo "List of Critical Events with line numbers:"
    grep -in "CRITICAL" "$LOG_FILE"
} > "$REPORT_FILE"

# Optional: Move the processed log file to an archive directory
ARCHIVE_DIR="./log_archive"
mkdir -p "$ARCHIVE_DIR"
mv "$LOG_FILE" "$ARCHIVE_DIR/"

echo "Log analysis complete. Report saved in $REPORT_FILE and log file moved to $ARCHIVE_DIR."

exit 0

Key Breakdown:

  • Log File Path: The script takes the log file as an argument ($1).

  • Error Count: grep -Ei "ERROR|Failed" "$LOG_FILE" | wc -l counts lines that match "ERROR" or "Failed".

  • Critical Events: grep -in "CRITICAL" "$LOG_FILE" finds all critical events and prints their line numbers (-in flag).

  • Top 5 Error Messages: awk extracts the error messages, sort | uniq -c | sort -nr sorts them by frequency, and head -n 5 selects the top 5.

  • Summary Report: The report includes analysis date, log file name, total lines, total errors, top 5 errors, and critical events.

  • Archiving: The processed log file is moved to a log_archive directory after analysis.

How to Use:

  1. Save the script as log_analyzer.sh.

  2. Make it executable with:

     chmod +x log_analyzer.sh
    

  3. Run the script, passing the log file as an argument:

     ./log_analyzer.sh /home/ubuntu/logs/sample_log.log
    

0
Subscribe to my newsletter

Read articles from Faizan Shaikh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Faizan Shaikh
Faizan Shaikh