Day 10 Log Analyzer and Report Generator

Day 10 Log Analyzer and Report Generator

·

3 min read

Log Analyzer and Report Generator Script

#!/bin/bash

# Log Analyzer and Report Generator
# Usage: ./log_analyzer.sh <path_to_log_file>

# Function to display usage
function display_usage {
    echo "Usage: $0 <path_to_log_file>"
}

# Check if the log file path is provided
if [ $# -ne 1 ]; then
    display_usage
    exit 1
fi

log_file="$1"

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

# Initialize variables
error_count=0
declare -A error_messages
critical_events=()
total_lines=0

# Analyze the log file
while IFS= read -r line; do
    total_lines=$((total_lines + 1))

    # Count error messages
    if [[ "$line" == *"ERROR"* || "$line" == *"Failed"* ]]; then
        error_count=$((error_count + 1))
        message=$(echo "$line" | grep -oP 'ERROR: \K.*') # Extract message after "ERROR: "
        ((error_messages["$message"]++))
    fi

    # Capture critical events
    if [[ "$line" == *"CRITICAL"* ]]; then
        critical_events+=("$line")
    fi
done < "$log_file"

# Create the summary report
report_file="summary_report_$(date +'%Y-%m-%d').txt"
{
    echo "Date of Analysis: $(date +'%Y-%m-%d')"
    echo "Log File Name: $log_file"
    echo "Total Lines Processed: $total_lines"
    echo "Total Error Count: $error_count"

    echo -e "\nTop 5 Error Messages:"
    for message in "${!error_messages[@]}"; do
        echo "$message: ${error_messages[$message]}"
    done | sort -k2,2nr | head -n 5

    echo -e "\nList of Critical Events:"
    for index in "${!critical_events[@]}"; do
        echo "$((index + 1)): ${critical_events[$index]}"
    done
} > "$report_file"

echo "Summary report generated: $report_file"

# Optional Enhancement: Archive the processed log file
archive_dir="processed_logs"
mkdir -p "$archive_dir"
mv "$log_file" "$archive_dir"

echo "Processed log file moved to: $archive_dir"

Explanation of the Script

  1. Usage and Input Check:

    • The script checks if the correct number of arguments is provided and if the log file exists.
  2. Initialize Variables:

    • error_count: Counts the total number of error messages.

    • error_messages: An associative array to keep track of individual error messages and their counts.

    • critical_events: An array to store lines containing critical events.

    • total_lines: A counter for the total number of lines processed.

  3. Log File Analysis:

    • The script reads the log file line by line.

    • It checks for error messages by searching for keywords like "ERROR" and "Failed."

    • It extracts the error message for counting using grep.

    • It captures lines with the keyword "CRITICAL" and adds them to the critical_events array.

  4. Summary Report Generation:

    • A summary report is created, including the date of analysis, log file name, total lines processed, total error count, top 5 error messages, and the list of critical events.

    • The report is saved to a file named summary_report_<date>.txt.

  5. Optional Enhancement:

    • The script moves the processed log file to a directory called processed_logs for archiving purposes.

How to Run the Script

  1. Save the script to a file named log_analyzer.sh.

  2. Make the script executable:chmod +x log_analyzer.sh

  3. Run the script with the path to your log file:./log_analyzer.sh sample_log.log

    Output

    The script will produce a summary report detailing the analysis results and archive the log file. You can check the summary_report_<date>.txt for the output.

    We appreciate❤️ you taking the time to read and connect with us! Your engagement means a lot to us, and we look forward to hearing more from you📝