Ingres Community Forums Login Register Ingres.com  

Ingres Community Wiki

Navigation
Learn About
Developing With
Ingres Talk
Information
Toolbox

JDBC log query extractor

From Ingres Community Wiki

Jump to: navigation, search

A python script example of parsing a JDBC log and extracting the queries and timestamps. Also attempts to insert the values into an Ingres table, for example,

create sequence qc1 start with 1 increment by 1;
\p\g
create table qlog (
    i integer with default next value for qc1,
    seq integer not null,
    ts timestamp,
    te timestamp,
    qtext varchar(6000)
);
\p\g

Doesn't have any error checking.

import sys
import re
import ingresdbi
import getopt

# common variables

# rawstr = r"""(\")(.*)(\")"""
query = r"""(?P<timestamp>^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3})(: DrvPrep\[)(?P<seq>[0-9]*)(\]: preparing statement) '(?P<query>.*)'"""
cursclose = r"""(?P<endstamp>^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3})(: Curs\[%d\]: result-set closed)"""
endstr = None

vals = []
o = {}

def usage():
    print "-v<vnode> -d<database> -t<table> -f<filename>"
    print "-v, --vnode      Vnode name"
    print "-d, --database   Database name"
    print "-t, --table      Table name"
    print "-f, --filename   Filename name"
    print "-h, --help       Print this message"

if __name__ == '__main__':
    try:
        opts, args = getopt.getopt(sys.argv[1:], "v:d:t:f:h", ["vnode=", "database=", "table=", "filename=", "help"])
        if len(opts) == 0:
            usage()
            sys.exit(1)
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)

## Defaults
vnode = "(local)"
db = None
fn = None
tb = 'qlog'

## Get command line argument values.
for o, a in opts:
    if o in ("-v", "--vnode"):
        vnode = a
    if o in ("-d", "--database"):
        db = a
    if o in ("-t", "--table"):
        tb = a
    if o in ("-f", "--filename"):
        fn = a
    if o in ("-h", "--help"):
        usage()
        sys.exit(1)

if db != None:
    ingdb = ingresdbi.connect(vnode=vnode, database=db)
    cur = ingdb.cursor();

if fn != None and fn != "-":
    f = open(fn, "r")
else:
    f = sys.stdin

dc=ingresdbi.connect(database=db, vnode=vnode)
c=dc.cursor()

compile_obj1 = re.compile(query)

for l in f:
    tmp = l
    if len(tmp) > 0:
        mobj1 = re.search(query, tmp)
        if mobj1 != None:
            o = {}
            vals = []
            o['flag'] = True
            o['starttime'] = mobj1.group('timestamp')
            o['seq'] = int(mobj1.group('seq'))
            o['query'] = mobj1.group('query')
            o['table'] = tb
            endstr = cursclose % (int(o['seq']) + 1)
            vals.append(o['seq'])
            vals.append(o['starttime'])
            vals.append(o['query'])
        else:
            if endstr != None:
                mobj2 = re.match(endstr, tmp)
                if mobj2 != None:
                    o['endtime'] = mobj2.group('endstamp')
                    vals.append(o['endtime'])
                    c.execute("insert into %(table)s (seq, ts, te, qtext) values (%(seq)d, '%(starttime)s', '%(endtime)s', '%(query)s')" % o)
                    print vals
dc.commit();
dc.close();

Personal tools
© 2009 Ingres Corporation. All Rights Reserved