parsezeolog.py :  » Web-Frameworks » Zope » Zope-2.6.0 » utilities » ZODBTools » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » utilities » ZODBTools » parsezeolog.py
"""Parse the BLATHER logging generated by ZEO.

An example of the log format is:
2002-04-15T13:05:29 BLATHER(-100) ZEO Server storea(3235680, [714], 235339406490168806) ('10.0.26.30', 45514)
"""

import operator
import re
import time

rx_time = re.compile('(\d\d\d\d-\d\d-\d\d)T(\d\d:\d\d:\d\d)')

def parse_time(line):
    """Return the time portion of a zLOG line in seconds or None."""
    mo = rx_time.match(line)
    if mo is None:
        return None
    date, time_ = mo.group(1, 2)
    date_l = [int(elt) for elt in date.split('-')]
    time_l = [int(elt) for elt in time_.split(':')]
    return int(time.mktime(date_l + time_l + [0, 0, 0]))

rx_meth = re.compile("ZEO Server (\w+)\((.*)\) \('(.*)', (\d+)")

def parse_method(line):
    pass

def parse_line(line):
    """Parse a log entry and return time, method info, and client."""
    t = parse_time(line)
    if t is None:
        return None, None, None
    mo = rx_meth.search(line)
    if mo is None:
        return None, None, None
    meth_name = mo.group(1)
    meth_args = mo.group(2)
    meth_args = [s.strip() for s in meth_args.split(",")]
    m = meth_name, tuple(meth_args)
    c = mo.group(3), mo.group(4)
    return t, m, c

class TStats:
    pass

class TransactionParser:

    def __init__(self):
        self.transactions = []
        self.cur_t = {}
        self.skipped = 0

    def parse(self, line):
        t, m, c = parse_line(line)
        if t is None:
            return
        name = m[0]
        meth = getattr(self, name, None)
        if meth is not None:
            meth(t, m[1], c)

    def tpc_begin(self, time, args, client):
        t = TStats()
        t.begin = time
        t.url = args[2]
        t.objects = []
        self.cur_t[client] = t

    def tpc_finish(self, time, args, client):
        t = self.cur_t.get(client, None)
        if t is None:
            self.skipped += 1
            return
        t.finish = time
##        self.transactions.append(t)
        self.report(t)
        self.cur_t[client] = None

    def storea(self, time, args, client):
        t = self.cur_t.get(client, None)
        if t is None:
            self.skipped += 1
            return
        # append the oid and the length of the object
        # parse the length as [NNN]
        info = int(args[0]), int(args[1][1:-1])
        t.objects.append(info)

    def report(self, t):
        """Print a report about the transaction"""
        if t.objects:
            bytes = reduce(operator.add, [size for oid, size in t.objects])
        else:
            bytes = 0
        print "%s %2d %4d %10d %s" % (t.begin, t.finish - t.begin,
                                      len(t.objects), bytes,
                                      time.ctime(t.begin)), t.url

if __name__ == "__main__":
    import fileinput

    p = TransactionParser()
    i = 0
    for line in fileinput.input():
        i += 1
        try:
            p.parse(line)
        except:
            print "line", i
            raise
    print len(p.transactions)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.