5.3 KiB
5.3 KiB
In [ ]:
raw_logs = [
"2026-09-01T08:00:00Z,203.0.113.55,alice,SUCCESS",
"2026-09-01T08:01:00Z,198.51.100.14,admin,FAILED",
"2026-09-01T08:01:20Z,198.51.100.14,root,FAILED",
"2026-09-01T08:02:00Z,192.0.2.88,bob,SUCCESS",
"2026-09-01T08:02:30Z,198.51.100.14,guest,FAILED",
"2026-09-01T08:03:00Z,192.0.2.99,carol,FAILED",
]
critical_fields = ("timestamp", "source_ip", "username", "status")
blocked_ips = {"198.51.100.14", "203.0.113.200"}
print(f"Rows: {len(raw_logs)}")
print("Critical fields:", critical_fields)
In [ ]:
def parse_log_line(line):
# TODO 1: split the line into four variables
# TODO 2: return a dictionary with the four keys in critical_fields
# Normalize username to lowercase and status to uppercase.
pass
sample_event = parse_log_line(raw_logs[0])
print(sample_event)
In [ ]:
# TODO 3: create the events list by parsing each raw log line
events = []
# TODO 4: create the unique_ips set from events
unique_ips = set()
print("Unique IPs:", sorted(unique_ips))
print("Number of unique IPs:", len(unique_ips))
In [ ]:
failed_by_ip = {}
# TODO 5: count FAILED events for each source_ip.
# Hint: dictionary.get(ip, 0)
print(failed_by_ip)
In [ ]:
alerts = []
# TODO 6: create an alert when count >= 3 OR the IP is in blocked_ips.
# Each alert is a dictionary containing source_ip, failed_attempts, and blocked.
print(alerts)
In [ ]:
assert sample_event["username"] == "alice"
assert len(events) == 6
assert unique_ips == {"203.0.113.55", "198.51.100.14", "192.0.2.88", "192.0.2.99"}
assert failed_by_ip["198.51.100.14"] == 3
assert alerts == [{"source_ip": "198.51.100.14", "failed_attempts": 3, "blocked": True}]
print("✓ Lab 3 completed")