Skip to content

Instantly share code, notes, and snippets.

@guixing
Created December 14, 2013 09:49
Show Gist options
  • Save guixing/7957385 to your computer and use it in GitHub Desktop.
Save guixing/7957385 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import os
import datetime
import socket
MONTH = {
"Jan":1 ,
"Feb":2 ,
"Mar":3 ,
"Apr":4 ,
"May":5 ,
"Jun":6 ,
"Jul":7 ,
"Aug":8 ,
"Sep":9 ,
"Oct":10,
"Nov":11,
"Dec":12,
}
def scale_minute(minute):
if int(minute[1]) >= 5:
return minute[0] + '5'
else:
return minute[0] + '0'
def parse_apache_date(datestr):
day,month,yearandtime = datestr.split('/')
year, hour, minute, second = yearandtime.split(':')
return datetime.datetime(int(year),int(MONTH[month]),int(day),int(hour),int(minute))
def update_count_dict(d, k):
if k in d:
d[k] += 1
else:
d[k] = 1
def parse_apache_log(logfile):
result = {}
with open(logfile) as fd:
for line in fd:
splited_line = line.split()
datestr = splited_line[4][1:]
scaled_date = parse_apache_date(datestr)
update_count_dict(result, scaled_date.strftime('%s'))
return result
def send_to_graphite(result):
key = 'http.count'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 2003))
for k,v in result.items():
sock.send("%s %d %s\n" % (key, v, k))
if __name__ == "__main__":
data = parse_apache_log(sys.argv[1])
print data
send_to_graphite(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment