iwla/plugins/display/ip_to_geo.py

119 lines
3.9 KiB
Python
Raw Normal View History

2016-02-06 14:41:41 +01:00
# -*- coding: utf-8 -*-
#
2016-02-06 14:47:47 +01:00
# Copyright Grégory Soutadé 2016
2016-02-06 14:41:41 +01:00
# This file is part of iwla
# iwla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iwla is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iwla. If not, see <http://www.gnu.org/licenses/>.
#
import re
from iwla import IWLA
from iplugin import IPlugin
from display import *
"""
Display hook
Add geo statistics
Plugin requirements :
post_analysis/ip_to_geo
Conf values needed :
create_geo_page*
Output files :
OUTPUT_ROOT/year/month/index.html
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayIPToGeo(IPlugin):
2016-02-06 14:41:41 +01:00
def __init__(self, iwla):
super(IWLADisplayIPToGeo, self).__init__(iwla)
2016-02-06 14:41:41 +01:00
self.API_VERSION = 1
self.requires = ['IWLAPostAnalysisIPToGeo']
2016-02-06 14:41:41 +01:00
def load(self):
self.icon_path = self.iwla.getConfValue('icon_path', '/')
self.create_geo_page = self.iwla.getConfValue('create_geo_page_page', True)
display = self.iwla.getDisplay()
display.addColumnFilter(self.iwla._(u'Host'), self.FlagFilter, {'self':self})
return True
2016-04-12 08:17:34 +02:00
@staticmethod # Needed to have unbound method
2016-02-06 14:41:41 +01:00
def FlagFilter(host, self):
cc = None
host_name = host.split(' ')[0] # hostname or ip
if host_name in self.visitors.keys():
cc = self.visitors[host_name].get('country_code', None)
2016-02-06 14:41:41 +01:00
else:
for visitor in self.visitors.values():
2016-02-06 14:41:41 +01:00
if visitor['remote_addr'] == host_name:
cc = visitor.get('country_code', None)
2016-02-06 14:41:41 +01:00
break
if not cc or cc == 'ip': return None
icon = '<img alt="%s flag" src="/%s/flags/%s.png"/>' % (cc, self.icon_path, cc)
2016-02-06 14:41:41 +01:00
return '%s %s' % (icon ,host)
def hook(self):
display = self.iwla.getDisplay()
geo = self.iwla.getMonthStats().get('geo', {})
2016-02-06 14:41:41 +01:00
geo = sorted(geo.items(), key=lambda t: t[1], reverse=True)
self.visitors = self.iwla.getCurrentVisits()
2016-02-06 14:41:41 +01:00
# All in a page
if self.create_geo_page:
title = createCurTitle(self.iwla, u'All Coutries')
filename = 'geo.html'
path = self.iwla.getCurDisplayPath(filename)
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Countries'), ['', self.iwla._(u'Country'), self.iwla._(u'Visitors')])
table.setColsCSSClass(['', '', 'iwla_hit'])
for (cc, visitors) in geo:
icon = '<img alt="%s flag" src="/%s/flags/%s.png"/>' % (cc, self.icon_path, cc)
2016-02-06 14:41:41 +01:00
table.appendRow([icon, cc, visitors])
table.computeRatio(2)
page.appendBlock(table)
display.addPage(page)
# Countries in index
title = self.iwla._(u'Countries')
if self.create_geo_page:
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All countries'))
2016-02-06 14:41:41 +01:00
title = '%s - %s' % (title, link)
index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, title, ['', self.iwla._(u'Countries'), self.iwla._(u'Visitors')])
table.setColsCSSClass(['', '', 'iwla_hit'])
for (cc, visitors) in geo[:10]:
icon = '<img alt="%s flag" src="/%s/flags/%s.png"/>' % (cc, self.icon_path, cc)
2016-02-06 14:41:41 +01:00
table.appendRow([icon, cc, visitors])
table.computeRatio(2)
index.appendBlock(table)