logide-parsimine/raport.py

35 lines
1.0 KiB
Python

#!/usr/bin/python
# encoding: utf-8
"""
Parsi standardsisendist Apache logikirjed ja kuva edetabelid
Käivitamiseks: (cat access.log; zcat access.log.1.gz) | python raport.py
"""
import re
import sys
from collections import Counter
from log_parsers import ApacheLogParser
hits = Counter() # IP-d kust tuldi
urls = Counter() # URL-id mida külastati
agents = Counter() # User agent mida kasutati külastamisel
for timestamp, log_entry, stack_trace in ApacheLogParser(sys.stdin):
urls[log_entry.get("path")] += 1
hits[log_entry.get("remote_addr")] += 1
agents[log_entry.get("user_agent")] += 1
print "Top5 külastatud URL-id veebiserveris:"
for path, count in urls.most_common(5):
sys.stdout.write("% 9d %s\n" % (count, path))
print
print "Top5 külastusi teinud IP aadressid:"
for remote_addr, count in hits.most_common(5):
sys.stdout.write("% 9d %s\n" % (count, remote_addr))
print
print "Top5 kasutatud veebilehitsejad/OS-id:"
for user_agent, count in agents.most_common(5):
sys.stdout.write("% 9d %s\n" % (count, user_agent))