🌟 Day 10: Log Analyzer and Report Generator

🌟 Day 10: Log Analyzer and Report Generator

Β·

3 min read

Hello DevOps Enthusiasts! πŸ‘‹

Welcome to Day 10 of our 90 Days of DevOps challenge! Today, we'll learn how to analyze logs and create reports using a simple Bash script. Let's dive in! πŸš€

πŸ“œ Scenario :

Imagine you're a system administrator responsible for several servers. Each server keeps a daily record called a "log file," where it writes down what happens throughout the day – like important events or errors. Your job is to read these logs, find specific events, and create a summary report. Let's make it easy! πŸ˜„

πŸ› οΈ Steps to Complete the Task :

  1. Collect Log Files πŸ“‚

    • Gather all the log files from your servers.
  2. Analyze Log Files πŸ”

    • Look through each log file to find things like errors or other important events.
  3. Generate a Report πŸ“

    • Make a summary report that lists these events clearly.

πŸ“ Example

1. Collect Log Files πŸ“‚

  • Server 1: server1_log.txt

  • Server 2: server2_log.txt

  • Server 3: server3_log.txt

2. Analyze Log Files πŸ”

Search each log file for lines that have words like "ERROR."

3. Generate a Report πŸ“

Summary Report for Today
========================

Server 1:
- ERROR: Failed to connect to database at 10:00 AM

Server 2:
- ERROR: Disk space low at 2:00 PM

Server 3:
- ERROR: Network timeout at 1:30 PM

πŸ›‘οΈ Explanation

1. List of Log Files

log_files=("server1_log.txt" "server2_log.txt" "server3_log.txt")

We create a list of our log files. Each item in the list is the name of a log file from different servers.

2. Create an Empty Report File

report_file="summary_report.txt"
echo "Summary Report for Today" > $report_file
echo "========================" >> $report_file
echo "" >> $report_file

We set up a new file called summary_report.txt where we will store our final report. We start with a title and some formatting.

3. Function to Analyze Each Log File

analyze_log_file() {
    local file_name=$1
    echo "$file_name:" >> $report_file  # Write the server's log file name to the report
    while IFS= read -r line; do  # Read each line in the log file
        if [[ "$line" == *"ERROR"* ]]; then  # If the line contains "ERROR"
            echo "- $line" >> $report_file  # Write it to the report
        fi
    done < "$file_name"  # Read from the log file
    echo "" >> $report_file  # Add a blank line after each file's entries
}

This function (analyze_log_file) reads each line of a log file. If a line contains the word "ERROR," it adds that line to our report.

4. Loop Through Log Files and Analyze

for log_file in "${log_files[@]}"; do
    analyze_log_file $log_file
done

We go through each log file in our list (log_files) and use our function (analyze_log_file) to process it.

5. Print the Summary Report

cat $report_file

Finally, we display our complete summary report using cat.

πŸ“Combine Everything :

Putting it all together, we get the complete script:

#!/bin/bash

# List of log files
log_files=("server1_log.txt" "server2_log.txt" "server3_log.txt")

# Create an empty report file
report_file="summary_report.txt"
echo "Summary Report for Today" > $report_file
echo "========================" >> $report_file
echo "" >> $report_file

# Function to analyze each log file
analyze_log_file() {
    local file_name=$1
    echo "$file_name:" >> $report_file
    # Read each line of the log file
    while IFS= read -r line; do
        # Check if the line contains "ERROR"
        if [[ "$line" == *"ERROR"* ]]; then
            echo "- $line" >> $report_file
        fi
    done < "$file_name"  # Read from the log file
    echo "" >> $report_file  # Add a blank line after each file's entries
}

# Loop through each log file and analyze it
for log_file in "${log_files[@]}"; do
    analyze_log_file $log_file
done

# Print the summary report
cat $report_file

πŸŽ‰Congratulations! πŸŽ‰

We have successfully learned how to use Bash scripting to analyze log files and create a summary report.πŸ’ͺ

Stay tuned for more exciting tasks in our 90 Days of DevOps challenge.

Happy learning! πŸŽ‰

Β