Day 10 Task: Log Analyzer and Report Generator πŸ“ŠπŸ› οΈ

Β·

3 min read

Scenario

You are a system administrator responsible for managing a network of servers. Every day, a log file is generated on each server containing important system events and error messages. As part of your daily tasks, you need to analyze these log files, identify specific events, and generate a summary report.

Task

Write a Bash script that automates the process of analyzing log files and generating a daily summary report. The script should perform the following steps:

  1. Input: The script should take the path to the log file as a command-line argument.

  2. Error Count: Analyze the log file and count the number of error messages. An error message can be identified by a specific keyword (e.g., "ERROR" or "Failed"). Print the total error count.

  3. Critical Events: Search for lines containing the keyword "CRITICAL" and print those lines along with the line number.

  4. Top Error Messages: Identify the top 5 most common error messages and display them along with their occurrence count.

  5. Summary Report: Generate a summary report in a separate text file. The report should include:

    • Date of analysis

    • Log file name

    • Total lines processed

    • Total error count

    • Top 5 error messages with their occurrence count

    • List of critical events with line numbers

Answer:

Let’s go through the process we followed to set up our Log Analyzer and Report Generator:

  1. Create a Logs Folder πŸ“‚

  2. Create/Import a Log File πŸ“

  3. Write the Analysis Script πŸ–₯️

    #!/bin/bash

    # Check if a log file path is provided
    if [ $# -ne 1 ]; then
        echo "Usage: $0 /path/to/logfile"
        exit 1
    fi

    LOGFILE="$1"
    SUMMARY_FILE="log_summary_$(date +'%Y-%m-%d').txt"

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

    # Get the current date
    DATE_OF_ANALYSIS=$(date +'%Y-%m-%d')

    # Count the total number of lines in the log file
    TOTAL_LINES=$(wc -l < "$LOGFILE")

    # Count the total number of error messages
    ERROR_COUNT=$(grep -E "ERROR|Failed" "$LOGFILE" | wc -l)

    # Extract critical events with line numbers
    CRITICAL_EVENTS=$(grep -n "CRITICAL" "$LOGFILE")

    # Find the top 5 most common error messages
    TOP_ERRORS=$(grep -E "ERROR|Failed" "$LOGFILE" | sort | uniq -c | sort -nr | head -n 5)

    # Generate the summary report
    {
        echo "Log Analysis Summary - $DATE_OF_ANALYSIS"
        echo "Log File: $LOGFILE"
        echo "----------------------------------------"
        echo "Total Lines Processed: $TOTAL_LINES"
        echo "Total Error Count: $ERROR_COUNT"
        echo "----------------------------------------"
        echo "Top 5 Error Messages:"
        echo "$TOP_ERRORS"
        echo "----------------------------------------"
        echo "Critical Events:"
        if [ -z "$CRITICAL_EVENTS" ]; then
            echo "No critical events found."
        else
            echo "$CRITICAL_EVENTS"
        fi
    } > "$SUMMARY_FILE"

    # Display a message that the summary report has been created
    echo "Summary report generated: $SUMMARY_FILE"

    #Print the summary report
    cat $SUMMARY_FILE
  1. Print Results by running script πŸ–¨οΈ

Β