bugün

python

Kendi yazdığım Metin tabanlı sunucu loglarını analiz edip özet çıkaran Yani basit bir trafik / hata analizi aracı, kod bırakıyorum sizlere .

from collections import Counter
import re

def analyze_access_logs(log_data: list[str]) -> dict:
"""Log satırlarından IP ve HTTP status kodlarını ayıklayıp frekans çıkarır."""
pattern = re.compile(
r"(?P<ip>\d{1,3}(?:\.\d{1,3}){3})"
r" - - "
r"\[.*?\] "
r'".*?" '
r"(?P<status>\d{3})"
)

stats = {"ips": Counter(), "statuses": Counter()}

for line in log_data:
match = pattern.search(line)
if match:
stats["ips"][match.group("ip")] += 1
stats["statuses"][match.group("status")] += 1

return stats

if __name__ == "__main__":
raw_logs = [
'192.168.1.15 - - [11/Aug/2026:10:00:01 +0300] "GET /api/v1/products HTTP/1.1" 200',
'192.168.1.15 - - [11/Aug/2026:10:00:04 +0300] "POST /api/v1/checkout HTTP/1.1" 500',
'10.0.0.42 - - [11/Aug/2026:10:01:12 +0300] "GET /admin/login HTTP/1.1" 403',
'192.168.1.15 - - [11/Aug/2026:10:02:00 +0300] "GET /api/v1/products HTTP/1.1" 200',
'172.16.0.8 - - [11/Aug/2026:10:02:45 +0300] "GET /static/style.css HTTP/1.1" 404',
]

report = analyze_access_logs(raw_logs)

print("=== Trafik Özeti ===")
print("En aktif IP'ler:")
for ip, hit in report["ips"].most_common(2):
print(f" -> {ip}: {hit} istek")

print("\nHTTP Yanıt Dağılımı:")
for code, count in sorted(report["statuses"].items()):
print(f" -> Status {code}: {count} adet")
© copyright 2005 - 2026