Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
YouTube Security VideosIntel Devs: Smart AI Edge Solutions - 0 Introduction | Intel Software(22.09.2026 um 23:48 Uhr)
Windows Tipps & SecurityGoogles gibt Chrome 154 frei und schließt über 100 Lücken(23.09.2026 um 09:11 Uhr)
Windows Tipps & SecurityKlangerlebnis & Sicherheit im Vonovia Ruhrstadion(23.09.2026 um 08:45 Uhr)
Unix & Linux ServerLocal AI Jargon Quiz: Do You Know All These Buzzwords?(23.09.2026 um 08:38 Uhr)
Sichere ProgrammierungNeu von AWS: Weniger Kontextpflege für selbst gebaute KI-Agenten(23.09.2026 um 09:52 Uhr)
Sichere ProgrammierungLINQ GroupBy: The Operator Everyone Uses Wrong(23.09.2026 um 09:41 Uhr)
Sichere ProgrammierungIT Heard About the Acquisition Nine Days Before It Closed(23.09.2026 um 09:45 Uhr)
YouTube Security VideosIntel Devs: Smart AI Edge Solutions - 0 Introduction | Intel Software(22.09.2026 um 23:48 Uhr)
Windows Tipps & SecurityGoogles gibt Chrome 154 frei und schließt über 100 Lücken(23.09.2026 um 09:11 Uhr)
Windows Tipps & SecurityKlangerlebnis & Sicherheit im Vonovia Ruhrstadion(23.09.2026 um 08:45 Uhr)
Unix & Linux ServerLocal AI Jargon Quiz: Do You Know All These Buzzwords?(23.09.2026 um 08:38 Uhr)
Sichere ProgrammierungNeu von AWS: Weniger Kontextpflege für selbst gebaute KI-Agenten(23.09.2026 um 09:52 Uhr)
Sichere ProgrammierungLINQ GroupBy: The Operator Everyone Uses Wrong(23.09.2026 um 09:41 Uhr)
Sichere ProgrammierungIT Heard About the Acquisition Nine Days Before It Closed(23.09.2026 um 09:45 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Bash Script Series: Automating Log Analysis with Bash Script or Shell Script

Automating Log Analysis with Bash: A Simple Way to Manage System Logs If you've ever managed servers, you know how messy log files can get. Every day, these files record important events and errors, which are crucial for keeping your…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




Automating Log Analysis with Bash: A Simple Way to Manage System Logs



If you've ever managed servers, you know how messy log files can get. Every day, these files record important events and errors, which are crucial for keeping your systems running smoothly. But let’s face it: going through logs manually is boring, time-consuming, and easy to mess up.



To make life easier, I created a Bash script that automates log analysis. Think of it as your assistant that sifts through the logs, picks out the important stuff, and hands you a clear, easy-to-read summary.









Why Did I Build This Script?



Imagine you have a pile of logs that tell you things like:




  • Errors when a disk is full.

  • Services that failed to start.

  • Critical problems like network failures.



Wouldn’t it be great if you could quickly get a summary instead of reading line by line? That’s exactly what my script does!









What Does the Script Do?



Here’s how it works:





  1. Counts Errors: It finds all the error messages in the log and gives you the total count.


  2. Finds Critical Issues: It hunts down "CRITICAL" problems and tells you exactly where to find them in the log.


  3. Lists Top Errors: It identifies the most common errors so you can prioritize fixing them.


  4. Generates a Report: It creates a neat summary report with all this information for you to review.


  5. Moves Processed Logs: After analyzing a log file, it moves it to a folder for safekeeping.









#!/bin/bash


#####################################
#
# Script Author: Jamiu
#
# Date: 02-01-2025
#
# Version: v1
#
# Automate Log Analysis
#
#####################################


# Check if the log file path is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <log_file_path>"
exit 1
fi

LOG_FILE=$1

# Check if the log file exists
if [ ! -f "$LOG_FILE" ]; then
echo "Error: Log file '$LOG_FILE' does not exist."
exit 1
fi

# Variables
REPORT_FILE="log_summary_report_$(date +%F).txt"
ERROR_KEYWORDS=("ERROR" "Failed")
CRITICAL_KEYWORD="CRITICAL"

echo "Analyzing log file: $LOG_FILE"

# Total lines processed
TOTAL_LINES=$(wc -l < "$LOG_FILE")

# Count errors
ERROR_COUNT=0
for keyword in "${ERROR_KEYWORDS[@]}"; do
ERROR_COUNT=$((ERROR_COUNT + $(grep -c "$keyword" "$LOG_FILE")))
done

# Identify critical events
CRITICAL_EVENTS=$(grep -n "$CRITICAL_KEYWORD" "$LOG_FILE")

# Extract top 5 error messages
ERROR_MESSAGES=$(grep -E "${ERROR_KEYWORDS[*]}" "$LOG_FILE" | awk '{for(i=2;i<=NF;i++) printf $i " "; print ""}' | sort | uniq -c | sort -nr | head -n 5)

# Generate summary report
echo "Generating summary report: $REPORT_FILE"
echo "Log Analysis Report - $(date)" > "$REPORT_FILE"
echo "Log File: $LOG_FILE" >> "$REPORT_FILE"
echo "Total Lines Processed: $TOTAL_LINES" >> "$REPORT_FILE"
echo "Total Error Count: $ERROR_COUNT" >> "$REPORT_FILE"
echo -e "\nTop 5 Error Messages:" >> "$REPORT_FILE"
echo "$ERROR_MESSAGES" >> "$REPORT_FILE"
echo -e "\nCritical Events (Line Numbers):" >> "$REPORT_FILE"
if [ -z "$CRITICAL_EVENTS" ]; then
echo "None" >> "$REPORT_FILE"
else
echo "$CRITICAL_EVENTS" >> "$REPORT_FILE"
fi

# Optional: Archive or move the processed log file
ARCHIVE_DIR="processed_logs"
mkdir -p "$ARCHIVE_DIR"
mv "$LOG_FILE" "$ARCHIVE_DIR/"
echo "Log file moved to $ARCHIVE_DIR/"

echo "Log analysis complete. Report saved to $REPORT_FILE."
exit 0










How to Use It




  1. Place your log file (e.g., sample_log.log) in the same folder as the script.

  2. Run the script with:




   ./log_analyzer.sh sample_log.log







  1. Check out the generated report:




   cat log_summary_report_<date>.txt







  1. The original log file will be safely moved to a folder called processed_logs.









Sample Output



Here’s what the report looks like:




Log Analysis Report - Thu Jan 2 2025
Log File: sample_log.log
Total Lines Processed: 4
Total Error Count: 3

Top 5 Error Messages:
1. ERROR: Disk full - 1 time
2. ERROR: Failed to start service - 1 time

Critical Events:
3: CRITICAL: Network down






It’s simple, clear, and gives you everything you need at a glance.



Image description









Why This is Useful





  • Saves Time: No more wasting hours skimming through logs.


  • Organized: All logs are stored neatly in a folder after processing.


  • Quick Insights: You immediately know what went wrong and where to look.









What’s Next?



I’m planning to improve this script by:




  • Sending email alerts for critical issues.

  • Allowing it to analyze multiple log files at once.

  • Integrating it with real-time monitoring tools.






I plan to enhance the script with features like email notifications for critical issues and support for analyzing multiple log files simultaneously.



This has been a fun project to showcase the power of Bash scripting for automation and server management. If you’re managing logs, I’d love to hear your feedback or ideas for improvement.






Final Thoughts



This script is a game-changer for anyone who manages servers or deals with logs regularly. It’s simple, effective, and can save you tons of time. If you’d like to try it out or have ideas for improvement, let me know in the comments!



💡 Stay tuned for more updates and similar series coming soon!



Reference

Day 1: https://dev.to/jamiu_cloud/join-me-on-my-7-day-bash-scripting-challenge-4j6j



Day 2: https://dev.to/jamiu_cloud/automating-backups-with-bash-scripting-day-2-3ahm



Day 3: https://dev.to/jamiu_cloud/simplifying-user-account-management-with-bash-scripting-day3-371m



Day 4:

https://dev.to/jamiu_cloud/automating-process-monitoring-restarting-with-bash-scripting-day4-5e0i

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Bash Script Series: Automating Log Analysis with Bash Script or Shell Script

Thematisch verwandte Begriffe: Bash, Script, Series, Automating · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-96258 | A vulnerability has been found in onSite internet GmbH Auktion NG Auktio…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick