iwla/plugins/post_analysis/browsers.py
2015-01-08 21:01:33 +01:00

104 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2015
# 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
import awstats_data
"""
Post analysis hook
Detect browser information from requests
Plugin requirements :
None
Conf values needed :
None
Output files :
None
Statistics creation :
visits :
remote_addr =>
browser
month_stats :
browsers =>
browser => count
Statistics update :
None
Statistics deletion :
None
"""
class IWLAPostAnalysisBrowsers(IPlugin):
def __init__(self, iwla):
super(IWLAPostAnalysisBrowsers, self).__init__(iwla)
self.API_VERSION = 1
def load(self):
self.browsers = []
for hashid in awstats_data.browsers:
hashid_re = re.compile(r'.*%s.*' % (hashid), re.IGNORECASE)
if hashid in awstats_data.browsers_hashid.keys():
self.browsers.append((hashid_re, awstats_data.browsers_hashid[hashid]))
return True
def hook(self):
stats = self.iwla.getValidVisitors()
month_stats = self.iwla.getMonthStats()
browsers = month_stats.get('browsers', {})
browsers_stats = {}
for (k, super_hit) in stats.items():
if not 'browser' in super_hit:
for r in super_hit['requests'][::-1]:
user_agent = r['http_user_agent']
if not user_agent: continue
browser_name = 'unknown'
for (hashid_re, browser) in self.browsers:
if hashid_re.match(user_agent):
browser_name = browser
break
super_hit['browser'] = browser_name
break
else:
browser_name = super_hit['browser']
if not browser_name in browsers_stats.keys():
browsers_stats[browser_name] = 1
else:
browsers_stats[browser_name] += 1
month_stats['browsers'] = browsers_stats