usr bin python coding utf-8 import os LOGS_DIR var log lighttpd STAT f

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
LOGS_DIR = '/var/log/lighttpd/%s'
STAT = {}
for file in os.listdir(LOGS_DIR % ''):
if file.startswith('access.log') and not file.endswith('.gz') or file == 'access.log':
FILE = open(LOGS_DIR % file)
for line in FILE.readlines():
ip = line.split(' ',1)[0]
STAT[ip] = STAT.get(ip,0) + 1
FILE.close()
_STAT = []
for t in STAT.keys(): _STAT.append([STAT[t],t])
_STAT.sort(reverse=True)
print 'Count .. IP'
print '\n'.join('%s ... %s' % tuple(t) for t in _STAT)
print 'Total: %s' % len(_STAT)