Skip to content
Extraits de code Groupes Projets
import_logs.py 59,8 ko
Newer Older
  • Learn to ignore specific revisions
  • #!/usr/bin/python
    
    # vim: et sw=4 ts=4:
    
    # -*- coding: utf-8 -*-
    
    #
    # Piwik - Open source web analytics
    #
    # @link http://piwik.org
    # @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
    # @version $Id$
    
    # For more info see: http://piwik.org/log-analytics/
    
    import ConfigParser
    import datetime
    import fnmatch
    import gzip
    
    import inspect
    import itertools
    import logging
    import optparse
    import os
    
    import Queue
    import re
    
    import sys
    
    import threading
    import time
    import urllib
    import urllib2
    
    try:
        import json
    except ImportError:
        try:
            import simplejson as json
        except ImportError:
            if sys.version_info < (2, 6):
                print >> sys.stderr, 'simplejson (http://pypi.python.org/pypi/simplejson/) is required.'
                sys.exit(1)
    
    
    Cyril Bay's avatar
    Cyril Bay a validé
    STATIC_EXTENSIONS = (
        'gif jpg jpeg png bmp ico svg ttf eot woff class swf css js xml robots.txt'
    ).split()
    
    
    
    DOWNLOAD_EXTENSIONS = (
    
        '7z aac arc arj asf asx avi bin csv deb dmg doc exe flv gz gzip hqx '
        'jar mpg mp2 mp3 mp4 mpeg mov movie msi msp odb odf odg odp '
        'ods odt ogg ogv pdf phps ppt qt qtm ra ram rar rpm sea sit tar tbz '
    
        'bz2 tbz tgz torrent txt wav wma wmv wpd xls xml xsd z zip'
    
    # A good source is: http://phpbb-bots.blogspot.com/
    EXCLUDED_USER_AGENTS = (
    
    Cyril Bay's avatar
    Cyril Bay a validé
        'adsbot-google',
        'ask jeeves',
    
    Daniel Aleksandersen's avatar
    Daniel Aleksandersen a validé
        'baidubot',
    
    Cyril Bay's avatar
    Cyril Bay a validé
        'exabot',
    
    Cyril Bay's avatar
    Cyril Bay a validé
        'googlebot',
    
    Cyril Bay's avatar
    Cyril Bay a validé
        'mediapartners-google',
    
        'netcraftsurvey',
    
    mattpiwik's avatar
    mattpiwik a validé
        'spider',
    
    Cyril Bay's avatar
    Cyril Bay a validé
        'surveybot',
        'twiceler',
        'voilabot',
        'yahoo',
        'yandex',
    
    )
    
    
    PIWIK_MAX_ATTEMPTS = 3
    PIWIK_DELAY_AFTER_FAILURE = 2
    
    PIWIK_EXPECTED_IMAGE = base64.b64decode(
        'R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
    )
    
    
    
    
    class BaseFormatException(Exception): pass
    
    
    class BaseFormat(object):
        def __init__(self, name):
    
            self.regex = None
    
            self.date_format = '%d/%b/%Y:%H:%M:%S'
    
    
        def check_format(self, file):
            line = file.readline()
            file.seek(0)
    
        def check_format_line(self, line):
            return False
    
    
    class JsonFormat(BaseFormat):
        def __init__(self, name):
    
            super(JsonFormat, self).__init__(name)
    
            self.json = None
            self.date_format = '%Y-%m-%dT%H:%M:%S'
        
        def check_format_line(self, line):
            try:
    
                self.json = json.loads(line)
    
                return True
            except:
                return False
    
        def match(self, line):
            try:
                self.json = json.loads(line)
                return self
            except:
                self.json = None
                return None
    
    
        def get(self, key):
    
            # Some ugly patchs ...
            if key == 'generation_time_milli':
                self.json[key] =  int(self.json[key] * 1000)
            # Patch date format ISO 8601 
            elif key == 'date':
                tz = self.json[key][19:]
                self.json['timezone'] = tz.replace(':', '')
                self.json[key] = self.json[key][:19]
    
    
            try:
                return self.json[key]
            except KeyError:
                raise BaseFormatException()
        
        def get_all(self,):
            return self.json
    
    
    
    
    class RegexFormat(BaseFormat):
    
        def __init__(self, name, regex, date_format=None):
    
            super(RegexFormat, self).__init__(name)
    
            if regex is not None:
                self.regex = re.compile(regex)
            if date_format is not None:
                self.date_format = date_format
    
            self.matched = None
    
            return self.match(line)
    
            self.matched = self.regex.match(line)
            return self.matched
    
        def get(self, key):
            try:
                return self.matched.group(key)
            except IndexError:
                raise BaseFormatException()
    
        def get_all(self,):
            return self.matched.groupdict()
                
    
    
        def __init__(self):
            super(IisFormat, self).__init__('iis', None, '%Y-%m-%d %H:%M:%S')
    
    
        def check_format(self, file):
            line = file.readline()
            if not line.startswith('#Software: Microsoft Internet Information Services '):
                file.seek(0)
                return
            # Skip the next 2 lines.
            for i in xrange(2):
                file.readline()
            # Parse the 4th line (regex)
            full_regex = []
            line = file.readline()
            fields = {
                'date': '(?P<date>^\d+[-\d+]+',
                'time': '[\d+:]+)',
                'cs-uri-stem': '(?P<path>/\S*)',
                'cs-uri-query': '(?P<query_string>\S*)',
                'c-ip': '(?P<ip>[\d*.]*)',
                'cs(User-Agent)': '(?P<user_agent>\S+)',
                'cs(Referer)': '(?P<referrer>\S+)',
                'sc-status': '(?P<status>\d+)',
                'sc-bytes': '(?P<length>\S+)',
                'cs-host': '(?P<host>\S+)',
            }
            # Skip the 'Fields: ' prefix.
            line = line[9:]
            for field in line.split():
                try:
                    regex = fields[field]
                except KeyError:
                    regex = '\S+'
                full_regex.append(regex)
    
            self.regex = re.compile(' '.join(full_regex))
    
            start_pos = file.tell()
            nextline = file.readline()
            file.seek(start_pos)
            return self.check_format_line(nextline)
    
    
    
    
    _HOST_PREFIX = '(?P<host>[\w\-\.]*)(?::\d+)? '
    _COMMON_LOG_FORMAT = (
        '(?P<ip>\S+) \S+ \S+ \[(?P<date>.*?) (?P<timezone>.*?)\] '
        '"\S+ (?P<path>.*?) \S+" (?P<status>\S+) (?P<length>\S+)'
    )
    _NCSA_EXTENDED_LOG_FORMAT = (_COMMON_LOG_FORMAT +
        ' "(?P<referrer>.*?)" "(?P<user_agent>.*?)"'
    )
    
    _S3_LOG_FORMAT = (
        '\S+ (?P<host>\S+) \[(?P<date>.*?) (?P<timezone>.*?)\] (?P<ip>\S+) '
        '\S+ \S+ \S+ \S+ "\S+ (?P<path>.*?) \S+" (?P<status>\S+) \S+ (?P<length>\S+) '
    
        '\S+ \S+ \S+ "(?P<referrer>.*?)" "(?P<user_agent>.*?)"'
    
    AlejandroF's avatar
    AlejandroF a validé
    _ICECAST2_LOG_FORMAT = ( _NCSA_EXTENDED_LOG_FORMAT +
        ' (?P<session_time>\S+)'
    )
    
    
    FORMATS = {
        'common': RegexFormat('common', _COMMON_LOG_FORMAT),
        'common_vhost': RegexFormat('common_vhost', _HOST_PREFIX + _COMMON_LOG_FORMAT),
        'ncsa_extended': RegexFormat('ncsa_extended', _NCSA_EXTENDED_LOG_FORMAT),
        'common_complete': RegexFormat('common_complete', _HOST_PREFIX + _NCSA_EXTENDED_LOG_FORMAT),
        'iis': IisFormat(),
    
        's3': RegexFormat('s3', _S3_LOG_FORMAT),
    
    AlejandroF's avatar
    AlejandroF a validé
        'icecast2': RegexFormat('icecast2', _ICECAST2_LOG_FORMAT),
    
        'nginx_json': JsonFormat('nginx_json'),
    
    ##
    ## Code.
    ##
    
    
    class Configuration(object):
        """
        Stores all the configuration options by reading sys.argv and parsing,
        if needed, the config.inc.php.
    
        It has 2 attributes: options and filenames.
        """
    
        class Error(Exception):
            pass
    
    
        def _create_parser(self):
            """
            Initialize and return the OptionParser instance.
            """
    
            option_parser = optparse.OptionParser(
                usage='Usage: %prog [options] log_file [ log_file [...] ]',
    
    mattpiwik's avatar
    mattpiwik a validé
                description="Import HTTP access logs to Piwik. "
    
                             "log_file is the path to a server access log file (uncompressed, .gz, .bz2, or specify - to read from stdin). "
                             " By default, the script will try to produce clean reports and will exclude bots, static files, discard http error and redirects, etc. This is customizable, see below.",
    
                epilog="About Piwik Server Log Analytics: http://piwik.org/log-analytics/ "
                       "              Found a bug? Please create a ticket in http://dev.piwik.org/ "
                       "              Please send your suggestions or successful user story to hello@piwik.org "
    
            )
            option_parser.add_option(
    
                '--debug', '-d', dest='debug', action='count', default=0,
    
                help="Enable debug output (specify multiple times for more verbose)",
    
            )
            option_parser.add_option(
    
                '--url', dest='piwik_url',
                help="REQUIRED Piwik base URL, eg. http://example.com/piwik/ or http://analytics.example.net",
            )
            option_parser.add_option(
                '--dry-run', dest='dry_run',
    
                action='store_true', default=False,
    
                help="Perform a trial run with no tracking data being inserted into Piwik",
    
            )
            option_parser.add_option(
    
                '--show-progress', dest='show_progress',
                action='store_true', default=os.isatty(sys.stdout.fileno()),
    
                help="Print a progress report X seconds (default: 1, use --show-progress-delay to override)"
            )
            option_parser.add_option(
                '--show-progress-delay', dest='show_progress_delay',
                type='int', default=1,
                help="Change the default progress delay"
    
            )
            option_parser.add_option(
                '--add-sites-new-hosts', dest='add_sites_new_hosts',
                action='store_true', default=False,
                help="When a hostname is found in the log file, but not matched to any website "
                "in Piwik, automatically create a new website in Piwik with this hostname to "
                "import the logs"
            )
            option_parser.add_option(
                '--idsite', dest='site_id',
                help= ("When specified, "
                       "data in the specified log files will be tracked for this Piwik site ID."
                       " The script will not auto-detect the website based on the log line hostname (new websites will not be automatically created).")
            )
            option_parser.add_option(
                '--idsite-fallback', dest='site_id_fallback',
                help="Default Piwik site ID to use if the hostname doesn't match any "
                "known Website's URL. New websites will not be automatically created. "
    
                "                         Used only if --add-sites-new-hosts or --idsite are not set",
    
            default_config = os.path.abspath(
                os.path.join(os.path.dirname(__file__),
                '../../config/config.ini.php'),
            )
    
            option_parser.add_option(
    
                '--config', dest='config_file', default=default_config,
    
                    "This is only used when --login and --password is not used. "
    
    mattpiwik's avatar
    mattpiwik a validé
                    "Piwik will read the configuration file (default: %default) to "
    
                    "fetch the Super User token_auth from the config file. "
    
                )
            )
            option_parser.add_option(
    
                help="You can manually specify the Piwik Super User login"
    
            )
            option_parser.add_option(
    
                help="You can manually specify the Piwik Super User password"
    
            )
            option_parser.add_option(
    
                '--token-auth', dest='piwik_token_auth',
    
                help="Piwik Super User token_auth, 32 characters hexadecimal string, found in Piwik > API",
    
            option_parser.add_option(
    
    Cyril Bay's avatar
    Cyril Bay a validé
                '--hostname', dest='hostnames', action='append', default=[],
    
                help="Accepted hostname (requests with other hostnames will be excluded). "
    
                "Can be specified multiple times"
    
            option_parser.add_option(
                '--exclude-path', dest='excluded_paths', action='append', default=[],
                help="Paths to exclude. Can be specified multiple times"
            )
            option_parser.add_option(
                '--exclude-path-from', dest='exclude_path_from',
                help="Each line from this file is a path to exclude"
            )
    
            option_parser.add_option(
                '--include-path', dest='included_paths', action='append', default=[],
                help="Paths to include. Can be specified multiple times. If not specified, all paths are included."
            )
            option_parser.add_option(
                '--include-path-from', dest='include_path_from',
                help="Each line from this file is a path to include"
            )
    
            option_parser.add_option(
                '--useragent-exclude', dest='excluded_useragents',
                action='append', default=[],
                help="User agents to exclude (in addition to the standard excluded "
    
    Cyril Bay's avatar
    Cyril Bay a validé
                "user agents). Can be specified multiple times",
    
            )
            option_parser.add_option(
    
                '--enable-static', dest='enable_static',
                action='store_true', default=False,
                help="Track static files (images, css, js, etc.)"
    
            option_parser.add_option(
    
                '--enable-bots', dest='enable_bots',
                action='store_true', default=False,
                help="Track bots. All bot visits will have a Custom Variable set with name='Bot' and value='$Bot_user_agent_here$'"
    
            option_parser.add_option(
                '--enable-http-errors', dest='enable_http_errors',
                action='store_true', default=False,
                help="Track HTTP errors (status code 4xx or 5xx)"
            )
            option_parser.add_option(
                '--enable-http-redirects', dest='enable_http_redirects',
                action='store_true', default=False,
                help="Track HTTP redirects (status code 3xx except 304)"
            )
    
            option_parser.add_option(
                '--enable-reverse-dns', dest='reverse_dns',
                action='store_true', default=False,
    
                help="Enable reverse DNS, used to generate the 'Providers' report in Piwik. "
                     "Disabled by default, as it impacts performance"
    
    Cyril Bay's avatar
    Cyril Bay a validé
            option_parser.add_option(
    
                '--strip-query-string', dest='strip_query_string',
    
    Cyril Bay's avatar
    Cyril Bay a validé
                action='store_true', default=False,
    
                help="Strip the query string from the URL"
    
            option_parser.add_option(
                '--query-string-delimiter', dest='query_string_delimiter', default='?',
                help="The query string delimiter (default: %default)"
            )
    
    Cyril Bay's avatar
    Cyril Bay a validé
            option_parser.add_option(
    
                '--log-format-name', dest='log_format_name', default=None,
    
                help=("Access log format to detect (supported are: %s). "
    
                      "When not specified, the log format will be autodetected by trying all supported log formats."
    
                      % ', '.join(sorted(FORMATS.iterkeys())))
            )
    
            option_parser.add_option(
                '--log-format-regex', dest='log_format_regex', default=None,
                help="Access log regular expression. For an example of a supported Regex, see the source code of this file. "
                     "Overrides --log-format-name"
    
            option_parser.add_option(
                '--log-hostname', dest='log_hostname', default=None,
                help="Force this hostname for a log format that doesn't incldude it. All hits "
                "will seem to came to this host"
            )
    
    Cyril Bay's avatar
    Cyril Bay a validé
            option_parser.add_option(
    
                '--skip', dest='skip', default=0, type='int',
                help="Skip the n first lines to start parsing/importing data at a given line for the specified log file",
            )
            option_parser.add_option(
                '--recorders', dest='recorders', default=1, type='int',
                help="Number of simultaneous recorders (default: %default). "
    
                "It should be set to the number of CPU cores in your server. "
    
                "You can also experiment with higher values which may increase performance until a certain point",
            )
    
                '--recorder-max-payload-size', dest='recorder_max_payload_size', default=200, type='int',
    
                help="Maximum number of log entries to record in one tracking request (default: %default). "
            )
    
            option_parser.add_option(
                '--replay-tracking', dest='replay_tracking',
                action='store_true', default=False,
    
    mattab's avatar
    mattab a validé
                help="Replay piwik.php requests found in custom logs (only piwik.php requests expected). \nSee http://piwik.org/faq/how-to/faq_17033/"
    
            option_parser.add_option(
                '--output', dest='output',
                help="Redirect output (stdout and stderr) to the specified file"
    
            option_parser.add_option(
                '--encoding', dest='encoding', default='utf8',
                help="Log files encoding (default: %default)"
            )
    
            option_parser.add_option(
                '--disable-bulk-tracking', dest='use_bulk_tracking',
                default=True, action='store_false',
                help="Disables use of bulk tracking so recorders record one hit at a time."
            )
            option_parser.add_option(
                '--debug-force-one-hit-every-Ns', dest='force_one_action_interval', default=False, type='float',
                help="Debug option that will force each recorder to record one hit every N secs."
            )
    
            option_parser.add_option(
                '--invalidate-dates', dest='invalidate_dates', default=None,
                help="Invalidate reports for the specified dates (format: YYYY-MM-DD,YYYY-MM-DD,...). "
                     "By default, all dates found in the logs will be invalidated.",
            )
    
            option_parser.add_option(
                '--force-lowercase-path', dest='force_lowercase_path', default=False, action='store_true',
                help="Make URL path lowercase so paths with the same letters but different cases are "
                     "treated the same."
            )
    
            option_parser.add_option(
                '--enable-testmode', dest='enable_testmode', default=False, action='store_true',
                help="If set, it will try to get the token_auth from the piwik_tests directory"
            )
    
    
        def _parse_args(self, option_parser):
            """
            Parse the command line args and create self.options and self.filenames.
            """
    
            self.options, self.filenames = option_parser.parse_args(sys.argv[1:])
    
                sys.stdout = sys.stderr = open(self.options.output, 'a+', 0)
    
    Cyril Bay's avatar
    Cyril Bay a validé
            if not self.filenames:
                print(option_parser.format_help())
                sys.exit(1)
    
    
            # Configure logging before calling logging.{debug,info}.
            logging.basicConfig(
                format='%(asctime)s: [%(levelname)s] %(message)s',
    
                level=logging.DEBUG if self.options.debug >= 1 else logging.INFO,
    
    Cyril Bay's avatar
    Cyril Bay a validé
            self.options.excluded_useragents = [s.lower() for s in self.options.excluded_useragents]
    
    
            if self.options.exclude_path_from:
                paths = [path.strip() for path in open(self.options.exclude_path_from).readlines()]
                self.options.excluded_paths.extend(path for path in paths if len(path) > 0)
            if self.options.excluded_paths:
                logging.debug('Excluded paths: %s', ' '.join(self.options.excluded_paths))
    
    
            if self.options.include_path_from:
                paths = [path.strip() for path in open(self.options.include_path_from).readlines()]
                self.options.included_paths.extend(path for path in paths if len(path) > 0)
            if self.options.included_paths:
                logging.debug('Included paths: %s', ' '.join(self.options.included_paths))
    
    
            if self.options.hostnames:
    
                logging.debug('Accepted hostnames: %s', ', '.join(self.options.hostnames))
    
            else:
                logging.debug('Accepted hostnames: all')
    
    
    Cyril Bay's avatar
    Cyril Bay a validé
            if self.options.log_format_regex:
    
                self.format = RegexFormat('custom', self.options.log_format_regex)
    
    Cyril Bay's avatar
    Cyril Bay a validé
            elif self.options.log_format_name:
    
                    self.format = FORMATS[self.options.log_format_name]
    
                except KeyError:
    
    Cyril Bay's avatar
    Cyril Bay a validé
                    fatal_error('invalid log format: %s' % self.options.log_format_name)
    
    
            if not self.options.piwik_url:
                fatal_error('no URL given for Piwik')
    
            if not (self.options.piwik_url.startswith('http://') or self.options.piwik_url.startswith('https://')):
                self.options.piwik_url = 'http://' + self.options.piwik_url
            logging.debug('Piwik URL is: %s', self.options.piwik_url)
    
            if not self.options.piwik_token_auth:
    
                try:
                    self.options.piwik_token_auth = self._get_token_auth()
                except Piwik.Error, e:
                    fatal_error(e)
    
            logging.debug('Authentication token token_auth is: %s', self.options.piwik_token_auth)
    
    
            if self.options.recorders < 1:
                self.options.recorders = 1
    
    
    
        def __init__(self):
            self._parse_args(self._create_parser())
    
    
    
        def _get_token_auth(self):
            """
            If the token auth is not specified in the options, get it from Piwik.
            """
            # Get superuser login/password from the options.
    
            logging.debug('No token-auth specified')
    
            if self.options.login and self.options.password:
                piwik_login = self.options.login
                piwik_password = hashlib.md5(self.options.password).hexdigest()
            else:
                # Fallback to the given (or default) configuration file, then
                # get the token from the API.
    
                logging.debug(
                    'No credentials specified, reading them from "%s"',
    
                )
                config_file = ConfigParser.RawConfigParser()
                success = len(config_file.read(self.options.config_file)) > 0
                if not success:
                    fatal_error(
                        "couldn't open the configuration file, "
                        "required to get the authentication token"
                    )
    
    
                updatetokenfile = os.path.abspath(
                    os.path.join(os.path.dirname(__file__),
                    '../../misc/cron/updatetoken.php'),
    
    
                process =  "php " + updatetokenfile
                if self.options.enable_testmode:
                    process = process + " --testmode"
    
                filename    = subprocess.check_output(process, shell=True);
    
                credentials = open(filename, 'r').readline()
                credentials = credentials.split('\t')
                return credentials[1]
    
    
    
        def get_resolver(self):
            if self.options.site_id:
                logging.debug('Resolver: static')
                return StaticResolver(self.options.site_id)
            else:
                logging.debug('Resolver: dynamic')
                return DynamicResolver()
    
    
    
    class Statistics(object):
        """
        Store statistics about parsed logs and recorded entries.
        Can optionally print statistics on standard output every second.
        """
    
        class Counter(object):
            """
            Simple integers cannot be used by multithreaded programs. See:
            http://stackoverflow.com/questions/6320107/are-python-ints-thread-safe
            """
            def __init__(self):
                # itertools.count's implementation in C does not release the GIL and
                # therefore is thread-safe.
                self.counter = itertools.count(1)
                self.value = 0
    
            def increment(self):
                self.value = self.counter.next()
    
            def advance(self, n):
                for i in range(n):
                    self.increment()
    
    
            def __str__(self):
                return str(int(self.value))
    
    
        def __init__(self):
            self.time_start = None
            self.time_stop = None
    
            self.piwik_sites = set()                # sites ID
            self.piwik_sites_created = []           # (hostname, site ID)
            self.piwik_sites_ignored = set()        # hostname
    
            self.count_lines_parsed = self.Counter()
            self.count_lines_recorded = self.Counter()
    
            # Do not match the regexp.
            self.count_lines_invalid = self.Counter()
            # No site ID found by the resolver.
            self.count_lines_no_site = self.Counter()
            # Hostname filtered by config.options.hostnames
            self.count_lines_hostname_skipped = self.Counter()
            # Static files.
            self.count_lines_static = self.Counter()
            # Ignored user-agents.
            self.count_lines_skipped_user_agent = self.Counter()
    
            # Ignored HTTP erors.
            self.count_lines_skipped_http_errors = self.Counter()
            # Ignored HTTP redirects.
            self.count_lines_skipped_http_redirects = self.Counter()
    
            # Downloads
            self.count_lines_downloads = self.Counter()
    
            # Misc
            self.dates_recorded = set()
            self.monitor_stop = False
    
    
        def set_time_start(self):
            self.time_start = time.time()
    
        def set_time_stop(self):
            self.time_stop = time.time()
    
        def _compute_speed(self, value, start, end):
            delta_time = end - start
            if value == 0:
                return 0
            if delta_time == 0:
                return 'very high!'
            else:
                return value / delta_time
    
        def _round_value(self, value, base=100):
            return round(value * base) / base
    
        def _indent_text(self, lines, level=1):
            """
            Return an indented text. 'lines' can be a list of lines or a single
            line (as a string). One level of indentation is 4 spaces.
            """
            prefix = ' ' * (4 * level)
            if isinstance(lines, basestring):
                return prefix + lines
            else:
                return '\n'.join(
                    prefix + line
                    for line in lines
                )
    
        def print_summary(self):
            print '''
    Logs import summary
    -------------------
    
        %(count_lines_recorded)d requests imported successfully
        %(count_lines_downloads)d requests were downloads
        %(total_lines_ignored)d requests ignored:
            %(count_lines_invalid)d invalid log lines
            %(count_lines_skipped_user_agent)d requests done by bots, search engines, ...
    
            %(count_lines_skipped_http_errors)d HTTP errors
            %(count_lines_skipped_http_redirects)d HTTP redirects
    
            %(count_lines_static)d requests to static resources (css, js, ...)
    
            %(count_lines_no_site)d requests did not match any known site
            %(count_lines_hostname_skipped)d requests did not match any requested hostname
    
    Website import summary
    ----------------------
    
        %(count_lines_recorded)d requests imported to %(total_sites)d sites
            %(total_sites_existing)d sites already existed
            %(total_sites_created)d sites were created:
    %(sites_created)s
        %(total_sites_ignored)d distinct hostnames did not match any existing site:
    %(sites_ignored)s
    
    %(sites_ignored_tips)s
    
    
    Performance summary
    -------------------
    
        Total time: %(total_time)d seconds
        Requests imported per second: %(speed_recording)s requests per second
    ''' % {
    
        'count_lines_recorded': self.count_lines_recorded.value,
        'count_lines_downloads': self.count_lines_downloads.value,
        'total_lines_ignored': sum([
                self.count_lines_invalid.value,
                self.count_lines_skipped_user_agent.value,
    
                self.count_lines_skipped_http_errors.value,
    
    mattpiwik's avatar
    mattpiwik a validé
                self.count_lines_skipped_http_redirects.value,
    
                self.count_lines_static.value,
                self.count_lines_no_site.value,
                self.count_lines_hostname_skipped.value,
            ]),
        'count_lines_invalid': self.count_lines_invalid.value,
        'count_lines_skipped_user_agent': self.count_lines_skipped_user_agent.value,
    
        'count_lines_skipped_http_errors': self.count_lines_skipped_http_errors.value,
        'count_lines_skipped_http_redirects': self.count_lines_skipped_http_redirects.value,
    
        'count_lines_static': self.count_lines_static.value,
        'count_lines_no_site': self.count_lines_no_site.value,
        'count_lines_hostname_skipped': self.count_lines_hostname_skipped.value,
        'total_sites': len(self.piwik_sites),
        'total_sites_existing': len(self.piwik_sites - set(site_id for hostname, site_id in self.piwik_sites_created)),
        'total_sites_created': len(self.piwik_sites_created),
        'sites_created': self._indent_text(
                ['%s (ID: %d)' % (hostname, site_id) for hostname, site_id in self.piwik_sites_created],
                level=3,
            ),
        'total_sites_ignored': len(self.piwik_sites_ignored),
        'sites_ignored': self._indent_text(
                self.piwik_sites_ignored, level=3,
            ),
    
        'sites_ignored_tips': '''
            TIPs:
             - if one of these hosts is an alias host for one of the websites
               in Piwik, you can add this host as an "Alias URL" in Settings > Websites.
             - use --add-sites-new-hosts if you wish to automatically create
               one website for each of these hosts in Piwik rather than discarding
               these requests.
             - use --idsite-fallback to force all these log lines with a new hostname
               to be recorded in a specific idsite (for example for troubleshooting/visualizing the data)
             - use --idsite to force all lines in the specified log files
               to be all recorded in the specified idsite
             - or you can also manually create a new Website in Piwik with the URL set to this hostname
    ''' if self.piwik_sites_ignored else '',
    
        'total_time': self.time_stop - self.time_start,
        'speed_recording': self._round_value(self._compute_speed(
                self.count_lines_recorded.value,
                self.time_start, self.time_stop,
            )),
    }
    
    
        ##
        ## The monitor is a thread that prints a short summary each second.
        ##
    
        def _monitor(self):
            latest_total_recorded = 0
            while not self.monitor_stop:
                current_total = stats.count_lines_recorded.value
    
                print '%d lines parsed, %d lines recorded, %d records/sec (avg), %d records/sec (current)' % (
    
                    stats.count_lines_parsed.value,
                    current_total,
    
                    current_total / time_elapsed if time_elapsed != 0 else 0,
    
                    (current_total - latest_total_recorded) / config.options.show_progress_delay,
    
                )
                latest_total_recorded = current_total
    
                time.sleep(config.options.show_progress_delay)
    
    
        def start_monitor(self):
            t = threading.Thread(target=self._monitor)
            t.daemon = True
            t.start()
    
        def stop_monitor(self):
            self.monitor_stop = True
    
    
    
    class Piwik(object):
        """
        Make requests to Piwik.
        """
    
        class Error(Exception):
            pass
    
        @staticmethod
    
        def _call(path, args, headers=None, url=None, data=None):
    
            """
            Make a request to the Piwik site. It is up to the caller to format
            arguments, to embed authentication, etc.
            """
            if url is None:
                url = config.options.piwik_url
            headers = headers or {}
    
            if data is None:
                # If Content-Type isn't defined, PHP do not parse the request's body.
                headers['Content-type'] = 'application/x-www-form-urlencoded'
                data = urllib.urlencode(args)
            elif not isinstance(data, basestring) and headers['Content-type'] == 'application/json':
                data = json.dumps(data)
    
    mattab's avatar
    mattab a validé
            headers['User-Agent'] = 'Piwik/LogImport'
    
            request = urllib2.Request(url + path, data, headers)
            response = urllib2.urlopen(request)
    
            result = response.read()
            response.close()
            return result
    
    
        @staticmethod
        def _call_api(method, **kwargs):
            """
            Make a request to the Piwik API taking care of authentication, body
            formatting, etc.
            """
            args = {
                'module' : 'API',
                'format' : 'json',
                'method' : method,
            }
            # token_auth, by default, is taken from config.
            token_auth = kwargs.pop('_token_auth', None)
            if token_auth is None:
                token_auth = config.options.piwik_token_auth
            if token_auth:
                args['token_auth'] = token_auth
    
            url = kwargs.pop('_url', None)
    
            if kwargs:
                args.update(kwargs)
    
            # Convert lists into appropriate format.
            # See: http://dev.piwik.org/trac/wiki/API/Reference#PassinganArrayParameter
            # Warning: we have to pass the parameters in order: foo[0], foo[1], foo[2]
            # and not foo[1], foo[0], foo[2] (it will break Piwik otherwise.)
            final_args = []
            for key, value in args.iteritems():
                if isinstance(value, (list, tuple)):
                    for index, obj in enumerate(value):
                        final_args.append(('%s[%d]' % (key, index), obj))
                else:
                    final_args.append((key, value))
            res = Piwik._call('/', final_args, url=url)
            try:
                return json.loads(res)
            except ValueError:
    
    mattab's avatar
    mattab a validé
                truncate_after = 1100
    
    Benaka Moorthi's avatar
    Benaka Moorthi a validé
                raise urllib2.URLError('Piwik returned an invalid response: ' + res[:truncate_after])
    
        @staticmethod
        def _call_wrapper(func, expected_response, on_failure, *args, **kwargs):
    
            """
            Try to make requests to Piwik at most PIWIK_FAILURE_MAX_RETRY times.
            """
            errors = 0
            while True:
                try:
                    response = func(*args, **kwargs)
                    if expected_response is not None and response != expected_response:
    
                        if on_failure is not None:
                            error_message = on_failure(response, kwargs.get('data'))
                        else:
    
    mattab's avatar
    mattab a validé
                            truncate_after = 1100
    
    mattpiwik's avatar
    mattpiwik a validé
                            truncated_response = (response[:truncate_after] + '..') if len(response) > truncate_after else response
    
                            error_message = "didn't receive the expected response. Response was %s " % truncated_response
    
                        raise urllib2.URLError(error_message)
    
                    return response
    
                except (urllib2.URLError, httplib.HTTPException, ValueError), e:
    
                    logging.debug('Error when connecting to Piwik: %s', e)
                    errors += 1
                    if errors == PIWIK_MAX_ATTEMPTS:
    
                        if isinstance(e, urllib2.HTTPError):
                            # See Python issue 13211.
                            message = e.msg
                        elif isinstance(e, urllib2.URLError):
    
                            message = e.reason
                        else:
                            message = str(e)
                        raise Piwik.Error(message)
    
                    else:
                        time.sleep(PIWIK_DELAY_AFTER_FAILURE)
    
    
        @classmethod
        def call(cls, path, args, expected_content=None, headers=None, data=None, on_failure=None):
            return cls._call_wrapper(cls._call, expected_content, on_failure, path, args, headers,
    
    mattpiwik's avatar
    mattpiwik a validé
                                        data=data)
    
    
        @classmethod
        def call_api(cls, method, **kwargs):
            return cls._call_wrapper(cls._call_api, None, None, method, **kwargs)
    
    
    
    ##
    ## Resolvers.
    ##
    ## A resolver is a class that turns a hostname into a Piwik site ID.
    ##
    
    class StaticResolver(object):
        """
        Always return the same site ID, specified in the configuration.
        """
    
        def __init__(self, site_id):
            self.site_id = site_id
            # Go get the main URL
            sites = piwik.call_api(
                'SitesManager.getSiteFromId', idSite=self.site_id
            )
            try:
                site = sites[0]
            except (IndexError, KeyError):
    
                logging.debug('response for SitesManager.getSiteFromId: %s', str(sites))
    
                fatal_error(
                    "cannot get the main URL of this site: invalid site ID: %s" % site_id
                )
            if site.get('result') == 'error':
                fatal_error(
                    "cannot get the main URL of this site: %s" % site.get('message')
                )
            self._main_url = site['main_url']
            stats.piwik_sites.add(self.site_id)
    
        def resolve(self, hit):
            return (self.site_id, self._main_url)
    
        def check_format(self, format):
            pass
    
    
    class DynamicResolver(object):
        """
        Use Piwik API to determine the site ID.
        """
    
    
        def __init__(self):
            self._cache = {}
    
            if config.options.replay_tracking:
                # get existing sites
    
                self._cache['sites'] = piwik.call_api('SitesManager.getAllSites')
    
        def _get_site_id_from_hit_host(self, hit):
    
            main_url = 'http://' + hit.host
    
                'SitesManager.getSitesIdFromSiteUrl',
                url=main_url,
            )