iwla/plugins/display/robot_bandwidth.py
2023-01-28 09:38:59 +01:00

131 lines
4.5 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2016
# 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 time
from iwla import IWLA
from iplugin import IPlugin
from display import *
"""
Display hook
Display top 10 robot bandwidth use
Plugin requirements :
None
Conf values needed :
display_visitor_ip*
create_all_robot_bandwidth_page*
Output files :
OUTPUT_ROOT/year/month/top_robots_bandwidth.html
OUTPUT_ROOT/year/month/index.html
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayRobotBandwidth(IPlugin):
def __init__(self, iwla):
super(IWLADisplayRobotBandwidth, self).__init__(iwla)
self.API_VERSION = 1
self.display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False)
self.create_all_pages = self.iwla.getConfValue('create_all_robot_bandwidth_page', True)
def load(self):
return True
def hook(self):
display = self.iwla.getDisplay()
hits = self.iwla.getCurrentVisits()
bandwidths = []
bandwidths_group = {}
for (k, super_hit) in hits.items():
if not self.iwla.isRobot(super_hit):
continue
bandwidths.append((super_hit, super_hit['bandwidth'][0]))
address = super_hit.get('robot_name', '') or super_hit['remote_addr']
if address in bandwidths_group.keys():
group = bandwidths_group[address]
if group['last_access'] < super_hit['last_access']:
group['last_access'] = super_hit['last_access']
group['bandwidth'] += super_hit['bandwidth'][0]
else:
bandwidths_group[address] = {
'last_access':super_hit['last_access'],
'bandwidth':super_hit['bandwidth'][0]
}
# All in a page
if self.create_all_pages:
title = createCurTitle(self.iwla, u'Robots bandwidth')
filename = 'top_robots_bandwidth.html'
path = self.iwla.getCurDisplayPath(filename)
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Bandwidth'), self.iwla._(u'Name'), self.iwla._(u'Last seen')], [1])
table.setColsCSSClass(['', 'iwla_bandwidth', '', ''])
for (super_hit, bandwidth) in bandwidths:
address = super_hit['remote_addr']
if self.display_visitor_ip and\
super_hit.get('dns_name_replaced', False):
address = '%s [%s]' % (address, super_hit['remote_ip'])
row = [
address,
bandwidth,
super_hit.get('robot_name', ''),
time.asctime(super_hit['last_access'])
]
table.appendRow(row)
page.appendBlock(table)
display.addPage(page)
title = self.iwla._(u'Robots bandwidth')
if self.create_all_pages:
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All robots bandwidth'))
title = '%s - %s' % (title, link)
# Top in index
index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Robot'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')], [1])
table.setColsCSSClass(['', 'iwla_bandwidth', ''])
_bandwidths_group = dict(sorted(bandwidths_group.items(), key=lambda g: g[1]['bandwidth'], reverse=True))
for i, (k, group) in enumerate(_bandwidths_group.items()):
if i >= 10: break
row = [
k,
group['bandwidth'],
time.asctime(group['last_access'])
]
table.appendRow(row)
index.appendBlock(table)