# #TWSBashBlazeChallenge - Day 5

## Challenge: 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:

1. **Input:** The script should take the path to the log file as a command-line argument.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691162892228/24620188-2f65-4d9b-82d9-3cca884313f9.png align="center")
    
2. **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.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691163253118/6031346b-33fb-4ae4-9bc1-9828fd69a5f3.png align="center")
    
3. **Critical Events:** Search for lines containing the keyword "CRITICAL" and print those lines along with the line number.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691163222726/47d74995-0da7-4667-a377-c971278442fd.png align="center")
    
4. **Top Error Messages:** Identify the top 5 most common error messages and display them along with their occurrence count.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691163149442/42d8808c-18f5-4d67-8eab-dff59865ecea.png align="left")
    
5. **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
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691163279288/d50192b5-5c24-4740-a1a0-9549e5b815a3.png align="center")
        

**Optional Enhancement:** Add a feature to automatically archive or move processed log files to a designated directory after analysis.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691163341336/5b37611a-80b4-4af8-805f-e49a175ee604.png align="center")

### Usage of the script:

1. Create a log file using the logfile.sh-
    
    ./logfile.sh &lt;log\_filename&gt; &lt;no\_of\_logs\_in\_file&gt;
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691163481779/d83b51ff-a633-46ab-a46d-bccd125103c9.png align="center")
    
2. Use the log\_anayzer.sh to create the report.
    
    ./log\_analyzer.sh &lt;log\_filename&gt;
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691163612841/4acab8ea-27d5-4191-a909-c5628d9d3401.png align="center")
    
    You will find the report in report.txt and the log files will be renamed according to the date and time of analysis in the logs\_archive folder.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691163707210/5d64f98b-a582-447f-9361-fd039852adf2.png align="center")
    
    The code -
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691163072334/b39f4721-051d-40a5-a63d-a6ac884dc8d2.png align="center")

The code has been uploaded on [Github](https://github.com/shivaniii23/BashBlaze-7-Days-of-Bash-Scripting-Challenge/tree/main/Challenges/Day_5).

Thanks for checking out my blog!
