Created
July 6, 2012 06:28
-
-
Save zouguangxian/3058396 to your computer and use it in GitHub Desktop.
analyze access.log to get bandwidth statistics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import re | |
import datetime | |
import time | |
STEP = 60 | |
result = {} | |
while True: | |
line = sys.stdin.readline() | |
if line == '': | |
break | |
line = line.strip() | |
m = re.search('\[(.*?)\] ".*?" (\d*) (\d*) (\d*) (\d*)', line) | |
if not m: | |
continue | |
timeval = m.group(1) | |
bytes = int(m.group(4)) | |
duration = int(m.group(5)) | |
timeval = re.sub(' \+\d{4}', '', timeval) | |
t = datetime.datetime.strptime(timeval, '%d/%b/%Y:%H:%M:%S') | |
t = int(time.mktime(t.timetuple())) | |
if duration == 0: | |
if t not in result: | |
result[t] = bytes | |
else: | |
result[t] += bytes | |
continue | |
s = 1.0*bytes/duration | |
for x in xrange(t, t+int(duration)): | |
if x not in result: | |
result[x] = s | |
else: | |
result[x] += s | |
_last = 0 | |
_max = 0 | |
for k, v in result.iteritems(): | |
ds = (k / STEP) * STEP | |
if _last == ds: | |
_max = max(_max, v) | |
else: | |
if _last == 0: | |
_last = ds | |
_max = v | |
else: | |
print "%s\t%f" % (time.strftime('%d/%b/%YT%H:%M:%S', time.localtime(_last)), _max) | |
_last = ds | |
_max = v | |
if _last != 0: | |
print "%s\t%f" % (time.strftime('%d/%b/%YT%H:%M:%S', time.localtime(_last)), _max) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment