-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess_ipgeo.py
75 lines (58 loc) · 2.04 KB
/
access_ipgeo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import re
import json
import time
import socket
import apachelog
import urllib, urllib2
def parseLog(logfile, outfile):
"""Parses apache logs and performs lookup on where the access is from"""
#Options
LOG_ACCESS_DATA = False
PRINT_PROGRESS = True
IPINFODB_API_KEY = ''
apache_log_format = r'%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
p = apachelog.parser(apache_log_format)
total_lines = str(sum(1 for line in open(logfile)))
current_line = 1
parsed_log = {}
for line in open(logfile, 'r'):
try:
data = p.parse(line)
ip = data['%h']
if ip not in parsed_log: #new entry
parsed_log[ip] = {}
parsed_log[ip]['access_count'] = 1
if LOG_ACCESS_DATA:
parsed_log[ip].setdefault('accessed_files', []).append((data['%t'], data['%r'])) #convert to date time
ipinfo_url = 'http://api.ipinfodb.com/v3/ip-city/?key=' + IPINFODB_API_KEY + '&ip=' + ip + '&format=json'
try:
req = urllib2.Request(ipinfo_url)
response = urllib2.urlopen(req)
except URLError as e:
print 'TODO'
ipinfo = json.load(response)
parsed_log[ip]['country'] = ipinfo['countryName']
parsed_log[ip]['city'] = ipinfo['cityName']
parsed_log[ip]['lat'] = ipinfo['latitude']
parsed_log[ip]['lon'] = ipinfo['longitude']
try:
parsed_log[ip]['hostname'] = socket.gethostbyaddr(ip)[0]
except:
parsed_log[ip]['hostname'] = ''
if PRINT_PROGRESS:
print "INSERTED: " + ip + " LINES: " + str(current_line) + "/" + total_lines
else:
parsed_log[ip]['access_count'] += 1
if LOG_ACCESS_DATA:
parsed_log[ip].setdefault('accessed_files', []).append((data['%t'], data['%r']))
if PRINT_PROGRESS:
print "UPDATED: " + ip + " LINES: " + str(current_line) + "/" + total_lines
except:
print "Parse error: %s" % line
current_line += 1
#output file in pretty JSON print
with open(outfile, 'w') as output:
json.dump(parsed_log, output, sort_keys=True, indent=4, separators=(',', ': '))
logfile = 'access_log-20130609.txt'
output = 'parsed_log_test.json'
parseLog(logfile, output)