642N/A#!/usr/bin/python
642N/A#
642N/A# CDDL HEADER START
642N/A#
642N/A# The contents of this file are subject to the terms of the
642N/A# Common Development and Distribution License (the "License").
642N/A# You may not use this file except in compliance with the License.
642N/A#
642N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
642N/A# or http://www.opensolaris.org/os/licensing.
642N/A# See the License for the specific language governing permissions
642N/A# and limitations under the License.
642N/A#
642N/A# When distributing Covered Code, include this CDDL HEADER in each
642N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
642N/A# If applicable, add the following below this CDDL HEADER, with the
642N/A# fields enclosed by brackets "[]" replaced with your own identifying
642N/A# information: Portions Copyright [yyyy] [name of copyright owner]
642N/A#
642N/A# CDDL HEADER END
642N/A#
642N/A
3143N/A#
3158N/A# Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
3143N/A#
642N/A
3143N/Afrom __future__ import print_function
642N/Aimport datetime
642N/Aimport fileinput
642N/Aimport getopt
642N/Aimport md5
642N/Aimport os
642N/Aimport re
642N/Aimport sys
642N/Aimport time
642N/A
642N/Afrom an_report import *
3234N/Afrom six.moves.urllib.parse import unquote
642N/A
642N/Aafter = None
642N/Abefore = None
642N/Asummary_file = None
642N/A
642N/Amanifest_by_date = {}
642N/Amanifest_by_ip = {}
642N/Amanifest_by_arch = {}
642N/Amanifest_by_lang = {}
642N/Amanifest_by_raw_agent = {}
642N/A
642N/Amanifest_by_pkg = {}
642N/Amanifest_by_ver_pkg = {}
642N/A
642N/Apkg_pat = re.compile("/manifest/(?P<mversion>\d+)/(?P<stem>[^@]*)@(?P<version>.*)")
642N/A
642N/Adef report_manifest_by_arch():
3143N/A print("<pre>")
642N/A for i in manifest_by_arch.keys():
3143N/A print(i, manifest_by_arch[i])
3143N/A print("</pre>")
642N/A
642N/Adef report_manifest_by_pkg():
3143N/A print("<pre>")
3194N/A for i, n in (sorted(manifest_by_pkg.items(), key=lambda k_v: (k_v[1],k_v[0]))):
3143N/A print(i, n)
3143N/A print("</pre>")
642N/A
642N/Adef report_manifest_by_ver_pkg():
3143N/A print("<pre>")
3194N/A for i, n in (sorted(manifest_by_ver_pkg.items(), key=lambda k_v: (k_v[1],k_v[0]))):
3143N/A print(i, n)
3143N/A print("</pre>")
642N/A
642N/Adef count_manifest(mg, d):
642N/A try:
642N/A manifest_by_date[d.date().isoformat()] += 1
642N/A except KeyError:
642N/A manifest_by_date[d.date().isoformat()] = 1
642N/A try:
642N/A manifest_by_ip[mg["ip"]] += 1
642N/A except KeyError:
642N/A manifest_by_ip[mg["ip"]] = 1
642N/A
642N/A pm = pkg_pat.search(mg["uri"])
642N/A if pm != None and mg["response"] == "200":
642N/A pg = pm.groupdict()
642N/A
642N/A try:
3234N/A manifest_by_pkg[unquote(pg["stem"])] += 1
642N/A except KeyError:
3234N/A manifest_by_pkg[unquote(pg["stem"])] = 1
642N/A
642N/A try:
3234N/A manifest_by_ver_pkg[unquote(pg["stem"] + "@" + pg["version"])] += 1
642N/A except KeyError:
3234N/A manifest_by_ver_pkg[unquote(pg["stem"] + "@" + pg["version"])] = 1
642N/A
642N/A agent = pkg_agent_pat.search(mg["agent"])
642N/A if agent == None:
642N/A return
642N/A
642N/A ag = agent.groupdict()
642N/A try:
642N/A manifest_by_arch[ag["arch"]] += 1
642N/A except KeyError:
642N/A manifest_by_arch[ag["arch"]] = 1
642N/A
642N/Aopts, args = getopt.getopt(sys.argv[1:], "a:b:sw:")
642N/A
642N/Afor opt, arg in opts:
642N/A if opt == "-a":
642N/A try:
642N/A after = datetime.datetime(*(time.strptime(arg, "%Y-%b-%d")[0:6]))
642N/A except ValueError:
642N/A after = datetime.datetime(*(time.strptime(arg, "%Y-%m-%d")[0:6]))
642N/A
642N/A if opt == "-b":
642N/A before = arg
642N/A
642N/A if opt == "-s":
642N/A summary_file = prefix_summary_open("manifest")
642N/A
642N/A if opt == "-w":
642N/A active_window = arg
642N/A
642N/Ahost_cache_set_file_name()
642N/Ahost_cache_load()
642N/A
642N/Alastdate = None
642N/Alastdatetime = None
642N/A
642N/Afor l in fileinput.input(args):
642N/A m = comb_log_pat.search(l)
642N/A if not m:
642N/A continue
642N/A
642N/A mg = m.groupdict()
3194N/A
642N/A d = None
642N/A
642N/A if lastdatetime and mg["date"] == lastdate:
642N/A d = lastdatetime
642N/A else:
642N/A try:
642N/A d = datetime.datetime(*(time.strptime(mg["date"], "%d/%b/%Y")[0:6]))
642N/A lastdate = mg["date"]
642N/A lastdatetime = d
642N/A except ValueError:
642N/A # In the case the line can't be parsed for a date, it's
642N/A # probably corrupt.
642N/A continue
642N/A
642N/A if after and d < after:
642N/A continue
642N/A
642N/A count_manifest(mg, d)
642N/A
642N/Ahost_cache_save()
3194N/Amanifest_by_country = ip_to_country(manifest_by_ip)
642N/A
642N/Areport_section_begin("Manifest", summary_file = summary_file)
642N/Areport_cols_begin(summary_file = summary_file)
642N/Areport_col_begin("l", summary_file = summary_file)
642N/Areport_by_date(manifest_by_date, "manifest", summary_file = summary_file)
642N/Areport_col_end("l", summary_file = summary_file)
642N/Areport_col_begin("r", summary_file = summary_file)
642N/Areport_by_country(manifest_by_country, "manifest", summary_file = summary_file)
642N/Areport_col_end("r", summary_file = summary_file)
642N/Areport_cols_end(summary_file = summary_file)
642N/Areport_by_ip(manifest_by_ip, "manifest", summary_file = summary_file)
642N/Areport_by_raw_agent(manifest_by_raw_agent, "manifest", summary_file = summary_file)
642N/A
642N/Areport_manifest_by_pkg()
642N/Areport_manifest_by_ver_pkg()
642N/Areport_section_end(summary_file = summary_file)