#TWSBashBlazeChallenge - Day 5

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.
###################################################################
# Author: Sasiram Beeke
# Date: 04/08/2023
# Description: Log Analyzer and Report Generator
# Tip: to execute shell give permision +x scriptname
###################################################################
#!/bin/bash
# Check if a log file path is provided as a command-line argument
if [ $# -ne 1 ]; then
echo "Usage: $0 $1"
exit 1
fi
# Check if the log file exists
if [ ! -f "$1" ]; then
echo "Error: Log file '$1' not found."
exit 1
fi
#Prints the path of the log file that was provided as a command-line argument.
echo "Path for log file is $1"
#Counts the occurrences of lines containing either "ERROR" or "Failed" in the log file and stores the count in the error_count variable & print errro count
error_count=$(grep -c -i "ERROR\|Failed" "$1")
echo "The total error count is: $error_count"
#Searches for lines containing the keyword "CRITICAL" in the log file and stores them in the critical_event variable & print critical event with line numbers
critical_event=$(grep -n -i "CRITICAL" "$1")
echo "$critical_event"
#Prints the top 5 error messages along with their occurrence count.
top_errors=$(grep -i "ERROR\|Failed" $1 | awk -F ': ' '{print $2}' | sort | uniq -c | sort -rn | head -n 5 | awk '{print "count is", $1, "and then error message =", substr($0, index($0,$2))}')
echo "$top_errors"
#Generates a summary report file named summary_report_YYYYMMDD.txt
report_file="summary_report_$(date +%Y%m%d).txt"
echo "Date of analysis: $(date)" > "$report_file"
echo "Log file name: $1" >> "$report_file"
echo "Total lines processed: $(wc -l < "$1")" >> "$report_file"
echo "Total error count: $error_count" >> "$report_file"
echo -e "Top 5 error messages with their occurrence count:\n$top_errors" >> "$report_file"
echo -e "List of critical events with line numbers:\n$critical_event" >> "$report_file"
#Creates an archive directory if it doesn't exist.
archive_dir="archive"
mkdir -p "$archive_dir"
# Move the processed log file to the archive directory
mv "$1" "$archive_dir/"
echo "Analysis completed. Summary report saved in '$report_file'."
Test case 1 - file present at script location. Here I have created a log file sampl_log.log

Here I have given the wrong path or filename which does not exist to validate our script.

so we check our script with a log file which is present in the home directory .i.e home/ubuntu/sampl_log.log.so it gives an error count then events with line numbers and after that top 5 most common errors with their occurrence count and finally create a report. also, it moves our home/ubuntu/sampl_log.log file inside the archive directory.

This is a summary report which is created in .txt file.

Test case 2 - file present at another directory location. Here I have created a log file inside the test directory.

we execute the script with the directory path. summary report was created and the log file moved from the test directory to the archive directory.

summary report for the second test case.




