Python

Nginx log analyzer: full traffic report

A small tool that walks an access.log, aggregates traffic by hour and by HTTP status class, then prints a console report with ASCII bars and percentages.

Prerequisites

Python 3.9+ (bibliothèque standard)

Python
from collections import Counter

heures, statuts = Counter(), Counter()
total = 0
with open("access.log", encoding="utf-8") as f:
    for ligne in f:
        p = ligne.split()
        if len(p) < 9:
            continue
        total += 1
        heures[p[3][13:15]] += 1        # [10/Jun/2026:14:02:11 → "14"
        statuts[p[8][0] + "xx"] += 1

print(f"Requêtes analysées : {total:,}".replace(",", " "))
print()
print("Trafic par heure (top 5) :")
pic = max(heures.values())
for h, n in sorted(heures.items(), key=lambda x: -x[1])[:5]:
    print(f"  {h}h  {n:>6}  " + "█" * round(n * 28 / pic))
print()
print("Codes HTTP :")
for code, n in sorted(statuts.items()):
    print(f"  {code} : {n:>6}  ({n / total:6.1%})")

Result

Requêtes analysées : 184 302

Trafic par heure (top 5) :
  14h   21450  ████████████████████████████
  15h   19872  ██████████████████████████
  10h   17204  ██████████████████████
  11h   16881  ██████████████████████
  16h   15110  ████████████████████

Codes HTTP :
  2xx : 171220  ( 92.9%)
  3xx :   7905  (  4.3%)
  4xx :   4312  (  2.3%)
  5xx :    865  (  0.5%)
LogsNginxRapportCounterOps

Related snippets

Back to the Data Lab