Skip to content

harshad8782/Bash_Scripting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

🔍 Bash Log Analysis Script

A shell script that automatically scans log files, identifies critical issues (errors, fatals, critiques), generates a structured analysis report, and alerts when thresholds are exceeded.


📁 Project Structure

bash-log-analysis/
├── logs/
│   ├── application.log         # App-level events (login, payments, jobs)
│   └── system.log              # OS-level events (kernel, disk, memory)
│   └── bash_script.sh          # Main log analysis script
│   └── log_analysis_report.txt # Auto-generated output report

⚙️ What the Script Does

  1. Scans all .log files modified in the last 24 hours inside the logs/ directory
  2. Searches for three severity patterns: error, fatal, critical (case-insensitive)
  3. Outputs all matching log lines into a structured log_analysis_report.txt
  4. Counts occurrences of each pattern per log file
  5. Alerts in the terminal if any pattern count exceeds 10

📋 Sample Log Events Covered

application.log

Severity Event
ERROR Payment gateway timeout
ERROR Insufficient disk space
FATAL Out of memory in recommendation engine
CRITICAL Database connection lost during backup
CRITICAL Multiple failed login attempts on admin account

system.log

Severity Event
ERROR SSH brute-force failed password attempts
FATAL EXT4 filesystem journal abort
CRITICAL SMART disk failure on /dev/sda and /dev/sdc
CRITICAL Hardware MCE (Machine Check Exception) on CPU

🚀 How to Run

Prerequisites: Bash (Linux/macOS/Git Bash on Windows)

# 1. Clone the repository
git clone https://github.com/your-username/bash-log-analysis.git
cd bash-log-analysis

# 2. Make the script executable
chmod +x bash_script.sh

# 3. Update the log_directory path in bash_script.sh to match your system
# Example (Linux/macOS):  log_directory="./logs/"
# Example (Git Bash):     log_directory="D:/Bash_Scripting/logs/"

# 4. Run the script
./bash_script.sh

The report will be saved to log_analysis_report.txt inside the logs directory.


📊 Sample Report Output

========================================
Analyzing log file: logs/application.log
========================================

Searching error logs in logs/application.log:
[2025-06-01 08:17:48] [ERROR] Payment gateway timeout after 30s ...
[2025-06-01 08:25:48] [ERROR] Insufficient disk space for file upload
...
Number of error logs: 9

Searching fatal logs in logs/application.log:
[2025-06-01 08:47:22] [FATAL] Out of memory error in recommendation engine
Number of fatal logs: 2
...

🧠 Key Concepts Practiced

  • Bash variables and arrays
  • find command with -mtime flag
  • while loops with IFS= read -r for safe file iteration
  • for loops over arrays
  • grep with -i (case-insensitive) and -c (count) flags
  • Conditional statements (if [ ])
  • Appending output to files with >>

🙏 Credits & Acknowledgements

This project was built while following the Bash Scripting tutorials by TechWorld with Nana on YouTube.

📺 TechWorld with Nana – YouTube Channel

Nana's tutorials are an excellent resource for DevOps and scripting fundamentals — highly recommended for anyone getting started with Bash, Docker, Kubernetes, and more.


📌 Notes

  • The script currently uses a hardcoded Windows-style path (D:/Bash_Scripting/logs/) for Git Bash compatibility. Update the log_directory variable to match your local path before running.
  • The alert threshold is set to 10 — modify the condition in the script to adjust sensitivity.

👨‍💻 Author

Harshad Raurale
DevOps / Cloud Enthusiast

GitHub LinkedIn


📄 License

This project is for learning purposes. Feel free to fork, modify, and build on it!

⭐ If you found this project helpful, please consider giving it a star!

About

Bash script that scans .log files to detect errors, fatals, and critical events using grep, loops, and conditionals. Generates a structured analysis report and triggers terminal alerts when thresholds are exceeded. Built as a hands-on practice project following TechWorld with Nana's Bash scripting tutorials.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages