iwla

iwla Git Source Tree

Root/plugins/display/track_users.py

1# -*- coding: utf-8 -*-
2#
3# Copyright Grégory Soutadé 2015
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
21from iwla import IWLA
22from iplugin import IPlugin
23from display import *
24
25import awstats_data
26
27"""
28Display hook
29
30Track users
31
32Plugin requirements :
33 None
34
35Conf values needed :
36 tracked_ip
37 create_tracked_page*
38
39Output files :
40 OUTPUT_ROOT/year/month/index.html
41 OUTPUT_ROOT/year/month/tracked_users.html
42
43Statistics creation :
44 None
45
46Statistics update :
47 None
48
49Statistics deletion :
50 None
51"""
52
53class IWLADisplayTrackUsers(IPlugin):
54 def __init__(self, iwla):
55 super(IWLADisplayTrackUsers, self).__init__(iwla)
56 self.API_VERSION = 1
57 self.conf_requires = ['tracked_ip']
58
59 def load(self):
60 self.create_tracked_page = self.iwla.getConfValue('create_tracked_page', True)
61 self.tracked_ip = self.iwla.getConfValue('tracked_ip', [])
62
63 return True
64
65 def hook(self):
66 display = self.iwla.getDisplay()
67 hits = self.iwla.getCurrentVisists()
68 stats = {}
69
70 # All in a page
71 if self.create_tracked_page:
72 title = createCurTitle(self.iwla, u'Tracked users')
73 filename = 'tracked_users.html'
74 path = self.iwla.getCurDisplayPath(filename)
75
76 page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
77 table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Tracked users'), [self.iwla._(u'Pages'), self.iwla._(u'Last Access')])
78 table.setColsCSSClass(['iwla_page', ''])
79 for ip in self.tracked_ip:
80 if not ip in hits.keys(): continue
81 if 'dns_name_replaced' in hits[ip].keys():
82 ip_title = '<b>%s [%s]</b>' % (hits[ip]['remote_addr'], ip)
83 else:
84 ip_title = '<b>%s</b>' % (ip)
85 table.appendRow([ip_title, ''])
86 nb_hits = 0
87 nb_pages = 0
88 for r in hits[ip]['requests'][::-1]:
89 uri = r['extract_request']['extract_uri'].lower()
90 if not self.iwla.hasBeenViewed(r): continue
91 if not self.iwla.isPage(uri) or\
92 self.iwla.isMultimediaFile(uri):
93 nb_hits += 1
94 continue
95
96 nb_pages += 1
97 uri = "%s%s" % (r.get('server_name', ''),
98 r['extract_request']['extract_uri'])
99 table.appendRow([generateHTMLLink(uri), time.asctime(r['time_decoded'])])
100 stats[ip] = (nb_pages, nb_hits)
101 page.appendBlock(table)
102
103 display.addPage(page)
104
105 # Last access in index
106 title = self.iwla._(u'Tracked users')
107 if self.create_tracked_page:
108 link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'Details'))
109 title = '%s - %s' % (title, link)
110
111 index = self.iwla.getDisplayIndex()
112
113 table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'IP'), self.iwla._(u'Last Access'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
114 table.setColsCSSClass(['', '', 'iwla_page', 'iwla_hit'])
115 for ip in self.tracked_ip:
116 if not ip in hits.keys(): continue
117 if 'dns_name_replaced' in hits[ip].keys():
118 ip_title = '%s [%s]' % (hits[ip]['remote_addr'], ip)
119 else:
120 ip_title = ip
121 table.appendRow([ip_title, time.asctime(hits[ip]['last_access']), stats[ip][0], stats[ip][1]])
122 index.appendBlock(table)

Archive Download this file

Branches

Tags