from urllib import urlopen
import os
import glob
import pickle
import re
from time import strptime, strftime
def run(filterString='index.ind',
filterNumber=1,
start_date='0001-01-01-01-01-01',
end_date='9999-12-31-23-59-61',
directory=''):
a = access_log_stats(
filterString=filterString,
filterNumber=filterNumber,
start_date=start_date,
end_date=end_date,
directory=directory)
return a.html
class access_log_stats:
def __init__(self,
filterString,
filterNumber,
start_date,
end_date,
directory):
self.time_string_format = '%Y-%m-%d-%H-%M-%S'
self.start_date = parse_timeStr(start_date)
self.end_date = parse_timeStr(end_date)
self.filterString = filterString
self.filterNumber = filterNumber
self.olex_users = {}
if not directory:
self.write_directory = '/opt/Plone-3.1/olex2/parts/instance/Extensions'
#self.write_directory = '/var/distro/bin'
self.read_directory = '/var/distro/bin'
else:
self.write_directory = directory
self.read_directory = directory
self.geocodeIP_cache = self.load_geocodeIP_cache()
#logFiles = glob.glob('%s/*access_log*' %self.read_directory)
logFiles = ['%s/cds.log' %self.read_directory]
search_caches = self.load_search_cache()
search_cache = search_caches.setdefault(filterString,{})
for log in logFiles:
logName = os.path.basename(log)
if search_cache.has_key(logName) and 0:
users = search_cache[logName]
else:
a = reader(open(log, 'r'), filterString=self.filterString, write_directory=self.write_directory)
users = a.users
if not logName.endswith('access_log'):
search_cache.setdefault(logName, users)
for user, info in users.items():
if self.olex_users.has_key(user):
self.olex_users[user].update(info)
else:
self.olex_users.setdefault(user,info)
search_caches[self.filterString] = search_cache
self.save_search_cache(search_caches)
print self.html_hit_stats()
#print self.user_stats()
self.html = self.output_html_map(self.olex_users)
def load_search_cache(self):
pickle_path = '%s/search_cache.pickle' %self.write_directory
if os.path.exists(pickle_path):
pickle_file = open(pickle_path,'r')
try:
return pickle.load(pickle_file)
except:
return {}
else: return {}
def save_search_cache(self, cache):
pickle_file = open('%s/search_cache.pickle' %self.write_directory,'w')
pickle.dump(cache, pickle_file)
def geocodeIP(self, addr):
if self.geocodeIP_cache.has_key(addr):
return self.geocodeIP_cache[addr]
elif 0:
url = "http://freegeoip.appspot.com/json/%s" % (addr)
proxies = {'http': 'http://wwwcache.dur.ac.uk:8080'}
data = urlopen(url, proxies=proxies).read()
import json
data_dict = json.loads(data)
self.geocodeIP_cache.setdefault(addr, data_dict)
self.save_geocodeIP_cache()
return data_dict
elif 1:
url = "http://freegeoip.appspot.com/csv/%s" % (addr)
# url = "http://www.ip2location.com/%s" % (addr)
proxies = {'http': 'http://wwwcache.dur.ac.uk:8080'}
data = urlopen(url, proxies=proxies).read()
if "Over Quota" in data:
return {}
data = data.split(',')
data_dict = {}
fields = ['status','ip','countrycode','countryname','regioncode',
'regionname','city','zipcode','latitude','longitude']
for i in range(min(len(data),len(fields))):
data_dict.setdefault(fields[i],data[i])
self.geocodeIP_cache.setdefault(addr, data_dict)
self.save_geocodeIP_cache()
return data_dict
else:
url = "http://api.hostip.info/rough.php?ip=%s&position=true" % (addr) # geolocate ip address
proxies = {'http': 'http://wwwcache.dur.ac.uk:8080'}
data = urlopen(url, proxies=proxies).read()
data = data.split('\n')
data_dict = {}
for field in data:
name, value = field.split(':')
data_dict.setdefault(name.lower(), value.strip())
self.geocodeIP_cache.setdefault(addr, data_dict)
self.save_geocodeIP_cache()
return data_dict
def load_geocodeIP_cache(self):
pickle_path = '%s/geocodeIP_cache.pickle' %self.write_directory
if os.path.exists(pickle_path):
pickle_file = open(pickle_path, 'r')
geocodeIP_cache = pickle.load(pickle_file)
else:
geocodeIP_cache = {}
return geocodeIP_cache
def save_geocodeIP_cache(self):
pickle_file = open('%s/geocodeIP_cache.pickle' %self.write_directory, 'w')
pickle.dump(self.geocodeIP_cache, pickle_file)
def output_html_map(self, users):
markers = []
for user, access_log in users.items():
geocode = self.geocodeIP(user)
if not geocode:
continue
if "Over Quota" in repr(geocode):
try:
del self.geocodeIP_cache[user]
self.save_geocodeIP_cache()
except:
pass
continue
latitude = geocode['latitude']
longitude = geocode['longitude']
num_hits, first_access, last_access = self.get_num_hits(user)
if latitude and longitude and num_hits >= int(self.filterNumber):
first_access = strftime(self.time_string_format, first_access)
last_access = strftime(self.time_string_format, last_access)
if num_hits == 1:
icon = 'greyIcon'
zindex = '3'
elif num_hits >= 10:
icon = 'redIcon'
zindex = '1'
else:
icon = 'orangeIcon'
zindex = '2'
country = geocode.get('country', geocode.get('countryname'))
userInfo = "IP: %s
No. hits: %s
Location: %s, %s
Last access: %s" %(user,num_hits,geocode['city'],country,last_access)
markers.append("""
var marker = new GMarker(new GLatLng(%s, %s),{title:"%s", icon:%s});
marker.html = "%s";
map.addOverlay(marker);
gmarkers.push(marker);
""" %(latitude,longitude,user,icon,userInfo))
if markers:
markers_html = '\n '.join(markers)
else:
markers_html = ''
html = """
IP | No. hits | Location | First access | Last access | %s
---|