iwla

iwla Git Source Tree

Root/plugins/display/robot_bandwidth.py

1# -*- coding: utf-8 -*-
2#
3# Copyright Grégory Soutadé 2016
4
5# This file is part of iwla
6
7# iwla is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# iwla is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with iwla. If not, see <http://www.gnu.org/licenses/>.
19#
20
21import time
22
23from iwla import IWLA
24from iplugin import IPlugin
25from display import *
26
27"""
28Display hook
29
30Display top 10 robot bandwidth use
31
32Plugin requirements :
33 None
34
35Conf values needed :
36 display_visitor_ip*
37 create_all_robot_bandwidth_page*
38
39Output files :
40 OUTPUT_ROOT/year/month/top_robots_bandwidth.html
41 OUTPUT_ROOT/year/month/index.html
42
43Statistics creation :
44 None
45
46Statistics update :
47 None
48
49Statistics deletion :
50 None
51"""
52
53class IWLADisplayRobotBandwidth(IPlugin):
54 def __init__(self, iwla):
55 super(IWLADisplayRobotBandwidth, self).__init__(iwla)
56 self.API_VERSION = 1
57 self.display_visitor_ip = self.iwla.getConfValue('display_visitor_ip', False)
58 self.create_all_pages = self.iwla.getConfValue('create_all_robot_bandwidth_page', True)
59
60 def load(self):
61 return True
62
63 def hook(self):
64 display = self.iwla.getDisplay()
65 hits = self.iwla.getCurrentVisits()
66
67 bandwidths = []
68 for (k, super_hit) in hits.items():
69 if not self.iwla.isRobot(super_hit):
70 continue
71 bandwidths.append((super_hit, super_hit['bandwidth'][0]))
72 bandwidths.sort(key=lambda tup: tup[1], reverse=True)
73
74 # All in a page
75 if self.create_all_pages:
76 title = createCurTitle(self.iwla, u'Robots bandwidth')
77 filename = 'top_robots_bandwidth.html'
78 path = self.iwla.getCurDisplayPath(filename)
79
80 page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
81 table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')], [1])
82 table.setColsCSSClass(['', 'iwla_bandwidth', ''])
83 for (super_hit, bandwidth) in bandwidths:
84 address = super_hit['remote_addr']
85 if self.display_visitor_ip and\
86 super_hit.get('dns_name_replaced', False):
87 address = '%s [%s]' % (address, super_hit['remote_ip'])
88
89 row = [
90 address,
91 bandwidth,
92 time.asctime(super_hit['last_access'])
93 ]
94 table.appendRow(row)
95 page.appendBlock(table)
96
97 display.addPage(page)
98
99 title = self.iwla._(u'Robots bandwidth')
100 if self.create_all_pages:
101 link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All robots bandwidth'))
102 title = '%s - %s' % (title, link)
103
104 # Top in index
105 index = self.iwla.getDisplayIndex()
106 table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Bandwidth'), self.iwla._(u'Last seen')], [1])
107 table.setColsCSSClass(['', 'iwla_bandwidth', ''])
108
109 for (super_hit, bandwidth) in bandwidths[:10]:
110 address = super_hit['remote_addr']
111 if self.display_visitor_ip and\
112 super_hit.get('dns_name_replaced', False):
113 address = '%s [%s]' % (address, super_hit['remote_ip'])
114
115 row = [
116 address,
117 bandwidth,
118 time.asctime(super_hit['last_access'])
119 ]
120 table.appendRow(row)
121 index.appendBlock(table)

Archive Download this file

Branches

Tags