Day 10 Log Analyzer and Report Generator

Hi there! I'm Fauzeya 👩💻, a passionate DevOps Engineer with a background in Computer Science Engineering🎓. I’m committed to enhancing security🔒, efficiency⚙️, and effectiveness in software development and deployment processes. With extensive knowledge in cloud computing☁️, containerization📦, and automation🤖, I aim to stay updated with the latest tools and methodologies in the DevOps field. Currently, I’m on a journey to deepen my understanding of DevOps I enjoy sharing my learning experiences and insights through my blog, 📝where I cover topics related to DevOps practices, tutorials, and challenges. I believe in continuous growth and learning and am excited to connect with fellow tech enthusiasts and professionals🤝. Let’s embark on this journey together!🚀
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
Usage and Input Check:
- The script checks if the correct number of arguments is provided and if the log file exists.
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.
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_eventsarray.
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.
Optional Enhancement:
- The script moves the processed log file to a directory called
processed_logsfor archiving purposes.
- The script moves the processed log file to a directory called
How to Run the Script
Save the script to a file named
log_analyzer.sh.Make the script executable:
chmod +x log_analyzer.shRun the script with the path to your log file:
./log_analyzer.sh sample_log.logOutput
The script will produce a summary report detailing the analysis results and archive the log file. You can check the
summary_report_<date>.txtfor 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📝




