Automation Lab 3 - Python Log Parser & Alert Script
This lab shows a Python script that parses log files, detects patterns tied to errors or suspicious activity, and generates a summary report for review.
Overview
Manual log review can miss critical signals when data volume grows. This script automates pattern matching, tracks event counts by category, and captures sample lines for quick triage context.
Real-world relevance: Automated log parsing supports SOC workflows by reducing repetitive manual review and improving visibility into recurring error and security events.
Objective
- Read and process a target log file line by line
- Search for configurable keyword patterns such as ERROR, FAILED, WARNING, and authentication failure events
- Count occurrences of each pattern and identify the lines where they appear
- Output a timestamped summary report to the terminal and a text file
- Keep the script flexible so patterns and the target log file can be easily updated
Script Walkthrough
- Defined a named dictionary of event patterns using regex and keyword matches.
- Opened the target log file and processed each line with buffered reading.
- Checked each line against all configured patterns and recorded matches with line numbers.
- Calculated per pattern totals and assembled structured report output.
- Wrote a timestamped report file and printed a concise terminal summary.
Script Preview
#!/usr/bin/env python3
"""
Automation Lab 3 - Python Log Parser & Alert Script
Scans a log file for defined event patterns and generates a summary report.
"""
import re
from datetime import datetime
from pathlib import Path
LOG_FILE = "/var/log/syslog" # Change to your target log path
OUTPUT_DIR = Path("./reports")
OUTPUT_DIR.mkdir(exist_ok=True)
PATTERNS = {
"ERROR": r"\bERROR\b",
"WARNING": r"\bWARNING\b",
"CRITICAL": r"\bCRITICAL\b",
"Authentication Failure": r"authentication failure|Failed password",
"Segfault": r"segfault|kernel BUG",
"Disk Full": r"No space left on device",
}
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
output_file = OUTPUT_DIR / f"log_report_{timestamp}.txt"
matches = {name: [] for name in PATTERNS}
print(f"Parsing: {LOG_FILE}")
with open(LOG_FILE, "r", errors="replace") as fh:
for line_num, line in enumerate(fh, start=1):
for name, pattern in PATTERNS.items():
if re.search(pattern, line, re.IGNORECASE):
matches[name].append((line_num, line.rstrip()))
with open(output_file, "w") as report:
report.write(f"Log Parser Report - {timestamp}\n")
report.write(f"Source: {LOG_FILE}\n")
report.write("=" * 60 + "\n\n")
for name, hits in matches.items():
report.write(f"[{name}] - {len(hits)} occurrence(s)\n")
for line_num, content in hits[:10]: # cap preview at 10 lines
report.write(f" Line {line_num}: {content}\n")
if len(hits) > 10:
report.write(f" ... and {len(hits) - 10} more.\n")
report.write("\n")
print("\nSummary:")
for name, hits in matches.items():
print(f" {name}: {len(hits)} match(es)")
print(f"\nFull report saved to: {output_file}")
Key Skills Demonstrated
- Python scripting with file handling and regular expressions
- Log analysis and event pattern detection
- Structured report output with timestamped artifacts
- Maintainable design using configurable pattern dictionaries
Tools & Technologies
- Python 3.8+
- Standard library modules including
re,datetime, andpathlib - Linux and application log sources
- Plain text output for ticket and analyst handoff use
Results & Outcomes
- Automated repetitive log review steps that are often done manually.
- Generated categorized reporting that can support incident notes and ticket updates.
- Improved flexibility by allowing new event types to be added quickly.
- Prepared the workflow for scheduled execution or on demand triage support.