Day 10: Log Analyzer and Report Generator โ 90 Days of DevOps
๐ฏ Challenge Title: 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
Optional Enhancement: Add a feature to automatically archive or move processed log files to a designated directory after analysis.
๐ Solution:
Step 1: Create a file named log_analyzer.sh
Step 2: Open the Script File
Step 3: Write the Script
In this blog post, I'll share a script that I developed with the help of AI's code generation tool. The AI assistance helped me generate the initial code structure, which I then modified and refined to suit my needs. Which saved me time and effort. The final code is a result of my own modifications and refinements.
Step 4: Make the script executable
Step 5: Run the script with a sample log file as an argument
Step 6: View the summary report
Test: Another Test sample_log.log written by Meta AI
Open the editor and add the content written by AI for testing and save it
by ":wq"
Conclusion
Completing the "Log Analyzer and Report Generator" task has equipped us with the skills to automate log file analysis, streamline daily system administration tasks, and generate comprehensive reports efficiently. This solution showcases the power of Bash scripting and command-line tools in managing system logs, identifying critical events, and generating actionable insights.
By following the steps outlined in this blog, you can:
Create a robust Bash script to automate log file analysis.
Identify and count error messages and critical events.
Generate detailed summary reports for system events.
Archive processed log files for future reference.
Automation not only saves time but also reduces the risk of human error, ensuring more reliable and consistent log analysis. Feel free to adapt and expand this script to suit your specific needs and environment.
Thank you for reading! I hope this guide has been helpful. If you have any questions or feedback, please leave a comment below. Happy scripting! ๐