Day 10 Task: Log Analyzer and Report Generator ππ οΈ
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:
Input: The script should take the path to the log file as a command-line argument.
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.
Critical Events: Search for lines containing the keyword "CRITICAL" and print those lines along with the line number.
Top Error Messages: Identify the top 5 most common error messages and display them along with their occurrence count.
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:
Create a Logs Folder π
Create/Import a Log File π
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
Print Results by running script π¨οΈ